xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
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 "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
150b57cec5SDimitry Andric #include "InterCheckerAPI.h"
160b57cec5SDimitry Andric #include "clang/Basic/CharInfo.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"
200b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
250b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace clang;
280b57cec5SDimitry Andric using namespace ento;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric namespace {
310b57cec5SDimitry Andric class CStringChecker : public Checker< eval::Call,
320b57cec5SDimitry Andric                                          check::PreStmt<DeclStmt>,
330b57cec5SDimitry Andric                                          check::LiveSymbols,
340b57cec5SDimitry Andric                                          check::DeadSymbols,
350b57cec5SDimitry Andric                                          check::RegionChanges
360b57cec5SDimitry Andric                                          > {
370b57cec5SDimitry Andric   mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
380b57cec5SDimitry Andric       BT_NotCString, BT_AdditionOverflow;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   mutable const char *CurrentFunctionDescription;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric public:
430b57cec5SDimitry Andric   /// The filter is used to filter out the diagnostics which are not enabled by
440b57cec5SDimitry Andric   /// the user.
450b57cec5SDimitry Andric   struct CStringChecksFilter {
460b57cec5SDimitry Andric     DefaultBool CheckCStringNullArg;
470b57cec5SDimitry Andric     DefaultBool CheckCStringOutOfBounds;
480b57cec5SDimitry Andric     DefaultBool CheckCStringBufferOverlap;
490b57cec5SDimitry Andric     DefaultBool CheckCStringNotNullTerm;
500b57cec5SDimitry Andric 
51*a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNullArg;
52*a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringOutOfBounds;
53*a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringBufferOverlap;
54*a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNotNullTerm;
550b57cec5SDimitry Andric   };
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   CStringChecksFilter Filter;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   static void *getTag() { static int tag; return &tag; }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
620b57cec5SDimitry Andric   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
630b57cec5SDimitry Andric   void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
640b57cec5SDimitry Andric   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   ProgramStateRef
670b57cec5SDimitry Andric     checkRegionChanges(ProgramStateRef state,
680b57cec5SDimitry Andric                        const InvalidatedSymbols *,
690b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> ExplicitRegions,
700b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> Regions,
710b57cec5SDimitry Andric                        const LocationContext *LCtx,
720b57cec5SDimitry Andric                        const CallEvent *Call) const;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   typedef void (CStringChecker::*FnCheck)(CheckerContext &,
750b57cec5SDimitry Andric                                           const CallExpr *) const;
760b57cec5SDimitry Andric   CallDescriptionMap<FnCheck> Callbacks = {
770b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memcpy", 3}, &CStringChecker::evalMemcpy},
780b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy},
790b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp},
800b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove},
810b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset},
820b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset},
830b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy},
840b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy},
850b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy},
860b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy},
870b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat},
880b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat},
890b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat},
900b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength},
910b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength},
920b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp},
930b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp},
940b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp},
950b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp},
960b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep},
970b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy},
980b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp},
990b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero},
1000b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero},
1010b57cec5SDimitry Andric   };
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   // These require a bit of special handling.
1040b57cec5SDimitry Andric   CallDescription StdCopy{{"std", "copy"}, 3},
1050b57cec5SDimitry Andric       StdCopyBackward{{"std", "copy_backward"}, 3};
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
1080b57cec5SDimitry Andric   void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
1090b57cec5SDimitry Andric   void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
1100b57cec5SDimitry Andric   void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
1110b57cec5SDimitry Andric   void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
1120b57cec5SDimitry Andric   void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
1130b57cec5SDimitry Andric                       ProgramStateRef state,
1140b57cec5SDimitry Andric                       const Expr *Size,
1150b57cec5SDimitry Andric                       const Expr *Source,
1160b57cec5SDimitry Andric                       const Expr *Dest,
1170b57cec5SDimitry Andric                       bool Restricted = false,
1180b57cec5SDimitry Andric                       bool IsMempcpy = false) const;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
1230b57cec5SDimitry Andric   void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
1240b57cec5SDimitry Andric   void evalstrLengthCommon(CheckerContext &C,
1250b57cec5SDimitry Andric                            const CallExpr *CE,
1260b57cec5SDimitry Andric                            bool IsStrnlen = false) const;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
1290b57cec5SDimitry Andric   void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
1300b57cec5SDimitry Andric   void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
1310b57cec5SDimitry Andric   void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
1320b57cec5SDimitry Andric   void evalStrcpyCommon(CheckerContext &C,
1330b57cec5SDimitry Andric                         const CallExpr *CE,
1340b57cec5SDimitry Andric                         bool returnEnd,
1350b57cec5SDimitry Andric                         bool isBounded,
1360b57cec5SDimitry Andric                         bool isAppending,
1370b57cec5SDimitry Andric                         bool returnPtr = true) const;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
1400b57cec5SDimitry Andric   void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
1410b57cec5SDimitry Andric   void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
1440b57cec5SDimitry Andric   void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
1450b57cec5SDimitry Andric   void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
1460b57cec5SDimitry Andric   void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
1470b57cec5SDimitry Andric   void evalStrcmpCommon(CheckerContext &C,
1480b57cec5SDimitry Andric                         const CallExpr *CE,
1490b57cec5SDimitry Andric                         bool isBounded = false,
1500b57cec5SDimitry Andric                         bool ignoreCase = false) const;
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
1550b57cec5SDimitry Andric   void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
1560b57cec5SDimitry Andric   void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
1570b57cec5SDimitry Andric   void evalMemset(CheckerContext &C, const CallExpr *CE) const;
1580b57cec5SDimitry Andric   void evalBzero(CheckerContext &C, const CallExpr *CE) const;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Utility methods
1610b57cec5SDimitry Andric   std::pair<ProgramStateRef , ProgramStateRef >
1620b57cec5SDimitry Andric   static assumeZero(CheckerContext &C,
1630b57cec5SDimitry Andric                     ProgramStateRef state, SVal V, QualType Ty);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   static ProgramStateRef setCStringLength(ProgramStateRef state,
1660b57cec5SDimitry Andric                                               const MemRegion *MR,
1670b57cec5SDimitry Andric                                               SVal strLength);
1680b57cec5SDimitry Andric   static SVal getCStringLengthForRegion(CheckerContext &C,
1690b57cec5SDimitry Andric                                         ProgramStateRef &state,
1700b57cec5SDimitry Andric                                         const Expr *Ex,
1710b57cec5SDimitry Andric                                         const MemRegion *MR,
1720b57cec5SDimitry Andric                                         bool hypothetical);
1730b57cec5SDimitry Andric   SVal getCStringLength(CheckerContext &C,
1740b57cec5SDimitry Andric                         ProgramStateRef &state,
1750b57cec5SDimitry Andric                         const Expr *Ex,
1760b57cec5SDimitry Andric                         SVal Buf,
1770b57cec5SDimitry Andric                         bool hypothetical = false) const;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   const StringLiteral *getCStringLiteral(CheckerContext &C,
1800b57cec5SDimitry Andric                                          ProgramStateRef &state,
1810b57cec5SDimitry Andric                                          const Expr *expr,
1820b57cec5SDimitry Andric                                          SVal val) const;
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   static ProgramStateRef InvalidateBuffer(CheckerContext &C,
1850b57cec5SDimitry Andric                                           ProgramStateRef state,
1860b57cec5SDimitry Andric                                           const Expr *Ex, SVal V,
1870b57cec5SDimitry Andric                                           bool IsSourceBuffer,
1880b57cec5SDimitry Andric                                           const Expr *Size);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
1910b57cec5SDimitry Andric                               const MemRegion *MR);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   static bool memsetAux(const Expr *DstBuffer, SVal CharE,
1940b57cec5SDimitry Andric                         const Expr *Size, CheckerContext &C,
1950b57cec5SDimitry Andric                         ProgramStateRef &State);
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   // Re-usable checks
1980b57cec5SDimitry Andric   ProgramStateRef checkNonNull(CheckerContext &C,
1990b57cec5SDimitry Andric                                    ProgramStateRef state,
2000b57cec5SDimitry Andric                                    const Expr *S,
201*a7dea167SDimitry Andric                                    SVal l,
202*a7dea167SDimitry Andric                                    unsigned IdxOfArg) const;
2030b57cec5SDimitry Andric   ProgramStateRef CheckLocation(CheckerContext &C,
2040b57cec5SDimitry Andric                                     ProgramStateRef state,
2050b57cec5SDimitry Andric                                     const Expr *S,
2060b57cec5SDimitry Andric                                     SVal l,
2070b57cec5SDimitry Andric                                     const char *message = nullptr) const;
2080b57cec5SDimitry Andric   ProgramStateRef CheckBufferAccess(CheckerContext &C,
2090b57cec5SDimitry Andric                                         ProgramStateRef state,
2100b57cec5SDimitry Andric                                         const Expr *Size,
2110b57cec5SDimitry Andric                                         const Expr *FirstBuf,
2120b57cec5SDimitry Andric                                         const Expr *SecondBuf,
2130b57cec5SDimitry Andric                                         const char *firstMessage = nullptr,
2140b57cec5SDimitry Andric                                         const char *secondMessage = nullptr,
2150b57cec5SDimitry Andric                                         bool WarnAboutSize = false) const;
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   ProgramStateRef CheckBufferAccess(CheckerContext &C,
2180b57cec5SDimitry Andric                                         ProgramStateRef state,
2190b57cec5SDimitry Andric                                         const Expr *Size,
2200b57cec5SDimitry Andric                                         const Expr *Buf,
2210b57cec5SDimitry Andric                                         const char *message = nullptr,
2220b57cec5SDimitry Andric                                         bool WarnAboutSize = false) const {
2230b57cec5SDimitry Andric     // This is a convenience overload.
2240b57cec5SDimitry Andric     return CheckBufferAccess(C, state, Size, Buf, nullptr, message, nullptr,
2250b57cec5SDimitry Andric                              WarnAboutSize);
2260b57cec5SDimitry Andric   }
2270b57cec5SDimitry Andric   ProgramStateRef CheckOverlap(CheckerContext &C,
2280b57cec5SDimitry Andric                                    ProgramStateRef state,
2290b57cec5SDimitry Andric                                    const Expr *Size,
2300b57cec5SDimitry Andric                                    const Expr *First,
2310b57cec5SDimitry Andric                                    const Expr *Second) const;
2320b57cec5SDimitry Andric   void emitOverlapBug(CheckerContext &C,
2330b57cec5SDimitry Andric                       ProgramStateRef state,
2340b57cec5SDimitry Andric                       const Stmt *First,
2350b57cec5SDimitry Andric                       const Stmt *Second) const;
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
2380b57cec5SDimitry Andric                       StringRef WarningMsg) const;
2390b57cec5SDimitry Andric   void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
2400b57cec5SDimitry Andric                           const Stmt *S, StringRef WarningMsg) const;
2410b57cec5SDimitry Andric   void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
2420b57cec5SDimitry Andric                          const Stmt *S, StringRef WarningMsg) const;
2430b57cec5SDimitry Andric   void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   ProgramStateRef checkAdditionOverflow(CheckerContext &C,
2460b57cec5SDimitry Andric                                             ProgramStateRef state,
2470b57cec5SDimitry Andric                                             NonLoc left,
2480b57cec5SDimitry Andric                                             NonLoc right) const;
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   // Return true if the destination buffer of the copy function may be in bound.
2510b57cec5SDimitry Andric   // Expects SVal of Size to be positive and unsigned.
2520b57cec5SDimitry Andric   // Expects SVal of FirstBuf to be a FieldRegion.
2530b57cec5SDimitry Andric   static bool IsFirstBufInBound(CheckerContext &C,
2540b57cec5SDimitry Andric                                 ProgramStateRef state,
2550b57cec5SDimitry Andric                                 const Expr *FirstBuf,
2560b57cec5SDimitry Andric                                 const Expr *Size);
2570b57cec5SDimitry Andric };
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric } //end anonymous namespace
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2640b57cec5SDimitry Andric // Individual checks and utility methods.
2650b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef >
2680b57cec5SDimitry Andric CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
2690b57cec5SDimitry Andric                            QualType Ty) {
2700b57cec5SDimitry Andric   Optional<DefinedSVal> val = V.getAs<DefinedSVal>();
2710b57cec5SDimitry Andric   if (!val)
2720b57cec5SDimitry Andric     return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
2750b57cec5SDimitry Andric   DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
2760b57cec5SDimitry Andric   return state->assume(svalBuilder.evalEQ(state, *val, zero));
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
2800b57cec5SDimitry Andric                                             ProgramStateRef state,
281*a7dea167SDimitry Andric                                             const Expr *S, SVal l,
282*a7dea167SDimitry Andric                                             unsigned IdxOfArg) const {
2830b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
2840b57cec5SDimitry Andric   if (!state)
2850b57cec5SDimitry Andric     return nullptr;
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   ProgramStateRef stateNull, stateNonNull;
2880b57cec5SDimitry Andric   std::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   if (stateNull && !stateNonNull) {
2910b57cec5SDimitry Andric     if (Filter.CheckCStringNullArg) {
2920b57cec5SDimitry Andric       SmallString<80> buf;
293*a7dea167SDimitry Andric       llvm::raw_svector_ostream OS(buf);
2940b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
295*a7dea167SDimitry Andric       OS << "Null pointer argument in call to " << CurrentFunctionDescription
296*a7dea167SDimitry Andric          << ' ' << IdxOfArg << llvm::getOrdinalSuffix(IdxOfArg)
297*a7dea167SDimitry Andric          << " parameter";
2980b57cec5SDimitry Andric 
299*a7dea167SDimitry Andric       emitNullArgBug(C, stateNull, S, OS.str());
3000b57cec5SDimitry Andric     }
3010b57cec5SDimitry Andric     return nullptr;
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   // From here on, assume that the value is non-null.
3050b57cec5SDimitry Andric   assert(stateNonNull);
3060b57cec5SDimitry Andric   return stateNonNull;
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
3100b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
3110b57cec5SDimitry Andric                                              ProgramStateRef state,
3120b57cec5SDimitry Andric                                              const Expr *S, SVal l,
3130b57cec5SDimitry Andric                                              const char *warningMsg) const {
3140b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3150b57cec5SDimitry Andric   if (!state)
3160b57cec5SDimitry Andric     return nullptr;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // Check for out of bound array element access.
3190b57cec5SDimitry Andric   const MemRegion *R = l.getAsRegion();
3200b57cec5SDimitry Andric   if (!R)
3210b57cec5SDimitry Andric     return state;
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
3240b57cec5SDimitry Andric   if (!ER)
3250b57cec5SDimitry Andric     return state;
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   if (ER->getValueType() != C.getASTContext().CharTy)
3280b57cec5SDimitry Andric     return state;
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   // Get the size of the array.
3310b57cec5SDimitry Andric   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
3320b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
3330b57cec5SDimitry Andric   SVal Extent =
3340b57cec5SDimitry Andric     svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
3350b57cec5SDimitry Andric   DefinedOrUnknownSVal Size = Extent.castAs<DefinedOrUnknownSVal>();
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   // Get the index of the accessed element.
3380b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true);
3410b57cec5SDimitry Andric   ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false);
3420b57cec5SDimitry Andric   if (StOutBound && !StInBound) {
3430b57cec5SDimitry Andric     // These checks are either enabled by the CString out-of-bounds checker
3440b57cec5SDimitry Andric     // explicitly or implicitly by the Malloc checker.
3450b57cec5SDimitry Andric     // In the latter case we only do modeling but do not emit warning.
3460b57cec5SDimitry Andric     if (!Filter.CheckCStringOutOfBounds)
3470b57cec5SDimitry Andric       return nullptr;
3480b57cec5SDimitry Andric     // Emit a bug report.
3490b57cec5SDimitry Andric     if (warningMsg) {
3500b57cec5SDimitry Andric       emitOutOfBoundsBug(C, StOutBound, S, warningMsg);
3510b57cec5SDimitry Andric     } else {
3520b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
3530b57cec5SDimitry Andric       assert(CurrentFunctionDescription[0] != '\0');
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric       SmallString<80> buf;
3560b57cec5SDimitry Andric       llvm::raw_svector_ostream os(buf);
3570b57cec5SDimitry Andric       os << toUppercase(CurrentFunctionDescription[0])
3580b57cec5SDimitry Andric          << &CurrentFunctionDescription[1]
3590b57cec5SDimitry Andric          << " accesses out-of-bound array element";
3600b57cec5SDimitry Andric       emitOutOfBoundsBug(C, StOutBound, S, os.str());
3610b57cec5SDimitry Andric     }
3620b57cec5SDimitry Andric     return nullptr;
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // Array bound check succeeded.  From this point forward the array bound
3660b57cec5SDimitry Andric   // should always succeed.
3670b57cec5SDimitry Andric   return StInBound;
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
3710b57cec5SDimitry Andric                                                  ProgramStateRef state,
3720b57cec5SDimitry Andric                                                  const Expr *Size,
3730b57cec5SDimitry Andric                                                  const Expr *FirstBuf,
3740b57cec5SDimitry Andric                                                  const Expr *SecondBuf,
3750b57cec5SDimitry Andric                                                  const char *firstMessage,
3760b57cec5SDimitry Andric                                                  const char *secondMessage,
3770b57cec5SDimitry Andric                                                  bool WarnAboutSize) const {
3780b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3790b57cec5SDimitry Andric   if (!state)
3800b57cec5SDimitry Andric     return nullptr;
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
3830b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
3840b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
3870b57cec5SDimitry Andric   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   // Check that the first buffer is non-null.
3900b57cec5SDimitry Andric   SVal BufVal = C.getSVal(FirstBuf);
391*a7dea167SDimitry Andric   state = checkNonNull(C, state, FirstBuf, BufVal, 1);
3920b57cec5SDimitry Andric   if (!state)
3930b57cec5SDimitry Andric     return nullptr;
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
3960b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
3970b57cec5SDimitry Andric     return state;
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   // Get the access length and make sure it is known.
4000b57cec5SDimitry Andric   // FIXME: This assumes the caller has already checked that the access length
4010b57cec5SDimitry Andric   // is positive. And that it's unsigned.
4020b57cec5SDimitry Andric   SVal LengthVal = C.getSVal(Size);
4030b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
4040b57cec5SDimitry Andric   if (!Length)
4050b57cec5SDimitry Andric     return state;
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
4080b57cec5SDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
4090b57cec5SDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
4100b57cec5SDimitry Andric   if (Offset.isUnknown())
4110b57cec5SDimitry Andric     return nullptr;
4120b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
4150b57cec5SDimitry Andric   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
4160b57cec5SDimitry Andric   if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
4170b57cec5SDimitry Andric     const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf);
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric     SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
4200b57cec5SDimitry Andric                                           LastOffset, PtrTy);
4210b57cec5SDimitry Andric     state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage);
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric     // If the buffer isn't large enough, abort.
4240b57cec5SDimitry Andric     if (!state)
4250b57cec5SDimitry Andric       return nullptr;
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   // If there's a second buffer, check it as well.
4290b57cec5SDimitry Andric   if (SecondBuf) {
4300b57cec5SDimitry Andric     BufVal = state->getSVal(SecondBuf, LCtx);
431*a7dea167SDimitry Andric     state = checkNonNull(C, state, SecondBuf, BufVal, 2);
4320b57cec5SDimitry Andric     if (!state)
4330b57cec5SDimitry Andric       return nullptr;
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric     BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
4360b57cec5SDimitry Andric     if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
4370b57cec5SDimitry Andric       const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf);
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric       SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
4400b57cec5SDimitry Andric                                             LastOffset, PtrTy);
4410b57cec5SDimitry Andric       state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage);
4420b57cec5SDimitry Andric     }
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   // Large enough or not, return this state!
4460b57cec5SDimitry Andric   return state;
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
4500b57cec5SDimitry Andric                                             ProgramStateRef state,
4510b57cec5SDimitry Andric                                             const Expr *Size,
4520b57cec5SDimitry Andric                                             const Expr *First,
4530b57cec5SDimitry Andric                                             const Expr *Second) const {
4540b57cec5SDimitry Andric   if (!Filter.CheckCStringBufferOverlap)
4550b57cec5SDimitry Andric     return state;
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   // Do a simple check for overlap: if the two arguments are from the same
4580b57cec5SDimitry Andric   // buffer, see if the end of the first is greater than the start of the second
4590b57cec5SDimitry Andric   // or vice versa.
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
4620b57cec5SDimitry Andric   if (!state)
4630b57cec5SDimitry Andric     return nullptr;
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   ProgramStateRef stateTrue, stateFalse;
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   // Get the buffer values and make sure they're known locations.
4680b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
4690b57cec5SDimitry Andric   SVal firstVal = state->getSVal(First, LCtx);
4700b57cec5SDimitry Andric   SVal secondVal = state->getSVal(Second, LCtx);
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   Optional<Loc> firstLoc = firstVal.getAs<Loc>();
4730b57cec5SDimitry Andric   if (!firstLoc)
4740b57cec5SDimitry Andric     return state;
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric   Optional<Loc> secondLoc = secondVal.getAs<Loc>();
4770b57cec5SDimitry Andric   if (!secondLoc)
4780b57cec5SDimitry Andric     return state;
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   // Are the two values the same?
4810b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
4820b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) =
4830b57cec5SDimitry Andric     state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
4860b57cec5SDimitry Andric     // If the values are known to be equal, that's automatically an overlap.
4870b57cec5SDimitry Andric     emitOverlapBug(C, stateTrue, First, Second);
4880b57cec5SDimitry Andric     return nullptr;
4890b57cec5SDimitry Andric   }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   // assume the two expressions are not equal.
4920b57cec5SDimitry Andric   assert(stateFalse);
4930b57cec5SDimitry Andric   state = stateFalse;
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   // Which value comes first?
4960b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
4970b57cec5SDimitry Andric   SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
4980b57cec5SDimitry Andric                                          *firstLoc, *secondLoc, cmpTy);
4990b57cec5SDimitry Andric   Optional<DefinedOrUnknownSVal> reverseTest =
5000b57cec5SDimitry Andric       reverse.getAs<DefinedOrUnknownSVal>();
5010b57cec5SDimitry Andric   if (!reverseTest)
5020b57cec5SDimitry Andric     return state;
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
5050b57cec5SDimitry Andric   if (stateTrue) {
5060b57cec5SDimitry Andric     if (stateFalse) {
5070b57cec5SDimitry Andric       // If we don't know which one comes first, we can't perform this test.
5080b57cec5SDimitry Andric       return state;
5090b57cec5SDimitry Andric     } else {
5100b57cec5SDimitry Andric       // Switch the values so that firstVal is before secondVal.
5110b57cec5SDimitry Andric       std::swap(firstLoc, secondLoc);
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric       // Switch the Exprs as well, so that they still correspond.
5140b57cec5SDimitry Andric       std::swap(First, Second);
5150b57cec5SDimitry Andric     }
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric   // Get the length, and make sure it too is known.
5190b57cec5SDimitry Andric   SVal LengthVal = state->getSVal(Size, LCtx);
5200b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
5210b57cec5SDimitry Andric   if (!Length)
5220b57cec5SDimitry Andric     return state;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric   // Convert the first buffer's start address to char*.
5250b57cec5SDimitry Andric   // Bail out if the cast fails.
5260b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
5270b57cec5SDimitry Andric   QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
5280b57cec5SDimitry Andric   SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy,
5290b57cec5SDimitry Andric                                          First->getType());
5300b57cec5SDimitry Andric   Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
5310b57cec5SDimitry Andric   if (!FirstStartLoc)
5320b57cec5SDimitry Andric     return state;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   // Compute the end of the first buffer. Bail out if THAT fails.
5350b57cec5SDimitry Andric   SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
5360b57cec5SDimitry Andric                                  *FirstStartLoc, *Length, CharPtrTy);
5370b57cec5SDimitry Andric   Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
5380b57cec5SDimitry Andric   if (!FirstEndLoc)
5390b57cec5SDimitry Andric     return state;
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric   // Is the end of the first buffer past the start of the second buffer?
5420b57cec5SDimitry Andric   SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
5430b57cec5SDimitry Andric                                 *FirstEndLoc, *secondLoc, cmpTy);
5440b57cec5SDimitry Andric   Optional<DefinedOrUnknownSVal> OverlapTest =
5450b57cec5SDimitry Andric       Overlap.getAs<DefinedOrUnknownSVal>();
5460b57cec5SDimitry Andric   if (!OverlapTest)
5470b57cec5SDimitry Andric     return state;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
5520b57cec5SDimitry Andric     // Overlap!
5530b57cec5SDimitry Andric     emitOverlapBug(C, stateTrue, First, Second);
5540b57cec5SDimitry Andric     return nullptr;
5550b57cec5SDimitry Andric   }
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   // assume the two expressions don't overlap.
5580b57cec5SDimitry Andric   assert(stateFalse);
5590b57cec5SDimitry Andric   return stateFalse;
5600b57cec5SDimitry Andric }
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
5630b57cec5SDimitry Andric                                   const Stmt *First, const Stmt *Second) const {
5640b57cec5SDimitry Andric   ExplodedNode *N = C.generateErrorNode(state);
5650b57cec5SDimitry Andric   if (!N)
5660b57cec5SDimitry Andric     return;
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   if (!BT_Overlap)
5690b57cec5SDimitry Andric     BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
5700b57cec5SDimitry Andric                                  categories::UnixAPI, "Improper arguments"));
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   // Generate a report for this bug.
573*a7dea167SDimitry Andric   auto report = std::make_unique<PathSensitiveBugReport>(
5740b57cec5SDimitry Andric       *BT_Overlap, "Arguments must not be overlapping buffers", N);
5750b57cec5SDimitry Andric   report->addRange(First->getSourceRange());
5760b57cec5SDimitry Andric   report->addRange(Second->getSourceRange());
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric   C.emitReport(std::move(report));
5790b57cec5SDimitry Andric }
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
5820b57cec5SDimitry Andric                                     const Stmt *S, StringRef WarningMsg) const {
5830b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
5840b57cec5SDimitry Andric     if (!BT_Null)
5850b57cec5SDimitry Andric       BT_Null.reset(new BuiltinBug(
5860b57cec5SDimitry Andric           Filter.CheckNameCStringNullArg, categories::UnixAPI,
5870b57cec5SDimitry Andric           "Null pointer argument in call to byte string function"));
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
590*a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
5910b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
5920b57cec5SDimitry Andric     if (const auto *Ex = dyn_cast<Expr>(S))
5930b57cec5SDimitry Andric       bugreporter::trackExpressionValue(N, Ex, *Report);
5940b57cec5SDimitry Andric     C.emitReport(std::move(Report));
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
5990b57cec5SDimitry Andric                                         ProgramStateRef State, const Stmt *S,
6000b57cec5SDimitry Andric                                         StringRef WarningMsg) const {
6010b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6020b57cec5SDimitry Andric     if (!BT_Bounds)
6030b57cec5SDimitry Andric       BT_Bounds.reset(new BuiltinBug(
6040b57cec5SDimitry Andric           Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
6050b57cec5SDimitry Andric                                          : Filter.CheckNameCStringNullArg,
6060b57cec5SDimitry Andric           "Out-of-bound array access",
6070b57cec5SDimitry Andric           "Byte string function accesses out-of-bound array element"));
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric     // FIXME: It would be nice to eventually make this diagnostic more clear,
6120b57cec5SDimitry Andric     // e.g., by referencing the original declaration or by saying *why* this
6130b57cec5SDimitry Andric     // reference is outside the range.
614*a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
6150b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6160b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6170b57cec5SDimitry Andric   }
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
6210b57cec5SDimitry Andric                                        const Stmt *S,
6220b57cec5SDimitry Andric                                        StringRef WarningMsg) const {
6230b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
6240b57cec5SDimitry Andric     if (!BT_NotCString)
6250b57cec5SDimitry Andric       BT_NotCString.reset(new BuiltinBug(
6260b57cec5SDimitry Andric           Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
6270b57cec5SDimitry Andric           "Argument is not a null-terminated string."));
6280b57cec5SDimitry Andric 
629*a7dea167SDimitry Andric     auto Report =
630*a7dea167SDimitry Andric         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6330b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6340b57cec5SDimitry Andric   }
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
6380b57cec5SDimitry Andric                                              ProgramStateRef State) const {
6390b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6400b57cec5SDimitry Andric     if (!BT_NotCString)
6410b57cec5SDimitry Andric       BT_NotCString.reset(
6420b57cec5SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
6430b57cec5SDimitry Andric                          "Sum of expressions causes overflow."));
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric     // This isn't a great error message, but this should never occur in real
6460b57cec5SDimitry Andric     // code anyway -- you'd have to create a buffer longer than a size_t can
6470b57cec5SDimitry Andric     // represent, which is sort of a contradiction.
6480b57cec5SDimitry Andric     const char *WarningMsg =
6490b57cec5SDimitry Andric         "This expression will create a string whose length is too big to "
6500b57cec5SDimitry Andric         "be represented as a size_t";
6510b57cec5SDimitry Andric 
652*a7dea167SDimitry Andric     auto Report =
653*a7dea167SDimitry Andric         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
6540b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric }
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
6590b57cec5SDimitry Andric                                                      ProgramStateRef state,
6600b57cec5SDimitry Andric                                                      NonLoc left,
6610b57cec5SDimitry Andric                                                      NonLoc right) const {
6620b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
6630b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
6640b57cec5SDimitry Andric     return state;
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
6670b57cec5SDimitry Andric   if (!state)
6680b57cec5SDimitry Andric     return nullptr;
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
6710b57cec5SDimitry Andric   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
6740b57cec5SDimitry Andric   const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
6750b57cec5SDimitry Andric   NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric   SVal maxMinusRight;
6780b57cec5SDimitry Andric   if (right.getAs<nonloc::ConcreteInt>()) {
6790b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
6800b57cec5SDimitry Andric                                                  sizeTy);
6810b57cec5SDimitry Andric   } else {
6820b57cec5SDimitry Andric     // Try switching the operands. (The order of these two assignments is
6830b57cec5SDimitry Andric     // important!)
6840b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
6850b57cec5SDimitry Andric                                             sizeTy);
6860b57cec5SDimitry Andric     left = right;
6870b57cec5SDimitry Andric   }
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric   if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
6900b57cec5SDimitry Andric     QualType cmpTy = svalBuilder.getConditionType();
6910b57cec5SDimitry Andric     // If left > max - right, we have an overflow.
6920b57cec5SDimitry Andric     SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
6930b57cec5SDimitry Andric                                                 *maxMinusRightNL, cmpTy);
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric     ProgramStateRef stateOverflow, stateOkay;
6960b57cec5SDimitry Andric     std::tie(stateOverflow, stateOkay) =
6970b57cec5SDimitry Andric       state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric     if (stateOverflow && !stateOkay) {
7000b57cec5SDimitry Andric       // We have an overflow. Emit a bug report.
7010b57cec5SDimitry Andric       emitAdditionOverflowBug(C, stateOverflow);
7020b57cec5SDimitry Andric       return nullptr;
7030b57cec5SDimitry Andric     }
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric     // From now on, assume an overflow didn't occur.
7060b57cec5SDimitry Andric     assert(stateOkay);
7070b57cec5SDimitry Andric     state = stateOkay;
7080b57cec5SDimitry Andric   }
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric   return state;
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
7140b57cec5SDimitry Andric                                                 const MemRegion *MR,
7150b57cec5SDimitry Andric                                                 SVal strLength) {
7160b57cec5SDimitry Andric   assert(!strLength.isUndef() && "Attempt to set an undefined string length");
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   MR = MR->StripCasts();
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   switch (MR->getKind()) {
7210b57cec5SDimitry Andric   case MemRegion::StringRegionKind:
7220b57cec5SDimitry Andric     // FIXME: This can happen if we strcpy() into a string region. This is
7230b57cec5SDimitry Andric     // undefined [C99 6.4.5p6], but we should still warn about it.
7240b57cec5SDimitry Andric     return state;
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
7270b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
7280b57cec5SDimitry Andric   case MemRegion::VarRegionKind:
7290b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
7300b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
7310b57cec5SDimitry Andric     // These are the types we can currently track string lengths for.
7320b57cec5SDimitry Andric     break;
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
7350b57cec5SDimitry Andric     // FIXME: Handle element regions by upper-bounding the parent region's
7360b57cec5SDimitry Andric     // string length.
7370b57cec5SDimitry Andric     return state;
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   default:
7400b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
7410b57cec5SDimitry Andric     // For now, just ignore the change.
7420b57cec5SDimitry Andric     // FIXME: These are rare but not impossible. We should output some kind of
7430b57cec5SDimitry Andric     // warning for things like strcpy((char[]){'a', 0}, "b");
7440b57cec5SDimitry Andric     return state;
7450b57cec5SDimitry Andric   }
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   if (strLength.isUnknown())
7480b57cec5SDimitry Andric     return state->remove<CStringLength>(MR);
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   return state->set<CStringLength>(MR, strLength);
7510b57cec5SDimitry Andric }
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
7540b57cec5SDimitry Andric                                                ProgramStateRef &state,
7550b57cec5SDimitry Andric                                                const Expr *Ex,
7560b57cec5SDimitry Andric                                                const MemRegion *MR,
7570b57cec5SDimitry Andric                                                bool hypothetical) {
7580b57cec5SDimitry Andric   if (!hypothetical) {
7590b57cec5SDimitry Andric     // If there's a recorded length, go ahead and return it.
7600b57cec5SDimitry Andric     const SVal *Recorded = state->get<CStringLength>(MR);
7610b57cec5SDimitry Andric     if (Recorded)
7620b57cec5SDimitry Andric       return *Recorded;
7630b57cec5SDimitry Andric   }
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric   // Otherwise, get a new symbol and update the state.
7660b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
7670b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
7680b57cec5SDimitry Andric   SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
7690b57cec5SDimitry Andric                                                     MR, Ex, sizeTy,
7700b57cec5SDimitry Andric                                                     C.getLocationContext(),
7710b57cec5SDimitry Andric                                                     C.blockCount());
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric   if (!hypothetical) {
7740b57cec5SDimitry Andric     if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
7750b57cec5SDimitry Andric       // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
7760b57cec5SDimitry Andric       BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
7770b57cec5SDimitry Andric       const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
7780b57cec5SDimitry Andric       llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
7790b57cec5SDimitry Andric       const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
7800b57cec5SDimitry Andric                                                         fourInt);
7810b57cec5SDimitry Andric       NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
7820b57cec5SDimitry Andric       SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
7830b57cec5SDimitry Andric                                                 maxLength, sizeTy);
7840b57cec5SDimitry Andric       state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
7850b57cec5SDimitry Andric     }
7860b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
7870b57cec5SDimitry Andric   }
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric   return strLength;
7900b57cec5SDimitry Andric }
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
7930b57cec5SDimitry Andric                                       const Expr *Ex, SVal Buf,
7940b57cec5SDimitry Andric                                       bool hypothetical) const {
7950b57cec5SDimitry Andric   const MemRegion *MR = Buf.getAsRegion();
7960b57cec5SDimitry Andric   if (!MR) {
7970b57cec5SDimitry Andric     // If we can't get a region, see if it's something we /know/ isn't a
7980b57cec5SDimitry Andric     // C string. In the context of locations, the only time we can issue such
7990b57cec5SDimitry Andric     // a warning is for labels.
8000b57cec5SDimitry Andric     if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
8010b57cec5SDimitry Andric       if (Filter.CheckCStringNotNullTerm) {
8020b57cec5SDimitry Andric         SmallString<120> buf;
8030b57cec5SDimitry Andric         llvm::raw_svector_ostream os(buf);
8040b57cec5SDimitry Andric         assert(CurrentFunctionDescription);
8050b57cec5SDimitry Andric         os << "Argument to " << CurrentFunctionDescription
8060b57cec5SDimitry Andric            << " is the address of the label '" << Label->getLabel()->getName()
8070b57cec5SDimitry Andric            << "', which is not a null-terminated string";
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric         emitNotCStringBug(C, state, Ex, os.str());
8100b57cec5SDimitry Andric       }
8110b57cec5SDimitry Andric       return UndefinedVal();
8120b57cec5SDimitry Andric     }
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric     // If it's not a region and not a label, give up.
8150b57cec5SDimitry Andric     return UnknownVal();
8160b57cec5SDimitry Andric   }
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric   // If we have a region, strip casts from it and see if we can figure out
8190b57cec5SDimitry Andric   // its length. For anything we can't figure out, just return UnknownVal.
8200b57cec5SDimitry Andric   MR = MR->StripCasts();
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric   switch (MR->getKind()) {
8230b57cec5SDimitry Andric   case MemRegion::StringRegionKind: {
8240b57cec5SDimitry Andric     // Modifying the contents of string regions is undefined [C99 6.4.5p6],
8250b57cec5SDimitry Andric     // so we can assume that the byte length is the correct C string length.
8260b57cec5SDimitry Andric     SValBuilder &svalBuilder = C.getSValBuilder();
8270b57cec5SDimitry Andric     QualType sizeTy = svalBuilder.getContext().getSizeType();
8280b57cec5SDimitry Andric     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
8290b57cec5SDimitry Andric     return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
8300b57cec5SDimitry Andric   }
8310b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
8320b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
8330b57cec5SDimitry Andric   case MemRegion::VarRegionKind:
8340b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
8350b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
8360b57cec5SDimitry Andric     return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
8370b57cec5SDimitry Andric   case MemRegion::CompoundLiteralRegionKind:
8380b57cec5SDimitry Andric     // FIXME: Can we track this? Is it necessary?
8390b57cec5SDimitry Andric     return UnknownVal();
8400b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
8410b57cec5SDimitry Andric     // FIXME: How can we handle this? It's not good enough to subtract the
8420b57cec5SDimitry Andric     // offset from the base string length; consider "123\x00567" and &a[5].
8430b57cec5SDimitry Andric     return UnknownVal();
8440b57cec5SDimitry Andric   default:
8450b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
8460b57cec5SDimitry Andric     // In this case, an error is emitted and UndefinedVal is returned.
8470b57cec5SDimitry Andric     // The caller should always be prepared to handle this case.
8480b57cec5SDimitry Andric     if (Filter.CheckCStringNotNullTerm) {
8490b57cec5SDimitry Andric       SmallString<120> buf;
8500b57cec5SDimitry Andric       llvm::raw_svector_ostream os(buf);
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
8530b57cec5SDimitry Andric       os << "Argument to " << CurrentFunctionDescription << " is ";
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric       if (SummarizeRegion(os, C.getASTContext(), MR))
8560b57cec5SDimitry Andric         os << ", which is not a null-terminated string";
8570b57cec5SDimitry Andric       else
8580b57cec5SDimitry Andric         os << "not a null-terminated string";
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric       emitNotCStringBug(C, state, Ex, os.str());
8610b57cec5SDimitry Andric     }
8620b57cec5SDimitry Andric     return UndefinedVal();
8630b57cec5SDimitry Andric   }
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
8670b57cec5SDimitry Andric   ProgramStateRef &state, const Expr *expr, SVal val) const {
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   // Get the memory region pointed to by the val.
8700b57cec5SDimitry Andric   const MemRegion *bufRegion = val.getAsRegion();
8710b57cec5SDimitry Andric   if (!bufRegion)
8720b57cec5SDimitry Andric     return nullptr;
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric   // Strip casts off the memory region.
8750b57cec5SDimitry Andric   bufRegion = bufRegion->StripCasts();
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   // Cast the memory region to a string region.
8780b57cec5SDimitry Andric   const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
8790b57cec5SDimitry Andric   if (!strRegion)
8800b57cec5SDimitry Andric     return nullptr;
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric   // Return the actual string in the string region.
8830b57cec5SDimitry Andric   return strRegion->getStringLiteral();
8840b57cec5SDimitry Andric }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
8870b57cec5SDimitry Andric                                        ProgramStateRef state,
8880b57cec5SDimitry Andric                                        const Expr *FirstBuf,
8890b57cec5SDimitry Andric                                        const Expr *Size) {
8900b57cec5SDimitry Andric   // If we do not know that the buffer is long enough we return 'true'.
8910b57cec5SDimitry Andric   // Otherwise the parent region of this field region would also get
8920b57cec5SDimitry Andric   // invalidated, which would lead to warnings based on an unknown state.
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric   // Originally copied from CheckBufferAccess and CheckLocation.
8950b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
8960b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
8970b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
9000b57cec5SDimitry Andric   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
9010b57cec5SDimitry Andric   SVal BufVal = state->getSVal(FirstBuf, LCtx);
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric   SVal LengthVal = state->getSVal(Size, LCtx);
9040b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
9050b57cec5SDimitry Andric   if (!Length)
9060b57cec5SDimitry Andric     return true; // cf top comment.
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
9090b57cec5SDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
9100b57cec5SDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
9110b57cec5SDimitry Andric   if (Offset.isUnknown())
9120b57cec5SDimitry Andric     return true; // cf top comment
9130b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
9160b57cec5SDimitry Andric   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
9170b57cec5SDimitry Andric   Optional<Loc> BufLoc = BufStart.getAs<Loc>();
9180b57cec5SDimitry Andric   if (!BufLoc)
9190b57cec5SDimitry Andric     return true; // cf top comment.
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   SVal BufEnd =
9220b57cec5SDimitry Andric       svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric   // Check for out of bound array element access.
9250b57cec5SDimitry Andric   const MemRegion *R = BufEnd.getAsRegion();
9260b57cec5SDimitry Andric   if (!R)
9270b57cec5SDimitry Andric     return true; // cf top comment.
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
9300b57cec5SDimitry Andric   if (!ER)
9310b57cec5SDimitry Andric     return true; // cf top comment.
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric   // FIXME: Does this crash when a non-standard definition
9340b57cec5SDimitry Andric   // of a library function is encountered?
9350b57cec5SDimitry Andric   assert(ER->getValueType() == C.getASTContext().CharTy &&
9360b57cec5SDimitry Andric          "IsFirstBufInBound should only be called with char* ElementRegions");
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   // Get the size of the array.
9390b57cec5SDimitry Andric   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
9400b57cec5SDimitry Andric   SVal Extent =
9410b57cec5SDimitry Andric       svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
9420b57cec5SDimitry Andric   DefinedOrUnknownSVal ExtentSize = Extent.castAs<DefinedOrUnknownSVal>();
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric   // Get the index of the accessed element.
9450b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   ProgramStateRef StInBound = state->assumeInBound(Idx, ExtentSize, true);
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric   return static_cast<bool>(StInBound);
9500b57cec5SDimitry Andric }
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
9530b57cec5SDimitry Andric                                                  ProgramStateRef state,
9540b57cec5SDimitry Andric                                                  const Expr *E, SVal V,
9550b57cec5SDimitry Andric                                                  bool IsSourceBuffer,
9560b57cec5SDimitry Andric                                                  const Expr *Size) {
9570b57cec5SDimitry Andric   Optional<Loc> L = V.getAs<Loc>();
9580b57cec5SDimitry Andric   if (!L)
9590b57cec5SDimitry Andric     return state;
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric   // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
9620b57cec5SDimitry Andric   // some assumptions about the value that CFRefCount can't. Even so, it should
9630b57cec5SDimitry Andric   // probably be refactored.
9640b57cec5SDimitry Andric   if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
9650b57cec5SDimitry Andric     const MemRegion *R = MR->getRegion()->StripCasts();
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric     // Are we dealing with an ElementRegion?  If so, we should be invalidating
9680b57cec5SDimitry Andric     // the super-region.
9690b57cec5SDimitry Andric     if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
9700b57cec5SDimitry Andric       R = ER->getSuperRegion();
9710b57cec5SDimitry Andric       // FIXME: What about layers of ElementRegions?
9720b57cec5SDimitry Andric     }
9730b57cec5SDimitry Andric 
9740b57cec5SDimitry Andric     // Invalidate this region.
9750b57cec5SDimitry Andric     const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric     bool CausesPointerEscape = false;
9780b57cec5SDimitry Andric     RegionAndSymbolInvalidationTraits ITraits;
9790b57cec5SDimitry Andric     // Invalidate and escape only indirect regions accessible through the source
9800b57cec5SDimitry Andric     // buffer.
9810b57cec5SDimitry Andric     if (IsSourceBuffer) {
9820b57cec5SDimitry Andric       ITraits.setTrait(R->getBaseRegion(),
9830b57cec5SDimitry Andric                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
9840b57cec5SDimitry Andric       ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
9850b57cec5SDimitry Andric       CausesPointerEscape = true;
9860b57cec5SDimitry Andric     } else {
9870b57cec5SDimitry Andric       const MemRegion::Kind& K = R->getKind();
9880b57cec5SDimitry Andric       if (K == MemRegion::FieldRegionKind)
9890b57cec5SDimitry Andric         if (Size && IsFirstBufInBound(C, state, E, Size)) {
9900b57cec5SDimitry Andric           // If destination buffer is a field region and access is in bound,
9910b57cec5SDimitry Andric           // do not invalidate its super region.
9920b57cec5SDimitry Andric           ITraits.setTrait(
9930b57cec5SDimitry Andric               R,
9940b57cec5SDimitry Andric               RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
9950b57cec5SDimitry Andric         }
9960b57cec5SDimitry Andric     }
9970b57cec5SDimitry Andric 
9980b57cec5SDimitry Andric     return state->invalidateRegions(R, E, C.blockCount(), LCtx,
9990b57cec5SDimitry Andric                                     CausesPointerEscape, nullptr, nullptr,
10000b57cec5SDimitry Andric                                     &ITraits);
10010b57cec5SDimitry Andric   }
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric   // If we have a non-region value by chance, just remove the binding.
10040b57cec5SDimitry Andric   // FIXME: is this necessary or correct? This handles the non-Region
10050b57cec5SDimitry Andric   //  cases.  Is it ever valid to store to these?
10060b57cec5SDimitry Andric   return state->killBinding(*L);
10070b57cec5SDimitry Andric }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
10100b57cec5SDimitry Andric                                      const MemRegion *MR) {
10110b57cec5SDimitry Andric   const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR);
10120b57cec5SDimitry Andric 
10130b57cec5SDimitry Andric   switch (MR->getKind()) {
10140b57cec5SDimitry Andric   case MemRegion::FunctionCodeRegionKind: {
10150b57cec5SDimitry Andric     const NamedDecl *FD = cast<FunctionCodeRegion>(MR)->getDecl();
10160b57cec5SDimitry Andric     if (FD)
10170b57cec5SDimitry Andric       os << "the address of the function '" << *FD << '\'';
10180b57cec5SDimitry Andric     else
10190b57cec5SDimitry Andric       os << "the address of a function";
10200b57cec5SDimitry Andric     return true;
10210b57cec5SDimitry Andric   }
10220b57cec5SDimitry Andric   case MemRegion::BlockCodeRegionKind:
10230b57cec5SDimitry Andric     os << "block text";
10240b57cec5SDimitry Andric     return true;
10250b57cec5SDimitry Andric   case MemRegion::BlockDataRegionKind:
10260b57cec5SDimitry Andric     os << "a block";
10270b57cec5SDimitry Andric     return true;
10280b57cec5SDimitry Andric   case MemRegion::CXXThisRegionKind:
10290b57cec5SDimitry Andric   case MemRegion::CXXTempObjectRegionKind:
10300b57cec5SDimitry Andric     os << "a C++ temp object of type " << TVR->getValueType().getAsString();
10310b57cec5SDimitry Andric     return true;
10320b57cec5SDimitry Andric   case MemRegion::VarRegionKind:
10330b57cec5SDimitry Andric     os << "a variable of type" << TVR->getValueType().getAsString();
10340b57cec5SDimitry Andric     return true;
10350b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
10360b57cec5SDimitry Andric     os << "a field of type " << TVR->getValueType().getAsString();
10370b57cec5SDimitry Andric     return true;
10380b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
10390b57cec5SDimitry Andric     os << "an instance variable of type " << TVR->getValueType().getAsString();
10400b57cec5SDimitry Andric     return true;
10410b57cec5SDimitry Andric   default:
10420b57cec5SDimitry Andric     return false;
10430b57cec5SDimitry Andric   }
10440b57cec5SDimitry Andric }
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
10470b57cec5SDimitry Andric                                const Expr *Size, CheckerContext &C,
10480b57cec5SDimitry Andric                                ProgramStateRef &State) {
10490b57cec5SDimitry Andric   SVal MemVal = C.getSVal(DstBuffer);
10500b57cec5SDimitry Andric   SVal SizeVal = C.getSVal(Size);
10510b57cec5SDimitry Andric   const MemRegion *MR = MemVal.getAsRegion();
10520b57cec5SDimitry Andric   if (!MR)
10530b57cec5SDimitry Andric     return false;
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric   // We're about to model memset by producing a "default binding" in the Store.
10560b57cec5SDimitry Andric   // Our current implementation - RegionStore - doesn't support default bindings
10570b57cec5SDimitry Andric   // that don't cover the whole base region. So we should first get the offset
10580b57cec5SDimitry Andric   // and the base region to figure out whether the offset of buffer is 0.
10590b57cec5SDimitry Andric   RegionOffset Offset = MR->getAsOffset();
10600b57cec5SDimitry Andric   const MemRegion *BR = Offset.getRegion();
10610b57cec5SDimitry Andric 
10620b57cec5SDimitry Andric   Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
10630b57cec5SDimitry Andric   if (!SizeNL)
10640b57cec5SDimitry Andric     return false;
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
10670b57cec5SDimitry Andric   ASTContext &Ctx = C.getASTContext();
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric   // void *memset(void *dest, int ch, size_t count);
10700b57cec5SDimitry Andric   // For now we can only handle the case of offset is 0 and concrete char value.
10710b57cec5SDimitry Andric   if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
10720b57cec5SDimitry Andric       Offset.getOffset() == 0) {
10730b57cec5SDimitry Andric     // Get the base region's extent.
10740b57cec5SDimitry Andric     auto *SubReg = cast<SubRegion>(BR);
10750b57cec5SDimitry Andric     DefinedOrUnknownSVal Extent = SubReg->getExtent(svalBuilder);
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric     ProgramStateRef StateWholeReg, StateNotWholeReg;
10780b57cec5SDimitry Andric     std::tie(StateWholeReg, StateNotWholeReg) =
10790b57cec5SDimitry Andric         State->assume(svalBuilder.evalEQ(State, Extent, *SizeNL));
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric     // With the semantic of 'memset()', we should convert the CharVal to
10820b57cec5SDimitry Andric     // unsigned char.
10830b57cec5SDimitry Andric     CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric     ProgramStateRef StateNullChar, StateNonNullChar;
10860b57cec5SDimitry Andric     std::tie(StateNullChar, StateNonNullChar) =
10870b57cec5SDimitry Andric         assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric     if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
10900b57cec5SDimitry Andric         !StateNonNullChar) {
10910b57cec5SDimitry Andric       // If the 'memset()' acts on the whole region of destination buffer and
10920b57cec5SDimitry Andric       // the value of the second argument of 'memset()' is zero, bind the second
10930b57cec5SDimitry Andric       // argument's value to the destination buffer with 'default binding'.
10940b57cec5SDimitry Andric       // FIXME: Since there is no perfect way to bind the non-zero character, we
10950b57cec5SDimitry Andric       // can only deal with zero value here. In the future, we need to deal with
10960b57cec5SDimitry Andric       // the binding of non-zero value in the case of whole region.
10970b57cec5SDimitry Andric       State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
10980b57cec5SDimitry Andric                                      C.getLocationContext());
10990b57cec5SDimitry Andric     } else {
11000b57cec5SDimitry Andric       // If the destination buffer's extent is not equal to the value of
11010b57cec5SDimitry Andric       // third argument, just invalidate buffer.
11020b57cec5SDimitry Andric       State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11030b57cec5SDimitry Andric                                /*IsSourceBuffer*/ false, Size);
11040b57cec5SDimitry Andric     }
11050b57cec5SDimitry Andric 
11060b57cec5SDimitry Andric     if (StateNullChar && !StateNonNullChar) {
11070b57cec5SDimitry Andric       // If the value of the second argument of 'memset()' is zero, set the
11080b57cec5SDimitry Andric       // string length of destination buffer to 0 directly.
11090b57cec5SDimitry Andric       State = setCStringLength(State, MR,
11100b57cec5SDimitry Andric                                svalBuilder.makeZeroVal(Ctx.getSizeType()));
11110b57cec5SDimitry Andric     } else if (!StateNullChar && StateNonNullChar) {
11120b57cec5SDimitry Andric       SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
11130b57cec5SDimitry Andric           CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
11140b57cec5SDimitry Andric           C.getLocationContext(), C.blockCount());
11150b57cec5SDimitry Andric 
11160b57cec5SDimitry Andric       // If the value of second argument is not zero, then the string length
11170b57cec5SDimitry Andric       // is at least the size argument.
11180b57cec5SDimitry Andric       SVal NewStrLenGESize = svalBuilder.evalBinOp(
11190b57cec5SDimitry Andric           State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric       State = setCStringLength(
11220b57cec5SDimitry Andric           State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
11230b57cec5SDimitry Andric           MR, NewStrLen);
11240b57cec5SDimitry Andric     }
11250b57cec5SDimitry Andric   } else {
11260b57cec5SDimitry Andric     // If the offset is not zero and char value is not concrete, we can do
11270b57cec5SDimitry Andric     // nothing but invalidate the buffer.
11280b57cec5SDimitry Andric     State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11290b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, Size);
11300b57cec5SDimitry Andric   }
11310b57cec5SDimitry Andric   return true;
11320b57cec5SDimitry Andric }
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11350b57cec5SDimitry Andric // evaluation of individual function calls.
11360b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11370b57cec5SDimitry Andric 
11380b57cec5SDimitry Andric void CStringChecker::evalCopyCommon(CheckerContext &C,
11390b57cec5SDimitry Andric                                     const CallExpr *CE,
11400b57cec5SDimitry Andric                                     ProgramStateRef state,
11410b57cec5SDimitry Andric                                     const Expr *Size, const Expr *Dest,
11420b57cec5SDimitry Andric                                     const Expr *Source, bool Restricted,
11430b57cec5SDimitry Andric                                     bool IsMempcpy) const {
11440b57cec5SDimitry Andric   CurrentFunctionDescription = "memory copy function";
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   // See if the size argument is zero.
11470b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
11480b57cec5SDimitry Andric   SVal sizeVal = state->getSVal(Size, LCtx);
11490b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
11520b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
11530b57cec5SDimitry Andric     assumeZero(C, state, sizeVal, sizeTy);
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric   // Get the value of the Dest.
11560b57cec5SDimitry Andric   SVal destVal = state->getSVal(Dest, LCtx);
11570b57cec5SDimitry Andric 
11580b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
11590b57cec5SDimitry Andric   // just bind the return value to the destination buffer and return.
11600b57cec5SDimitry Andric   if (stateZeroSize && !stateNonZeroSize) {
11610b57cec5SDimitry Andric     stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
11620b57cec5SDimitry Andric     C.addTransition(stateZeroSize);
11630b57cec5SDimitry Andric     return;
11640b57cec5SDimitry Andric   }
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
11670b57cec5SDimitry Andric   if (stateNonZeroSize) {
11680b57cec5SDimitry Andric     state = stateNonZeroSize;
11690b57cec5SDimitry Andric 
11700b57cec5SDimitry Andric     // Ensure the destination is not null. If it is NULL there will be a
11710b57cec5SDimitry Andric     // NULL pointer dereference.
1172*a7dea167SDimitry Andric     state = checkNonNull(C, state, Dest, destVal, 1);
11730b57cec5SDimitry Andric     if (!state)
11740b57cec5SDimitry Andric       return;
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric     // Get the value of the Src.
11770b57cec5SDimitry Andric     SVal srcVal = state->getSVal(Source, LCtx);
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric     // Ensure the source is not null. If it is NULL there will be a
11800b57cec5SDimitry Andric     // NULL pointer dereference.
1181*a7dea167SDimitry Andric     state = checkNonNull(C, state, Source, srcVal, 2);
11820b57cec5SDimitry Andric     if (!state)
11830b57cec5SDimitry Andric       return;
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric     // Ensure the accesses are valid and that the buffers do not overlap.
11860b57cec5SDimitry Andric     const char * const writeWarning =
11870b57cec5SDimitry Andric       "Memory copy function overflows destination buffer";
11880b57cec5SDimitry Andric     state = CheckBufferAccess(C, state, Size, Dest, Source,
11890b57cec5SDimitry Andric                               writeWarning, /* sourceWarning = */ nullptr);
11900b57cec5SDimitry Andric     if (Restricted)
11910b57cec5SDimitry Andric       state = CheckOverlap(C, state, Size, Dest, Source);
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric     if (!state)
11940b57cec5SDimitry Andric       return;
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric     // If this is mempcpy, get the byte after the last byte copied and
11970b57cec5SDimitry Andric     // bind the expr.
11980b57cec5SDimitry Andric     if (IsMempcpy) {
11990b57cec5SDimitry Andric       // Get the byte after the last byte copied.
12000b57cec5SDimitry Andric       SValBuilder &SvalBuilder = C.getSValBuilder();
12010b57cec5SDimitry Andric       ASTContext &Ctx = SvalBuilder.getContext();
12020b57cec5SDimitry Andric       QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
12030b57cec5SDimitry Andric       SVal DestRegCharVal =
12040b57cec5SDimitry Andric           SvalBuilder.evalCast(destVal, CharPtrTy, Dest->getType());
12050b57cec5SDimitry Andric       SVal lastElement = C.getSValBuilder().evalBinOp(
12060b57cec5SDimitry Andric           state, BO_Add, DestRegCharVal, sizeVal, Dest->getType());
12070b57cec5SDimitry Andric       // If we don't know how much we copied, we can at least
12080b57cec5SDimitry Andric       // conjure a return value for later.
12090b57cec5SDimitry Andric       if (lastElement.isUnknown())
12100b57cec5SDimitry Andric         lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
12110b57cec5SDimitry Andric                                                           C.blockCount());
12120b57cec5SDimitry Andric 
12130b57cec5SDimitry Andric       // The byte after the last byte copied is the return value.
12140b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, lastElement);
12150b57cec5SDimitry Andric     } else {
12160b57cec5SDimitry Andric       // All other copies return the destination buffer.
12170b57cec5SDimitry Andric       // (Well, bcopy() has a void return type, but this won't hurt.)
12180b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, destVal);
12190b57cec5SDimitry Andric     }
12200b57cec5SDimitry Andric 
12210b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
12220b57cec5SDimitry Andric     // the address of the top-level region).
12230b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
12240b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
12250b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
12260b57cec5SDimitry Andric     // copied region, but that's still an improvement over blank invalidation.
12270b57cec5SDimitry Andric     state = InvalidateBuffer(C, state, Dest, C.getSVal(Dest),
12280b57cec5SDimitry Andric                              /*IsSourceBuffer*/false, Size);
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
12310b57cec5SDimitry Andric     // the address of the top-level region).
12320b57cec5SDimitry Andric     state = InvalidateBuffer(C, state, Source, C.getSVal(Source),
12330b57cec5SDimitry Andric                              /*IsSourceBuffer*/true, nullptr);
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric     C.addTransition(state);
12360b57cec5SDimitry Andric   }
12370b57cec5SDimitry Andric }
12380b57cec5SDimitry Andric 
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
12410b57cec5SDimitry Andric   // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
12420b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
12430b57cec5SDimitry Andric   const Expr *Dest = CE->getArg(0);
12440b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric   evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
12470b57cec5SDimitry Andric }
12480b57cec5SDimitry Andric 
12490b57cec5SDimitry Andric void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
12500b57cec5SDimitry Andric   // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
12510b57cec5SDimitry Andric   // The return value is a pointer to the byte following the last written byte.
12520b57cec5SDimitry Andric   const Expr *Dest = CE->getArg(0);
12530b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
12540b57cec5SDimitry Andric 
12550b57cec5SDimitry Andric   evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
12560b57cec5SDimitry Andric }
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
12590b57cec5SDimitry Andric   // void *memmove(void *dst, const void *src, size_t n);
12600b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
12610b57cec5SDimitry Andric   const Expr *Dest = CE->getArg(0);
12620b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
12630b57cec5SDimitry Andric 
12640b57cec5SDimitry Andric   evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
12650b57cec5SDimitry Andric }
12660b57cec5SDimitry Andric 
12670b57cec5SDimitry Andric void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
12680b57cec5SDimitry Andric   // void bcopy(const void *src, void *dst, size_t n);
12690b57cec5SDimitry Andric   evalCopyCommon(C, CE, C.getState(),
12700b57cec5SDimitry Andric                  CE->getArg(2), CE->getArg(1), CE->getArg(0));
12710b57cec5SDimitry Andric }
12720b57cec5SDimitry Andric 
12730b57cec5SDimitry Andric void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
12740b57cec5SDimitry Andric   // int memcmp(const void *s1, const void *s2, size_t n);
12750b57cec5SDimitry Andric   CurrentFunctionDescription = "memory comparison function";
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric   const Expr *Left = CE->getArg(0);
12780b57cec5SDimitry Andric   const Expr *Right = CE->getArg(1);
12790b57cec5SDimitry Andric   const Expr *Size = CE->getArg(2);
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
12820b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric   // See if the size argument is zero.
12850b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
12860b57cec5SDimitry Andric   SVal sizeVal = state->getSVal(Size, LCtx);
12870b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
12900b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
12910b57cec5SDimitry Andric     assumeZero(C, state, sizeVal, sizeTy);
12920b57cec5SDimitry Andric 
12930b57cec5SDimitry Andric   // If the size can be zero, the result will be 0 in that case, and we don't
12940b57cec5SDimitry Andric   // have to check either of the buffers.
12950b57cec5SDimitry Andric   if (stateZeroSize) {
12960b57cec5SDimitry Andric     state = stateZeroSize;
12970b57cec5SDimitry Andric     state = state->BindExpr(CE, LCtx,
12980b57cec5SDimitry Andric                             svalBuilder.makeZeroVal(CE->getType()));
12990b57cec5SDimitry Andric     C.addTransition(state);
13000b57cec5SDimitry Andric   }
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
13030b57cec5SDimitry Andric   if (stateNonZeroSize) {
13040b57cec5SDimitry Andric     state = stateNonZeroSize;
13050b57cec5SDimitry Andric     // If we know the two buffers are the same, we know the result is 0.
13060b57cec5SDimitry Andric     // First, get the two buffers' addresses. Another checker will have already
13070b57cec5SDimitry Andric     // made sure they're not undefined.
13080b57cec5SDimitry Andric     DefinedOrUnknownSVal LV =
13090b57cec5SDimitry Andric         state->getSVal(Left, LCtx).castAs<DefinedOrUnknownSVal>();
13100b57cec5SDimitry Andric     DefinedOrUnknownSVal RV =
13110b57cec5SDimitry Andric         state->getSVal(Right, LCtx).castAs<DefinedOrUnknownSVal>();
13120b57cec5SDimitry Andric 
13130b57cec5SDimitry Andric     // See if they are the same.
13140b57cec5SDimitry Andric     DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
13150b57cec5SDimitry Andric     ProgramStateRef StSameBuf, StNotSameBuf;
13160b57cec5SDimitry Andric     std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric     // If the two arguments might be the same buffer, we know the result is 0,
13190b57cec5SDimitry Andric     // and we only need to check one size.
13200b57cec5SDimitry Andric     if (StSameBuf) {
13210b57cec5SDimitry Andric       state = StSameBuf;
13220b57cec5SDimitry Andric       state = CheckBufferAccess(C, state, Size, Left);
13230b57cec5SDimitry Andric       if (state) {
13240b57cec5SDimitry Andric         state = StSameBuf->BindExpr(CE, LCtx,
13250b57cec5SDimitry Andric                                     svalBuilder.makeZeroVal(CE->getType()));
13260b57cec5SDimitry Andric         C.addTransition(state);
13270b57cec5SDimitry Andric       }
13280b57cec5SDimitry Andric     }
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric     // If the two arguments might be different buffers, we have to check the
13310b57cec5SDimitry Andric     // size of both of them.
13320b57cec5SDimitry Andric     if (StNotSameBuf) {
13330b57cec5SDimitry Andric       state = StNotSameBuf;
13340b57cec5SDimitry Andric       state = CheckBufferAccess(C, state, Size, Left, Right);
13350b57cec5SDimitry Andric       if (state) {
13360b57cec5SDimitry Andric         // The return value is the comparison result, which we don't know.
13370b57cec5SDimitry Andric         SVal CmpV = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
13380b57cec5SDimitry Andric                                                  C.blockCount());
13390b57cec5SDimitry Andric         state = state->BindExpr(CE, LCtx, CmpV);
13400b57cec5SDimitry Andric         C.addTransition(state);
13410b57cec5SDimitry Andric       }
13420b57cec5SDimitry Andric     }
13430b57cec5SDimitry Andric   }
13440b57cec5SDimitry Andric }
13450b57cec5SDimitry Andric 
13460b57cec5SDimitry Andric void CStringChecker::evalstrLength(CheckerContext &C,
13470b57cec5SDimitry Andric                                    const CallExpr *CE) const {
13480b57cec5SDimitry Andric   // size_t strlen(const char *s);
13490b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
13500b57cec5SDimitry Andric }
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric void CStringChecker::evalstrnLength(CheckerContext &C,
13530b57cec5SDimitry Andric                                     const CallExpr *CE) const {
13540b57cec5SDimitry Andric   // size_t strnlen(const char *s, size_t maxlen);
13550b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
13560b57cec5SDimitry Andric }
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
13590b57cec5SDimitry Andric                                          bool IsStrnlen) const {
13600b57cec5SDimitry Andric   CurrentFunctionDescription = "string length function";
13610b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
13620b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric   if (IsStrnlen) {
13650b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
13660b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric     ProgramStateRef stateZeroSize, stateNonZeroSize;
13690b57cec5SDimitry Andric     std::tie(stateZeroSize, stateNonZeroSize) =
13700b57cec5SDimitry Andric       assumeZero(C, state, maxlenVal, maxlenExpr->getType());
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric     // If the size can be zero, the result will be 0 in that case, and we don't
13730b57cec5SDimitry Andric     // have to check the string itself.
13740b57cec5SDimitry Andric     if (stateZeroSize) {
13750b57cec5SDimitry Andric       SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
13760b57cec5SDimitry Andric       stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
13770b57cec5SDimitry Andric       C.addTransition(stateZeroSize);
13780b57cec5SDimitry Andric     }
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric     // If the size is GUARANTEED to be zero, we're done!
13810b57cec5SDimitry Andric     if (!stateNonZeroSize)
13820b57cec5SDimitry Andric       return;
13830b57cec5SDimitry Andric 
13840b57cec5SDimitry Andric     // Otherwise, record the assumption that the size is nonzero.
13850b57cec5SDimitry Andric     state = stateNonZeroSize;
13860b57cec5SDimitry Andric   }
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric   // Check that the string argument is non-null.
13890b57cec5SDimitry Andric   const Expr *Arg = CE->getArg(0);
13900b57cec5SDimitry Andric   SVal ArgVal = state->getSVal(Arg, LCtx);
13910b57cec5SDimitry Andric 
1392*a7dea167SDimitry Andric   state = checkNonNull(C, state, Arg, ArgVal, 1);
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric   if (!state)
13950b57cec5SDimitry Andric     return;
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   SVal strLength = getCStringLength(C, state, Arg, ArgVal);
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric   // If the argument isn't a valid C string, there's no valid state to
14000b57cec5SDimitry Andric   // transition to.
14010b57cec5SDimitry Andric   if (strLength.isUndef())
14020b57cec5SDimitry Andric     return;
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric   DefinedOrUnknownSVal result = UnknownVal();
14050b57cec5SDimitry Andric 
14060b57cec5SDimitry Andric   // If the check is for strnlen() then bind the return value to no more than
14070b57cec5SDimitry Andric   // the maxlen value.
14080b57cec5SDimitry Andric   if (IsStrnlen) {
14090b57cec5SDimitry Andric     QualType cmpTy = C.getSValBuilder().getConditionType();
14100b57cec5SDimitry Andric 
14110b57cec5SDimitry Andric     // It's a little unfortunate to be getting this again,
14120b57cec5SDimitry Andric     // but it's not that expensive...
14130b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
14140b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric     Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
14170b57cec5SDimitry Andric     Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric     if (strLengthNL && maxlenValNL) {
14200b57cec5SDimitry Andric       ProgramStateRef stateStringTooLong, stateStringNotTooLong;
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric       // Check if the strLength is greater than the maxlen.
14230b57cec5SDimitry Andric       std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
14240b57cec5SDimitry Andric           C.getSValBuilder()
14250b57cec5SDimitry Andric               .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
14260b57cec5SDimitry Andric               .castAs<DefinedOrUnknownSVal>());
14270b57cec5SDimitry Andric 
14280b57cec5SDimitry Andric       if (stateStringTooLong && !stateStringNotTooLong) {
14290b57cec5SDimitry Andric         // If the string is longer than maxlen, return maxlen.
14300b57cec5SDimitry Andric         result = *maxlenValNL;
14310b57cec5SDimitry Andric       } else if (stateStringNotTooLong && !stateStringTooLong) {
14320b57cec5SDimitry Andric         // If the string is shorter than maxlen, return its length.
14330b57cec5SDimitry Andric         result = *strLengthNL;
14340b57cec5SDimitry Andric       }
14350b57cec5SDimitry Andric     }
14360b57cec5SDimitry Andric 
14370b57cec5SDimitry Andric     if (result.isUnknown()) {
14380b57cec5SDimitry Andric       // If we don't have enough information for a comparison, there's
14390b57cec5SDimitry Andric       // no guarantee the full string length will actually be returned.
14400b57cec5SDimitry Andric       // All we know is the return value is the min of the string length
14410b57cec5SDimitry Andric       // and the limit. This is better than nothing.
14420b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
14430b57cec5SDimitry Andric                                                    C.blockCount());
14440b57cec5SDimitry Andric       NonLoc resultNL = result.castAs<NonLoc>();
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric       if (strLengthNL) {
14470b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
14480b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *strLengthNL, cmpTy)
14490b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
14500b57cec5SDimitry Andric       }
14510b57cec5SDimitry Andric 
14520b57cec5SDimitry Andric       if (maxlenValNL) {
14530b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
14540b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *maxlenValNL, cmpTy)
14550b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
14560b57cec5SDimitry Andric       }
14570b57cec5SDimitry Andric     }
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric   } else {
14600b57cec5SDimitry Andric     // This is a plain strlen(), not strnlen().
14610b57cec5SDimitry Andric     result = strLength.castAs<DefinedOrUnknownSVal>();
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric     // If we don't know the length of the string, conjure a return
14640b57cec5SDimitry Andric     // value, so it can be used in constraints, at least.
14650b57cec5SDimitry Andric     if (result.isUnknown()) {
14660b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
14670b57cec5SDimitry Andric                                                    C.blockCount());
14680b57cec5SDimitry Andric     }
14690b57cec5SDimitry Andric   }
14700b57cec5SDimitry Andric 
14710b57cec5SDimitry Andric   // Bind the return value.
14720b57cec5SDimitry Andric   assert(!result.isUnknown() && "Should have conjured a value by now");
14730b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, result);
14740b57cec5SDimitry Andric   C.addTransition(state);
14750b57cec5SDimitry Andric }
14760b57cec5SDimitry Andric 
14770b57cec5SDimitry Andric void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
14780b57cec5SDimitry Andric   // char *strcpy(char *restrict dst, const char *restrict src);
14790b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
14800b57cec5SDimitry Andric                    /* returnEnd = */ false,
14810b57cec5SDimitry Andric                    /* isBounded = */ false,
14820b57cec5SDimitry Andric                    /* isAppending = */ false);
14830b57cec5SDimitry Andric }
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
14860b57cec5SDimitry Andric   // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
14870b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
14880b57cec5SDimitry Andric                    /* returnEnd = */ false,
14890b57cec5SDimitry Andric                    /* isBounded = */ true,
14900b57cec5SDimitry Andric                    /* isAppending = */ false);
14910b57cec5SDimitry Andric }
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
14940b57cec5SDimitry Andric   // char *stpcpy(char *restrict dst, const char *restrict src);
14950b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
14960b57cec5SDimitry Andric                    /* returnEnd = */ true,
14970b57cec5SDimitry Andric                    /* isBounded = */ false,
14980b57cec5SDimitry Andric                    /* isAppending = */ false);
14990b57cec5SDimitry Andric }
15000b57cec5SDimitry Andric 
15010b57cec5SDimitry Andric void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
15020b57cec5SDimitry Andric   // char *strlcpy(char *dst, const char *src, size_t n);
15030b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
15040b57cec5SDimitry Andric                    /* returnEnd = */ true,
15050b57cec5SDimitry Andric                    /* isBounded = */ true,
15060b57cec5SDimitry Andric                    /* isAppending = */ false,
15070b57cec5SDimitry Andric                    /* returnPtr = */ false);
15080b57cec5SDimitry Andric }
15090b57cec5SDimitry Andric 
15100b57cec5SDimitry Andric void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
15110b57cec5SDimitry Andric   //char *strcat(char *restrict s1, const char *restrict s2);
15120b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
15130b57cec5SDimitry Andric                    /* returnEnd = */ false,
15140b57cec5SDimitry Andric                    /* isBounded = */ false,
15150b57cec5SDimitry Andric                    /* isAppending = */ true);
15160b57cec5SDimitry Andric }
15170b57cec5SDimitry Andric 
15180b57cec5SDimitry Andric void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
15190b57cec5SDimitry Andric   //char *strncat(char *restrict s1, const char *restrict s2, size_t n);
15200b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
15210b57cec5SDimitry Andric                    /* returnEnd = */ false,
15220b57cec5SDimitry Andric                    /* isBounded = */ true,
15230b57cec5SDimitry Andric                    /* isAppending = */ true);
15240b57cec5SDimitry Andric }
15250b57cec5SDimitry Andric 
15260b57cec5SDimitry Andric void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
15270b57cec5SDimitry Andric   // FIXME: strlcat() uses a different rule for bound checking, i.e. 'n' means
15280b57cec5SDimitry Andric   // a different thing as compared to strncat(). This currently causes
15290b57cec5SDimitry Andric   // false positives in the alpha string bound checker.
15300b57cec5SDimitry Andric 
15310b57cec5SDimitry Andric   //char *strlcat(char *s1, const char *s2, size_t n);
15320b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
15330b57cec5SDimitry Andric                    /* returnEnd = */ false,
15340b57cec5SDimitry Andric                    /* isBounded = */ true,
15350b57cec5SDimitry Andric                    /* isAppending = */ true,
15360b57cec5SDimitry Andric                    /* returnPtr = */ false);
15370b57cec5SDimitry Andric }
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
15400b57cec5SDimitry Andric                                       bool returnEnd, bool isBounded,
15410b57cec5SDimitry Andric                                       bool isAppending, bool returnPtr) const {
15420b57cec5SDimitry Andric   CurrentFunctionDescription = "string copy function";
15430b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
15440b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric   // Check that the destination is non-null.
15470b57cec5SDimitry Andric   const Expr *Dst = CE->getArg(0);
15480b57cec5SDimitry Andric   SVal DstVal = state->getSVal(Dst, LCtx);
15490b57cec5SDimitry Andric 
1550*a7dea167SDimitry Andric   state = checkNonNull(C, state, Dst, DstVal, 1);
15510b57cec5SDimitry Andric   if (!state)
15520b57cec5SDimitry Andric     return;
15530b57cec5SDimitry Andric 
15540b57cec5SDimitry Andric   // Check that the source is non-null.
15550b57cec5SDimitry Andric   const Expr *srcExpr = CE->getArg(1);
15560b57cec5SDimitry Andric   SVal srcVal = state->getSVal(srcExpr, LCtx);
1557*a7dea167SDimitry Andric   state = checkNonNull(C, state, srcExpr, srcVal, 2);
15580b57cec5SDimitry Andric   if (!state)
15590b57cec5SDimitry Andric     return;
15600b57cec5SDimitry Andric 
15610b57cec5SDimitry Andric   // Get the string length of the source.
15620b57cec5SDimitry Andric   SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric   // If the source isn't a valid C string, give up.
15650b57cec5SDimitry Andric   if (strLength.isUndef())
15660b57cec5SDimitry Andric     return;
15670b57cec5SDimitry Andric 
15680b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
15690b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
15700b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric   // These two values allow checking two kinds of errors:
15730b57cec5SDimitry Andric   // - actual overflows caused by a source that doesn't fit in the destination
15740b57cec5SDimitry Andric   // - potential overflows caused by a bound that could exceed the destination
15750b57cec5SDimitry Andric   SVal amountCopied = UnknownVal();
15760b57cec5SDimitry Andric   SVal maxLastElementIndex = UnknownVal();
15770b57cec5SDimitry Andric   const char *boundWarning = nullptr;
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric   state = CheckOverlap(C, state, isBounded ? CE->getArg(2) : CE->getArg(1), Dst, srcExpr);
15800b57cec5SDimitry Andric 
15810b57cec5SDimitry Andric   if (!state)
15820b57cec5SDimitry Andric     return;
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric   // If the function is strncpy, strncat, etc... it is bounded.
15850b57cec5SDimitry Andric   if (isBounded) {
15860b57cec5SDimitry Andric     // Get the max number of characters to copy.
15870b57cec5SDimitry Andric     const Expr *lenExpr = CE->getArg(2);
15880b57cec5SDimitry Andric     SVal lenVal = state->getSVal(lenExpr, LCtx);
15890b57cec5SDimitry Andric 
15900b57cec5SDimitry Andric     // Protect against misdeclared strncpy().
15910b57cec5SDimitry Andric     lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType());
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric     Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
15940b57cec5SDimitry Andric     Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric     // If we know both values, we might be able to figure out how much
15970b57cec5SDimitry Andric     // we're copying.
15980b57cec5SDimitry Andric     if (strLengthNL && lenValNL) {
15990b57cec5SDimitry Andric       ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
16000b57cec5SDimitry Andric 
16010b57cec5SDimitry Andric       // Check if the max number to copy is less than the length of the src.
16020b57cec5SDimitry Andric       // If the bound is equal to the source length, strncpy won't null-
16030b57cec5SDimitry Andric       // terminate the result!
16040b57cec5SDimitry Andric       std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
16050b57cec5SDimitry Andric           svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
16060b57cec5SDimitry Andric               .castAs<DefinedOrUnknownSVal>());
16070b57cec5SDimitry Andric 
16080b57cec5SDimitry Andric       if (stateSourceTooLong && !stateSourceNotTooLong) {
16090b57cec5SDimitry Andric         // Max number to copy is less than the length of the src, so the actual
16100b57cec5SDimitry Andric         // strLength copied is the max number arg.
16110b57cec5SDimitry Andric         state = stateSourceTooLong;
16120b57cec5SDimitry Andric         amountCopied = lenVal;
16130b57cec5SDimitry Andric 
16140b57cec5SDimitry Andric       } else if (!stateSourceTooLong && stateSourceNotTooLong) {
16150b57cec5SDimitry Andric         // The source buffer entirely fits in the bound.
16160b57cec5SDimitry Andric         state = stateSourceNotTooLong;
16170b57cec5SDimitry Andric         amountCopied = strLength;
16180b57cec5SDimitry Andric       }
16190b57cec5SDimitry Andric     }
16200b57cec5SDimitry Andric 
16210b57cec5SDimitry Andric     // We still want to know if the bound is known to be too large.
16220b57cec5SDimitry Andric     if (lenValNL) {
16230b57cec5SDimitry Andric       if (isAppending) {
16240b57cec5SDimitry Andric         // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric         // Get the string length of the destination. If the destination is
16270b57cec5SDimitry Andric         // memory that can't have a string length, we shouldn't be copying
16280b57cec5SDimitry Andric         // into it anyway.
16290b57cec5SDimitry Andric         SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
16300b57cec5SDimitry Andric         if (dstStrLength.isUndef())
16310b57cec5SDimitry Andric           return;
16320b57cec5SDimitry Andric 
16330b57cec5SDimitry Andric         if (Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>()) {
16340b57cec5SDimitry Andric           maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add,
16350b57cec5SDimitry Andric                                                         *lenValNL,
16360b57cec5SDimitry Andric                                                         *dstStrLengthNL,
16370b57cec5SDimitry Andric                                                         sizeTy);
16380b57cec5SDimitry Andric           boundWarning = "Size argument is greater than the free space in the "
16390b57cec5SDimitry Andric                          "destination buffer";
16400b57cec5SDimitry Andric         }
16410b57cec5SDimitry Andric 
16420b57cec5SDimitry Andric       } else {
16430b57cec5SDimitry Andric         // For strncpy, this is just checking that lenVal <= sizeof(dst)
16440b57cec5SDimitry Andric         // (Yes, strncpy and strncat differ in how they treat termination.
16450b57cec5SDimitry Andric         // strncat ALWAYS terminates, but strncpy doesn't.)
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric         // We need a special case for when the copy size is zero, in which
16480b57cec5SDimitry Andric         // case strncpy will do no work at all. Our bounds check uses n-1
16490b57cec5SDimitry Andric         // as the last element accessed, so n == 0 is problematic.
16500b57cec5SDimitry Andric         ProgramStateRef StateZeroSize, StateNonZeroSize;
16510b57cec5SDimitry Andric         std::tie(StateZeroSize, StateNonZeroSize) =
16520b57cec5SDimitry Andric           assumeZero(C, state, *lenValNL, sizeTy);
16530b57cec5SDimitry Andric 
16540b57cec5SDimitry Andric         // If the size is known to be zero, we're done.
16550b57cec5SDimitry Andric         if (StateZeroSize && !StateNonZeroSize) {
16560b57cec5SDimitry Andric           if (returnPtr) {
16570b57cec5SDimitry Andric             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
16580b57cec5SDimitry Andric           } else {
16590b57cec5SDimitry Andric             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, *lenValNL);
16600b57cec5SDimitry Andric           }
16610b57cec5SDimitry Andric           C.addTransition(StateZeroSize);
16620b57cec5SDimitry Andric           return;
16630b57cec5SDimitry Andric         }
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric         // Otherwise, go ahead and figure out the last element we'll touch.
16660b57cec5SDimitry Andric         // We don't record the non-zero assumption here because we can't
16670b57cec5SDimitry Andric         // be sure. We won't warn on a possible zero.
16680b57cec5SDimitry Andric         NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
16690b57cec5SDimitry Andric         maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
16700b57cec5SDimitry Andric                                                       one, sizeTy);
16710b57cec5SDimitry Andric         boundWarning = "Size argument is greater than the length of the "
16720b57cec5SDimitry Andric                        "destination buffer";
16730b57cec5SDimitry Andric       }
16740b57cec5SDimitry Andric     }
16750b57cec5SDimitry Andric 
16760b57cec5SDimitry Andric     // If we couldn't pin down the copy length, at least bound it.
16770b57cec5SDimitry Andric     // FIXME: We should actually run this code path for append as well, but
16780b57cec5SDimitry Andric     // right now it creates problems with constraints (since we can end up
16790b57cec5SDimitry Andric     // trying to pass constraints from symbol to symbol).
16800b57cec5SDimitry Andric     if (amountCopied.isUnknown() && !isAppending) {
16810b57cec5SDimitry Andric       // Try to get a "hypothetical" string length symbol, which we can later
16820b57cec5SDimitry Andric       // set as a real value if that turns out to be the case.
16830b57cec5SDimitry Andric       amountCopied = getCStringLength(C, state, lenExpr, srcVal, true);
16840b57cec5SDimitry Andric       assert(!amountCopied.isUndef());
16850b57cec5SDimitry Andric 
16860b57cec5SDimitry Andric       if (Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>()) {
16870b57cec5SDimitry Andric         if (lenValNL) {
16880b57cec5SDimitry Andric           // amountCopied <= lenVal
16890b57cec5SDimitry Andric           SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE,
16900b57cec5SDimitry Andric                                                              *amountCopiedNL,
16910b57cec5SDimitry Andric                                                              *lenValNL,
16920b57cec5SDimitry Andric                                                              cmpTy);
16930b57cec5SDimitry Andric           state = state->assume(
16940b57cec5SDimitry Andric               copiedLessThanBound.castAs<DefinedOrUnknownSVal>(), true);
16950b57cec5SDimitry Andric           if (!state)
16960b57cec5SDimitry Andric             return;
16970b57cec5SDimitry Andric         }
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric         if (strLengthNL) {
17000b57cec5SDimitry Andric           // amountCopied <= strlen(source)
17010b57cec5SDimitry Andric           SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE,
17020b57cec5SDimitry Andric                                                            *amountCopiedNL,
17030b57cec5SDimitry Andric                                                            *strLengthNL,
17040b57cec5SDimitry Andric                                                            cmpTy);
17050b57cec5SDimitry Andric           state = state->assume(
17060b57cec5SDimitry Andric               copiedLessThanSrc.castAs<DefinedOrUnknownSVal>(), true);
17070b57cec5SDimitry Andric           if (!state)
17080b57cec5SDimitry Andric             return;
17090b57cec5SDimitry Andric         }
17100b57cec5SDimitry Andric       }
17110b57cec5SDimitry Andric     }
17120b57cec5SDimitry Andric 
17130b57cec5SDimitry Andric   } else {
17140b57cec5SDimitry Andric     // The function isn't bounded. The amount copied should match the length
17150b57cec5SDimitry Andric     // of the source buffer.
17160b57cec5SDimitry Andric     amountCopied = strLength;
17170b57cec5SDimitry Andric   }
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   assert(state);
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   // This represents the number of characters copied into the destination
17220b57cec5SDimitry Andric   // buffer. (It may not actually be the strlen if the destination buffer
17230b57cec5SDimitry Andric   // is not terminated.)
17240b57cec5SDimitry Andric   SVal finalStrLength = UnknownVal();
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric   // If this is an appending function (strcat, strncat...) then set the
17270b57cec5SDimitry Andric   // string length to strlen(src) + strlen(dst) since the buffer will
17280b57cec5SDimitry Andric   // ultimately contain both.
17290b57cec5SDimitry Andric   if (isAppending) {
17300b57cec5SDimitry Andric     // Get the string length of the destination. If the destination is memory
17310b57cec5SDimitry Andric     // that can't have a string length, we shouldn't be copying into it anyway.
17320b57cec5SDimitry Andric     SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
17330b57cec5SDimitry Andric     if (dstStrLength.isUndef())
17340b57cec5SDimitry Andric       return;
17350b57cec5SDimitry Andric 
17360b57cec5SDimitry Andric     Optional<NonLoc> srcStrLengthNL = amountCopied.getAs<NonLoc>();
17370b57cec5SDimitry Andric     Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric     // If we know both string lengths, we might know the final string length.
17400b57cec5SDimitry Andric     if (srcStrLengthNL && dstStrLengthNL) {
17410b57cec5SDimitry Andric       // Make sure the two lengths together don't overflow a size_t.
17420b57cec5SDimitry Andric       state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL);
17430b57cec5SDimitry Andric       if (!state)
17440b57cec5SDimitry Andric         return;
17450b57cec5SDimitry Andric 
17460b57cec5SDimitry Andric       finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL,
17470b57cec5SDimitry Andric                                                *dstStrLengthNL, sizeTy);
17480b57cec5SDimitry Andric     }
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric     // If we couldn't get a single value for the final string length,
17510b57cec5SDimitry Andric     // we can at least bound it by the individual lengths.
17520b57cec5SDimitry Andric     if (finalStrLength.isUnknown()) {
17530b57cec5SDimitry Andric       // Try to get a "hypothetical" string length symbol, which we can later
17540b57cec5SDimitry Andric       // set as a real value if that turns out to be the case.
17550b57cec5SDimitry Andric       finalStrLength = getCStringLength(C, state, CE, DstVal, true);
17560b57cec5SDimitry Andric       assert(!finalStrLength.isUndef());
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric       if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) {
17590b57cec5SDimitry Andric         if (srcStrLengthNL) {
17600b57cec5SDimitry Andric           // finalStrLength >= srcStrLength
17610b57cec5SDimitry Andric           SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE,
17620b57cec5SDimitry Andric                                                         *finalStrLengthNL,
17630b57cec5SDimitry Andric                                                         *srcStrLengthNL,
17640b57cec5SDimitry Andric                                                         cmpTy);
17650b57cec5SDimitry Andric           state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
17660b57cec5SDimitry Andric                                 true);
17670b57cec5SDimitry Andric           if (!state)
17680b57cec5SDimitry Andric             return;
17690b57cec5SDimitry Andric         }
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric         if (dstStrLengthNL) {
17720b57cec5SDimitry Andric           // finalStrLength >= dstStrLength
17730b57cec5SDimitry Andric           SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
17740b57cec5SDimitry Andric                                                       *finalStrLengthNL,
17750b57cec5SDimitry Andric                                                       *dstStrLengthNL,
17760b57cec5SDimitry Andric                                                       cmpTy);
17770b57cec5SDimitry Andric           state =
17780b57cec5SDimitry Andric               state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
17790b57cec5SDimitry Andric           if (!state)
17800b57cec5SDimitry Andric             return;
17810b57cec5SDimitry Andric         }
17820b57cec5SDimitry Andric       }
17830b57cec5SDimitry Andric     }
17840b57cec5SDimitry Andric 
17850b57cec5SDimitry Andric   } else {
17860b57cec5SDimitry Andric     // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
17870b57cec5SDimitry Andric     // the final string length will match the input string length.
17880b57cec5SDimitry Andric     finalStrLength = amountCopied;
17890b57cec5SDimitry Andric   }
17900b57cec5SDimitry Andric 
17910b57cec5SDimitry Andric   SVal Result;
17920b57cec5SDimitry Andric 
17930b57cec5SDimitry Andric   if (returnPtr) {
17940b57cec5SDimitry Andric     // The final result of the function will either be a pointer past the last
17950b57cec5SDimitry Andric     // copied element, or a pointer to the start of the destination buffer.
17960b57cec5SDimitry Andric     Result = (returnEnd ? UnknownVal() : DstVal);
17970b57cec5SDimitry Andric   } else {
17980b57cec5SDimitry Andric     Result = finalStrLength;
17990b57cec5SDimitry Andric   }
18000b57cec5SDimitry Andric 
18010b57cec5SDimitry Andric   assert(state);
18020b57cec5SDimitry Andric 
18030b57cec5SDimitry Andric   // If the destination is a MemRegion, try to check for a buffer overflow and
18040b57cec5SDimitry Andric   // record the new string length.
18050b57cec5SDimitry Andric   if (Optional<loc::MemRegionVal> dstRegVal =
18060b57cec5SDimitry Andric       DstVal.getAs<loc::MemRegionVal>()) {
18070b57cec5SDimitry Andric     QualType ptrTy = Dst->getType();
18080b57cec5SDimitry Andric 
18090b57cec5SDimitry Andric     // If we have an exact value on a bounded copy, use that to check for
18100b57cec5SDimitry Andric     // overflows, rather than our estimate about how much is actually copied.
18110b57cec5SDimitry Andric     if (boundWarning) {
18120b57cec5SDimitry Andric       if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
18130b57cec5SDimitry Andric         SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
18140b57cec5SDimitry Andric             *maxLastNL, ptrTy);
18150b57cec5SDimitry Andric         state = CheckLocation(C, state, CE->getArg(2), maxLastElement,
18160b57cec5SDimitry Andric             boundWarning);
18170b57cec5SDimitry Andric         if (!state)
18180b57cec5SDimitry Andric           return;
18190b57cec5SDimitry Andric       }
18200b57cec5SDimitry Andric     }
18210b57cec5SDimitry Andric 
18220b57cec5SDimitry Andric     // Then, if the final length is known...
18230b57cec5SDimitry Andric     if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
18240b57cec5SDimitry Andric       SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
18250b57cec5SDimitry Andric           *knownStrLength, ptrTy);
18260b57cec5SDimitry Andric 
18270b57cec5SDimitry Andric       // ...and we haven't checked the bound, we'll check the actual copy.
18280b57cec5SDimitry Andric       if (!boundWarning) {
18290b57cec5SDimitry Andric         const char * const warningMsg =
18300b57cec5SDimitry Andric           "String copy function overflows destination buffer";
18310b57cec5SDimitry Andric         state = CheckLocation(C, state, Dst, lastElement, warningMsg);
18320b57cec5SDimitry Andric         if (!state)
18330b57cec5SDimitry Andric           return;
18340b57cec5SDimitry Andric       }
18350b57cec5SDimitry Andric 
18360b57cec5SDimitry Andric       // If this is a stpcpy-style copy, the last element is the return value.
18370b57cec5SDimitry Andric       if (returnPtr && returnEnd)
18380b57cec5SDimitry Andric         Result = lastElement;
18390b57cec5SDimitry Andric     }
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
18420b57cec5SDimitry Andric     // the address of the top-level region). This must happen before we set the
18430b57cec5SDimitry Andric     // C string length because invalidation will clear the length.
18440b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
18450b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
18460b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
18470b57cec5SDimitry Andric     // string, but that's still an improvement over blank invalidation.
18480b57cec5SDimitry Andric     state = InvalidateBuffer(C, state, Dst, *dstRegVal,
18490b57cec5SDimitry Andric         /*IsSourceBuffer*/false, nullptr);
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
18520b57cec5SDimitry Andric     // the address of the top-level region).
18530b57cec5SDimitry Andric     state = InvalidateBuffer(C, state, srcExpr, srcVal, /*IsSourceBuffer*/true,
18540b57cec5SDimitry Andric         nullptr);
18550b57cec5SDimitry Andric 
18560b57cec5SDimitry Andric     // Set the C string length of the destination, if we know it.
18570b57cec5SDimitry Andric     if (isBounded && !isAppending) {
18580b57cec5SDimitry Andric       // strncpy is annoying in that it doesn't guarantee to null-terminate
18590b57cec5SDimitry Andric       // the result string. If the original string didn't fit entirely inside
18600b57cec5SDimitry Andric       // the bound (including the null-terminator), we don't know how long the
18610b57cec5SDimitry Andric       // result is.
18620b57cec5SDimitry Andric       if (amountCopied != strLength)
18630b57cec5SDimitry Andric         finalStrLength = UnknownVal();
18640b57cec5SDimitry Andric     }
18650b57cec5SDimitry Andric     state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
18660b57cec5SDimitry Andric   }
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric   assert(state);
18690b57cec5SDimitry Andric 
18700b57cec5SDimitry Andric   if (returnPtr) {
18710b57cec5SDimitry Andric     // If this is a stpcpy-style copy, but we were unable to check for a buffer
18720b57cec5SDimitry Andric     // overflow, we still need a result. Conjure a return value.
18730b57cec5SDimitry Andric     if (returnEnd && Result.isUnknown()) {
18740b57cec5SDimitry Andric       Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
18750b57cec5SDimitry Andric     }
18760b57cec5SDimitry Andric   }
18770b57cec5SDimitry Andric   // Set the return value.
18780b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, Result);
18790b57cec5SDimitry Andric   C.addTransition(state);
18800b57cec5SDimitry Andric }
18810b57cec5SDimitry Andric 
18820b57cec5SDimitry Andric void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
18830b57cec5SDimitry Andric   //int strcmp(const char *s1, const char *s2);
18840b57cec5SDimitry Andric   evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false);
18850b57cec5SDimitry Andric }
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
18880b57cec5SDimitry Andric   //int strncmp(const char *s1, const char *s2, size_t n);
18890b57cec5SDimitry Andric   evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false);
18900b57cec5SDimitry Andric }
18910b57cec5SDimitry Andric 
18920b57cec5SDimitry Andric void CStringChecker::evalStrcasecmp(CheckerContext &C,
18930b57cec5SDimitry Andric     const CallExpr *CE) const {
18940b57cec5SDimitry Andric   //int strcasecmp(const char *s1, const char *s2);
18950b57cec5SDimitry Andric   evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true);
18960b57cec5SDimitry Andric }
18970b57cec5SDimitry Andric 
18980b57cec5SDimitry Andric void CStringChecker::evalStrncasecmp(CheckerContext &C,
18990b57cec5SDimitry Andric     const CallExpr *CE) const {
19000b57cec5SDimitry Andric   //int strncasecmp(const char *s1, const char *s2, size_t n);
19010b57cec5SDimitry Andric   evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true);
19020b57cec5SDimitry Andric }
19030b57cec5SDimitry Andric 
19040b57cec5SDimitry Andric void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
19050b57cec5SDimitry Andric     bool isBounded, bool ignoreCase) const {
19060b57cec5SDimitry Andric   CurrentFunctionDescription = "string comparison function";
19070b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
19080b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
19090b57cec5SDimitry Andric 
19100b57cec5SDimitry Andric   // Check that the first string is non-null
19110b57cec5SDimitry Andric   const Expr *s1 = CE->getArg(0);
19120b57cec5SDimitry Andric   SVal s1Val = state->getSVal(s1, LCtx);
1913*a7dea167SDimitry Andric   state = checkNonNull(C, state, s1, s1Val, 1);
19140b57cec5SDimitry Andric   if (!state)
19150b57cec5SDimitry Andric     return;
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric   // Check that the second string is non-null.
19180b57cec5SDimitry Andric   const Expr *s2 = CE->getArg(1);
19190b57cec5SDimitry Andric   SVal s2Val = state->getSVal(s2, LCtx);
1920*a7dea167SDimitry Andric   state = checkNonNull(C, state, s2, s2Val, 2);
19210b57cec5SDimitry Andric   if (!state)
19220b57cec5SDimitry Andric     return;
19230b57cec5SDimitry Andric 
19240b57cec5SDimitry Andric   // Get the string length of the first string or give up.
19250b57cec5SDimitry Andric   SVal s1Length = getCStringLength(C, state, s1, s1Val);
19260b57cec5SDimitry Andric   if (s1Length.isUndef())
19270b57cec5SDimitry Andric     return;
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric   // Get the string length of the second string or give up.
19300b57cec5SDimitry Andric   SVal s2Length = getCStringLength(C, state, s2, s2Val);
19310b57cec5SDimitry Andric   if (s2Length.isUndef())
19320b57cec5SDimitry Andric     return;
19330b57cec5SDimitry Andric 
19340b57cec5SDimitry Andric   // If we know the two buffers are the same, we know the result is 0.
19350b57cec5SDimitry Andric   // First, get the two buffers' addresses. Another checker will have already
19360b57cec5SDimitry Andric   // made sure they're not undefined.
19370b57cec5SDimitry Andric   DefinedOrUnknownSVal LV = s1Val.castAs<DefinedOrUnknownSVal>();
19380b57cec5SDimitry Andric   DefinedOrUnknownSVal RV = s2Val.castAs<DefinedOrUnknownSVal>();
19390b57cec5SDimitry Andric 
19400b57cec5SDimitry Andric   // See if they are the same.
19410b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
19420b57cec5SDimitry Andric   DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
19430b57cec5SDimitry Andric   ProgramStateRef StSameBuf, StNotSameBuf;
19440b57cec5SDimitry Andric   std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric   // If the two arguments might be the same buffer, we know the result is 0,
19470b57cec5SDimitry Andric   // and we only need to check one size.
19480b57cec5SDimitry Andric   if (StSameBuf) {
19490b57cec5SDimitry Andric     StSameBuf = StSameBuf->BindExpr(CE, LCtx,
19500b57cec5SDimitry Andric         svalBuilder.makeZeroVal(CE->getType()));
19510b57cec5SDimitry Andric     C.addTransition(StSameBuf);
19520b57cec5SDimitry Andric 
19530b57cec5SDimitry Andric     // If the two arguments are GUARANTEED to be the same, we're done!
19540b57cec5SDimitry Andric     if (!StNotSameBuf)
19550b57cec5SDimitry Andric       return;
19560b57cec5SDimitry Andric   }
19570b57cec5SDimitry Andric 
19580b57cec5SDimitry Andric   assert(StNotSameBuf);
19590b57cec5SDimitry Andric   state = StNotSameBuf;
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric   // At this point we can go about comparing the two buffers.
19620b57cec5SDimitry Andric   // For now, we only do this if they're both known string literals.
19630b57cec5SDimitry Andric 
19640b57cec5SDimitry Andric   // Attempt to extract string literals from both expressions.
19650b57cec5SDimitry Andric   const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val);
19660b57cec5SDimitry Andric   const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val);
19670b57cec5SDimitry Andric   bool canComputeResult = false;
19680b57cec5SDimitry Andric   SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
19690b57cec5SDimitry Andric       C.blockCount());
19700b57cec5SDimitry Andric 
19710b57cec5SDimitry Andric   if (s1StrLiteral && s2StrLiteral) {
19720b57cec5SDimitry Andric     StringRef s1StrRef = s1StrLiteral->getString();
19730b57cec5SDimitry Andric     StringRef s2StrRef = s2StrLiteral->getString();
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric     if (isBounded) {
19760b57cec5SDimitry Andric       // Get the max number of characters to compare.
19770b57cec5SDimitry Andric       const Expr *lenExpr = CE->getArg(2);
19780b57cec5SDimitry Andric       SVal lenVal = state->getSVal(lenExpr, LCtx);
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric       // If the length is known, we can get the right substrings.
19810b57cec5SDimitry Andric       if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
19820b57cec5SDimitry Andric         // Create substrings of each to compare the prefix.
19830b57cec5SDimitry Andric         s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue());
19840b57cec5SDimitry Andric         s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue());
19850b57cec5SDimitry Andric         canComputeResult = true;
19860b57cec5SDimitry Andric       }
19870b57cec5SDimitry Andric     } else {
19880b57cec5SDimitry Andric       // This is a normal, unbounded strcmp.
19890b57cec5SDimitry Andric       canComputeResult = true;
19900b57cec5SDimitry Andric     }
19910b57cec5SDimitry Andric 
19920b57cec5SDimitry Andric     if (canComputeResult) {
19930b57cec5SDimitry Andric       // Real strcmp stops at null characters.
19940b57cec5SDimitry Andric       size_t s1Term = s1StrRef.find('\0');
19950b57cec5SDimitry Andric       if (s1Term != StringRef::npos)
19960b57cec5SDimitry Andric         s1StrRef = s1StrRef.substr(0, s1Term);
19970b57cec5SDimitry Andric 
19980b57cec5SDimitry Andric       size_t s2Term = s2StrRef.find('\0');
19990b57cec5SDimitry Andric       if (s2Term != StringRef::npos)
20000b57cec5SDimitry Andric         s2StrRef = s2StrRef.substr(0, s2Term);
20010b57cec5SDimitry Andric 
20020b57cec5SDimitry Andric       // Use StringRef's comparison methods to compute the actual result.
20030b57cec5SDimitry Andric       int compareRes = ignoreCase ? s1StrRef.compare_lower(s2StrRef)
20040b57cec5SDimitry Andric         : s1StrRef.compare(s2StrRef);
20050b57cec5SDimitry Andric 
20060b57cec5SDimitry Andric       // The strcmp function returns an integer greater than, equal to, or less
20070b57cec5SDimitry Andric       // than zero, [c11, p7.24.4.2].
20080b57cec5SDimitry Andric       if (compareRes == 0) {
20090b57cec5SDimitry Andric         resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
20100b57cec5SDimitry Andric       }
20110b57cec5SDimitry Andric       else {
20120b57cec5SDimitry Andric         DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
20130b57cec5SDimitry Andric         // Constrain strcmp's result range based on the result of StringRef's
20140b57cec5SDimitry Andric         // comparison methods.
20150b57cec5SDimitry Andric         BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT;
20160b57cec5SDimitry Andric         SVal compareWithZero =
20170b57cec5SDimitry Andric           svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
20180b57cec5SDimitry Andric               svalBuilder.getConditionType());
20190b57cec5SDimitry Andric         DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
20200b57cec5SDimitry Andric         state = state->assume(compareWithZeroVal, true);
20210b57cec5SDimitry Andric       }
20220b57cec5SDimitry Andric     }
20230b57cec5SDimitry Andric   }
20240b57cec5SDimitry Andric 
20250b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, resultVal);
20260b57cec5SDimitry Andric 
20270b57cec5SDimitry Andric   // Record this as a possible path.
20280b57cec5SDimitry Andric   C.addTransition(state);
20290b57cec5SDimitry Andric }
20300b57cec5SDimitry Andric 
20310b57cec5SDimitry Andric void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
20320b57cec5SDimitry Andric   //char *strsep(char **stringp, const char *delim);
20330b57cec5SDimitry Andric   // Sanity: does the search string parameter match the return type?
20340b57cec5SDimitry Andric   const Expr *SearchStrPtr = CE->getArg(0);
20350b57cec5SDimitry Andric   QualType CharPtrTy = SearchStrPtr->getType()->getPointeeType();
20360b57cec5SDimitry Andric   if (CharPtrTy.isNull() ||
20370b57cec5SDimitry Andric       CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
20380b57cec5SDimitry Andric     return;
20390b57cec5SDimitry Andric 
20400b57cec5SDimitry Andric   CurrentFunctionDescription = "strsep()";
20410b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
20420b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
20430b57cec5SDimitry Andric 
20440b57cec5SDimitry Andric   // Check that the search string pointer is non-null (though it may point to
20450b57cec5SDimitry Andric   // a null string).
20460b57cec5SDimitry Andric   SVal SearchStrVal = State->getSVal(SearchStrPtr, LCtx);
2047*a7dea167SDimitry Andric   State = checkNonNull(C, State, SearchStrPtr, SearchStrVal, 1);
20480b57cec5SDimitry Andric   if (!State)
20490b57cec5SDimitry Andric     return;
20500b57cec5SDimitry Andric 
20510b57cec5SDimitry Andric   // Check that the delimiter string is non-null.
20520b57cec5SDimitry Andric   const Expr *DelimStr = CE->getArg(1);
20530b57cec5SDimitry Andric   SVal DelimStrVal = State->getSVal(DelimStr, LCtx);
2054*a7dea167SDimitry Andric   State = checkNonNull(C, State, DelimStr, DelimStrVal, 2);
20550b57cec5SDimitry Andric   if (!State)
20560b57cec5SDimitry Andric     return;
20570b57cec5SDimitry Andric 
20580b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
20590b57cec5SDimitry Andric   SVal Result;
20600b57cec5SDimitry Andric   if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
20610b57cec5SDimitry Andric     // Get the current value of the search string pointer, as a char*.
20620b57cec5SDimitry Andric     Result = State->getSVal(*SearchStrLoc, CharPtrTy);
20630b57cec5SDimitry Andric 
20640b57cec5SDimitry Andric     // Invalidate the search string, representing the change of one delimiter
20650b57cec5SDimitry Andric     // character to NUL.
20660b57cec5SDimitry Andric     State = InvalidateBuffer(C, State, SearchStrPtr, Result,
20670b57cec5SDimitry Andric         /*IsSourceBuffer*/false, nullptr);
20680b57cec5SDimitry Andric 
20690b57cec5SDimitry Andric     // Overwrite the search string pointer. The new value is either an address
20700b57cec5SDimitry Andric     // further along in the same string, or NULL if there are no more tokens.
20710b57cec5SDimitry Andric     State = State->bindLoc(*SearchStrLoc,
20720b57cec5SDimitry Andric         SVB.conjureSymbolVal(getTag(),
20730b57cec5SDimitry Andric           CE,
20740b57cec5SDimitry Andric           LCtx,
20750b57cec5SDimitry Andric           CharPtrTy,
20760b57cec5SDimitry Andric           C.blockCount()),
20770b57cec5SDimitry Andric         LCtx);
20780b57cec5SDimitry Andric   } else {
20790b57cec5SDimitry Andric     assert(SearchStrVal.isUnknown());
20800b57cec5SDimitry Andric     // Conjure a symbolic value. It's the best we can do.
20810b57cec5SDimitry Andric     Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
20820b57cec5SDimitry Andric   }
20830b57cec5SDimitry Andric 
20840b57cec5SDimitry Andric   // Set the return value, and finish.
20850b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, Result);
20860b57cec5SDimitry Andric   C.addTransition(State);
20870b57cec5SDimitry Andric }
20880b57cec5SDimitry Andric 
20890b57cec5SDimitry Andric // These should probably be moved into a C++ standard library checker.
20900b57cec5SDimitry Andric void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
20910b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
20920b57cec5SDimitry Andric }
20930b57cec5SDimitry Andric 
20940b57cec5SDimitry Andric void CStringChecker::evalStdCopyBackward(CheckerContext &C,
20950b57cec5SDimitry Andric     const CallExpr *CE) const {
20960b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
20970b57cec5SDimitry Andric }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric void CStringChecker::evalStdCopyCommon(CheckerContext &C,
21000b57cec5SDimitry Andric     const CallExpr *CE) const {
21010b57cec5SDimitry Andric   if (!CE->getArg(2)->getType()->isPointerType())
21020b57cec5SDimitry Andric     return;
21030b57cec5SDimitry Andric 
21040b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21050b57cec5SDimitry Andric 
21060b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
21070b57cec5SDimitry Andric 
21080b57cec5SDimitry Andric   // template <class _InputIterator, class _OutputIterator>
21090b57cec5SDimitry Andric   // _OutputIterator
21100b57cec5SDimitry Andric   // copy(_InputIterator __first, _InputIterator __last,
21110b57cec5SDimitry Andric   //        _OutputIterator __result)
21120b57cec5SDimitry Andric 
21130b57cec5SDimitry Andric   // Invalidate the destination buffer
21140b57cec5SDimitry Andric   const Expr *Dst = CE->getArg(2);
21150b57cec5SDimitry Andric   SVal DstVal = State->getSVal(Dst, LCtx);
21160b57cec5SDimitry Andric   State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
21170b57cec5SDimitry Andric       /*Size=*/nullptr);
21180b57cec5SDimitry Andric 
21190b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
21200b57cec5SDimitry Andric 
21210b57cec5SDimitry Andric   SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
21220b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, ResultVal);
21230b57cec5SDimitry Andric 
21240b57cec5SDimitry Andric   C.addTransition(State);
21250b57cec5SDimitry Andric }
21260b57cec5SDimitry Andric 
21270b57cec5SDimitry Andric void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
21280b57cec5SDimitry Andric   CurrentFunctionDescription = "memory set function";
21290b57cec5SDimitry Andric 
21300b57cec5SDimitry Andric   const Expr *Mem = CE->getArg(0);
21310b57cec5SDimitry Andric   const Expr *CharE = CE->getArg(1);
21320b57cec5SDimitry Andric   const Expr *Size = CE->getArg(2);
21330b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21340b57cec5SDimitry Andric 
21350b57cec5SDimitry Andric   // See if the size argument is zero.
21360b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
21370b57cec5SDimitry Andric   SVal SizeVal = State->getSVal(Size, LCtx);
21380b57cec5SDimitry Andric   QualType SizeTy = Size->getType();
21390b57cec5SDimitry Andric 
21400b57cec5SDimitry Andric   ProgramStateRef StateZeroSize, StateNonZeroSize;
21410b57cec5SDimitry Andric   std::tie(StateZeroSize, StateNonZeroSize) =
21420b57cec5SDimitry Andric     assumeZero(C, State, SizeVal, SizeTy);
21430b57cec5SDimitry Andric 
21440b57cec5SDimitry Andric   // Get the value of the memory area.
21450b57cec5SDimitry Andric   SVal MemVal = State->getSVal(Mem, LCtx);
21460b57cec5SDimitry Andric 
21470b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
21480b57cec5SDimitry Andric   // just bind the return value to the Mem buffer and return.
21490b57cec5SDimitry Andric   if (StateZeroSize && !StateNonZeroSize) {
21500b57cec5SDimitry Andric     StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, MemVal);
21510b57cec5SDimitry Andric     C.addTransition(StateZeroSize);
21520b57cec5SDimitry Andric     return;
21530b57cec5SDimitry Andric   }
21540b57cec5SDimitry Andric 
21550b57cec5SDimitry Andric   // Ensure the memory area is not null.
21560b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
2157*a7dea167SDimitry Andric   State = checkNonNull(C, StateNonZeroSize, Mem, MemVal, 1);
21580b57cec5SDimitry Andric   if (!State)
21590b57cec5SDimitry Andric     return;
21600b57cec5SDimitry Andric 
21610b57cec5SDimitry Andric   State = CheckBufferAccess(C, State, Size, Mem);
21620b57cec5SDimitry Andric   if (!State)
21630b57cec5SDimitry Andric     return;
21640b57cec5SDimitry Andric 
21650b57cec5SDimitry Andric   // According to the values of the arguments, bind the value of the second
21660b57cec5SDimitry Andric   // argument to the destination buffer and set string length, or just
21670b57cec5SDimitry Andric   // invalidate the destination buffer.
21680b57cec5SDimitry Andric   if (!memsetAux(Mem, C.getSVal(CharE), Size, C, State))
21690b57cec5SDimitry Andric     return;
21700b57cec5SDimitry Andric 
21710b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, MemVal);
21720b57cec5SDimitry Andric   C.addTransition(State);
21730b57cec5SDimitry Andric }
21740b57cec5SDimitry Andric 
21750b57cec5SDimitry Andric void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
21760b57cec5SDimitry Andric   CurrentFunctionDescription = "memory clearance function";
21770b57cec5SDimitry Andric 
21780b57cec5SDimitry Andric   const Expr *Mem = CE->getArg(0);
21790b57cec5SDimitry Andric   const Expr *Size = CE->getArg(1);
21800b57cec5SDimitry Andric   SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
21810b57cec5SDimitry Andric 
21820b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21830b57cec5SDimitry Andric 
21840b57cec5SDimitry Andric   // See if the size argument is zero.
21850b57cec5SDimitry Andric   SVal SizeVal = C.getSVal(Size);
21860b57cec5SDimitry Andric   QualType SizeTy = Size->getType();
21870b57cec5SDimitry Andric 
21880b57cec5SDimitry Andric   ProgramStateRef StateZeroSize, StateNonZeroSize;
21890b57cec5SDimitry Andric   std::tie(StateZeroSize, StateNonZeroSize) =
21900b57cec5SDimitry Andric     assumeZero(C, State, SizeVal, SizeTy);
21910b57cec5SDimitry Andric 
21920b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access,
21930b57cec5SDimitry Andric   // In this case we just return.
21940b57cec5SDimitry Andric   if (StateZeroSize && !StateNonZeroSize) {
21950b57cec5SDimitry Andric     C.addTransition(StateZeroSize);
21960b57cec5SDimitry Andric     return;
21970b57cec5SDimitry Andric   }
21980b57cec5SDimitry Andric 
21990b57cec5SDimitry Andric   // Get the value of the memory area.
22000b57cec5SDimitry Andric   SVal MemVal = C.getSVal(Mem);
22010b57cec5SDimitry Andric 
22020b57cec5SDimitry Andric   // Ensure the memory area is not null.
22030b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
2204*a7dea167SDimitry Andric   State = checkNonNull(C, StateNonZeroSize, Mem, MemVal, 1);
22050b57cec5SDimitry Andric   if (!State)
22060b57cec5SDimitry Andric     return;
22070b57cec5SDimitry Andric 
22080b57cec5SDimitry Andric   State = CheckBufferAccess(C, State, Size, Mem);
22090b57cec5SDimitry Andric   if (!State)
22100b57cec5SDimitry Andric     return;
22110b57cec5SDimitry Andric 
22120b57cec5SDimitry Andric   if (!memsetAux(Mem, Zero, Size, C, State))
22130b57cec5SDimitry Andric     return;
22140b57cec5SDimitry Andric 
22150b57cec5SDimitry Andric   C.addTransition(State);
22160b57cec5SDimitry Andric }
22170b57cec5SDimitry Andric 
22180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
22190b57cec5SDimitry Andric // The driver method, and other Checker callbacks.
22200b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
22210b57cec5SDimitry Andric 
22220b57cec5SDimitry Andric CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
22230b57cec5SDimitry Andric                                                      CheckerContext &C) const {
22240b57cec5SDimitry Andric   const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
22250b57cec5SDimitry Andric   if (!CE)
22260b57cec5SDimitry Andric     return nullptr;
22270b57cec5SDimitry Andric 
22280b57cec5SDimitry Andric   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
22290b57cec5SDimitry Andric   if (!FD)
22300b57cec5SDimitry Andric     return nullptr;
22310b57cec5SDimitry Andric 
22320b57cec5SDimitry Andric   if (Call.isCalled(StdCopy)) {
22330b57cec5SDimitry Andric     return &CStringChecker::evalStdCopy;
22340b57cec5SDimitry Andric   } else if (Call.isCalled(StdCopyBackward)) {
22350b57cec5SDimitry Andric     return &CStringChecker::evalStdCopyBackward;
22360b57cec5SDimitry Andric   }
22370b57cec5SDimitry Andric 
22380b57cec5SDimitry Andric   // Pro-actively check that argument types are safe to do arithmetic upon.
22390b57cec5SDimitry Andric   // We do not want to crash if someone accidentally passes a structure
22400b57cec5SDimitry Andric   // into, say, a C++ overload of any of these functions. We could not check
22410b57cec5SDimitry Andric   // that for std::copy because they may have arguments of other types.
22420b57cec5SDimitry Andric   for (auto I : CE->arguments()) {
22430b57cec5SDimitry Andric     QualType T = I->getType();
22440b57cec5SDimitry Andric     if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
22450b57cec5SDimitry Andric       return nullptr;
22460b57cec5SDimitry Andric   }
22470b57cec5SDimitry Andric 
22480b57cec5SDimitry Andric   const FnCheck *Callback = Callbacks.lookup(Call);
22490b57cec5SDimitry Andric   if (Callback)
22500b57cec5SDimitry Andric     return *Callback;
22510b57cec5SDimitry Andric 
22520b57cec5SDimitry Andric   return nullptr;
22530b57cec5SDimitry Andric }
22540b57cec5SDimitry Andric 
22550b57cec5SDimitry Andric bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
22560b57cec5SDimitry Andric   FnCheck Callback = identifyCall(Call, C);
22570b57cec5SDimitry Andric 
22580b57cec5SDimitry Andric   // If the callee isn't a string function, let another checker handle it.
22590b57cec5SDimitry Andric   if (!Callback)
22600b57cec5SDimitry Andric     return false;
22610b57cec5SDimitry Andric 
22620b57cec5SDimitry Andric   // Check and evaluate the call.
22630b57cec5SDimitry Andric   const auto *CE = cast<CallExpr>(Call.getOriginExpr());
22640b57cec5SDimitry Andric   (this->*Callback)(C, CE);
22650b57cec5SDimitry Andric 
22660b57cec5SDimitry Andric   // If the evaluate call resulted in no change, chain to the next eval call
22670b57cec5SDimitry Andric   // handler.
22680b57cec5SDimitry Andric   // Note, the custom CString evaluation calls assume that basic safety
22690b57cec5SDimitry Andric   // properties are held. However, if the user chooses to turn off some of these
22700b57cec5SDimitry Andric   // checks, we ignore the issues and leave the call evaluation to a generic
22710b57cec5SDimitry Andric   // handler.
22720b57cec5SDimitry Andric   return C.isDifferent();
22730b57cec5SDimitry Andric }
22740b57cec5SDimitry Andric 
22750b57cec5SDimitry Andric void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
22760b57cec5SDimitry Andric   // Record string length for char a[] = "abc";
22770b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
22780b57cec5SDimitry Andric 
22790b57cec5SDimitry Andric   for (const auto *I : DS->decls()) {
22800b57cec5SDimitry Andric     const VarDecl *D = dyn_cast<VarDecl>(I);
22810b57cec5SDimitry Andric     if (!D)
22820b57cec5SDimitry Andric       continue;
22830b57cec5SDimitry Andric 
22840b57cec5SDimitry Andric     // FIXME: Handle array fields of structs.
22850b57cec5SDimitry Andric     if (!D->getType()->isArrayType())
22860b57cec5SDimitry Andric       continue;
22870b57cec5SDimitry Andric 
22880b57cec5SDimitry Andric     const Expr *Init = D->getInit();
22890b57cec5SDimitry Andric     if (!Init)
22900b57cec5SDimitry Andric       continue;
22910b57cec5SDimitry Andric     if (!isa<StringLiteral>(Init))
22920b57cec5SDimitry Andric       continue;
22930b57cec5SDimitry Andric 
22940b57cec5SDimitry Andric     Loc VarLoc = state->getLValue(D, C.getLocationContext());
22950b57cec5SDimitry Andric     const MemRegion *MR = VarLoc.getAsRegion();
22960b57cec5SDimitry Andric     if (!MR)
22970b57cec5SDimitry Andric       continue;
22980b57cec5SDimitry Andric 
22990b57cec5SDimitry Andric     SVal StrVal = C.getSVal(Init);
23000b57cec5SDimitry Andric     assert(StrVal.isValid() && "Initializer string is unknown or undefined");
23010b57cec5SDimitry Andric     DefinedOrUnknownSVal strLength =
23020b57cec5SDimitry Andric       getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
23030b57cec5SDimitry Andric 
23040b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
23050b57cec5SDimitry Andric   }
23060b57cec5SDimitry Andric 
23070b57cec5SDimitry Andric   C.addTransition(state);
23080b57cec5SDimitry Andric }
23090b57cec5SDimitry Andric 
23100b57cec5SDimitry Andric ProgramStateRef
23110b57cec5SDimitry Andric CStringChecker::checkRegionChanges(ProgramStateRef state,
23120b57cec5SDimitry Andric     const InvalidatedSymbols *,
23130b57cec5SDimitry Andric     ArrayRef<const MemRegion *> ExplicitRegions,
23140b57cec5SDimitry Andric     ArrayRef<const MemRegion *> Regions,
23150b57cec5SDimitry Andric     const LocationContext *LCtx,
23160b57cec5SDimitry Andric     const CallEvent *Call) const {
23170b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
23180b57cec5SDimitry Andric   if (Entries.isEmpty())
23190b57cec5SDimitry Andric     return state;
23200b57cec5SDimitry Andric 
23210b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
23220b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
23230b57cec5SDimitry Andric 
23240b57cec5SDimitry Andric   // First build sets for the changed regions and their super-regions.
23250b57cec5SDimitry Andric   for (ArrayRef<const MemRegion *>::iterator
23260b57cec5SDimitry Andric       I = Regions.begin(), E = Regions.end(); I != E; ++I) {
23270b57cec5SDimitry Andric     const MemRegion *MR = *I;
23280b57cec5SDimitry Andric     Invalidated.insert(MR);
23290b57cec5SDimitry Andric 
23300b57cec5SDimitry Andric     SuperRegions.insert(MR);
23310b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
23320b57cec5SDimitry Andric       MR = SR->getSuperRegion();
23330b57cec5SDimitry Andric       SuperRegions.insert(MR);
23340b57cec5SDimitry Andric     }
23350b57cec5SDimitry Andric   }
23360b57cec5SDimitry Andric 
23370b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
23380b57cec5SDimitry Andric 
23390b57cec5SDimitry Andric   // Then loop over the entries in the current state.
23400b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(),
23410b57cec5SDimitry Andric       E = Entries.end(); I != E; ++I) {
23420b57cec5SDimitry Andric     const MemRegion *MR = I.getKey();
23430b57cec5SDimitry Andric 
23440b57cec5SDimitry Andric     // Is this entry for a super-region of a changed region?
23450b57cec5SDimitry Andric     if (SuperRegions.count(MR)) {
23460b57cec5SDimitry Andric       Entries = F.remove(Entries, MR);
23470b57cec5SDimitry Andric       continue;
23480b57cec5SDimitry Andric     }
23490b57cec5SDimitry Andric 
23500b57cec5SDimitry Andric     // Is this entry for a sub-region of a changed region?
23510b57cec5SDimitry Andric     const MemRegion *Super = MR;
23520b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
23530b57cec5SDimitry Andric       Super = SR->getSuperRegion();
23540b57cec5SDimitry Andric       if (Invalidated.count(Super)) {
23550b57cec5SDimitry Andric         Entries = F.remove(Entries, MR);
23560b57cec5SDimitry Andric         break;
23570b57cec5SDimitry Andric       }
23580b57cec5SDimitry Andric     }
23590b57cec5SDimitry Andric   }
23600b57cec5SDimitry Andric 
23610b57cec5SDimitry Andric   return state->set<CStringLength>(Entries);
23620b57cec5SDimitry Andric }
23630b57cec5SDimitry Andric 
23640b57cec5SDimitry Andric void CStringChecker::checkLiveSymbols(ProgramStateRef state,
23650b57cec5SDimitry Andric     SymbolReaper &SR) const {
23660b57cec5SDimitry Andric   // Mark all symbols in our string length map as valid.
23670b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
23680b57cec5SDimitry Andric 
23690b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
23700b57cec5SDimitry Andric       I != E; ++I) {
23710b57cec5SDimitry Andric     SVal Len = I.getData();
23720b57cec5SDimitry Andric 
23730b57cec5SDimitry Andric     for (SymExpr::symbol_iterator si = Len.symbol_begin(),
23740b57cec5SDimitry Andric         se = Len.symbol_end(); si != se; ++si)
23750b57cec5SDimitry Andric       SR.markInUse(*si);
23760b57cec5SDimitry Andric   }
23770b57cec5SDimitry Andric }
23780b57cec5SDimitry Andric 
23790b57cec5SDimitry Andric void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
23800b57cec5SDimitry Andric     CheckerContext &C) const {
23810b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
23820b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
23830b57cec5SDimitry Andric   if (Entries.isEmpty())
23840b57cec5SDimitry Andric     return;
23850b57cec5SDimitry Andric 
23860b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
23870b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
23880b57cec5SDimitry Andric       I != E; ++I) {
23890b57cec5SDimitry Andric     SVal Len = I.getData();
23900b57cec5SDimitry Andric     if (SymbolRef Sym = Len.getAsSymbol()) {
23910b57cec5SDimitry Andric       if (SR.isDead(Sym))
23920b57cec5SDimitry Andric         Entries = F.remove(Entries, I.getKey());
23930b57cec5SDimitry Andric     }
23940b57cec5SDimitry Andric   }
23950b57cec5SDimitry Andric 
23960b57cec5SDimitry Andric   state = state->set<CStringLength>(Entries);
23970b57cec5SDimitry Andric   C.addTransition(state);
23980b57cec5SDimitry Andric }
23990b57cec5SDimitry Andric 
24000b57cec5SDimitry Andric void ento::registerCStringModeling(CheckerManager &Mgr) {
24010b57cec5SDimitry Andric   Mgr.registerChecker<CStringChecker>();
24020b57cec5SDimitry Andric }
24030b57cec5SDimitry Andric 
24040b57cec5SDimitry Andric bool ento::shouldRegisterCStringModeling(const LangOptions &LO) {
24050b57cec5SDimitry Andric   return true;
24060b57cec5SDimitry Andric }
24070b57cec5SDimitry Andric 
24080b57cec5SDimitry Andric #define REGISTER_CHECKER(name)                                                 \
24090b57cec5SDimitry Andric   void ento::register##name(CheckerManager &mgr) {                             \
24100b57cec5SDimitry Andric     CStringChecker *checker = mgr.getChecker<CStringChecker>();                \
24110b57cec5SDimitry Andric     checker->Filter.Check##name = true;                                        \
2412*a7dea167SDimitry Andric     checker->Filter.CheckName##name = mgr.getCurrentCheckerName();             \
24130b57cec5SDimitry Andric   }                                                                            \
24140b57cec5SDimitry Andric                                                                                \
2415*a7dea167SDimitry Andric   bool ento::shouldRegister##name(const LangOptions &LO) { return true; }
24160b57cec5SDimitry Andric 
24170b57cec5SDimitry Andric REGISTER_CHECKER(CStringNullArg)
24180b57cec5SDimitry Andric REGISTER_CHECKER(CStringOutOfBounds)
24190b57cec5SDimitry Andric REGISTER_CHECKER(CStringBufferOverlap)
24200b57cec5SDimitry Andric REGISTER_CHECKER(CStringNotNullTerm)
2421