xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This defines CStringChecker, which is an assortment of checks on calls
100b57cec5SDimitry Andric // to functions in <string.h>.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "InterCheckerAPI.h"
150b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
165ffd83dbSDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23fe6060f1SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
240b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
250b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
260b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
275ffd83dbSDimitry Andric #include "llvm/ADT/StringExtras.h"
280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
29972a253aSDimitry Andric #include <functional>
30*bdd1243dSDimitry Andric #include <optional>
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric using namespace clang;
330b57cec5SDimitry Andric using namespace ento;
34972a253aSDimitry Andric using namespace std::placeholders;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric namespace {
375ffd83dbSDimitry Andric struct AnyArgExpr {
385ffd83dbSDimitry Andric   // FIXME: Remove constructor in C++17 to turn it into an aggregate.
395ffd83dbSDimitry Andric   AnyArgExpr(const Expr *Expression, unsigned ArgumentIndex)
405ffd83dbSDimitry Andric       : Expression{Expression}, ArgumentIndex{ArgumentIndex} {}
415ffd83dbSDimitry Andric   const Expr *Expression;
425ffd83dbSDimitry Andric   unsigned ArgumentIndex;
435ffd83dbSDimitry Andric };
445ffd83dbSDimitry Andric 
455ffd83dbSDimitry Andric struct SourceArgExpr : AnyArgExpr {
465ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Remove using in C++17.
475ffd83dbSDimitry Andric };
485ffd83dbSDimitry Andric 
495ffd83dbSDimitry Andric struct DestinationArgExpr : AnyArgExpr {
505ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
515ffd83dbSDimitry Andric };
525ffd83dbSDimitry Andric 
535ffd83dbSDimitry Andric struct SizeArgExpr : AnyArgExpr {
545ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
555ffd83dbSDimitry Andric };
565ffd83dbSDimitry Andric 
575ffd83dbSDimitry Andric using ErrorMessage = SmallString<128>;
585ffd83dbSDimitry Andric enum class AccessKind { write, read };
595ffd83dbSDimitry Andric 
605ffd83dbSDimitry Andric static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription,
615ffd83dbSDimitry Andric                                              AccessKind Access) {
625ffd83dbSDimitry Andric   ErrorMessage Message;
635ffd83dbSDimitry Andric   llvm::raw_svector_ostream Os(Message);
645ffd83dbSDimitry Andric 
655ffd83dbSDimitry Andric   // Function classification like: Memory copy function
665ffd83dbSDimitry Andric   Os << toUppercase(FunctionDescription.front())
675ffd83dbSDimitry Andric      << &FunctionDescription.data()[1];
685ffd83dbSDimitry Andric 
695ffd83dbSDimitry Andric   if (Access == AccessKind::write) {
705ffd83dbSDimitry Andric     Os << " overflows the destination buffer";
715ffd83dbSDimitry Andric   } else { // read access
725ffd83dbSDimitry Andric     Os << " accesses out-of-bound array element";
735ffd83dbSDimitry Andric   }
745ffd83dbSDimitry Andric 
755ffd83dbSDimitry Andric   return Message;
765ffd83dbSDimitry Andric }
775ffd83dbSDimitry Andric 
78480093f4SDimitry Andric enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 };
79*bdd1243dSDimitry Andric 
80*bdd1243dSDimitry Andric enum class CharKind { Regular = 0, Wide };
81*bdd1243dSDimitry Andric constexpr CharKind CK_Regular = CharKind::Regular;
82*bdd1243dSDimitry Andric constexpr CharKind CK_Wide = CharKind::Wide;
83*bdd1243dSDimitry Andric 
84*bdd1243dSDimitry Andric static QualType getCharPtrType(ASTContext &Ctx, CharKind CK) {
85*bdd1243dSDimitry Andric   return Ctx.getPointerType(CK == CharKind::Regular ? Ctx.CharTy
86*bdd1243dSDimitry Andric                                                     : Ctx.WideCharTy);
87*bdd1243dSDimitry Andric }
88*bdd1243dSDimitry Andric 
890b57cec5SDimitry Andric class CStringChecker : public Checker< eval::Call,
900b57cec5SDimitry Andric                                          check::PreStmt<DeclStmt>,
910b57cec5SDimitry Andric                                          check::LiveSymbols,
920b57cec5SDimitry Andric                                          check::DeadSymbols,
930b57cec5SDimitry Andric                                          check::RegionChanges
940b57cec5SDimitry Andric                                          > {
950b57cec5SDimitry Andric   mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
9681ad6265SDimitry Andric       BT_NotCString, BT_AdditionOverflow, BT_UninitRead;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   mutable const char *CurrentFunctionDescription;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric public:
1010b57cec5SDimitry Andric   /// The filter is used to filter out the diagnostics which are not enabled by
1020b57cec5SDimitry Andric   /// the user.
1030b57cec5SDimitry Andric   struct CStringChecksFilter {
10481ad6265SDimitry Andric     bool CheckCStringNullArg = false;
10581ad6265SDimitry Andric     bool CheckCStringOutOfBounds = false;
10681ad6265SDimitry Andric     bool CheckCStringBufferOverlap = false;
10781ad6265SDimitry Andric     bool CheckCStringNotNullTerm = false;
10881ad6265SDimitry Andric     bool CheckCStringUninitializedRead = false;
1090b57cec5SDimitry Andric 
110a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNullArg;
111a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringOutOfBounds;
112a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringBufferOverlap;
113a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNotNullTerm;
11481ad6265SDimitry Andric     CheckerNameRef CheckNameCStringUninitializedRead;
1150b57cec5SDimitry Andric   };
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   CStringChecksFilter Filter;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   static void *getTag() { static int tag; return &tag; }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
1220b57cec5SDimitry Andric   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
1230b57cec5SDimitry Andric   void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
1240b57cec5SDimitry Andric   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   ProgramStateRef
1270b57cec5SDimitry Andric     checkRegionChanges(ProgramStateRef state,
1280b57cec5SDimitry Andric                        const InvalidatedSymbols *,
1290b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> ExplicitRegions,
1300b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> Regions,
1310b57cec5SDimitry Andric                        const LocationContext *LCtx,
1320b57cec5SDimitry Andric                        const CallEvent *Call) const;
1330b57cec5SDimitry Andric 
134972a253aSDimitry Andric   using FnCheck = std::function<void(const CStringChecker *, CheckerContext &,
135972a253aSDimitry Andric                                      const CallExpr *)>;
136972a253aSDimitry Andric 
1370b57cec5SDimitry Andric   CallDescriptionMap<FnCheck> Callbacks = {
138*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"memcpy"}, 3},
139*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemcpy, _1, _2, _3, CK_Regular)},
140*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"wmemcpy"}, 3},
141*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemcpy, _1, _2, _3, CK_Wide)},
142*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"mempcpy"}, 3},
143*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMempcpy, _1, _2, _3, CK_Regular)},
144*bdd1243dSDimitry Andric       {{CDF_None, {"wmempcpy"}, 3},
145*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMempcpy, _1, _2, _3, CK_Wide)},
146*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"memcmp"}, 3},
147*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemcmp, _1, _2, _3, CK_Regular)},
148*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"wmemcmp"}, 3},
149*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemcmp, _1, _2, _3, CK_Wide)},
150*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"memmove"}, 3},
151*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemmove, _1, _2, _3, CK_Regular)},
152*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"wmemmove"}, 3},
153*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemmove, _1, _2, _3, CK_Wide)},
154*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"memset"}, 3}, &CStringChecker::evalMemset},
155*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"explicit_memset"}, 3}, &CStringChecker::evalMemset},
156*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strcpy"}, 2}, &CStringChecker::evalStrcpy},
157*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strncpy"}, 3}, &CStringChecker::evalStrncpy},
158*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"stpcpy"}, 2}, &CStringChecker::evalStpcpy},
159*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strlcpy"}, 3}, &CStringChecker::evalStrlcpy},
160*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strcat"}, 2}, &CStringChecker::evalStrcat},
161*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strncat"}, 3}, &CStringChecker::evalStrncat},
162*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strlcat"}, 3}, &CStringChecker::evalStrlcat},
163*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strlen"}, 1}, &CStringChecker::evalstrLength},
164*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"wcslen"}, 1}, &CStringChecker::evalstrLength},
165*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strnlen"}, 2}, &CStringChecker::evalstrnLength},
166*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"wcsnlen"}, 2}, &CStringChecker::evalstrnLength},
167*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strcmp"}, 2}, &CStringChecker::evalStrcmp},
168*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strncmp"}, 3}, &CStringChecker::evalStrncmp},
169*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strcasecmp"}, 2}, &CStringChecker::evalStrcasecmp},
170*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strncasecmp"}, 3},
171*bdd1243dSDimitry Andric        &CStringChecker::evalStrncasecmp},
172*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"strsep"}, 2}, &CStringChecker::evalStrsep},
173*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"bcopy"}, 3}, &CStringChecker::evalBcopy},
174*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"bcmp"}, 3},
175*bdd1243dSDimitry Andric        std::bind(&CStringChecker::evalMemcmp, _1, _2, _3, CK_Regular)},
176*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"bzero"}, 2}, &CStringChecker::evalBzero},
177*bdd1243dSDimitry Andric       {{CDF_MaybeBuiltin, {"explicit_bzero"}, 2}, &CStringChecker::evalBzero},
1780b57cec5SDimitry Andric   };
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   // These require a bit of special handling.
1810b57cec5SDimitry Andric   CallDescription StdCopy{{"std", "copy"}, 3},
1820b57cec5SDimitry Andric       StdCopyBackward{{"std", "copy_backward"}, 3};
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
185*bdd1243dSDimitry Andric   void evalMemcpy(CheckerContext &C, const CallExpr *CE, CharKind CK) const;
186*bdd1243dSDimitry Andric   void evalMempcpy(CheckerContext &C, const CallExpr *CE, CharKind CK) const;
187*bdd1243dSDimitry Andric   void evalMemmove(CheckerContext &C, const CallExpr *CE, CharKind CK) const;
1880b57cec5SDimitry Andric   void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
1890b57cec5SDimitry Andric   void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
1905ffd83dbSDimitry Andric                       ProgramStateRef state, SizeArgExpr Size,
1915ffd83dbSDimitry Andric                       DestinationArgExpr Dest, SourceArgExpr Source,
192*bdd1243dSDimitry Andric                       bool Restricted, bool IsMempcpy, CharKind CK) const;
1930b57cec5SDimitry Andric 
194*bdd1243dSDimitry Andric   void evalMemcmp(CheckerContext &C, const CallExpr *CE, CharKind CK) const;
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
1970b57cec5SDimitry Andric   void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
1980b57cec5SDimitry Andric   void evalstrLengthCommon(CheckerContext &C,
1990b57cec5SDimitry Andric                            const CallExpr *CE,
2000b57cec5SDimitry Andric                            bool IsStrnlen = false) const;
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
2030b57cec5SDimitry Andric   void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
2040b57cec5SDimitry Andric   void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
2050b57cec5SDimitry Andric   void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
206480093f4SDimitry Andric   void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd,
207480093f4SDimitry Andric                         bool IsBounded, ConcatFnKind appendK,
2080b57cec5SDimitry Andric                         bool returnPtr = true) const;
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
2110b57cec5SDimitry Andric   void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
2120b57cec5SDimitry Andric   void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
2150b57cec5SDimitry Andric   void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
2160b57cec5SDimitry Andric   void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
2170b57cec5SDimitry Andric   void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
2180b57cec5SDimitry Andric   void evalStrcmpCommon(CheckerContext &C,
2190b57cec5SDimitry Andric                         const CallExpr *CE,
220480093f4SDimitry Andric                         bool IsBounded = false,
221480093f4SDimitry Andric                         bool IgnoreCase = false) const;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
2260b57cec5SDimitry Andric   void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
2270b57cec5SDimitry Andric   void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
2280b57cec5SDimitry Andric   void evalMemset(CheckerContext &C, const CallExpr *CE) const;
2290b57cec5SDimitry Andric   void evalBzero(CheckerContext &C, const CallExpr *CE) const;
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   // Utility methods
2320b57cec5SDimitry Andric   std::pair<ProgramStateRef , ProgramStateRef >
2330b57cec5SDimitry Andric   static assumeZero(CheckerContext &C,
2340b57cec5SDimitry Andric                     ProgramStateRef state, SVal V, QualType Ty);
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   static ProgramStateRef setCStringLength(ProgramStateRef state,
2370b57cec5SDimitry Andric                                               const MemRegion *MR,
2380b57cec5SDimitry Andric                                               SVal strLength);
2390b57cec5SDimitry Andric   static SVal getCStringLengthForRegion(CheckerContext &C,
2400b57cec5SDimitry Andric                                         ProgramStateRef &state,
2410b57cec5SDimitry Andric                                         const Expr *Ex,
2420b57cec5SDimitry Andric                                         const MemRegion *MR,
2430b57cec5SDimitry Andric                                         bool hypothetical);
2440b57cec5SDimitry Andric   SVal getCStringLength(CheckerContext &C,
2450b57cec5SDimitry Andric                         ProgramStateRef &state,
2460b57cec5SDimitry Andric                         const Expr *Ex,
2470b57cec5SDimitry Andric                         SVal Buf,
2480b57cec5SDimitry Andric                         bool hypothetical = false) const;
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   const StringLiteral *getCStringLiteral(CheckerContext &C,
2510b57cec5SDimitry Andric                                          ProgramStateRef &state,
2520b57cec5SDimitry Andric                                          const Expr *expr,
2530b57cec5SDimitry Andric                                          SVal val) const;
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   static ProgramStateRef InvalidateBuffer(CheckerContext &C,
2560b57cec5SDimitry Andric                                           ProgramStateRef state,
2570b57cec5SDimitry Andric                                           const Expr *Ex, SVal V,
2580b57cec5SDimitry Andric                                           bool IsSourceBuffer,
2590b57cec5SDimitry Andric                                           const Expr *Size);
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
2620b57cec5SDimitry Andric                               const MemRegion *MR);
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   static bool memsetAux(const Expr *DstBuffer, SVal CharE,
2650b57cec5SDimitry Andric                         const Expr *Size, CheckerContext &C,
2660b57cec5SDimitry Andric                         ProgramStateRef &State);
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   // Re-usable checks
2695ffd83dbSDimitry Andric   ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State,
2705ffd83dbSDimitry Andric                                AnyArgExpr Arg, SVal l) const;
2715ffd83dbSDimitry Andric   ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state,
2725ffd83dbSDimitry Andric                                 AnyArgExpr Buffer, SVal Element,
273*bdd1243dSDimitry Andric                                 AccessKind Access,
274*bdd1243dSDimitry Andric                                 CharKind CK = CharKind::Regular) const;
2755ffd83dbSDimitry Andric   ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
2765ffd83dbSDimitry Andric                                     AnyArgExpr Buffer, SizeArgExpr Size,
277972a253aSDimitry Andric                                     AccessKind Access,
278*bdd1243dSDimitry Andric                                     CharKind CK = CharKind::Regular) const;
2795ffd83dbSDimitry Andric   ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state,
2805ffd83dbSDimitry Andric                                SizeArgExpr Size, AnyArgExpr First,
281*bdd1243dSDimitry Andric                                AnyArgExpr Second,
282*bdd1243dSDimitry Andric                                CharKind CK = CharKind::Regular) const;
2830b57cec5SDimitry Andric   void emitOverlapBug(CheckerContext &C,
2840b57cec5SDimitry Andric                       ProgramStateRef state,
2850b57cec5SDimitry Andric                       const Stmt *First,
2860b57cec5SDimitry Andric                       const Stmt *Second) const;
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
2890b57cec5SDimitry Andric                       StringRef WarningMsg) const;
2900b57cec5SDimitry Andric   void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
2910b57cec5SDimitry Andric                           const Stmt *S, StringRef WarningMsg) const;
2920b57cec5SDimitry Andric   void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
2930b57cec5SDimitry Andric                          const Stmt *S, StringRef WarningMsg) const;
2940b57cec5SDimitry Andric   void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
29581ad6265SDimitry Andric   void emitUninitializedReadBug(CheckerContext &C, ProgramStateRef State,
29681ad6265SDimitry Andric                              const Expr *E) const;
2970b57cec5SDimitry Andric   ProgramStateRef checkAdditionOverflow(CheckerContext &C,
2980b57cec5SDimitry Andric                                             ProgramStateRef state,
2990b57cec5SDimitry Andric                                             NonLoc left,
3000b57cec5SDimitry Andric                                             NonLoc right) const;
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   // Return true if the destination buffer of the copy function may be in bound.
3030b57cec5SDimitry Andric   // Expects SVal of Size to be positive and unsigned.
3040b57cec5SDimitry Andric   // Expects SVal of FirstBuf to be a FieldRegion.
3050b57cec5SDimitry Andric   static bool IsFirstBufInBound(CheckerContext &C,
3060b57cec5SDimitry Andric                                 ProgramStateRef state,
3070b57cec5SDimitry Andric                                 const Expr *FirstBuf,
3080b57cec5SDimitry Andric                                 const Expr *Size);
3090b57cec5SDimitry Andric };
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric } //end anonymous namespace
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3160b57cec5SDimitry Andric // Individual checks and utility methods.
3170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef >
3200b57cec5SDimitry Andric CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
3210b57cec5SDimitry Andric                            QualType Ty) {
322*bdd1243dSDimitry Andric   std::optional<DefinedSVal> val = V.getAs<DefinedSVal>();
3230b57cec5SDimitry Andric   if (!val)
3240b57cec5SDimitry Andric     return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
3270b57cec5SDimitry Andric   DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
3280b57cec5SDimitry Andric   return state->assume(svalBuilder.evalEQ(state, *val, zero));
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
3325ffd83dbSDimitry Andric                                              ProgramStateRef State,
3335ffd83dbSDimitry Andric                                              AnyArgExpr Arg, SVal l) const {
3340b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3355ffd83dbSDimitry Andric   if (!State)
3360b57cec5SDimitry Andric     return nullptr;
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   ProgramStateRef stateNull, stateNonNull;
3395ffd83dbSDimitry Andric   std::tie(stateNull, stateNonNull) =
3405ffd83dbSDimitry Andric       assumeZero(C, State, l, Arg.Expression->getType());
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   if (stateNull && !stateNonNull) {
3430b57cec5SDimitry Andric     if (Filter.CheckCStringNullArg) {
3440b57cec5SDimitry Andric       SmallString<80> buf;
345a7dea167SDimitry Andric       llvm::raw_svector_ostream OS(buf);
3460b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
3475ffd83dbSDimitry Andric       OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1)
3485ffd83dbSDimitry Andric          << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to "
349480093f4SDimitry Andric          << CurrentFunctionDescription;
3500b57cec5SDimitry Andric 
3515ffd83dbSDimitry Andric       emitNullArgBug(C, stateNull, Arg.Expression, OS.str());
3520b57cec5SDimitry Andric     }
3530b57cec5SDimitry Andric     return nullptr;
3540b57cec5SDimitry Andric   }
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   // From here on, assume that the value is non-null.
3570b57cec5SDimitry Andric   assert(stateNonNull);
3580b57cec5SDimitry Andric   return stateNonNull;
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
3620b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
3630b57cec5SDimitry Andric                                               ProgramStateRef state,
3645ffd83dbSDimitry Andric                                               AnyArgExpr Buffer, SVal Element,
365972a253aSDimitry Andric                                               AccessKind Access,
366*bdd1243dSDimitry Andric                                               CharKind CK) const {
3675ffd83dbSDimitry Andric 
3680b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3690b57cec5SDimitry Andric   if (!state)
3700b57cec5SDimitry Andric     return nullptr;
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   // Check for out of bound array element access.
3735ffd83dbSDimitry Andric   const MemRegion *R = Element.getAsRegion();
3740b57cec5SDimitry Andric   if (!R)
3750b57cec5SDimitry Andric     return state;
3760b57cec5SDimitry Andric 
3775ffd83dbSDimitry Andric   const auto *ER = dyn_cast<ElementRegion>(R);
3780b57cec5SDimitry Andric   if (!ER)
3790b57cec5SDimitry Andric     return state;
3800b57cec5SDimitry Andric 
381972a253aSDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
382972a253aSDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
383972a253aSDimitry Andric 
384972a253aSDimitry Andric   // Get the index of the accessed element.
385972a253aSDimitry Andric   NonLoc Idx = ER->getIndex();
386972a253aSDimitry Andric 
387*bdd1243dSDimitry Andric   if (CK == CharKind::Regular) {
388972a253aSDimitry Andric     if (ER->getValueType() != Ctx.CharTy)
3890b57cec5SDimitry Andric       return state;
390972a253aSDimitry Andric   } else {
391972a253aSDimitry Andric     if (ER->getValueType() != Ctx.WideCharTy)
392972a253aSDimitry Andric       return state;
393972a253aSDimitry Andric 
394972a253aSDimitry Andric     QualType SizeTy = Ctx.getSizeType();
395972a253aSDimitry Andric     NonLoc WideSize =
396972a253aSDimitry Andric         svalBuilder
397972a253aSDimitry Andric             .makeIntVal(Ctx.getTypeSizeInChars(Ctx.WideCharTy).getQuantity(),
398972a253aSDimitry Andric                         SizeTy)
399972a253aSDimitry Andric             .castAs<NonLoc>();
400972a253aSDimitry Andric     SVal Offset = svalBuilder.evalBinOpNN(state, BO_Mul, Idx, WideSize, SizeTy);
401972a253aSDimitry Andric     if (Offset.isUnknown())
402972a253aSDimitry Andric       return state;
403972a253aSDimitry Andric     Idx = Offset.castAs<NonLoc>();
404972a253aSDimitry Andric   }
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   // Get the size of the array.
4075ffd83dbSDimitry Andric   const auto *superReg = cast<SubRegion>(ER->getSuperRegion());
4085ffd83dbSDimitry Andric   DefinedOrUnknownSVal Size =
409fe6060f1SDimitry Andric       getDynamicExtent(state, superReg, C.getSValBuilder());
4100b57cec5SDimitry Andric 
41181ad6265SDimitry Andric   ProgramStateRef StInBound, StOutBound;
41281ad6265SDimitry Andric   std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, Size);
4130b57cec5SDimitry Andric   if (StOutBound && !StInBound) {
4140b57cec5SDimitry Andric     // These checks are either enabled by the CString out-of-bounds checker
4150b57cec5SDimitry Andric     // explicitly or implicitly by the Malloc checker.
4160b57cec5SDimitry Andric     // In the latter case we only do modeling but do not emit warning.
4170b57cec5SDimitry Andric     if (!Filter.CheckCStringOutOfBounds)
4180b57cec5SDimitry Andric       return nullptr;
4190b57cec5SDimitry Andric 
4205ffd83dbSDimitry Andric     // Emit a bug report.
4215ffd83dbSDimitry Andric     ErrorMessage Message =
4225ffd83dbSDimitry Andric         createOutOfBoundErrorMsg(CurrentFunctionDescription, Access);
4235ffd83dbSDimitry Andric     emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message);
4240b57cec5SDimitry Andric     return nullptr;
4250b57cec5SDimitry Andric   }
4260b57cec5SDimitry Andric 
42781ad6265SDimitry Andric   // Ensure that we wouldn't read uninitialized value.
42881ad6265SDimitry Andric   if (Access == AccessKind::read) {
42981ad6265SDimitry Andric     if (Filter.CheckCStringUninitializedRead &&
43081ad6265SDimitry Andric         StInBound->getSVal(ER).isUndef()) {
43181ad6265SDimitry Andric       emitUninitializedReadBug(C, StInBound, Buffer.Expression);
43281ad6265SDimitry Andric       return nullptr;
43381ad6265SDimitry Andric     }
43481ad6265SDimitry Andric   }
43581ad6265SDimitry Andric 
4360b57cec5SDimitry Andric   // Array bound check succeeded.  From this point forward the array bound
4370b57cec5SDimitry Andric   // should always succeed.
4380b57cec5SDimitry Andric   return StInBound;
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric 
441972a253aSDimitry Andric ProgramStateRef
442972a253aSDimitry Andric CStringChecker::CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
443972a253aSDimitry Andric                                   AnyArgExpr Buffer, SizeArgExpr Size,
444*bdd1243dSDimitry Andric                                   AccessKind Access, CharKind CK) const {
4450b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
4465ffd83dbSDimitry Andric   if (!State)
4470b57cec5SDimitry Andric     return nullptr;
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
4500b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
4510b57cec5SDimitry Andric 
4525ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
453*bdd1243dSDimitry Andric   QualType PtrTy = getCharPtrType(Ctx, CK);
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   // Check that the first buffer is non-null.
4565ffd83dbSDimitry Andric   SVal BufVal = C.getSVal(Buffer.Expression);
4575ffd83dbSDimitry Andric   State = checkNonNull(C, State, Buffer, BufVal);
4585ffd83dbSDimitry Andric   if (!State)
4590b57cec5SDimitry Andric     return nullptr;
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
4620b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
4635ffd83dbSDimitry Andric     return State;
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   // Get the access length and make sure it is known.
4660b57cec5SDimitry Andric   // FIXME: This assumes the caller has already checked that the access length
4670b57cec5SDimitry Andric   // is positive. And that it's unsigned.
4685ffd83dbSDimitry Andric   SVal LengthVal = C.getSVal(Size.Expression);
469*bdd1243dSDimitry Andric   std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
4700b57cec5SDimitry Andric   if (!Length)
4715ffd83dbSDimitry Andric     return State;
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
4745ffd83dbSDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>();
4755ffd83dbSDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy);
4760b57cec5SDimitry Andric   if (Offset.isUnknown())
4770b57cec5SDimitry Andric     return nullptr;
4780b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
4815ffd83dbSDimitry Andric   SVal BufStart =
4825ffd83dbSDimitry Andric       svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType());
483*bdd1243dSDimitry Andric   if (std::optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
4840b57cec5SDimitry Andric 
4855ffd83dbSDimitry Andric     SVal BufEnd =
4865ffd83dbSDimitry Andric         svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy);
487*bdd1243dSDimitry Andric     State = CheckLocation(C, State, Buffer, BufEnd, Access, CK);
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric     // If the buffer isn't large enough, abort.
4905ffd83dbSDimitry Andric     if (!State)
4910b57cec5SDimitry Andric       return nullptr;
4920b57cec5SDimitry Andric   }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   // Large enough or not, return this state!
4955ffd83dbSDimitry Andric   return State;
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
4990b57cec5SDimitry Andric                                              ProgramStateRef state,
5005ffd83dbSDimitry Andric                                              SizeArgExpr Size, AnyArgExpr First,
501972a253aSDimitry Andric                                              AnyArgExpr Second,
502*bdd1243dSDimitry Andric                                              CharKind CK) const {
5030b57cec5SDimitry Andric   if (!Filter.CheckCStringBufferOverlap)
5040b57cec5SDimitry Andric     return state;
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   // Do a simple check for overlap: if the two arguments are from the same
5070b57cec5SDimitry Andric   // buffer, see if the end of the first is greater than the start of the second
5080b57cec5SDimitry Andric   // or vice versa.
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
5110b57cec5SDimitry Andric   if (!state)
5120b57cec5SDimitry Andric     return nullptr;
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   ProgramStateRef stateTrue, stateFalse;
5150b57cec5SDimitry Andric 
51681ad6265SDimitry Andric   // Assume different address spaces cannot overlap.
51781ad6265SDimitry Andric   if (First.Expression->getType()->getPointeeType().getAddressSpace() !=
51881ad6265SDimitry Andric       Second.Expression->getType()->getPointeeType().getAddressSpace())
51981ad6265SDimitry Andric     return state;
52081ad6265SDimitry Andric 
5210b57cec5SDimitry Andric   // Get the buffer values and make sure they're known locations.
5220b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
5235ffd83dbSDimitry Andric   SVal firstVal = state->getSVal(First.Expression, LCtx);
5245ffd83dbSDimitry Andric   SVal secondVal = state->getSVal(Second.Expression, LCtx);
5250b57cec5SDimitry Andric 
526*bdd1243dSDimitry Andric   std::optional<Loc> firstLoc = firstVal.getAs<Loc>();
5270b57cec5SDimitry Andric   if (!firstLoc)
5280b57cec5SDimitry Andric     return state;
5290b57cec5SDimitry Andric 
530*bdd1243dSDimitry Andric   std::optional<Loc> secondLoc = secondVal.getAs<Loc>();
5310b57cec5SDimitry Andric   if (!secondLoc)
5320b57cec5SDimitry Andric     return state;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   // Are the two values the same?
5350b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
5360b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) =
5370b57cec5SDimitry Andric       state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
5400b57cec5SDimitry Andric     // If the values are known to be equal, that's automatically an overlap.
5415ffd83dbSDimitry Andric     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
5420b57cec5SDimitry Andric     return nullptr;
5430b57cec5SDimitry Andric   }
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric   // assume the two expressions are not equal.
5460b57cec5SDimitry Andric   assert(stateFalse);
5470b57cec5SDimitry Andric   state = stateFalse;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   // Which value comes first?
5500b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
5515ffd83dbSDimitry Andric   SVal reverse =
5525ffd83dbSDimitry Andric       svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy);
553*bdd1243dSDimitry Andric   std::optional<DefinedOrUnknownSVal> reverseTest =
5540b57cec5SDimitry Andric       reverse.getAs<DefinedOrUnknownSVal>();
5550b57cec5SDimitry Andric   if (!reverseTest)
5560b57cec5SDimitry Andric     return state;
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
5590b57cec5SDimitry Andric   if (stateTrue) {
5600b57cec5SDimitry Andric     if (stateFalse) {
5610b57cec5SDimitry Andric       // If we don't know which one comes first, we can't perform this test.
5620b57cec5SDimitry Andric       return state;
5630b57cec5SDimitry Andric     } else {
5640b57cec5SDimitry Andric       // Switch the values so that firstVal is before secondVal.
5650b57cec5SDimitry Andric       std::swap(firstLoc, secondLoc);
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric       // Switch the Exprs as well, so that they still correspond.
5680b57cec5SDimitry Andric       std::swap(First, Second);
5690b57cec5SDimitry Andric     }
5700b57cec5SDimitry Andric   }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   // Get the length, and make sure it too is known.
5735ffd83dbSDimitry Andric   SVal LengthVal = state->getSVal(Size.Expression, LCtx);
574*bdd1243dSDimitry Andric   std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
5750b57cec5SDimitry Andric   if (!Length)
5760b57cec5SDimitry Andric     return state;
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric   // Convert the first buffer's start address to char*.
5790b57cec5SDimitry Andric   // Bail out if the cast fails.
5800b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
581*bdd1243dSDimitry Andric   QualType CharPtrTy = getCharPtrType(Ctx, CK);
5825ffd83dbSDimitry Andric   SVal FirstStart =
5835ffd83dbSDimitry Andric       svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType());
584*bdd1243dSDimitry Andric   std::optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
5850b57cec5SDimitry Andric   if (!FirstStartLoc)
5860b57cec5SDimitry Andric     return state;
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric   // Compute the end of the first buffer. Bail out if THAT fails.
5895ffd83dbSDimitry Andric   SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc,
5905ffd83dbSDimitry Andric                                           *Length, CharPtrTy);
591*bdd1243dSDimitry Andric   std::optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
5920b57cec5SDimitry Andric   if (!FirstEndLoc)
5930b57cec5SDimitry Andric     return state;
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   // Is the end of the first buffer past the start of the second buffer?
5965ffd83dbSDimitry Andric   SVal Overlap =
5975ffd83dbSDimitry Andric       svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy);
598*bdd1243dSDimitry Andric   std::optional<DefinedOrUnknownSVal> OverlapTest =
5990b57cec5SDimitry Andric       Overlap.getAs<DefinedOrUnknownSVal>();
6000b57cec5SDimitry Andric   if (!OverlapTest)
6010b57cec5SDimitry Andric     return state;
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
6060b57cec5SDimitry Andric     // Overlap!
6075ffd83dbSDimitry Andric     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
6080b57cec5SDimitry Andric     return nullptr;
6090b57cec5SDimitry Andric   }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric   // assume the two expressions don't overlap.
6120b57cec5SDimitry Andric   assert(stateFalse);
6130b57cec5SDimitry Andric   return stateFalse;
6140b57cec5SDimitry Andric }
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
6170b57cec5SDimitry Andric                                   const Stmt *First, const Stmt *Second) const {
6180b57cec5SDimitry Andric   ExplodedNode *N = C.generateErrorNode(state);
6190b57cec5SDimitry Andric   if (!N)
6200b57cec5SDimitry Andric     return;
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   if (!BT_Overlap)
6230b57cec5SDimitry Andric     BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
6240b57cec5SDimitry Andric                                  categories::UnixAPI, "Improper arguments"));
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   // Generate a report for this bug.
627a7dea167SDimitry Andric   auto report = std::make_unique<PathSensitiveBugReport>(
6280b57cec5SDimitry Andric       *BT_Overlap, "Arguments must not be overlapping buffers", N);
6290b57cec5SDimitry Andric   report->addRange(First->getSourceRange());
6300b57cec5SDimitry Andric   report->addRange(Second->getSourceRange());
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   C.emitReport(std::move(report));
6330b57cec5SDimitry Andric }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
6360b57cec5SDimitry Andric                                     const Stmt *S, StringRef WarningMsg) const {
6370b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6380b57cec5SDimitry Andric     if (!BT_Null)
6390b57cec5SDimitry Andric       BT_Null.reset(new BuiltinBug(
6400b57cec5SDimitry Andric           Filter.CheckNameCStringNullArg, categories::UnixAPI,
6410b57cec5SDimitry Andric           "Null pointer argument in call to byte string function"));
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
644a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
6450b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6460b57cec5SDimitry Andric     if (const auto *Ex = dyn_cast<Expr>(S))
6470b57cec5SDimitry Andric       bugreporter::trackExpressionValue(N, Ex, *Report);
6480b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6490b57cec5SDimitry Andric   }
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric 
65281ad6265SDimitry Andric void CStringChecker::emitUninitializedReadBug(CheckerContext &C,
65381ad6265SDimitry Andric                                               ProgramStateRef State,
65481ad6265SDimitry Andric                                               const Expr *E) const {
65581ad6265SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
65681ad6265SDimitry Andric     const char *Msg =
65781ad6265SDimitry Andric         "Bytes string function accesses uninitialized/garbage values";
65881ad6265SDimitry Andric     if (!BT_UninitRead)
65981ad6265SDimitry Andric       BT_UninitRead.reset(
66081ad6265SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringUninitializedRead,
66181ad6265SDimitry Andric                          "Accessing unitialized/garbage values", Msg));
66281ad6265SDimitry Andric 
66381ad6265SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_UninitRead.get());
66481ad6265SDimitry Andric 
66581ad6265SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
66681ad6265SDimitry Andric     Report->addRange(E->getSourceRange());
66781ad6265SDimitry Andric     bugreporter::trackExpressionValue(N, E, *Report);
66881ad6265SDimitry Andric     C.emitReport(std::move(Report));
66981ad6265SDimitry Andric   }
67081ad6265SDimitry Andric }
67181ad6265SDimitry Andric 
6720b57cec5SDimitry Andric void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
6730b57cec5SDimitry Andric                                         ProgramStateRef State, const Stmt *S,
6740b57cec5SDimitry Andric                                         StringRef WarningMsg) const {
6750b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6760b57cec5SDimitry Andric     if (!BT_Bounds)
6770b57cec5SDimitry Andric       BT_Bounds.reset(new BuiltinBug(
6780b57cec5SDimitry Andric           Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
6790b57cec5SDimitry Andric                                          : Filter.CheckNameCStringNullArg,
6800b57cec5SDimitry Andric           "Out-of-bound array access",
6810b57cec5SDimitry Andric           "Byte string function accesses out-of-bound array element"));
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric     // FIXME: It would be nice to eventually make this diagnostic more clear,
6860b57cec5SDimitry Andric     // e.g., by referencing the original declaration or by saying *why* this
6870b57cec5SDimitry Andric     // reference is outside the range.
688a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
6890b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6900b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6910b57cec5SDimitry Andric   }
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
6950b57cec5SDimitry Andric                                        const Stmt *S,
6960b57cec5SDimitry Andric                                        StringRef WarningMsg) const {
6970b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
6980b57cec5SDimitry Andric     if (!BT_NotCString)
6990b57cec5SDimitry Andric       BT_NotCString.reset(new BuiltinBug(
7000b57cec5SDimitry Andric           Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
7010b57cec5SDimitry Andric           "Argument is not a null-terminated string."));
7020b57cec5SDimitry Andric 
703a7dea167SDimitry Andric     auto Report =
704a7dea167SDimitry Andric         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
7070b57cec5SDimitry Andric     C.emitReport(std::move(Report));
7080b57cec5SDimitry Andric   }
7090b57cec5SDimitry Andric }
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
7120b57cec5SDimitry Andric                                              ProgramStateRef State) const {
7130b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
71481ad6265SDimitry Andric     if (!BT_AdditionOverflow)
71581ad6265SDimitry Andric       BT_AdditionOverflow.reset(
7160b57cec5SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
7170b57cec5SDimitry Andric                          "Sum of expressions causes overflow."));
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric     // This isn't a great error message, but this should never occur in real
7200b57cec5SDimitry Andric     // code anyway -- you'd have to create a buffer longer than a size_t can
7210b57cec5SDimitry Andric     // represent, which is sort of a contradiction.
7220b57cec5SDimitry Andric     const char *WarningMsg =
7230b57cec5SDimitry Andric         "This expression will create a string whose length is too big to "
7240b57cec5SDimitry Andric         "be represented as a size_t";
7250b57cec5SDimitry Andric 
72681ad6265SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow,
72781ad6265SDimitry Andric                                                            WarningMsg, N);
7280b57cec5SDimitry Andric     C.emitReport(std::move(Report));
7290b57cec5SDimitry Andric   }
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
7320b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
7330b57cec5SDimitry Andric                                                      ProgramStateRef state,
7340b57cec5SDimitry Andric                                                      NonLoc left,
7350b57cec5SDimitry Andric                                                      NonLoc right) const {
7360b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
7370b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
7380b57cec5SDimitry Andric     return state;
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
7410b57cec5SDimitry Andric   if (!state)
7420b57cec5SDimitry Andric     return nullptr;
7430b57cec5SDimitry Andric 
7440b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
7450b57cec5SDimitry Andric   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
7480b57cec5SDimitry Andric   const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
7490b57cec5SDimitry Andric   NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric   SVal maxMinusRight;
75281ad6265SDimitry Andric   if (isa<nonloc::ConcreteInt>(right)) {
7530b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
7540b57cec5SDimitry Andric                                                  sizeTy);
7550b57cec5SDimitry Andric   } else {
7560b57cec5SDimitry Andric     // Try switching the operands. (The order of these two assignments is
7570b57cec5SDimitry Andric     // important!)
7580b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
7590b57cec5SDimitry Andric                                             sizeTy);
7600b57cec5SDimitry Andric     left = right;
7610b57cec5SDimitry Andric   }
7620b57cec5SDimitry Andric 
763*bdd1243dSDimitry Andric   if (std::optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
7640b57cec5SDimitry Andric     QualType cmpTy = svalBuilder.getConditionType();
7650b57cec5SDimitry Andric     // If left > max - right, we have an overflow.
7660b57cec5SDimitry Andric     SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
7670b57cec5SDimitry Andric                                                 *maxMinusRightNL, cmpTy);
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric     ProgramStateRef stateOverflow, stateOkay;
7700b57cec5SDimitry Andric     std::tie(stateOverflow, stateOkay) =
7710b57cec5SDimitry Andric       state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric     if (stateOverflow && !stateOkay) {
7740b57cec5SDimitry Andric       // We have an overflow. Emit a bug report.
7750b57cec5SDimitry Andric       emitAdditionOverflowBug(C, stateOverflow);
7760b57cec5SDimitry Andric       return nullptr;
7770b57cec5SDimitry Andric     }
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric     // From now on, assume an overflow didn't occur.
7800b57cec5SDimitry Andric     assert(stateOkay);
7810b57cec5SDimitry Andric     state = stateOkay;
7820b57cec5SDimitry Andric   }
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric   return state;
7850b57cec5SDimitry Andric }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
7880b57cec5SDimitry Andric                                                 const MemRegion *MR,
7890b57cec5SDimitry Andric                                                 SVal strLength) {
7900b57cec5SDimitry Andric   assert(!strLength.isUndef() && "Attempt to set an undefined string length");
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric   MR = MR->StripCasts();
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   switch (MR->getKind()) {
7950b57cec5SDimitry Andric   case MemRegion::StringRegionKind:
7960b57cec5SDimitry Andric     // FIXME: This can happen if we strcpy() into a string region. This is
7970b57cec5SDimitry Andric     // undefined [C99 6.4.5p6], but we should still warn about it.
7980b57cec5SDimitry Andric     return state;
7990b57cec5SDimitry Andric 
8000b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
8010b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
8025ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
8035ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
8040b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
8050b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
8060b57cec5SDimitry Andric     // These are the types we can currently track string lengths for.
8070b57cec5SDimitry Andric     break;
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
8100b57cec5SDimitry Andric     // FIXME: Handle element regions by upper-bounding the parent region's
8110b57cec5SDimitry Andric     // string length.
8120b57cec5SDimitry Andric     return state;
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric   default:
8150b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
8160b57cec5SDimitry Andric     // For now, just ignore the change.
8170b57cec5SDimitry Andric     // FIXME: These are rare but not impossible. We should output some kind of
8180b57cec5SDimitry Andric     // warning for things like strcpy((char[]){'a', 0}, "b");
8190b57cec5SDimitry Andric     return state;
8200b57cec5SDimitry Andric   }
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric   if (strLength.isUnknown())
8230b57cec5SDimitry Andric     return state->remove<CStringLength>(MR);
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric   return state->set<CStringLength>(MR, strLength);
8260b57cec5SDimitry Andric }
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
8290b57cec5SDimitry Andric                                                ProgramStateRef &state,
8300b57cec5SDimitry Andric                                                const Expr *Ex,
8310b57cec5SDimitry Andric                                                const MemRegion *MR,
8320b57cec5SDimitry Andric                                                bool hypothetical) {
8330b57cec5SDimitry Andric   if (!hypothetical) {
8340b57cec5SDimitry Andric     // If there's a recorded length, go ahead and return it.
8350b57cec5SDimitry Andric     const SVal *Recorded = state->get<CStringLength>(MR);
8360b57cec5SDimitry Andric     if (Recorded)
8370b57cec5SDimitry Andric       return *Recorded;
8380b57cec5SDimitry Andric   }
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   // Otherwise, get a new symbol and update the state.
8410b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
8420b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
8430b57cec5SDimitry Andric   SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
8440b57cec5SDimitry Andric                                                     MR, Ex, sizeTy,
8450b57cec5SDimitry Andric                                                     C.getLocationContext(),
8460b57cec5SDimitry Andric                                                     C.blockCount());
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric   if (!hypothetical) {
849*bdd1243dSDimitry Andric     if (std::optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
8500b57cec5SDimitry Andric       // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
8510b57cec5SDimitry Andric       BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
8520b57cec5SDimitry Andric       const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
8530b57cec5SDimitry Andric       llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
8540b57cec5SDimitry Andric       const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
8550b57cec5SDimitry Andric                                                         fourInt);
8560b57cec5SDimitry Andric       NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
8570b57cec5SDimitry Andric       SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
8580b57cec5SDimitry Andric                                                 maxLength, sizeTy);
8590b57cec5SDimitry Andric       state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
8600b57cec5SDimitry Andric     }
8610b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
8620b57cec5SDimitry Andric   }
8630b57cec5SDimitry Andric 
8640b57cec5SDimitry Andric   return strLength;
8650b57cec5SDimitry Andric }
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
8680b57cec5SDimitry Andric                                       const Expr *Ex, SVal Buf,
8690b57cec5SDimitry Andric                                       bool hypothetical) const {
8700b57cec5SDimitry Andric   const MemRegion *MR = Buf.getAsRegion();
8710b57cec5SDimitry Andric   if (!MR) {
8720b57cec5SDimitry Andric     // If we can't get a region, see if it's something we /know/ isn't a
8730b57cec5SDimitry Andric     // C string. In the context of locations, the only time we can issue such
8740b57cec5SDimitry Andric     // a warning is for labels.
875*bdd1243dSDimitry Andric     if (std::optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
8760b57cec5SDimitry Andric       if (Filter.CheckCStringNotNullTerm) {
8770b57cec5SDimitry Andric         SmallString<120> buf;
8780b57cec5SDimitry Andric         llvm::raw_svector_ostream os(buf);
8790b57cec5SDimitry Andric         assert(CurrentFunctionDescription);
8800b57cec5SDimitry Andric         os << "Argument to " << CurrentFunctionDescription
8810b57cec5SDimitry Andric            << " is the address of the label '" << Label->getLabel()->getName()
8820b57cec5SDimitry Andric            << "', which is not a null-terminated string";
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric         emitNotCStringBug(C, state, Ex, os.str());
8850b57cec5SDimitry Andric       }
8860b57cec5SDimitry Andric       return UndefinedVal();
8870b57cec5SDimitry Andric     }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric     // If it's not a region and not a label, give up.
8900b57cec5SDimitry Andric     return UnknownVal();
8910b57cec5SDimitry Andric   }
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   // If we have a region, strip casts from it and see if we can figure out
8940b57cec5SDimitry Andric   // its length. For anything we can't figure out, just return UnknownVal.
8950b57cec5SDimitry Andric   MR = MR->StripCasts();
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   switch (MR->getKind()) {
8980b57cec5SDimitry Andric   case MemRegion::StringRegionKind: {
8990b57cec5SDimitry Andric     // Modifying the contents of string regions is undefined [C99 6.4.5p6],
9000b57cec5SDimitry Andric     // so we can assume that the byte length is the correct C string length.
9010b57cec5SDimitry Andric     SValBuilder &svalBuilder = C.getSValBuilder();
9020b57cec5SDimitry Andric     QualType sizeTy = svalBuilder.getContext().getSizeType();
9030b57cec5SDimitry Andric     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
904753f127fSDimitry Andric     return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
9050b57cec5SDimitry Andric   }
9060b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
9070b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
9085ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
9095ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
9100b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
9110b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
9120b57cec5SDimitry Andric     return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
9130b57cec5SDimitry Andric   case MemRegion::CompoundLiteralRegionKind:
9140b57cec5SDimitry Andric     // FIXME: Can we track this? Is it necessary?
9150b57cec5SDimitry Andric     return UnknownVal();
9160b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
9170b57cec5SDimitry Andric     // FIXME: How can we handle this? It's not good enough to subtract the
9180b57cec5SDimitry Andric     // offset from the base string length; consider "123\x00567" and &a[5].
9190b57cec5SDimitry Andric     return UnknownVal();
9200b57cec5SDimitry Andric   default:
9210b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
9220b57cec5SDimitry Andric     // In this case, an error is emitted and UndefinedVal is returned.
9230b57cec5SDimitry Andric     // The caller should always be prepared to handle this case.
9240b57cec5SDimitry Andric     if (Filter.CheckCStringNotNullTerm) {
9250b57cec5SDimitry Andric       SmallString<120> buf;
9260b57cec5SDimitry Andric       llvm::raw_svector_ostream os(buf);
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
9290b57cec5SDimitry Andric       os << "Argument to " << CurrentFunctionDescription << " is ";
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric       if (SummarizeRegion(os, C.getASTContext(), MR))
9320b57cec5SDimitry Andric         os << ", which is not a null-terminated string";
9330b57cec5SDimitry Andric       else
9340b57cec5SDimitry Andric         os << "not a null-terminated string";
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric       emitNotCStringBug(C, state, Ex, os.str());
9370b57cec5SDimitry Andric     }
9380b57cec5SDimitry Andric     return UndefinedVal();
9390b57cec5SDimitry Andric   }
9400b57cec5SDimitry Andric }
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
9430b57cec5SDimitry Andric   ProgramStateRef &state, const Expr *expr, SVal val) const {
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric   // Get the memory region pointed to by the val.
9460b57cec5SDimitry Andric   const MemRegion *bufRegion = val.getAsRegion();
9470b57cec5SDimitry Andric   if (!bufRegion)
9480b57cec5SDimitry Andric     return nullptr;
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric   // Strip casts off the memory region.
9510b57cec5SDimitry Andric   bufRegion = bufRegion->StripCasts();
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric   // Cast the memory region to a string region.
9540b57cec5SDimitry Andric   const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
9550b57cec5SDimitry Andric   if (!strRegion)
9560b57cec5SDimitry Andric     return nullptr;
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   // Return the actual string in the string region.
9590b57cec5SDimitry Andric   return strRegion->getStringLiteral();
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
9630b57cec5SDimitry Andric                                        ProgramStateRef state,
9640b57cec5SDimitry Andric                                        const Expr *FirstBuf,
9650b57cec5SDimitry Andric                                        const Expr *Size) {
9660b57cec5SDimitry Andric   // If we do not know that the buffer is long enough we return 'true'.
9670b57cec5SDimitry Andric   // Otherwise the parent region of this field region would also get
9680b57cec5SDimitry Andric   // invalidated, which would lead to warnings based on an unknown state.
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   // Originally copied from CheckBufferAccess and CheckLocation.
9710b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
9720b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
9730b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
9760b57cec5SDimitry Andric   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
9770b57cec5SDimitry Andric   SVal BufVal = state->getSVal(FirstBuf, LCtx);
9780b57cec5SDimitry Andric 
9790b57cec5SDimitry Andric   SVal LengthVal = state->getSVal(Size, LCtx);
980*bdd1243dSDimitry Andric   std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
9810b57cec5SDimitry Andric   if (!Length)
9820b57cec5SDimitry Andric     return true; // cf top comment.
9830b57cec5SDimitry Andric 
9840b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
9850b57cec5SDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
9860b57cec5SDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
9870b57cec5SDimitry Andric   if (Offset.isUnknown())
9880b57cec5SDimitry Andric     return true; // cf top comment
9890b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
9920b57cec5SDimitry Andric   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
993*bdd1243dSDimitry Andric   std::optional<Loc> BufLoc = BufStart.getAs<Loc>();
9940b57cec5SDimitry Andric   if (!BufLoc)
9950b57cec5SDimitry Andric     return true; // cf top comment.
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   SVal BufEnd =
9980b57cec5SDimitry Andric       svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric   // Check for out of bound array element access.
10010b57cec5SDimitry Andric   const MemRegion *R = BufEnd.getAsRegion();
10020b57cec5SDimitry Andric   if (!R)
10030b57cec5SDimitry Andric     return true; // cf top comment.
10040b57cec5SDimitry Andric 
10050b57cec5SDimitry Andric   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
10060b57cec5SDimitry Andric   if (!ER)
10070b57cec5SDimitry Andric     return true; // cf top comment.
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric   // FIXME: Does this crash when a non-standard definition
10100b57cec5SDimitry Andric   // of a library function is encountered?
10110b57cec5SDimitry Andric   assert(ER->getValueType() == C.getASTContext().CharTy &&
10120b57cec5SDimitry Andric          "IsFirstBufInBound should only be called with char* ElementRegions");
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric   // Get the size of the array.
10150b57cec5SDimitry Andric   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
1016fe6060f1SDimitry Andric   DefinedOrUnknownSVal SizeDV = getDynamicExtent(state, superReg, svalBuilder);
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   // Get the index of the accessed element.
10190b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
10200b57cec5SDimitry Andric 
10215ffd83dbSDimitry Andric   ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true);
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   return static_cast<bool>(StInBound);
10240b57cec5SDimitry Andric }
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
10270b57cec5SDimitry Andric                                                  ProgramStateRef state,
10280b57cec5SDimitry Andric                                                  const Expr *E, SVal V,
10290b57cec5SDimitry Andric                                                  bool IsSourceBuffer,
10300b57cec5SDimitry Andric                                                  const Expr *Size) {
1031*bdd1243dSDimitry Andric   std::optional<Loc> L = V.getAs<Loc>();
10320b57cec5SDimitry Andric   if (!L)
10330b57cec5SDimitry Andric     return state;
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric   // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
10360b57cec5SDimitry Andric   // some assumptions about the value that CFRefCount can't. Even so, it should
10370b57cec5SDimitry Andric   // probably be refactored.
1038*bdd1243dSDimitry Andric   if (std::optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
10390b57cec5SDimitry Andric     const MemRegion *R = MR->getRegion()->StripCasts();
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric     // Are we dealing with an ElementRegion?  If so, we should be invalidating
10420b57cec5SDimitry Andric     // the super-region.
10430b57cec5SDimitry Andric     if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
10440b57cec5SDimitry Andric       R = ER->getSuperRegion();
10450b57cec5SDimitry Andric       // FIXME: What about layers of ElementRegions?
10460b57cec5SDimitry Andric     }
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric     // Invalidate this region.
10490b57cec5SDimitry Andric     const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric     bool CausesPointerEscape = false;
10520b57cec5SDimitry Andric     RegionAndSymbolInvalidationTraits ITraits;
10530b57cec5SDimitry Andric     // Invalidate and escape only indirect regions accessible through the source
10540b57cec5SDimitry Andric     // buffer.
10550b57cec5SDimitry Andric     if (IsSourceBuffer) {
10560b57cec5SDimitry Andric       ITraits.setTrait(R->getBaseRegion(),
10570b57cec5SDimitry Andric                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
10580b57cec5SDimitry Andric       ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
10590b57cec5SDimitry Andric       CausesPointerEscape = true;
10600b57cec5SDimitry Andric     } else {
10610b57cec5SDimitry Andric       const MemRegion::Kind& K = R->getKind();
10620b57cec5SDimitry Andric       if (K == MemRegion::FieldRegionKind)
10630b57cec5SDimitry Andric         if (Size && IsFirstBufInBound(C, state, E, Size)) {
10640b57cec5SDimitry Andric           // If destination buffer is a field region and access is in bound,
10650b57cec5SDimitry Andric           // do not invalidate its super region.
10660b57cec5SDimitry Andric           ITraits.setTrait(
10670b57cec5SDimitry Andric               R,
10680b57cec5SDimitry Andric               RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
10690b57cec5SDimitry Andric         }
10700b57cec5SDimitry Andric     }
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric     return state->invalidateRegions(R, E, C.blockCount(), LCtx,
10730b57cec5SDimitry Andric                                     CausesPointerEscape, nullptr, nullptr,
10740b57cec5SDimitry Andric                                     &ITraits);
10750b57cec5SDimitry Andric   }
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric   // If we have a non-region value by chance, just remove the binding.
10780b57cec5SDimitry Andric   // FIXME: is this necessary or correct? This handles the non-Region
10790b57cec5SDimitry Andric   //  cases.  Is it ever valid to store to these?
10800b57cec5SDimitry Andric   return state->killBinding(*L);
10810b57cec5SDimitry Andric }
10820b57cec5SDimitry Andric 
10830b57cec5SDimitry Andric bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
10840b57cec5SDimitry Andric                                      const MemRegion *MR) {
10850b57cec5SDimitry Andric   switch (MR->getKind()) {
10860b57cec5SDimitry Andric   case MemRegion::FunctionCodeRegionKind: {
1087480093f4SDimitry Andric     if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl())
10880b57cec5SDimitry Andric       os << "the address of the function '" << *FD << '\'';
10890b57cec5SDimitry Andric     else
10900b57cec5SDimitry Andric       os << "the address of a function";
10910b57cec5SDimitry Andric     return true;
10920b57cec5SDimitry Andric   }
10930b57cec5SDimitry Andric   case MemRegion::BlockCodeRegionKind:
10940b57cec5SDimitry Andric     os << "block text";
10950b57cec5SDimitry Andric     return true;
10960b57cec5SDimitry Andric   case MemRegion::BlockDataRegionKind:
10970b57cec5SDimitry Andric     os << "a block";
10980b57cec5SDimitry Andric     return true;
10990b57cec5SDimitry Andric   case MemRegion::CXXThisRegionKind:
11000b57cec5SDimitry Andric   case MemRegion::CXXTempObjectRegionKind:
1101480093f4SDimitry Andric     os << "a C++ temp object of type "
110281ad6265SDimitry Andric        << cast<TypedValueRegion>(MR)->getValueType();
11030b57cec5SDimitry Andric     return true;
11045ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
110581ad6265SDimitry Andric     os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType();
11060b57cec5SDimitry Andric     return true;
11075ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
110881ad6265SDimitry Andric     os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType();
11095ffd83dbSDimitry Andric     return true;
11100b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
111181ad6265SDimitry Andric     os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType();
11120b57cec5SDimitry Andric     return true;
11130b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
1114480093f4SDimitry Andric     os << "an instance variable of type "
111581ad6265SDimitry Andric        << cast<TypedValueRegion>(MR)->getValueType();
11160b57cec5SDimitry Andric     return true;
11170b57cec5SDimitry Andric   default:
11180b57cec5SDimitry Andric     return false;
11190b57cec5SDimitry Andric   }
11200b57cec5SDimitry Andric }
11210b57cec5SDimitry Andric 
11220b57cec5SDimitry Andric bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
11230b57cec5SDimitry Andric                                const Expr *Size, CheckerContext &C,
11240b57cec5SDimitry Andric                                ProgramStateRef &State) {
11250b57cec5SDimitry Andric   SVal MemVal = C.getSVal(DstBuffer);
11260b57cec5SDimitry Andric   SVal SizeVal = C.getSVal(Size);
11270b57cec5SDimitry Andric   const MemRegion *MR = MemVal.getAsRegion();
11280b57cec5SDimitry Andric   if (!MR)
11290b57cec5SDimitry Andric     return false;
11300b57cec5SDimitry Andric 
11310b57cec5SDimitry Andric   // We're about to model memset by producing a "default binding" in the Store.
11320b57cec5SDimitry Andric   // Our current implementation - RegionStore - doesn't support default bindings
11330b57cec5SDimitry Andric   // that don't cover the whole base region. So we should first get the offset
11340b57cec5SDimitry Andric   // and the base region to figure out whether the offset of buffer is 0.
11350b57cec5SDimitry Andric   RegionOffset Offset = MR->getAsOffset();
11360b57cec5SDimitry Andric   const MemRegion *BR = Offset.getRegion();
11370b57cec5SDimitry Andric 
1138*bdd1243dSDimitry Andric   std::optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
11390b57cec5SDimitry Andric   if (!SizeNL)
11400b57cec5SDimitry Andric     return false;
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
11430b57cec5SDimitry Andric   ASTContext &Ctx = C.getASTContext();
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric   // void *memset(void *dest, int ch, size_t count);
11460b57cec5SDimitry Andric   // For now we can only handle the case of offset is 0 and concrete char value.
11470b57cec5SDimitry Andric   if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
11480b57cec5SDimitry Andric       Offset.getOffset() == 0) {
11495ffd83dbSDimitry Andric     // Get the base region's size.
1150fe6060f1SDimitry Andric     DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder);
11510b57cec5SDimitry Andric 
11520b57cec5SDimitry Andric     ProgramStateRef StateWholeReg, StateNotWholeReg;
11530b57cec5SDimitry Andric     std::tie(StateWholeReg, StateNotWholeReg) =
11545ffd83dbSDimitry Andric         State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL));
11550b57cec5SDimitry Andric 
11560b57cec5SDimitry Andric     // With the semantic of 'memset()', we should convert the CharVal to
11570b57cec5SDimitry Andric     // unsigned char.
11580b57cec5SDimitry Andric     CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric     ProgramStateRef StateNullChar, StateNonNullChar;
11610b57cec5SDimitry Andric     std::tie(StateNullChar, StateNonNullChar) =
11620b57cec5SDimitry Andric         assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric     if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
11650b57cec5SDimitry Andric         !StateNonNullChar) {
11660b57cec5SDimitry Andric       // If the 'memset()' acts on the whole region of destination buffer and
11670b57cec5SDimitry Andric       // the value of the second argument of 'memset()' is zero, bind the second
11680b57cec5SDimitry Andric       // argument's value to the destination buffer with 'default binding'.
11690b57cec5SDimitry Andric       // FIXME: Since there is no perfect way to bind the non-zero character, we
11700b57cec5SDimitry Andric       // can only deal with zero value here. In the future, we need to deal with
11710b57cec5SDimitry Andric       // the binding of non-zero value in the case of whole region.
11720b57cec5SDimitry Andric       State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
11730b57cec5SDimitry Andric                                      C.getLocationContext());
11740b57cec5SDimitry Andric     } else {
11750b57cec5SDimitry Andric       // If the destination buffer's extent is not equal to the value of
11760b57cec5SDimitry Andric       // third argument, just invalidate buffer.
11770b57cec5SDimitry Andric       State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11780b57cec5SDimitry Andric                                /*IsSourceBuffer*/ false, Size);
11790b57cec5SDimitry Andric     }
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric     if (StateNullChar && !StateNonNullChar) {
11820b57cec5SDimitry Andric       // If the value of the second argument of 'memset()' is zero, set the
11830b57cec5SDimitry Andric       // string length of destination buffer to 0 directly.
11840b57cec5SDimitry Andric       State = setCStringLength(State, MR,
11850b57cec5SDimitry Andric                                svalBuilder.makeZeroVal(Ctx.getSizeType()));
11860b57cec5SDimitry Andric     } else if (!StateNullChar && StateNonNullChar) {
11870b57cec5SDimitry Andric       SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
11880b57cec5SDimitry Andric           CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
11890b57cec5SDimitry Andric           C.getLocationContext(), C.blockCount());
11900b57cec5SDimitry Andric 
11910b57cec5SDimitry Andric       // If the value of second argument is not zero, then the string length
11920b57cec5SDimitry Andric       // is at least the size argument.
11930b57cec5SDimitry Andric       SVal NewStrLenGESize = svalBuilder.evalBinOp(
11940b57cec5SDimitry Andric           State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric       State = setCStringLength(
11970b57cec5SDimitry Andric           State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
11980b57cec5SDimitry Andric           MR, NewStrLen);
11990b57cec5SDimitry Andric     }
12000b57cec5SDimitry Andric   } else {
12010b57cec5SDimitry Andric     // If the offset is not zero and char value is not concrete, we can do
12020b57cec5SDimitry Andric     // nothing but invalidate the buffer.
12030b57cec5SDimitry Andric     State = InvalidateBuffer(C, State, DstBuffer, MemVal,
12040b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, Size);
12050b57cec5SDimitry Andric   }
12060b57cec5SDimitry Andric   return true;
12070b57cec5SDimitry Andric }
12080b57cec5SDimitry Andric 
12090b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12100b57cec5SDimitry Andric // evaluation of individual function calls.
12110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12120b57cec5SDimitry Andric 
12135ffd83dbSDimitry Andric void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE,
12145ffd83dbSDimitry Andric                                     ProgramStateRef state, SizeArgExpr Size,
12155ffd83dbSDimitry Andric                                     DestinationArgExpr Dest,
12165ffd83dbSDimitry Andric                                     SourceArgExpr Source, bool Restricted,
1217*bdd1243dSDimitry Andric                                     bool IsMempcpy, CharKind CK) const {
12180b57cec5SDimitry Andric   CurrentFunctionDescription = "memory copy function";
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric   // See if the size argument is zero.
12210b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
12225ffd83dbSDimitry Andric   SVal sizeVal = state->getSVal(Size.Expression, LCtx);
12235ffd83dbSDimitry Andric   QualType sizeTy = Size.Expression->getType();
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
12260b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
12270b57cec5SDimitry Andric       assumeZero(C, state, sizeVal, sizeTy);
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric   // Get the value of the Dest.
12305ffd83dbSDimitry Andric   SVal destVal = state->getSVal(Dest.Expression, LCtx);
12310b57cec5SDimitry Andric 
12320b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
12330b57cec5SDimitry Andric   // just bind the return value to the destination buffer and return.
12340b57cec5SDimitry Andric   if (stateZeroSize && !stateNonZeroSize) {
12350b57cec5SDimitry Andric     stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
12360b57cec5SDimitry Andric     C.addTransition(stateZeroSize);
12370b57cec5SDimitry Andric     return;
12380b57cec5SDimitry Andric   }
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
12410b57cec5SDimitry Andric   if (stateNonZeroSize) {
12420b57cec5SDimitry Andric     state = stateNonZeroSize;
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric     // Ensure the destination is not null. If it is NULL there will be a
12450b57cec5SDimitry Andric     // NULL pointer dereference.
12465ffd83dbSDimitry Andric     state = checkNonNull(C, state, Dest, destVal);
12470b57cec5SDimitry Andric     if (!state)
12480b57cec5SDimitry Andric       return;
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric     // Get the value of the Src.
12515ffd83dbSDimitry Andric     SVal srcVal = state->getSVal(Source.Expression, LCtx);
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric     // Ensure the source is not null. If it is NULL there will be a
12540b57cec5SDimitry Andric     // NULL pointer dereference.
12555ffd83dbSDimitry Andric     state = checkNonNull(C, state, Source, srcVal);
12560b57cec5SDimitry Andric     if (!state)
12570b57cec5SDimitry Andric       return;
12580b57cec5SDimitry Andric 
12590b57cec5SDimitry Andric     // Ensure the accesses are valid and that the buffers do not overlap.
1260*bdd1243dSDimitry Andric     state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write, CK);
1261*bdd1243dSDimitry Andric     state = CheckBufferAccess(C, state, Source, Size, AccessKind::read, CK);
12625ffd83dbSDimitry Andric 
12630b57cec5SDimitry Andric     if (Restricted)
1264*bdd1243dSDimitry Andric       state = CheckOverlap(C, state, Size, Dest, Source, CK);
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric     if (!state)
12670b57cec5SDimitry Andric       return;
12680b57cec5SDimitry Andric 
12690b57cec5SDimitry Andric     // If this is mempcpy, get the byte after the last byte copied and
12700b57cec5SDimitry Andric     // bind the expr.
12710b57cec5SDimitry Andric     if (IsMempcpy) {
12720b57cec5SDimitry Andric       // Get the byte after the last byte copied.
12730b57cec5SDimitry Andric       SValBuilder &SvalBuilder = C.getSValBuilder();
12740b57cec5SDimitry Andric       ASTContext &Ctx = SvalBuilder.getContext();
1275*bdd1243dSDimitry Andric       QualType CharPtrTy = getCharPtrType(Ctx, CK);
12760b57cec5SDimitry Andric       SVal DestRegCharVal =
12775ffd83dbSDimitry Andric           SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType());
12780b57cec5SDimitry Andric       SVal lastElement = C.getSValBuilder().evalBinOp(
12795ffd83dbSDimitry Andric           state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType());
12800b57cec5SDimitry Andric       // If we don't know how much we copied, we can at least
12810b57cec5SDimitry Andric       // conjure a return value for later.
12820b57cec5SDimitry Andric       if (lastElement.isUnknown())
12830b57cec5SDimitry Andric         lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
12840b57cec5SDimitry Andric                                                           C.blockCount());
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric       // The byte after the last byte copied is the return value.
12870b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, lastElement);
12880b57cec5SDimitry Andric     } else {
12890b57cec5SDimitry Andric       // All other copies return the destination buffer.
12900b57cec5SDimitry Andric       // (Well, bcopy() has a void return type, but this won't hurt.)
12910b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, destVal);
12920b57cec5SDimitry Andric     }
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
12950b57cec5SDimitry Andric     // the address of the top-level region).
12960b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
12970b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
12980b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
12990b57cec5SDimitry Andric     // copied region, but that's still an improvement over blank invalidation.
13005ffd83dbSDimitry Andric     state =
13015ffd83dbSDimitry Andric         InvalidateBuffer(C, state, Dest.Expression, C.getSVal(Dest.Expression),
13025ffd83dbSDimitry Andric                          /*IsSourceBuffer*/ false, Size.Expression);
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
13050b57cec5SDimitry Andric     // the address of the top-level region).
13065ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, Source.Expression,
13075ffd83dbSDimitry Andric                              C.getSVal(Source.Expression),
13080b57cec5SDimitry Andric                              /*IsSourceBuffer*/ true, nullptr);
13090b57cec5SDimitry Andric 
13100b57cec5SDimitry Andric     C.addTransition(state);
13110b57cec5SDimitry Andric   }
13120b57cec5SDimitry Andric }
13130b57cec5SDimitry Andric 
1314972a253aSDimitry Andric void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE,
1315*bdd1243dSDimitry Andric                                 CharKind CK) const {
13160b57cec5SDimitry Andric   // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
13170b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
13185ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
13195ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
13205ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13210b57cec5SDimitry Andric 
13225ffd83dbSDimitry Andric   ProgramStateRef State = C.getState();
13235ffd83dbSDimitry Andric 
13245ffd83dbSDimitry Andric   constexpr bool IsRestricted = true;
13255ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
1326*bdd1243dSDimitry Andric   evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy, CK);
13270b57cec5SDimitry Andric }
13280b57cec5SDimitry Andric 
1329*bdd1243dSDimitry Andric void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE,
1330*bdd1243dSDimitry Andric                                  CharKind CK) const {
13310b57cec5SDimitry Andric   // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
13320b57cec5SDimitry Andric   // The return value is a pointer to the byte following the last written byte.
13335ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
13345ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
13355ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13360b57cec5SDimitry Andric 
13375ffd83dbSDimitry Andric   constexpr bool IsRestricted = true;
13385ffd83dbSDimitry Andric   constexpr bool IsMempcpy = true;
1339972a253aSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy,
1340*bdd1243dSDimitry Andric                  CK);
13410b57cec5SDimitry Andric }
13420b57cec5SDimitry Andric 
1343*bdd1243dSDimitry Andric void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE,
1344*bdd1243dSDimitry Andric                                  CharKind CK) const {
13450b57cec5SDimitry Andric   // void *memmove(void *dst, const void *src, size_t n);
13460b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
13475ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
13485ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
13495ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13500b57cec5SDimitry Andric 
13515ffd83dbSDimitry Andric   constexpr bool IsRestricted = false;
13525ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
1353972a253aSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy,
1354*bdd1243dSDimitry Andric                  CK);
13550b57cec5SDimitry Andric }
13560b57cec5SDimitry Andric 
13570b57cec5SDimitry Andric void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
13580b57cec5SDimitry Andric   // void bcopy(const void *src, void *dst, size_t n);
13595ffd83dbSDimitry Andric   SourceArgExpr Src(CE->getArg(0), 0);
13605ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(1), 1};
13615ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13625ffd83dbSDimitry Andric 
13635ffd83dbSDimitry Andric   constexpr bool IsRestricted = false;
13645ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
1365972a253aSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy,
1366*bdd1243dSDimitry Andric                  CharKind::Regular);
13670b57cec5SDimitry Andric }
13680b57cec5SDimitry Andric 
1369*bdd1243dSDimitry Andric void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE,
1370*bdd1243dSDimitry Andric                                 CharKind CK) const {
13710b57cec5SDimitry Andric   // int memcmp(const void *s1, const void *s2, size_t n);
13720b57cec5SDimitry Andric   CurrentFunctionDescription = "memory comparison function";
13730b57cec5SDimitry Andric 
13745ffd83dbSDimitry Andric   AnyArgExpr Left = {CE->getArg(0), 0};
13755ffd83dbSDimitry Andric   AnyArgExpr Right = {CE->getArg(1), 1};
13765ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13770b57cec5SDimitry Andric 
13785ffd83dbSDimitry Andric   ProgramStateRef State = C.getState();
13795ffd83dbSDimitry Andric   SValBuilder &Builder = C.getSValBuilder();
13805ffd83dbSDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   // See if the size argument is zero.
13835ffd83dbSDimitry Andric   SVal sizeVal = State->getSVal(Size.Expression, LCtx);
13845ffd83dbSDimitry Andric   QualType sizeTy = Size.Expression->getType();
13850b57cec5SDimitry Andric 
13860b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
13870b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
13885ffd83dbSDimitry Andric       assumeZero(C, State, sizeVal, sizeTy);
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric   // If the size can be zero, the result will be 0 in that case, and we don't
13910b57cec5SDimitry Andric   // have to check either of the buffers.
13920b57cec5SDimitry Andric   if (stateZeroSize) {
13935ffd83dbSDimitry Andric     State = stateZeroSize;
13945ffd83dbSDimitry Andric     State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
13955ffd83dbSDimitry Andric     C.addTransition(State);
13960b57cec5SDimitry Andric   }
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
13990b57cec5SDimitry Andric   if (stateNonZeroSize) {
14005ffd83dbSDimitry Andric     State = stateNonZeroSize;
14010b57cec5SDimitry Andric     // If we know the two buffers are the same, we know the result is 0.
14020b57cec5SDimitry Andric     // First, get the two buffers' addresses. Another checker will have already
14030b57cec5SDimitry Andric     // made sure they're not undefined.
14040b57cec5SDimitry Andric     DefinedOrUnknownSVal LV =
14055ffd83dbSDimitry Andric         State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
14060b57cec5SDimitry Andric     DefinedOrUnknownSVal RV =
14075ffd83dbSDimitry Andric         State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
14080b57cec5SDimitry Andric 
14090b57cec5SDimitry Andric     // See if they are the same.
14105ffd83dbSDimitry Andric     ProgramStateRef SameBuffer, NotSameBuffer;
14115ffd83dbSDimitry Andric     std::tie(SameBuffer, NotSameBuffer) =
14125ffd83dbSDimitry Andric         State->assume(Builder.evalEQ(State, LV, RV));
14130b57cec5SDimitry Andric 
1414480093f4SDimitry Andric     // If the two arguments are the same buffer, we know the result is 0,
14150b57cec5SDimitry Andric     // and we only need to check one size.
14165ffd83dbSDimitry Andric     if (SameBuffer && !NotSameBuffer) {
14175ffd83dbSDimitry Andric       State = SameBuffer;
14185ffd83dbSDimitry Andric       State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
14195ffd83dbSDimitry Andric       if (State) {
14205ffd83dbSDimitry Andric         State =
14215ffd83dbSDimitry Andric             SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
14225ffd83dbSDimitry Andric         C.addTransition(State);
14230b57cec5SDimitry Andric       }
1424480093f4SDimitry Andric       return;
14250b57cec5SDimitry Andric     }
14260b57cec5SDimitry Andric 
1427480093f4SDimitry Andric     // If the two arguments might be different buffers, we have to check
1428480093f4SDimitry Andric     // the size of both of them.
14295ffd83dbSDimitry Andric     assert(NotSameBuffer);
1430*bdd1243dSDimitry Andric     State = CheckBufferAccess(C, State, Right, Size, AccessKind::read, CK);
1431*bdd1243dSDimitry Andric     State = CheckBufferAccess(C, State, Left, Size, AccessKind::read, CK);
14325ffd83dbSDimitry Andric     if (State) {
14330b57cec5SDimitry Andric       // The return value is the comparison result, which we don't know.
14345ffd83dbSDimitry Andric       SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
14355ffd83dbSDimitry Andric       State = State->BindExpr(CE, LCtx, CmpV);
14365ffd83dbSDimitry Andric       C.addTransition(State);
14370b57cec5SDimitry Andric     }
14380b57cec5SDimitry Andric   }
14390b57cec5SDimitry Andric }
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric void CStringChecker::evalstrLength(CheckerContext &C,
14420b57cec5SDimitry Andric                                    const CallExpr *CE) const {
14430b57cec5SDimitry Andric   // size_t strlen(const char *s);
14440b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
14450b57cec5SDimitry Andric }
14460b57cec5SDimitry Andric 
14470b57cec5SDimitry Andric void CStringChecker::evalstrnLength(CheckerContext &C,
14480b57cec5SDimitry Andric                                     const CallExpr *CE) const {
14490b57cec5SDimitry Andric   // size_t strnlen(const char *s, size_t maxlen);
14500b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
14510b57cec5SDimitry Andric }
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
14540b57cec5SDimitry Andric                                          bool IsStrnlen) const {
14550b57cec5SDimitry Andric   CurrentFunctionDescription = "string length function";
14560b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
14570b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric   if (IsStrnlen) {
14600b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
14610b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric     ProgramStateRef stateZeroSize, stateNonZeroSize;
14640b57cec5SDimitry Andric     std::tie(stateZeroSize, stateNonZeroSize) =
14650b57cec5SDimitry Andric       assumeZero(C, state, maxlenVal, maxlenExpr->getType());
14660b57cec5SDimitry Andric 
14670b57cec5SDimitry Andric     // If the size can be zero, the result will be 0 in that case, and we don't
14680b57cec5SDimitry Andric     // have to check the string itself.
14690b57cec5SDimitry Andric     if (stateZeroSize) {
14700b57cec5SDimitry Andric       SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
14710b57cec5SDimitry Andric       stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
14720b57cec5SDimitry Andric       C.addTransition(stateZeroSize);
14730b57cec5SDimitry Andric     }
14740b57cec5SDimitry Andric 
14750b57cec5SDimitry Andric     // If the size is GUARANTEED to be zero, we're done!
14760b57cec5SDimitry Andric     if (!stateNonZeroSize)
14770b57cec5SDimitry Andric       return;
14780b57cec5SDimitry Andric 
14790b57cec5SDimitry Andric     // Otherwise, record the assumption that the size is nonzero.
14800b57cec5SDimitry Andric     state = stateNonZeroSize;
14810b57cec5SDimitry Andric   }
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric   // Check that the string argument is non-null.
14845ffd83dbSDimitry Andric   AnyArgExpr Arg = {CE->getArg(0), 0};
14855ffd83dbSDimitry Andric   SVal ArgVal = state->getSVal(Arg.Expression, LCtx);
14865ffd83dbSDimitry Andric   state = checkNonNull(C, state, Arg, ArgVal);
14870b57cec5SDimitry Andric 
14880b57cec5SDimitry Andric   if (!state)
14890b57cec5SDimitry Andric     return;
14900b57cec5SDimitry Andric 
14915ffd83dbSDimitry Andric   SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal);
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric   // If the argument isn't a valid C string, there's no valid state to
14940b57cec5SDimitry Andric   // transition to.
14950b57cec5SDimitry Andric   if (strLength.isUndef())
14960b57cec5SDimitry Andric     return;
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   DefinedOrUnknownSVal result = UnknownVal();
14990b57cec5SDimitry Andric 
15000b57cec5SDimitry Andric   // If the check is for strnlen() then bind the return value to no more than
15010b57cec5SDimitry Andric   // the maxlen value.
15020b57cec5SDimitry Andric   if (IsStrnlen) {
15030b57cec5SDimitry Andric     QualType cmpTy = C.getSValBuilder().getConditionType();
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric     // It's a little unfortunate to be getting this again,
15060b57cec5SDimitry Andric     // but it's not that expensive...
15070b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
15080b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
15090b57cec5SDimitry Andric 
1510*bdd1243dSDimitry Andric     std::optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1511*bdd1243dSDimitry Andric     std::optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
15120b57cec5SDimitry Andric 
15130b57cec5SDimitry Andric     if (strLengthNL && maxlenValNL) {
15140b57cec5SDimitry Andric       ProgramStateRef stateStringTooLong, stateStringNotTooLong;
15150b57cec5SDimitry Andric 
15160b57cec5SDimitry Andric       // Check if the strLength is greater than the maxlen.
15170b57cec5SDimitry Andric       std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
15180b57cec5SDimitry Andric           C.getSValBuilder()
15190b57cec5SDimitry Andric               .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
15200b57cec5SDimitry Andric               .castAs<DefinedOrUnknownSVal>());
15210b57cec5SDimitry Andric 
15220b57cec5SDimitry Andric       if (stateStringTooLong && !stateStringNotTooLong) {
15230b57cec5SDimitry Andric         // If the string is longer than maxlen, return maxlen.
15240b57cec5SDimitry Andric         result = *maxlenValNL;
15250b57cec5SDimitry Andric       } else if (stateStringNotTooLong && !stateStringTooLong) {
15260b57cec5SDimitry Andric         // If the string is shorter than maxlen, return its length.
15270b57cec5SDimitry Andric         result = *strLengthNL;
15280b57cec5SDimitry Andric       }
15290b57cec5SDimitry Andric     }
15300b57cec5SDimitry Andric 
15310b57cec5SDimitry Andric     if (result.isUnknown()) {
15320b57cec5SDimitry Andric       // If we don't have enough information for a comparison, there's
15330b57cec5SDimitry Andric       // no guarantee the full string length will actually be returned.
15340b57cec5SDimitry Andric       // All we know is the return value is the min of the string length
15350b57cec5SDimitry Andric       // and the limit. This is better than nothing.
15360b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
15370b57cec5SDimitry Andric                                                    C.blockCount());
15380b57cec5SDimitry Andric       NonLoc resultNL = result.castAs<NonLoc>();
15390b57cec5SDimitry Andric 
15400b57cec5SDimitry Andric       if (strLengthNL) {
15410b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
15420b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *strLengthNL, cmpTy)
15430b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
15440b57cec5SDimitry Andric       }
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric       if (maxlenValNL) {
15470b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
15480b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *maxlenValNL, cmpTy)
15490b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
15500b57cec5SDimitry Andric       }
15510b57cec5SDimitry Andric     }
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric   } else {
15540b57cec5SDimitry Andric     // This is a plain strlen(), not strnlen().
15550b57cec5SDimitry Andric     result = strLength.castAs<DefinedOrUnknownSVal>();
15560b57cec5SDimitry Andric 
15570b57cec5SDimitry Andric     // If we don't know the length of the string, conjure a return
15580b57cec5SDimitry Andric     // value, so it can be used in constraints, at least.
15590b57cec5SDimitry Andric     if (result.isUnknown()) {
15600b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
15610b57cec5SDimitry Andric                                                    C.blockCount());
15620b57cec5SDimitry Andric     }
15630b57cec5SDimitry Andric   }
15640b57cec5SDimitry Andric 
15650b57cec5SDimitry Andric   // Bind the return value.
15660b57cec5SDimitry Andric   assert(!result.isUnknown() && "Should have conjured a value by now");
15670b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, result);
15680b57cec5SDimitry Andric   C.addTransition(state);
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric 
15710b57cec5SDimitry Andric void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
15720b57cec5SDimitry Andric   // char *strcpy(char *restrict dst, const char *restrict src);
15730b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1574480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1575480093f4SDimitry Andric                    /* IsBounded = */ false,
1576480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15770b57cec5SDimitry Andric }
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
15800b57cec5SDimitry Andric   // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
15810b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1582480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1583480093f4SDimitry Andric                    /* IsBounded = */ true,
1584480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15850b57cec5SDimitry Andric }
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
15880b57cec5SDimitry Andric   // char *stpcpy(char *restrict dst, const char *restrict src);
15890b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1590480093f4SDimitry Andric                    /* ReturnEnd = */ true,
1591480093f4SDimitry Andric                    /* IsBounded = */ false,
1592480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15930b57cec5SDimitry Andric }
15940b57cec5SDimitry Andric 
15950b57cec5SDimitry Andric void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
1596480093f4SDimitry Andric   // size_t strlcpy(char *dest, const char *src, size_t size);
15970b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1598480093f4SDimitry Andric                    /* ReturnEnd = */ true,
1599480093f4SDimitry Andric                    /* IsBounded = */ true,
1600480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none,
16010b57cec5SDimitry Andric                    /* returnPtr = */ false);
16020b57cec5SDimitry Andric }
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
16050b57cec5SDimitry Andric   // char *strcat(char *restrict s1, const char *restrict s2);
16060b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1607480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1608480093f4SDimitry Andric                    /* IsBounded = */ false,
1609480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strcat);
16100b57cec5SDimitry Andric }
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
16130b57cec5SDimitry Andric   // char *strncat(char *restrict s1, const char *restrict s2, size_t n);
16140b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1615480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1616480093f4SDimitry Andric                    /* IsBounded = */ true,
1617480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strcat);
16180b57cec5SDimitry Andric }
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
1621480093f4SDimitry Andric   // size_t strlcat(char *dst, const char *src, size_t size);
1622480093f4SDimitry Andric   // It will append at most size - strlen(dst) - 1 bytes,
1623480093f4SDimitry Andric   // NULL-terminating the result.
16240b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1625480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1626480093f4SDimitry Andric                    /* IsBounded = */ true,
1627480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strlcat,
16280b57cec5SDimitry Andric                    /* returnPtr = */ false);
16290b57cec5SDimitry Andric }
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
1632480093f4SDimitry Andric                                       bool ReturnEnd, bool IsBounded,
1633480093f4SDimitry Andric                                       ConcatFnKind appendK,
1634480093f4SDimitry Andric                                       bool returnPtr) const {
1635480093f4SDimitry Andric   if (appendK == ConcatFnKind::none)
16360b57cec5SDimitry Andric     CurrentFunctionDescription = "string copy function";
1637480093f4SDimitry Andric   else
1638480093f4SDimitry Andric     CurrentFunctionDescription = "string concatenation function";
16395ffd83dbSDimitry Andric 
16400b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
16410b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
16420b57cec5SDimitry Andric 
16430b57cec5SDimitry Andric   // Check that the destination is non-null.
16445ffd83dbSDimitry Andric   DestinationArgExpr Dst = {CE->getArg(0), 0};
16455ffd83dbSDimitry Andric   SVal DstVal = state->getSVal(Dst.Expression, LCtx);
16465ffd83dbSDimitry Andric   state = checkNonNull(C, state, Dst, DstVal);
16470b57cec5SDimitry Andric   if (!state)
16480b57cec5SDimitry Andric     return;
16490b57cec5SDimitry Andric 
16500b57cec5SDimitry Andric   // Check that the source is non-null.
16515ffd83dbSDimitry Andric   SourceArgExpr srcExpr = {CE->getArg(1), 1};
16525ffd83dbSDimitry Andric   SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
16535ffd83dbSDimitry Andric   state = checkNonNull(C, state, srcExpr, srcVal);
16540b57cec5SDimitry Andric   if (!state)
16550b57cec5SDimitry Andric     return;
16560b57cec5SDimitry Andric 
16570b57cec5SDimitry Andric   // Get the string length of the source.
16585ffd83dbSDimitry Andric   SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal);
1659*bdd1243dSDimitry Andric   std::optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1660480093f4SDimitry Andric 
1661480093f4SDimitry Andric   // Get the string length of the destination buffer.
16625ffd83dbSDimitry Andric   SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal);
1663*bdd1243dSDimitry Andric   std::optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric   // If the source isn't a valid C string, give up.
16660b57cec5SDimitry Andric   if (strLength.isUndef())
16670b57cec5SDimitry Andric     return;
16680b57cec5SDimitry Andric 
16690b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
16700b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
16710b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric   // These two values allow checking two kinds of errors:
16740b57cec5SDimitry Andric   // - actual overflows caused by a source that doesn't fit in the destination
16750b57cec5SDimitry Andric   // - potential overflows caused by a bound that could exceed the destination
16760b57cec5SDimitry Andric   SVal amountCopied = UnknownVal();
16770b57cec5SDimitry Andric   SVal maxLastElementIndex = UnknownVal();
16780b57cec5SDimitry Andric   const char *boundWarning = nullptr;
16790b57cec5SDimitry Andric 
16805ffd83dbSDimitry Andric   // FIXME: Why do we choose the srcExpr if the access has no size?
16815ffd83dbSDimitry Andric   //  Note that the 3rd argument of the call would be the size parameter.
16825ffd83dbSDimitry Andric   SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
16835ffd83dbSDimitry Andric   state = CheckOverlap(
16845ffd83dbSDimitry Andric       C, state,
16855ffd83dbSDimitry Andric       (IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
1686480093f4SDimitry Andric       srcExpr);
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric   if (!state)
16890b57cec5SDimitry Andric     return;
16900b57cec5SDimitry Andric 
16910b57cec5SDimitry Andric   // If the function is strncpy, strncat, etc... it is bounded.
1692480093f4SDimitry Andric   if (IsBounded) {
16930b57cec5SDimitry Andric     // Get the max number of characters to copy.
16945ffd83dbSDimitry Andric     SizeArgExpr lenExpr = {CE->getArg(2), 2};
16955ffd83dbSDimitry Andric     SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric     // Protect against misdeclared strncpy().
16985ffd83dbSDimitry Andric     lenVal =
16995ffd83dbSDimitry Andric         svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType());
17000b57cec5SDimitry Andric 
1701*bdd1243dSDimitry Andric     std::optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
17020b57cec5SDimitry Andric 
17030b57cec5SDimitry Andric     // If we know both values, we might be able to figure out how much
17040b57cec5SDimitry Andric     // we're copying.
17050b57cec5SDimitry Andric     if (strLengthNL && lenValNL) {
1706480093f4SDimitry Andric       switch (appendK) {
1707480093f4SDimitry Andric       case ConcatFnKind::none:
1708480093f4SDimitry Andric       case ConcatFnKind::strcat: {
17090b57cec5SDimitry Andric         ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
17100b57cec5SDimitry Andric         // Check if the max number to copy is less than the length of the src.
17110b57cec5SDimitry Andric         // If the bound is equal to the source length, strncpy won't null-
17120b57cec5SDimitry Andric         // terminate the result!
17130b57cec5SDimitry Andric         std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
1714480093f4SDimitry Andric             svalBuilder
1715480093f4SDimitry Andric                 .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
17160b57cec5SDimitry Andric                 .castAs<DefinedOrUnknownSVal>());
17170b57cec5SDimitry Andric 
17180b57cec5SDimitry Andric         if (stateSourceTooLong && !stateSourceNotTooLong) {
1719480093f4SDimitry Andric           // Max number to copy is less than the length of the src, so the
1720480093f4SDimitry Andric           // actual strLength copied is the max number arg.
17210b57cec5SDimitry Andric           state = stateSourceTooLong;
17220b57cec5SDimitry Andric           amountCopied = lenVal;
17230b57cec5SDimitry Andric 
17240b57cec5SDimitry Andric         } else if (!stateSourceTooLong && stateSourceNotTooLong) {
17250b57cec5SDimitry Andric           // The source buffer entirely fits in the bound.
17260b57cec5SDimitry Andric           state = stateSourceNotTooLong;
17270b57cec5SDimitry Andric           amountCopied = strLength;
17280b57cec5SDimitry Andric         }
1729480093f4SDimitry Andric         break;
1730480093f4SDimitry Andric       }
1731480093f4SDimitry Andric       case ConcatFnKind::strlcat:
1732480093f4SDimitry Andric         if (!dstStrLengthNL)
1733480093f4SDimitry Andric           return;
1734480093f4SDimitry Andric 
1735480093f4SDimitry Andric         // amountCopied = min (size - dstLen - 1 , srcLen)
1736480093f4SDimitry Andric         SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1737480093f4SDimitry Andric                                                  *dstStrLengthNL, sizeTy);
173881ad6265SDimitry Andric         if (!isa<NonLoc>(freeSpace))
1739480093f4SDimitry Andric           return;
1740480093f4SDimitry Andric         freeSpace =
1741480093f4SDimitry Andric             svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
1742480093f4SDimitry Andric                                   svalBuilder.makeIntVal(1, sizeTy), sizeTy);
1743*bdd1243dSDimitry Andric         std::optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>();
1744480093f4SDimitry Andric 
1745480093f4SDimitry Andric         // While unlikely, it is possible that the subtraction is
1746480093f4SDimitry Andric         // too complex to compute, let's check whether it succeeded.
1747480093f4SDimitry Andric         if (!freeSpaceNL)
1748480093f4SDimitry Andric           return;
1749480093f4SDimitry Andric         SVal hasEnoughSpace = svalBuilder.evalBinOpNN(
1750480093f4SDimitry Andric             state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy);
1751480093f4SDimitry Andric 
1752480093f4SDimitry Andric         ProgramStateRef TrueState, FalseState;
1753480093f4SDimitry Andric         std::tie(TrueState, FalseState) =
1754480093f4SDimitry Andric             state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>());
1755480093f4SDimitry Andric 
1756480093f4SDimitry Andric         // srcStrLength <= size - dstStrLength -1
1757480093f4SDimitry Andric         if (TrueState && !FalseState) {
1758480093f4SDimitry Andric           amountCopied = strLength;
17590b57cec5SDimitry Andric         }
17600b57cec5SDimitry Andric 
1761480093f4SDimitry Andric         // srcStrLength > size - dstStrLength -1
1762480093f4SDimitry Andric         if (!TrueState && FalseState) {
1763480093f4SDimitry Andric           amountCopied = freeSpace;
1764480093f4SDimitry Andric         }
1765480093f4SDimitry Andric 
1766480093f4SDimitry Andric         if (TrueState && FalseState)
1767480093f4SDimitry Andric           amountCopied = UnknownVal();
1768480093f4SDimitry Andric         break;
1769480093f4SDimitry Andric       }
1770480093f4SDimitry Andric     }
17710b57cec5SDimitry Andric     // We still want to know if the bound is known to be too large.
17720b57cec5SDimitry Andric     if (lenValNL) {
1773480093f4SDimitry Andric       switch (appendK) {
1774480093f4SDimitry Andric       case ConcatFnKind::strcat:
17750b57cec5SDimitry Andric         // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
17760b57cec5SDimitry Andric 
17770b57cec5SDimitry Andric         // Get the string length of the destination. If the destination is
17780b57cec5SDimitry Andric         // memory that can't have a string length, we shouldn't be copying
17790b57cec5SDimitry Andric         // into it anyway.
17800b57cec5SDimitry Andric         if (dstStrLength.isUndef())
17810b57cec5SDimitry Andric           return;
17820b57cec5SDimitry Andric 
1783480093f4SDimitry Andric         if (dstStrLengthNL) {
1784480093f4SDimitry Andric           maxLastElementIndex = svalBuilder.evalBinOpNN(
1785480093f4SDimitry Andric               state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy);
1786480093f4SDimitry Andric 
17870b57cec5SDimitry Andric           boundWarning = "Size argument is greater than the free space in the "
17880b57cec5SDimitry Andric                          "destination buffer";
17890b57cec5SDimitry Andric         }
1790480093f4SDimitry Andric         break;
1791480093f4SDimitry Andric       case ConcatFnKind::none:
1792480093f4SDimitry Andric       case ConcatFnKind::strlcat:
1793480093f4SDimitry Andric         // For strncpy and strlcat, this is just checking
1794480093f4SDimitry Andric         //  that lenVal <= sizeof(dst).
17950b57cec5SDimitry Andric         // (Yes, strncpy and strncat differ in how they treat termination.
17960b57cec5SDimitry Andric         // strncat ALWAYS terminates, but strncpy doesn't.)
17970b57cec5SDimitry Andric 
17980b57cec5SDimitry Andric         // We need a special case for when the copy size is zero, in which
17990b57cec5SDimitry Andric         // case strncpy will do no work at all. Our bounds check uses n-1
18000b57cec5SDimitry Andric         // as the last element accessed, so n == 0 is problematic.
18010b57cec5SDimitry Andric         ProgramStateRef StateZeroSize, StateNonZeroSize;
18020b57cec5SDimitry Andric         std::tie(StateZeroSize, StateNonZeroSize) =
18030b57cec5SDimitry Andric             assumeZero(C, state, *lenValNL, sizeTy);
18040b57cec5SDimitry Andric 
18050b57cec5SDimitry Andric         // If the size is known to be zero, we're done.
18060b57cec5SDimitry Andric         if (StateZeroSize && !StateNonZeroSize) {
18070b57cec5SDimitry Andric           if (returnPtr) {
18080b57cec5SDimitry Andric             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
18090b57cec5SDimitry Andric           } else {
1810480093f4SDimitry Andric             if (appendK == ConcatFnKind::none) {
1811480093f4SDimitry Andric               // strlcpy returns strlen(src)
1812480093f4SDimitry Andric               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength);
1813480093f4SDimitry Andric             } else {
1814480093f4SDimitry Andric               // strlcat returns strlen(src) + strlen(dst)
1815480093f4SDimitry Andric               SVal retSize = svalBuilder.evalBinOp(
1816480093f4SDimitry Andric                   state, BO_Add, strLength, dstStrLength, sizeTy);
1817480093f4SDimitry Andric               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize);
1818480093f4SDimitry Andric             }
18190b57cec5SDimitry Andric           }
18200b57cec5SDimitry Andric           C.addTransition(StateZeroSize);
18210b57cec5SDimitry Andric           return;
18220b57cec5SDimitry Andric         }
18230b57cec5SDimitry Andric 
18240b57cec5SDimitry Andric         // Otherwise, go ahead and figure out the last element we'll touch.
18250b57cec5SDimitry Andric         // We don't record the non-zero assumption here because we can't
18260b57cec5SDimitry Andric         // be sure. We won't warn on a possible zero.
18270b57cec5SDimitry Andric         NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
1828480093f4SDimitry Andric         maxLastElementIndex =
1829480093f4SDimitry Andric             svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy);
18300b57cec5SDimitry Andric         boundWarning = "Size argument is greater than the length of the "
18310b57cec5SDimitry Andric                        "destination buffer";
1832480093f4SDimitry Andric         break;
18330b57cec5SDimitry Andric       }
18340b57cec5SDimitry Andric     }
18350b57cec5SDimitry Andric   } else {
18360b57cec5SDimitry Andric     // The function isn't bounded. The amount copied should match the length
18370b57cec5SDimitry Andric     // of the source buffer.
18380b57cec5SDimitry Andric     amountCopied = strLength;
18390b57cec5SDimitry Andric   }
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric   assert(state);
18420b57cec5SDimitry Andric 
18430b57cec5SDimitry Andric   // This represents the number of characters copied into the destination
18440b57cec5SDimitry Andric   // buffer. (It may not actually be the strlen if the destination buffer
18450b57cec5SDimitry Andric   // is not terminated.)
18460b57cec5SDimitry Andric   SVal finalStrLength = UnknownVal();
1847480093f4SDimitry Andric   SVal strlRetVal = UnknownVal();
1848480093f4SDimitry Andric 
1849480093f4SDimitry Andric   if (appendK == ConcatFnKind::none && !returnPtr) {
1850480093f4SDimitry Andric     // strlcpy returns the sizeof(src)
1851480093f4SDimitry Andric     strlRetVal = strLength;
1852480093f4SDimitry Andric   }
18530b57cec5SDimitry Andric 
18540b57cec5SDimitry Andric   // If this is an appending function (strcat, strncat...) then set the
18550b57cec5SDimitry Andric   // string length to strlen(src) + strlen(dst) since the buffer will
18560b57cec5SDimitry Andric   // ultimately contain both.
1857480093f4SDimitry Andric   if (appendK != ConcatFnKind::none) {
18580b57cec5SDimitry Andric     // Get the string length of the destination. If the destination is memory
18590b57cec5SDimitry Andric     // that can't have a string length, we shouldn't be copying into it anyway.
18600b57cec5SDimitry Andric     if (dstStrLength.isUndef())
18610b57cec5SDimitry Andric       return;
18620b57cec5SDimitry Andric 
1863480093f4SDimitry Andric     if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) {
1864480093f4SDimitry Andric       strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL,
1865480093f4SDimitry Andric                                            *dstStrLengthNL, sizeTy);
1866480093f4SDimitry Andric     }
1867480093f4SDimitry Andric 
1868*bdd1243dSDimitry Andric     std::optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>();
18690b57cec5SDimitry Andric 
18700b57cec5SDimitry Andric     // If we know both string lengths, we might know the final string length.
1871480093f4SDimitry Andric     if (amountCopiedNL && dstStrLengthNL) {
18720b57cec5SDimitry Andric       // Make sure the two lengths together don't overflow a size_t.
1873480093f4SDimitry Andric       state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL);
18740b57cec5SDimitry Andric       if (!state)
18750b57cec5SDimitry Andric         return;
18760b57cec5SDimitry Andric 
1877480093f4SDimitry Andric       finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL,
18780b57cec5SDimitry Andric                                                *dstStrLengthNL, sizeTy);
18790b57cec5SDimitry Andric     }
18800b57cec5SDimitry Andric 
18810b57cec5SDimitry Andric     // If we couldn't get a single value for the final string length,
18820b57cec5SDimitry Andric     // we can at least bound it by the individual lengths.
18830b57cec5SDimitry Andric     if (finalStrLength.isUnknown()) {
18840b57cec5SDimitry Andric       // Try to get a "hypothetical" string length symbol, which we can later
18850b57cec5SDimitry Andric       // set as a real value if that turns out to be the case.
18860b57cec5SDimitry Andric       finalStrLength = getCStringLength(C, state, CE, DstVal, true);
18870b57cec5SDimitry Andric       assert(!finalStrLength.isUndef());
18880b57cec5SDimitry Andric 
1889*bdd1243dSDimitry Andric       if (std::optional<NonLoc> finalStrLengthNL =
1890*bdd1243dSDimitry Andric               finalStrLength.getAs<NonLoc>()) {
1891480093f4SDimitry Andric         if (amountCopiedNL && appendK == ConcatFnKind::none) {
1892480093f4SDimitry Andric           // we overwrite dst string with the src
18930b57cec5SDimitry Andric           // finalStrLength >= srcStrLength
1894480093f4SDimitry Andric           SVal sourceInResult = svalBuilder.evalBinOpNN(
1895480093f4SDimitry Andric               state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy);
18960b57cec5SDimitry Andric           state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
18970b57cec5SDimitry Andric                                 true);
18980b57cec5SDimitry Andric           if (!state)
18990b57cec5SDimitry Andric             return;
19000b57cec5SDimitry Andric         }
19010b57cec5SDimitry Andric 
1902480093f4SDimitry Andric         if (dstStrLengthNL && appendK != ConcatFnKind::none) {
1903480093f4SDimitry Andric           // we extend the dst string with the src
19040b57cec5SDimitry Andric           // finalStrLength >= dstStrLength
19050b57cec5SDimitry Andric           SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
19060b57cec5SDimitry Andric                                                       *finalStrLengthNL,
19070b57cec5SDimitry Andric                                                       *dstStrLengthNL,
19080b57cec5SDimitry Andric                                                       cmpTy);
19090b57cec5SDimitry Andric           state =
19100b57cec5SDimitry Andric               state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
19110b57cec5SDimitry Andric           if (!state)
19120b57cec5SDimitry Andric             return;
19130b57cec5SDimitry Andric         }
19140b57cec5SDimitry Andric       }
19150b57cec5SDimitry Andric     }
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric   } else {
19180b57cec5SDimitry Andric     // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
19190b57cec5SDimitry Andric     // the final string length will match the input string length.
19200b57cec5SDimitry Andric     finalStrLength = amountCopied;
19210b57cec5SDimitry Andric   }
19220b57cec5SDimitry Andric 
19230b57cec5SDimitry Andric   SVal Result;
19240b57cec5SDimitry Andric 
19250b57cec5SDimitry Andric   if (returnPtr) {
19260b57cec5SDimitry Andric     // The final result of the function will either be a pointer past the last
19270b57cec5SDimitry Andric     // copied element, or a pointer to the start of the destination buffer.
1928480093f4SDimitry Andric     Result = (ReturnEnd ? UnknownVal() : DstVal);
19290b57cec5SDimitry Andric   } else {
1930480093f4SDimitry Andric     if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none)
1931480093f4SDimitry Andric       //strlcpy, strlcat
1932480093f4SDimitry Andric       Result = strlRetVal;
1933480093f4SDimitry Andric     else
19340b57cec5SDimitry Andric       Result = finalStrLength;
19350b57cec5SDimitry Andric   }
19360b57cec5SDimitry Andric 
19370b57cec5SDimitry Andric   assert(state);
19380b57cec5SDimitry Andric 
19390b57cec5SDimitry Andric   // If the destination is a MemRegion, try to check for a buffer overflow and
19400b57cec5SDimitry Andric   // record the new string length.
1941*bdd1243dSDimitry Andric   if (std::optional<loc::MemRegionVal> dstRegVal =
19420b57cec5SDimitry Andric           DstVal.getAs<loc::MemRegionVal>()) {
19435ffd83dbSDimitry Andric     QualType ptrTy = Dst.Expression->getType();
19440b57cec5SDimitry Andric 
19450b57cec5SDimitry Andric     // If we have an exact value on a bounded copy, use that to check for
19460b57cec5SDimitry Andric     // overflows, rather than our estimate about how much is actually copied.
1947*bdd1243dSDimitry Andric     if (std::optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
19485ffd83dbSDimitry Andric       SVal maxLastElement =
19495ffd83dbSDimitry Andric           svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy);
19505ffd83dbSDimitry Andric 
19515ffd83dbSDimitry Andric       state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write);
19520b57cec5SDimitry Andric       if (!state)
19530b57cec5SDimitry Andric         return;
19540b57cec5SDimitry Andric     }
19550b57cec5SDimitry Andric 
19560b57cec5SDimitry Andric     // Then, if the final length is known...
1957*bdd1243dSDimitry Andric     if (std::optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
19580b57cec5SDimitry Andric       SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
19590b57cec5SDimitry Andric           *knownStrLength, ptrTy);
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric       // ...and we haven't checked the bound, we'll check the actual copy.
19620b57cec5SDimitry Andric       if (!boundWarning) {
19635ffd83dbSDimitry Andric         state = CheckLocation(C, state, Dst, lastElement, AccessKind::write);
19640b57cec5SDimitry Andric         if (!state)
19650b57cec5SDimitry Andric           return;
19660b57cec5SDimitry Andric       }
19670b57cec5SDimitry Andric 
19680b57cec5SDimitry Andric       // If this is a stpcpy-style copy, the last element is the return value.
1969480093f4SDimitry Andric       if (returnPtr && ReturnEnd)
19700b57cec5SDimitry Andric         Result = lastElement;
19710b57cec5SDimitry Andric     }
19720b57cec5SDimitry Andric 
19730b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
19740b57cec5SDimitry Andric     // the address of the top-level region). This must happen before we set the
19750b57cec5SDimitry Andric     // C string length because invalidation will clear the length.
19760b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
19770b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
19780b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
19790b57cec5SDimitry Andric     // string, but that's still an improvement over blank invalidation.
19805ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, Dst.Expression, *dstRegVal,
19810b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, nullptr);
19820b57cec5SDimitry Andric 
19830b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
19840b57cec5SDimitry Andric     // the address of the top-level region).
19855ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, srcExpr.Expression, srcVal,
19865ffd83dbSDimitry Andric                              /*IsSourceBuffer*/ true, nullptr);
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric     // Set the C string length of the destination, if we know it.
1989480093f4SDimitry Andric     if (IsBounded && (appendK == ConcatFnKind::none)) {
19900b57cec5SDimitry Andric       // strncpy is annoying in that it doesn't guarantee to null-terminate
19910b57cec5SDimitry Andric       // the result string. If the original string didn't fit entirely inside
19920b57cec5SDimitry Andric       // the bound (including the null-terminator), we don't know how long the
19930b57cec5SDimitry Andric       // result is.
19940b57cec5SDimitry Andric       if (amountCopied != strLength)
19950b57cec5SDimitry Andric         finalStrLength = UnknownVal();
19960b57cec5SDimitry Andric     }
19970b57cec5SDimitry Andric     state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
19980b57cec5SDimitry Andric   }
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric   assert(state);
20010b57cec5SDimitry Andric 
20020b57cec5SDimitry Andric   if (returnPtr) {
20030b57cec5SDimitry Andric     // If this is a stpcpy-style copy, but we were unable to check for a buffer
20040b57cec5SDimitry Andric     // overflow, we still need a result. Conjure a return value.
2005480093f4SDimitry Andric     if (ReturnEnd && Result.isUnknown()) {
20060b57cec5SDimitry Andric       Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
20070b57cec5SDimitry Andric     }
20080b57cec5SDimitry Andric   }
20090b57cec5SDimitry Andric   // Set the return value.
20100b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, Result);
20110b57cec5SDimitry Andric   C.addTransition(state);
20120b57cec5SDimitry Andric }
20130b57cec5SDimitry Andric 
20140b57cec5SDimitry Andric void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
20150b57cec5SDimitry Andric   //int strcmp(const char *s1, const char *s2);
2016480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false);
20170b57cec5SDimitry Andric }
20180b57cec5SDimitry Andric 
20190b57cec5SDimitry Andric void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
20200b57cec5SDimitry Andric   //int strncmp(const char *s1, const char *s2, size_t n);
2021480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false);
20220b57cec5SDimitry Andric }
20230b57cec5SDimitry Andric 
20240b57cec5SDimitry Andric void CStringChecker::evalStrcasecmp(CheckerContext &C,
20250b57cec5SDimitry Andric     const CallExpr *CE) const {
20260b57cec5SDimitry Andric   //int strcasecmp(const char *s1, const char *s2);
2027480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true);
20280b57cec5SDimitry Andric }
20290b57cec5SDimitry Andric 
20300b57cec5SDimitry Andric void CStringChecker::evalStrncasecmp(CheckerContext &C,
20310b57cec5SDimitry Andric     const CallExpr *CE) const {
20320b57cec5SDimitry Andric   //int strncasecmp(const char *s1, const char *s2, size_t n);
2033480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true);
20340b57cec5SDimitry Andric }
20350b57cec5SDimitry Andric 
20360b57cec5SDimitry Andric void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
2037480093f4SDimitry Andric     bool IsBounded, bool IgnoreCase) const {
20380b57cec5SDimitry Andric   CurrentFunctionDescription = "string comparison function";
20390b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
20400b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric   // Check that the first string is non-null
20435ffd83dbSDimitry Andric   AnyArgExpr Left = {CE->getArg(0), 0};
20445ffd83dbSDimitry Andric   SVal LeftVal = state->getSVal(Left.Expression, LCtx);
20455ffd83dbSDimitry Andric   state = checkNonNull(C, state, Left, LeftVal);
20460b57cec5SDimitry Andric   if (!state)
20470b57cec5SDimitry Andric     return;
20480b57cec5SDimitry Andric 
20490b57cec5SDimitry Andric   // Check that the second string is non-null.
20505ffd83dbSDimitry Andric   AnyArgExpr Right = {CE->getArg(1), 1};
20515ffd83dbSDimitry Andric   SVal RightVal = state->getSVal(Right.Expression, LCtx);
20525ffd83dbSDimitry Andric   state = checkNonNull(C, state, Right, RightVal);
20530b57cec5SDimitry Andric   if (!state)
20540b57cec5SDimitry Andric     return;
20550b57cec5SDimitry Andric 
20560b57cec5SDimitry Andric   // Get the string length of the first string or give up.
20575ffd83dbSDimitry Andric   SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal);
20585ffd83dbSDimitry Andric   if (LeftLength.isUndef())
20590b57cec5SDimitry Andric     return;
20600b57cec5SDimitry Andric 
20610b57cec5SDimitry Andric   // Get the string length of the second string or give up.
20625ffd83dbSDimitry Andric   SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal);
20635ffd83dbSDimitry Andric   if (RightLength.isUndef())
20640b57cec5SDimitry Andric     return;
20650b57cec5SDimitry Andric 
20660b57cec5SDimitry Andric   // If we know the two buffers are the same, we know the result is 0.
20670b57cec5SDimitry Andric   // First, get the two buffers' addresses. Another checker will have already
20680b57cec5SDimitry Andric   // made sure they're not undefined.
20695ffd83dbSDimitry Andric   DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>();
20705ffd83dbSDimitry Andric   DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>();
20710b57cec5SDimitry Andric 
20720b57cec5SDimitry Andric   // See if they are the same.
20730b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
20740b57cec5SDimitry Andric   DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
20750b57cec5SDimitry Andric   ProgramStateRef StSameBuf, StNotSameBuf;
20760b57cec5SDimitry Andric   std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
20770b57cec5SDimitry Andric 
20780b57cec5SDimitry Andric   // If the two arguments might be the same buffer, we know the result is 0,
20790b57cec5SDimitry Andric   // and we only need to check one size.
20800b57cec5SDimitry Andric   if (StSameBuf) {
20810b57cec5SDimitry Andric     StSameBuf = StSameBuf->BindExpr(CE, LCtx,
20820b57cec5SDimitry Andric         svalBuilder.makeZeroVal(CE->getType()));
20830b57cec5SDimitry Andric     C.addTransition(StSameBuf);
20840b57cec5SDimitry Andric 
20850b57cec5SDimitry Andric     // If the two arguments are GUARANTEED to be the same, we're done!
20860b57cec5SDimitry Andric     if (!StNotSameBuf)
20870b57cec5SDimitry Andric       return;
20880b57cec5SDimitry Andric   }
20890b57cec5SDimitry Andric 
20900b57cec5SDimitry Andric   assert(StNotSameBuf);
20910b57cec5SDimitry Andric   state = StNotSameBuf;
20920b57cec5SDimitry Andric 
20930b57cec5SDimitry Andric   // At this point we can go about comparing the two buffers.
20940b57cec5SDimitry Andric   // For now, we only do this if they're both known string literals.
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric   // Attempt to extract string literals from both expressions.
20975ffd83dbSDimitry Andric   const StringLiteral *LeftStrLiteral =
20985ffd83dbSDimitry Andric       getCStringLiteral(C, state, Left.Expression, LeftVal);
20995ffd83dbSDimitry Andric   const StringLiteral *RightStrLiteral =
21005ffd83dbSDimitry Andric       getCStringLiteral(C, state, Right.Expression, RightVal);
21010b57cec5SDimitry Andric   bool canComputeResult = false;
21020b57cec5SDimitry Andric   SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
21030b57cec5SDimitry Andric       C.blockCount());
21040b57cec5SDimitry Andric 
21055ffd83dbSDimitry Andric   if (LeftStrLiteral && RightStrLiteral) {
21065ffd83dbSDimitry Andric     StringRef LeftStrRef = LeftStrLiteral->getString();
21075ffd83dbSDimitry Andric     StringRef RightStrRef = RightStrLiteral->getString();
21080b57cec5SDimitry Andric 
2109480093f4SDimitry Andric     if (IsBounded) {
21100b57cec5SDimitry Andric       // Get the max number of characters to compare.
21110b57cec5SDimitry Andric       const Expr *lenExpr = CE->getArg(2);
21120b57cec5SDimitry Andric       SVal lenVal = state->getSVal(lenExpr, LCtx);
21130b57cec5SDimitry Andric 
21140b57cec5SDimitry Andric       // If the length is known, we can get the right substrings.
21150b57cec5SDimitry Andric       if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
21160b57cec5SDimitry Andric         // Create substrings of each to compare the prefix.
21175ffd83dbSDimitry Andric         LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue());
21185ffd83dbSDimitry Andric         RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue());
21190b57cec5SDimitry Andric         canComputeResult = true;
21200b57cec5SDimitry Andric       }
21210b57cec5SDimitry Andric     } else {
21220b57cec5SDimitry Andric       // This is a normal, unbounded strcmp.
21230b57cec5SDimitry Andric       canComputeResult = true;
21240b57cec5SDimitry Andric     }
21250b57cec5SDimitry Andric 
21260b57cec5SDimitry Andric     if (canComputeResult) {
21270b57cec5SDimitry Andric       // Real strcmp stops at null characters.
21285ffd83dbSDimitry Andric       size_t s1Term = LeftStrRef.find('\0');
21290b57cec5SDimitry Andric       if (s1Term != StringRef::npos)
21305ffd83dbSDimitry Andric         LeftStrRef = LeftStrRef.substr(0, s1Term);
21310b57cec5SDimitry Andric 
21325ffd83dbSDimitry Andric       size_t s2Term = RightStrRef.find('\0');
21330b57cec5SDimitry Andric       if (s2Term != StringRef::npos)
21345ffd83dbSDimitry Andric         RightStrRef = RightStrRef.substr(0, s2Term);
21350b57cec5SDimitry Andric 
21360b57cec5SDimitry Andric       // Use StringRef's comparison methods to compute the actual result.
2137fe6060f1SDimitry Andric       int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef)
21385ffd83dbSDimitry Andric                                   : LeftStrRef.compare(RightStrRef);
21390b57cec5SDimitry Andric 
21400b57cec5SDimitry Andric       // The strcmp function returns an integer greater than, equal to, or less
21410b57cec5SDimitry Andric       // than zero, [c11, p7.24.4.2].
21420b57cec5SDimitry Andric       if (compareRes == 0) {
21430b57cec5SDimitry Andric         resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
21440b57cec5SDimitry Andric       }
21450b57cec5SDimitry Andric       else {
21460b57cec5SDimitry Andric         DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
21470b57cec5SDimitry Andric         // Constrain strcmp's result range based on the result of StringRef's
21480b57cec5SDimitry Andric         // comparison methods.
2149*bdd1243dSDimitry Andric         BinaryOperatorKind op = (compareRes > 0) ? BO_GT : BO_LT;
21500b57cec5SDimitry Andric         SVal compareWithZero =
21510b57cec5SDimitry Andric           svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
21520b57cec5SDimitry Andric               svalBuilder.getConditionType());
21530b57cec5SDimitry Andric         DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
21540b57cec5SDimitry Andric         state = state->assume(compareWithZeroVal, true);
21550b57cec5SDimitry Andric       }
21560b57cec5SDimitry Andric     }
21570b57cec5SDimitry Andric   }
21580b57cec5SDimitry Andric 
21590b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, resultVal);
21600b57cec5SDimitry Andric 
21610b57cec5SDimitry Andric   // Record this as a possible path.
21620b57cec5SDimitry Andric   C.addTransition(state);
21630b57cec5SDimitry Andric }
21640b57cec5SDimitry Andric 
21650b57cec5SDimitry Andric void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
21660b57cec5SDimitry Andric   // char *strsep(char **stringp, const char *delim);
21675e801ac6SDimitry Andric   // Verify whether the search string parameter matches the return type.
21685ffd83dbSDimitry Andric   SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
21695ffd83dbSDimitry Andric 
21705ffd83dbSDimitry Andric   QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
21710b57cec5SDimitry Andric   if (CharPtrTy.isNull() ||
21720b57cec5SDimitry Andric       CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
21730b57cec5SDimitry Andric     return;
21740b57cec5SDimitry Andric 
21750b57cec5SDimitry Andric   CurrentFunctionDescription = "strsep()";
21760b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21770b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
21780b57cec5SDimitry Andric 
21790b57cec5SDimitry Andric   // Check that the search string pointer is non-null (though it may point to
21800b57cec5SDimitry Andric   // a null string).
21815ffd83dbSDimitry Andric   SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx);
21825ffd83dbSDimitry Andric   State = checkNonNull(C, State, SearchStrPtr, SearchStrVal);
21830b57cec5SDimitry Andric   if (!State)
21840b57cec5SDimitry Andric     return;
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric   // Check that the delimiter string is non-null.
21875ffd83dbSDimitry Andric   AnyArgExpr DelimStr = {CE->getArg(1), 1};
21885ffd83dbSDimitry Andric   SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx);
21895ffd83dbSDimitry Andric   State = checkNonNull(C, State, DelimStr, DelimStrVal);
21900b57cec5SDimitry Andric   if (!State)
21910b57cec5SDimitry Andric     return;
21920b57cec5SDimitry Andric 
21930b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
21940b57cec5SDimitry Andric   SVal Result;
2195*bdd1243dSDimitry Andric   if (std::optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
21960b57cec5SDimitry Andric     // Get the current value of the search string pointer, as a char*.
21970b57cec5SDimitry Andric     Result = State->getSVal(*SearchStrLoc, CharPtrTy);
21980b57cec5SDimitry Andric 
21990b57cec5SDimitry Andric     // Invalidate the search string, representing the change of one delimiter
22000b57cec5SDimitry Andric     // character to NUL.
22015ffd83dbSDimitry Andric     State = InvalidateBuffer(C, State, SearchStrPtr.Expression, Result,
22020b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, nullptr);
22030b57cec5SDimitry Andric 
22040b57cec5SDimitry Andric     // Overwrite the search string pointer. The new value is either an address
22050b57cec5SDimitry Andric     // further along in the same string, or NULL if there are no more tokens.
22060b57cec5SDimitry Andric     State = State->bindLoc(*SearchStrLoc,
22070b57cec5SDimitry Andric         SVB.conjureSymbolVal(getTag(),
22080b57cec5SDimitry Andric           CE,
22090b57cec5SDimitry Andric           LCtx,
22100b57cec5SDimitry Andric           CharPtrTy,
22110b57cec5SDimitry Andric           C.blockCount()),
22120b57cec5SDimitry Andric         LCtx);
22130b57cec5SDimitry Andric   } else {
22140b57cec5SDimitry Andric     assert(SearchStrVal.isUnknown());
22150b57cec5SDimitry Andric     // Conjure a symbolic value. It's the best we can do.
22160b57cec5SDimitry Andric     Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
22170b57cec5SDimitry Andric   }
22180b57cec5SDimitry Andric 
22190b57cec5SDimitry Andric   // Set the return value, and finish.
22200b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, Result);
22210b57cec5SDimitry Andric   C.addTransition(State);
22220b57cec5SDimitry Andric }
22230b57cec5SDimitry Andric 
22240b57cec5SDimitry Andric // These should probably be moved into a C++ standard library checker.
22250b57cec5SDimitry Andric void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
22260b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
22270b57cec5SDimitry Andric }
22280b57cec5SDimitry Andric 
22290b57cec5SDimitry Andric void CStringChecker::evalStdCopyBackward(CheckerContext &C,
22300b57cec5SDimitry Andric     const CallExpr *CE) const {
22310b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
22320b57cec5SDimitry Andric }
22330b57cec5SDimitry Andric 
22340b57cec5SDimitry Andric void CStringChecker::evalStdCopyCommon(CheckerContext &C,
22350b57cec5SDimitry Andric     const CallExpr *CE) const {
22360b57cec5SDimitry Andric   if (!CE->getArg(2)->getType()->isPointerType())
22370b57cec5SDimitry Andric     return;
22380b57cec5SDimitry Andric 
22390b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22400b57cec5SDimitry Andric 
22410b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
22420b57cec5SDimitry Andric 
22430b57cec5SDimitry Andric   // template <class _InputIterator, class _OutputIterator>
22440b57cec5SDimitry Andric   // _OutputIterator
22450b57cec5SDimitry Andric   // copy(_InputIterator __first, _InputIterator __last,
22460b57cec5SDimitry Andric   //        _OutputIterator __result)
22470b57cec5SDimitry Andric 
22480b57cec5SDimitry Andric   // Invalidate the destination buffer
22490b57cec5SDimitry Andric   const Expr *Dst = CE->getArg(2);
22500b57cec5SDimitry Andric   SVal DstVal = State->getSVal(Dst, LCtx);
22510b57cec5SDimitry Andric   State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
22520b57cec5SDimitry Andric       /*Size=*/nullptr);
22530b57cec5SDimitry Andric 
22540b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
22550b57cec5SDimitry Andric 
22560b57cec5SDimitry Andric   SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
22570b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, ResultVal);
22580b57cec5SDimitry Andric 
22590b57cec5SDimitry Andric   C.addTransition(State);
22600b57cec5SDimitry Andric }
22610b57cec5SDimitry Andric 
22620b57cec5SDimitry Andric void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
22635ffd83dbSDimitry Andric   // void *memset(void *s, int c, size_t n);
22640b57cec5SDimitry Andric   CurrentFunctionDescription = "memory set function";
22650b57cec5SDimitry Andric 
22665ffd83dbSDimitry Andric   DestinationArgExpr Buffer = {CE->getArg(0), 0};
22675ffd83dbSDimitry Andric   AnyArgExpr CharE = {CE->getArg(1), 1};
22685ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
22695ffd83dbSDimitry Andric 
22700b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22710b57cec5SDimitry Andric 
22720b57cec5SDimitry Andric   // See if the size argument is zero.
22730b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
22745ffd83dbSDimitry Andric   SVal SizeVal = C.getSVal(Size.Expression);
22755ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
22760b57cec5SDimitry Andric 
22775ffd83dbSDimitry Andric   ProgramStateRef ZeroSize, NonZeroSize;
22785ffd83dbSDimitry Andric   std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy);
22790b57cec5SDimitry Andric 
22800b57cec5SDimitry Andric   // Get the value of the memory area.
22815ffd83dbSDimitry Andric   SVal BufferPtrVal = C.getSVal(Buffer.Expression);
22820b57cec5SDimitry Andric 
22830b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
22845ffd83dbSDimitry Andric   // just bind the return value to the buffer and return.
22855ffd83dbSDimitry Andric   if (ZeroSize && !NonZeroSize) {
22865ffd83dbSDimitry Andric     ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal);
22875ffd83dbSDimitry Andric     C.addTransition(ZeroSize);
22880b57cec5SDimitry Andric     return;
22890b57cec5SDimitry Andric   }
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric   // Ensure the memory area is not null.
22920b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
22935ffd83dbSDimitry Andric   State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal);
22940b57cec5SDimitry Andric   if (!State)
22950b57cec5SDimitry Andric     return;
22960b57cec5SDimitry Andric 
22975ffd83dbSDimitry Andric   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
22980b57cec5SDimitry Andric   if (!State)
22990b57cec5SDimitry Andric     return;
23000b57cec5SDimitry Andric 
23010b57cec5SDimitry Andric   // According to the values of the arguments, bind the value of the second
23020b57cec5SDimitry Andric   // argument to the destination buffer and set string length, or just
23030b57cec5SDimitry Andric   // invalidate the destination buffer.
23045ffd83dbSDimitry Andric   if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression),
23055ffd83dbSDimitry Andric                  Size.Expression, C, State))
23060b57cec5SDimitry Andric     return;
23070b57cec5SDimitry Andric 
23085ffd83dbSDimitry Andric   State = State->BindExpr(CE, LCtx, BufferPtrVal);
23090b57cec5SDimitry Andric   C.addTransition(State);
23100b57cec5SDimitry Andric }
23110b57cec5SDimitry Andric 
23120b57cec5SDimitry Andric void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
23130b57cec5SDimitry Andric   CurrentFunctionDescription = "memory clearance function";
23140b57cec5SDimitry Andric 
23155ffd83dbSDimitry Andric   DestinationArgExpr Buffer = {CE->getArg(0), 0};
23165ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(1), 1};
23170b57cec5SDimitry Andric   SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
23180b57cec5SDimitry Andric 
23190b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
23200b57cec5SDimitry Andric 
23210b57cec5SDimitry Andric   // See if the size argument is zero.
23225ffd83dbSDimitry Andric   SVal SizeVal = C.getSVal(Size.Expression);
23235ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
23240b57cec5SDimitry Andric 
23250b57cec5SDimitry Andric   ProgramStateRef StateZeroSize, StateNonZeroSize;
23260b57cec5SDimitry Andric   std::tie(StateZeroSize, StateNonZeroSize) =
23270b57cec5SDimitry Andric     assumeZero(C, State, SizeVal, SizeTy);
23280b57cec5SDimitry Andric 
23290b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access,
23300b57cec5SDimitry Andric   // In this case we just return.
23310b57cec5SDimitry Andric   if (StateZeroSize && !StateNonZeroSize) {
23320b57cec5SDimitry Andric     C.addTransition(StateZeroSize);
23330b57cec5SDimitry Andric     return;
23340b57cec5SDimitry Andric   }
23350b57cec5SDimitry Andric 
23360b57cec5SDimitry Andric   // Get the value of the memory area.
23375ffd83dbSDimitry Andric   SVal MemVal = C.getSVal(Buffer.Expression);
23380b57cec5SDimitry Andric 
23390b57cec5SDimitry Andric   // Ensure the memory area is not null.
23400b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
23415ffd83dbSDimitry Andric   State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal);
23420b57cec5SDimitry Andric   if (!State)
23430b57cec5SDimitry Andric     return;
23440b57cec5SDimitry Andric 
23455ffd83dbSDimitry Andric   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
23460b57cec5SDimitry Andric   if (!State)
23470b57cec5SDimitry Andric     return;
23480b57cec5SDimitry Andric 
23495ffd83dbSDimitry Andric   if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State))
23500b57cec5SDimitry Andric     return;
23510b57cec5SDimitry Andric 
23520b57cec5SDimitry Andric   C.addTransition(State);
23530b57cec5SDimitry Andric }
23540b57cec5SDimitry Andric 
23550b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
23560b57cec5SDimitry Andric // The driver method, and other Checker callbacks.
23570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
23600b57cec5SDimitry Andric                                                      CheckerContext &C) const {
23610b57cec5SDimitry Andric   const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
23620b57cec5SDimitry Andric   if (!CE)
23630b57cec5SDimitry Andric     return nullptr;
23640b57cec5SDimitry Andric 
23650b57cec5SDimitry Andric   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
23660b57cec5SDimitry Andric   if (!FD)
23670b57cec5SDimitry Andric     return nullptr;
23680b57cec5SDimitry Andric 
2369349cc55cSDimitry Andric   if (StdCopy.matches(Call))
23700b57cec5SDimitry Andric     return &CStringChecker::evalStdCopy;
2371349cc55cSDimitry Andric   if (StdCopyBackward.matches(Call))
23720b57cec5SDimitry Andric     return &CStringChecker::evalStdCopyBackward;
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric   // Pro-actively check that argument types are safe to do arithmetic upon.
23750b57cec5SDimitry Andric   // We do not want to crash if someone accidentally passes a structure
23760b57cec5SDimitry Andric   // into, say, a C++ overload of any of these functions. We could not check
23770b57cec5SDimitry Andric   // that for std::copy because they may have arguments of other types.
23780b57cec5SDimitry Andric   for (auto I : CE->arguments()) {
23790b57cec5SDimitry Andric     QualType T = I->getType();
23800b57cec5SDimitry Andric     if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
23810b57cec5SDimitry Andric       return nullptr;
23820b57cec5SDimitry Andric   }
23830b57cec5SDimitry Andric 
23840b57cec5SDimitry Andric   const FnCheck *Callback = Callbacks.lookup(Call);
23850b57cec5SDimitry Andric   if (Callback)
23860b57cec5SDimitry Andric     return *Callback;
23870b57cec5SDimitry Andric 
23880b57cec5SDimitry Andric   return nullptr;
23890b57cec5SDimitry Andric }
23900b57cec5SDimitry Andric 
23910b57cec5SDimitry Andric bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
23920b57cec5SDimitry Andric   FnCheck Callback = identifyCall(Call, C);
23930b57cec5SDimitry Andric 
23940b57cec5SDimitry Andric   // If the callee isn't a string function, let another checker handle it.
23950b57cec5SDimitry Andric   if (!Callback)
23960b57cec5SDimitry Andric     return false;
23970b57cec5SDimitry Andric 
23980b57cec5SDimitry Andric   // Check and evaluate the call.
23990b57cec5SDimitry Andric   const auto *CE = cast<CallExpr>(Call.getOriginExpr());
2400972a253aSDimitry Andric   Callback(this, C, CE);
24010b57cec5SDimitry Andric 
24020b57cec5SDimitry Andric   // If the evaluate call resulted in no change, chain to the next eval call
24030b57cec5SDimitry Andric   // handler.
24040b57cec5SDimitry Andric   // Note, the custom CString evaluation calls assume that basic safety
24050b57cec5SDimitry Andric   // properties are held. However, if the user chooses to turn off some of these
24060b57cec5SDimitry Andric   // checks, we ignore the issues and leave the call evaluation to a generic
24070b57cec5SDimitry Andric   // handler.
24080b57cec5SDimitry Andric   return C.isDifferent();
24090b57cec5SDimitry Andric }
24100b57cec5SDimitry Andric 
24110b57cec5SDimitry Andric void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
24120b57cec5SDimitry Andric   // Record string length for char a[] = "abc";
24130b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
24140b57cec5SDimitry Andric 
24150b57cec5SDimitry Andric   for (const auto *I : DS->decls()) {
24160b57cec5SDimitry Andric     const VarDecl *D = dyn_cast<VarDecl>(I);
24170b57cec5SDimitry Andric     if (!D)
24180b57cec5SDimitry Andric       continue;
24190b57cec5SDimitry Andric 
24200b57cec5SDimitry Andric     // FIXME: Handle array fields of structs.
24210b57cec5SDimitry Andric     if (!D->getType()->isArrayType())
24220b57cec5SDimitry Andric       continue;
24230b57cec5SDimitry Andric 
24240b57cec5SDimitry Andric     const Expr *Init = D->getInit();
24250b57cec5SDimitry Andric     if (!Init)
24260b57cec5SDimitry Andric       continue;
24270b57cec5SDimitry Andric     if (!isa<StringLiteral>(Init))
24280b57cec5SDimitry Andric       continue;
24290b57cec5SDimitry Andric 
24300b57cec5SDimitry Andric     Loc VarLoc = state->getLValue(D, C.getLocationContext());
24310b57cec5SDimitry Andric     const MemRegion *MR = VarLoc.getAsRegion();
24320b57cec5SDimitry Andric     if (!MR)
24330b57cec5SDimitry Andric       continue;
24340b57cec5SDimitry Andric 
24350b57cec5SDimitry Andric     SVal StrVal = C.getSVal(Init);
24360b57cec5SDimitry Andric     assert(StrVal.isValid() && "Initializer string is unknown or undefined");
24370b57cec5SDimitry Andric     DefinedOrUnknownSVal strLength =
24380b57cec5SDimitry Andric       getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
24390b57cec5SDimitry Andric 
24400b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
24410b57cec5SDimitry Andric   }
24420b57cec5SDimitry Andric 
24430b57cec5SDimitry Andric   C.addTransition(state);
24440b57cec5SDimitry Andric }
24450b57cec5SDimitry Andric 
24460b57cec5SDimitry Andric ProgramStateRef
24470b57cec5SDimitry Andric CStringChecker::checkRegionChanges(ProgramStateRef state,
24480b57cec5SDimitry Andric     const InvalidatedSymbols *,
24490b57cec5SDimitry Andric     ArrayRef<const MemRegion *> ExplicitRegions,
24500b57cec5SDimitry Andric     ArrayRef<const MemRegion *> Regions,
24510b57cec5SDimitry Andric     const LocationContext *LCtx,
24520b57cec5SDimitry Andric     const CallEvent *Call) const {
24530b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
24540b57cec5SDimitry Andric   if (Entries.isEmpty())
24550b57cec5SDimitry Andric     return state;
24560b57cec5SDimitry Andric 
24570b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
24580b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
24590b57cec5SDimitry Andric 
24600b57cec5SDimitry Andric   // First build sets for the changed regions and their super-regions.
24610b57cec5SDimitry Andric   for (ArrayRef<const MemRegion *>::iterator
24620b57cec5SDimitry Andric       I = Regions.begin(), E = Regions.end(); I != E; ++I) {
24630b57cec5SDimitry Andric     const MemRegion *MR = *I;
24640b57cec5SDimitry Andric     Invalidated.insert(MR);
24650b57cec5SDimitry Andric 
24660b57cec5SDimitry Andric     SuperRegions.insert(MR);
24670b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
24680b57cec5SDimitry Andric       MR = SR->getSuperRegion();
24690b57cec5SDimitry Andric       SuperRegions.insert(MR);
24700b57cec5SDimitry Andric     }
24710b57cec5SDimitry Andric   }
24720b57cec5SDimitry Andric 
24730b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
24740b57cec5SDimitry Andric 
24750b57cec5SDimitry Andric   // Then loop over the entries in the current state.
24760b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(),
24770b57cec5SDimitry Andric       E = Entries.end(); I != E; ++I) {
24780b57cec5SDimitry Andric     const MemRegion *MR = I.getKey();
24790b57cec5SDimitry Andric 
24800b57cec5SDimitry Andric     // Is this entry for a super-region of a changed region?
24810b57cec5SDimitry Andric     if (SuperRegions.count(MR)) {
24820b57cec5SDimitry Andric       Entries = F.remove(Entries, MR);
24830b57cec5SDimitry Andric       continue;
24840b57cec5SDimitry Andric     }
24850b57cec5SDimitry Andric 
24860b57cec5SDimitry Andric     // Is this entry for a sub-region of a changed region?
24870b57cec5SDimitry Andric     const MemRegion *Super = MR;
24880b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
24890b57cec5SDimitry Andric       Super = SR->getSuperRegion();
24900b57cec5SDimitry Andric       if (Invalidated.count(Super)) {
24910b57cec5SDimitry Andric         Entries = F.remove(Entries, MR);
24920b57cec5SDimitry Andric         break;
24930b57cec5SDimitry Andric       }
24940b57cec5SDimitry Andric     }
24950b57cec5SDimitry Andric   }
24960b57cec5SDimitry Andric 
24970b57cec5SDimitry Andric   return state->set<CStringLength>(Entries);
24980b57cec5SDimitry Andric }
24990b57cec5SDimitry Andric 
25000b57cec5SDimitry Andric void CStringChecker::checkLiveSymbols(ProgramStateRef state,
25010b57cec5SDimitry Andric     SymbolReaper &SR) const {
25020b57cec5SDimitry Andric   // Mark all symbols in our string length map as valid.
25030b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
25040b57cec5SDimitry Andric 
25050b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
25060b57cec5SDimitry Andric       I != E; ++I) {
25070b57cec5SDimitry Andric     SVal Len = I.getData();
25080b57cec5SDimitry Andric 
25090b57cec5SDimitry Andric     for (SymExpr::symbol_iterator si = Len.symbol_begin(),
25100b57cec5SDimitry Andric         se = Len.symbol_end(); si != se; ++si)
25110b57cec5SDimitry Andric       SR.markInUse(*si);
25120b57cec5SDimitry Andric   }
25130b57cec5SDimitry Andric }
25140b57cec5SDimitry Andric 
25150b57cec5SDimitry Andric void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
25160b57cec5SDimitry Andric     CheckerContext &C) const {
25170b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
25180b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
25190b57cec5SDimitry Andric   if (Entries.isEmpty())
25200b57cec5SDimitry Andric     return;
25210b57cec5SDimitry Andric 
25220b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
25230b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
25240b57cec5SDimitry Andric       I != E; ++I) {
25250b57cec5SDimitry Andric     SVal Len = I.getData();
25260b57cec5SDimitry Andric     if (SymbolRef Sym = Len.getAsSymbol()) {
25270b57cec5SDimitry Andric       if (SR.isDead(Sym))
25280b57cec5SDimitry Andric         Entries = F.remove(Entries, I.getKey());
25290b57cec5SDimitry Andric     }
25300b57cec5SDimitry Andric   }
25310b57cec5SDimitry Andric 
25320b57cec5SDimitry Andric   state = state->set<CStringLength>(Entries);
25330b57cec5SDimitry Andric   C.addTransition(state);
25340b57cec5SDimitry Andric }
25350b57cec5SDimitry Andric 
25360b57cec5SDimitry Andric void ento::registerCStringModeling(CheckerManager &Mgr) {
25370b57cec5SDimitry Andric   Mgr.registerChecker<CStringChecker>();
25380b57cec5SDimitry Andric }
25390b57cec5SDimitry Andric 
25405ffd83dbSDimitry Andric bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) {
25410b57cec5SDimitry Andric   return true;
25420b57cec5SDimitry Andric }
25430b57cec5SDimitry Andric 
25440b57cec5SDimitry Andric #define REGISTER_CHECKER(name)                                                 \
25450b57cec5SDimitry Andric   void ento::register##name(CheckerManager &mgr) {                             \
25460b57cec5SDimitry Andric     CStringChecker *checker = mgr.getChecker<CStringChecker>();                \
25470b57cec5SDimitry Andric     checker->Filter.Check##name = true;                                        \
2548a7dea167SDimitry Andric     checker->Filter.CheckName##name = mgr.getCurrentCheckerName();             \
25490b57cec5SDimitry Andric   }                                                                            \
25500b57cec5SDimitry Andric                                                                                \
25515ffd83dbSDimitry Andric   bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
25520b57cec5SDimitry Andric 
25530b57cec5SDimitry Andric REGISTER_CHECKER(CStringNullArg)
25540b57cec5SDimitry Andric REGISTER_CHECKER(CStringOutOfBounds)
25550b57cec5SDimitry Andric REGISTER_CHECKER(CStringBufferOverlap)
25560b57cec5SDimitry Andric REGISTER_CHECKER(CStringNotNullTerm)
255781ad6265SDimitry Andric REGISTER_CHECKER(CStringUninitializedRead)
2558