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" 1506c3fb27SDimitry Andric #include "clang/Basic/Builtins.h" 160b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h" 175ffd83dbSDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h" 200b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h" 21349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h" 220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 230b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 24fe6060f1SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h" 250b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 260b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 270b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 285ffd83dbSDimitry Andric #include "llvm/ADT/StringExtras.h" 290b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 30972a253aSDimitry Andric #include <functional> 31bdd1243dSDimitry Andric #include <optional> 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric using namespace clang; 340b57cec5SDimitry Andric using namespace ento; 35972a253aSDimitry Andric using namespace std::placeholders; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric namespace { 385ffd83dbSDimitry Andric struct AnyArgExpr { 395ffd83dbSDimitry Andric const Expr *Expression; 405ffd83dbSDimitry Andric unsigned ArgumentIndex; 415ffd83dbSDimitry Andric }; 4206c3fb27SDimitry Andric struct SourceArgExpr : AnyArgExpr {}; 4306c3fb27SDimitry Andric struct DestinationArgExpr : AnyArgExpr {}; 4406c3fb27SDimitry Andric struct SizeArgExpr : AnyArgExpr {}; 455ffd83dbSDimitry Andric 465ffd83dbSDimitry Andric using ErrorMessage = SmallString<128>; 475ffd83dbSDimitry Andric enum class AccessKind { write, read }; 485ffd83dbSDimitry Andric 495ffd83dbSDimitry Andric static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription, 505ffd83dbSDimitry Andric AccessKind Access) { 515ffd83dbSDimitry Andric ErrorMessage Message; 525ffd83dbSDimitry Andric llvm::raw_svector_ostream Os(Message); 535ffd83dbSDimitry Andric 545ffd83dbSDimitry Andric // Function classification like: Memory copy function 555ffd83dbSDimitry Andric Os << toUppercase(FunctionDescription.front()) 565ffd83dbSDimitry Andric << &FunctionDescription.data()[1]; 575ffd83dbSDimitry Andric 585ffd83dbSDimitry Andric if (Access == AccessKind::write) { 595ffd83dbSDimitry Andric Os << " overflows the destination buffer"; 605ffd83dbSDimitry Andric } else { // read access 615ffd83dbSDimitry Andric Os << " accesses out-of-bound array element"; 625ffd83dbSDimitry Andric } 635ffd83dbSDimitry Andric 645ffd83dbSDimitry Andric return Message; 655ffd83dbSDimitry Andric } 665ffd83dbSDimitry Andric 67480093f4SDimitry Andric enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 }; 68bdd1243dSDimitry Andric 69bdd1243dSDimitry Andric enum class CharKind { Regular = 0, Wide }; 70bdd1243dSDimitry Andric constexpr CharKind CK_Regular = CharKind::Regular; 71bdd1243dSDimitry Andric constexpr CharKind CK_Wide = CharKind::Wide; 72bdd1243dSDimitry Andric 73bdd1243dSDimitry Andric static QualType getCharPtrType(ASTContext &Ctx, CharKind CK) { 74bdd1243dSDimitry Andric return Ctx.getPointerType(CK == CharKind::Regular ? Ctx.CharTy 75bdd1243dSDimitry Andric : Ctx.WideCharTy); 76bdd1243dSDimitry Andric } 77bdd1243dSDimitry Andric 780b57cec5SDimitry Andric class CStringChecker : public Checker< eval::Call, 790b57cec5SDimitry Andric check::PreStmt<DeclStmt>, 800b57cec5SDimitry Andric check::LiveSymbols, 810b57cec5SDimitry Andric check::DeadSymbols, 820b57cec5SDimitry Andric check::RegionChanges 830b57cec5SDimitry Andric > { 840b57cec5SDimitry Andric mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap, 8581ad6265SDimitry Andric BT_NotCString, BT_AdditionOverflow, BT_UninitRead; 860b57cec5SDimitry Andric 8706c3fb27SDimitry Andric mutable const char *CurrentFunctionDescription = nullptr; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric public: 900b57cec5SDimitry Andric /// The filter is used to filter out the diagnostics which are not enabled by 910b57cec5SDimitry Andric /// the user. 920b57cec5SDimitry Andric struct CStringChecksFilter { 9381ad6265SDimitry Andric bool CheckCStringNullArg = false; 9481ad6265SDimitry Andric bool CheckCStringOutOfBounds = false; 9581ad6265SDimitry Andric bool CheckCStringBufferOverlap = false; 9681ad6265SDimitry Andric bool CheckCStringNotNullTerm = false; 9781ad6265SDimitry Andric bool CheckCStringUninitializedRead = false; 980b57cec5SDimitry Andric 99a7dea167SDimitry Andric CheckerNameRef CheckNameCStringNullArg; 100a7dea167SDimitry Andric CheckerNameRef CheckNameCStringOutOfBounds; 101a7dea167SDimitry Andric CheckerNameRef CheckNameCStringBufferOverlap; 102a7dea167SDimitry Andric CheckerNameRef CheckNameCStringNotNullTerm; 10381ad6265SDimitry Andric CheckerNameRef CheckNameCStringUninitializedRead; 1040b57cec5SDimitry Andric }; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric CStringChecksFilter Filter; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric static void *getTag() { static int tag; return &tag; } 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric bool evalCall(const CallEvent &Call, CheckerContext &C) const; 1110b57cec5SDimitry Andric void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; 1120b57cec5SDimitry Andric void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const; 1130b57cec5SDimitry Andric void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric ProgramStateRef 1160b57cec5SDimitry Andric checkRegionChanges(ProgramStateRef state, 1170b57cec5SDimitry Andric const InvalidatedSymbols *, 1180b57cec5SDimitry Andric ArrayRef<const MemRegion *> ExplicitRegions, 1190b57cec5SDimitry Andric ArrayRef<const MemRegion *> Regions, 1200b57cec5SDimitry Andric const LocationContext *LCtx, 1210b57cec5SDimitry Andric const CallEvent *Call) const; 1220b57cec5SDimitry Andric 123972a253aSDimitry Andric using FnCheck = std::function<void(const CStringChecker *, CheckerContext &, 124*647cbc5dSDimitry Andric const CallEvent &)>; 125972a253aSDimitry Andric 1260b57cec5SDimitry Andric CallDescriptionMap<FnCheck> Callbacks = { 127bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"memcpy"}, 3}, 128bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemcpy, _1, _2, _3, CK_Regular)}, 129bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"wmemcpy"}, 3}, 130bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemcpy, _1, _2, _3, CK_Wide)}, 131bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"mempcpy"}, 3}, 132bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMempcpy, _1, _2, _3, CK_Regular)}, 133bdd1243dSDimitry Andric {{CDF_None, {"wmempcpy"}, 3}, 134bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMempcpy, _1, _2, _3, CK_Wide)}, 135bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"memcmp"}, 3}, 136bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemcmp, _1, _2, _3, CK_Regular)}, 137bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"wmemcmp"}, 3}, 138bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemcmp, _1, _2, _3, CK_Wide)}, 139bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"memmove"}, 3}, 140bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemmove, _1, _2, _3, CK_Regular)}, 141bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"wmemmove"}, 3}, 142bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemmove, _1, _2, _3, CK_Wide)}, 143bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"memset"}, 3}, &CStringChecker::evalMemset}, 144bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"explicit_memset"}, 3}, &CStringChecker::evalMemset}, 145bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strcpy"}, 2}, &CStringChecker::evalStrcpy}, 146bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strncpy"}, 3}, &CStringChecker::evalStrncpy}, 147bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"stpcpy"}, 2}, &CStringChecker::evalStpcpy}, 148bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strlcpy"}, 3}, &CStringChecker::evalStrlcpy}, 149bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strcat"}, 2}, &CStringChecker::evalStrcat}, 150bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strncat"}, 3}, &CStringChecker::evalStrncat}, 151bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strlcat"}, 3}, &CStringChecker::evalStrlcat}, 152bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strlen"}, 1}, &CStringChecker::evalstrLength}, 153bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"wcslen"}, 1}, &CStringChecker::evalstrLength}, 154bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strnlen"}, 2}, &CStringChecker::evalstrnLength}, 155bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"wcsnlen"}, 2}, &CStringChecker::evalstrnLength}, 156bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strcmp"}, 2}, &CStringChecker::evalStrcmp}, 157bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strncmp"}, 3}, &CStringChecker::evalStrncmp}, 158bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strcasecmp"}, 2}, &CStringChecker::evalStrcasecmp}, 159bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strncasecmp"}, 3}, 160bdd1243dSDimitry Andric &CStringChecker::evalStrncasecmp}, 161bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"strsep"}, 2}, &CStringChecker::evalStrsep}, 162bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"bcopy"}, 3}, &CStringChecker::evalBcopy}, 163bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"bcmp"}, 3}, 164bdd1243dSDimitry Andric std::bind(&CStringChecker::evalMemcmp, _1, _2, _3, CK_Regular)}, 165bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"bzero"}, 2}, &CStringChecker::evalBzero}, 166bdd1243dSDimitry Andric {{CDF_MaybeBuiltin, {"explicit_bzero"}, 2}, &CStringChecker::evalBzero}, 16706c3fb27SDimitry Andric {{CDF_MaybeBuiltin, {"sprintf"}, 2}, &CStringChecker::evalSprintf}, 16806c3fb27SDimitry Andric {{CDF_MaybeBuiltin, {"snprintf"}, 2}, &CStringChecker::evalSnprintf}, 1690b57cec5SDimitry Andric }; 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric // These require a bit of special handling. 1720b57cec5SDimitry Andric CallDescription StdCopy{{"std", "copy"}, 3}, 1730b57cec5SDimitry Andric StdCopyBackward{{"std", "copy_backward"}, 3}; 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const; 176*647cbc5dSDimitry Andric void evalMemcpy(CheckerContext &C, const CallEvent &Call, CharKind CK) const; 177*647cbc5dSDimitry Andric void evalMempcpy(CheckerContext &C, const CallEvent &Call, CharKind CK) const; 178*647cbc5dSDimitry Andric void evalMemmove(CheckerContext &C, const CallEvent &Call, CharKind CK) const; 179*647cbc5dSDimitry Andric void evalBcopy(CheckerContext &C, const CallEvent &Call) const; 180*647cbc5dSDimitry Andric void evalCopyCommon(CheckerContext &C, const CallEvent &Call, 1815ffd83dbSDimitry Andric ProgramStateRef state, SizeArgExpr Size, 1825ffd83dbSDimitry Andric DestinationArgExpr Dest, SourceArgExpr Source, 183bdd1243dSDimitry Andric bool Restricted, bool IsMempcpy, CharKind CK) const; 1840b57cec5SDimitry Andric 185*647cbc5dSDimitry Andric void evalMemcmp(CheckerContext &C, const CallEvent &Call, CharKind CK) const; 1860b57cec5SDimitry Andric 187*647cbc5dSDimitry Andric void evalstrLength(CheckerContext &C, const CallEvent &Call) const; 188*647cbc5dSDimitry Andric void evalstrnLength(CheckerContext &C, const CallEvent &Call) const; 189*647cbc5dSDimitry Andric void evalstrLengthCommon(CheckerContext &C, const CallEvent &Call, 1900b57cec5SDimitry Andric bool IsStrnlen = false) const; 1910b57cec5SDimitry Andric 192*647cbc5dSDimitry Andric void evalStrcpy(CheckerContext &C, const CallEvent &Call) const; 193*647cbc5dSDimitry Andric void evalStrncpy(CheckerContext &C, const CallEvent &Call) const; 194*647cbc5dSDimitry Andric void evalStpcpy(CheckerContext &C, const CallEvent &Call) const; 195*647cbc5dSDimitry Andric void evalStrlcpy(CheckerContext &C, const CallEvent &Call) const; 196*647cbc5dSDimitry Andric void evalStrcpyCommon(CheckerContext &C, const CallEvent &Call, 197*647cbc5dSDimitry Andric bool ReturnEnd, bool IsBounded, ConcatFnKind appendK, 1980b57cec5SDimitry Andric bool returnPtr = true) const; 1990b57cec5SDimitry Andric 200*647cbc5dSDimitry Andric void evalStrcat(CheckerContext &C, const CallEvent &Call) const; 201*647cbc5dSDimitry Andric void evalStrncat(CheckerContext &C, const CallEvent &Call) const; 202*647cbc5dSDimitry Andric void evalStrlcat(CheckerContext &C, const CallEvent &Call) const; 2030b57cec5SDimitry Andric 204*647cbc5dSDimitry Andric void evalStrcmp(CheckerContext &C, const CallEvent &Call) const; 205*647cbc5dSDimitry Andric void evalStrncmp(CheckerContext &C, const CallEvent &Call) const; 206*647cbc5dSDimitry Andric void evalStrcasecmp(CheckerContext &C, const CallEvent &Call) const; 207*647cbc5dSDimitry Andric void evalStrncasecmp(CheckerContext &C, const CallEvent &Call) const; 208*647cbc5dSDimitry Andric void evalStrcmpCommon(CheckerContext &C, const CallEvent &Call, 209*647cbc5dSDimitry Andric bool IsBounded = false, bool IgnoreCase = false) const; 2100b57cec5SDimitry Andric 211*647cbc5dSDimitry Andric void evalStrsep(CheckerContext &C, const CallEvent &Call) const; 2120b57cec5SDimitry Andric 213*647cbc5dSDimitry Andric void evalStdCopy(CheckerContext &C, const CallEvent &Call) const; 214*647cbc5dSDimitry Andric void evalStdCopyBackward(CheckerContext &C, const CallEvent &Call) const; 215*647cbc5dSDimitry Andric void evalStdCopyCommon(CheckerContext &C, const CallEvent &Call) const; 216*647cbc5dSDimitry Andric void evalMemset(CheckerContext &C, const CallEvent &Call) const; 217*647cbc5dSDimitry Andric void evalBzero(CheckerContext &C, const CallEvent &Call) const; 2180b57cec5SDimitry Andric 219*647cbc5dSDimitry Andric void evalSprintf(CheckerContext &C, const CallEvent &Call) const; 220*647cbc5dSDimitry Andric void evalSnprintf(CheckerContext &C, const CallEvent &Call) const; 221*647cbc5dSDimitry Andric void evalSprintfCommon(CheckerContext &C, const CallEvent &Call, 222*647cbc5dSDimitry Andric bool IsBounded, bool IsBuiltin) const; 22306c3fb27SDimitry Andric 2240b57cec5SDimitry Andric // Utility methods 2250b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef > 2260b57cec5SDimitry Andric static assumeZero(CheckerContext &C, 2270b57cec5SDimitry Andric ProgramStateRef state, SVal V, QualType Ty); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric static ProgramStateRef setCStringLength(ProgramStateRef state, 2300b57cec5SDimitry Andric const MemRegion *MR, 2310b57cec5SDimitry Andric SVal strLength); 2320b57cec5SDimitry Andric static SVal getCStringLengthForRegion(CheckerContext &C, 2330b57cec5SDimitry Andric ProgramStateRef &state, 2340b57cec5SDimitry Andric const Expr *Ex, 2350b57cec5SDimitry Andric const MemRegion *MR, 2360b57cec5SDimitry Andric bool hypothetical); 2370b57cec5SDimitry Andric SVal getCStringLength(CheckerContext &C, 2380b57cec5SDimitry Andric ProgramStateRef &state, 2390b57cec5SDimitry Andric const Expr *Ex, 2400b57cec5SDimitry Andric SVal Buf, 2410b57cec5SDimitry Andric bool hypothetical = false) const; 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric const StringLiteral *getCStringLiteral(CheckerContext &C, 2440b57cec5SDimitry Andric ProgramStateRef &state, 2450b57cec5SDimitry Andric const Expr *expr, 2460b57cec5SDimitry Andric SVal val) const; 2470b57cec5SDimitry Andric 24806c3fb27SDimitry Andric /// Invalidate the destination buffer determined by characters copied. 24906c3fb27SDimitry Andric static ProgramStateRef 25006c3fb27SDimitry Andric invalidateDestinationBufferBySize(CheckerContext &C, ProgramStateRef S, 25106c3fb27SDimitry Andric const Expr *BufE, SVal BufV, SVal SizeV, 25206c3fb27SDimitry Andric QualType SizeTy); 25306c3fb27SDimitry Andric 25406c3fb27SDimitry Andric /// Operation never overflows, do not invalidate the super region. 25506c3fb27SDimitry Andric static ProgramStateRef invalidateDestinationBufferNeverOverflows( 25606c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV); 25706c3fb27SDimitry Andric 25806c3fb27SDimitry Andric /// We do not know whether the operation can overflow (e.g. size is unknown), 25906c3fb27SDimitry Andric /// invalidate the super region and escape related pointers. 26006c3fb27SDimitry Andric static ProgramStateRef invalidateDestinationBufferAlwaysEscapeSuperRegion( 26106c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV); 26206c3fb27SDimitry Andric 26306c3fb27SDimitry Andric /// Invalidate the source buffer for escaping pointers. 26406c3fb27SDimitry Andric static ProgramStateRef invalidateSourceBuffer(CheckerContext &C, 26506c3fb27SDimitry Andric ProgramStateRef S, 26606c3fb27SDimitry Andric const Expr *BufE, SVal BufV); 26706c3fb27SDimitry Andric 26806c3fb27SDimitry Andric /// @param InvalidationTraitOperations Determine how to invlidate the 26906c3fb27SDimitry Andric /// MemRegion by setting the invalidation traits. Return true to cause pointer 27006c3fb27SDimitry Andric /// escape, or false otherwise. 27106c3fb27SDimitry Andric static ProgramStateRef invalidateBufferAux( 27206c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef State, const Expr *Ex, SVal V, 27306c3fb27SDimitry Andric llvm::function_ref<bool(RegionAndSymbolInvalidationTraits &, 27406c3fb27SDimitry Andric const MemRegion *)> 27506c3fb27SDimitry Andric InvalidationTraitOperations); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx, 2780b57cec5SDimitry Andric const MemRegion *MR); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric static bool memsetAux(const Expr *DstBuffer, SVal CharE, 2810b57cec5SDimitry Andric const Expr *Size, CheckerContext &C, 2820b57cec5SDimitry Andric ProgramStateRef &State); 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric // Re-usable checks 2855ffd83dbSDimitry Andric ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State, 2865ffd83dbSDimitry Andric AnyArgExpr Arg, SVal l) const; 2875ffd83dbSDimitry Andric ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state, 2885ffd83dbSDimitry Andric AnyArgExpr Buffer, SVal Element, 289bdd1243dSDimitry Andric AccessKind Access, 290bdd1243dSDimitry Andric CharKind CK = CharKind::Regular) const; 2915ffd83dbSDimitry Andric ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State, 2925ffd83dbSDimitry Andric AnyArgExpr Buffer, SizeArgExpr Size, 293972a253aSDimitry Andric AccessKind Access, 294bdd1243dSDimitry Andric CharKind CK = CharKind::Regular) const; 2955ffd83dbSDimitry Andric ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state, 2965ffd83dbSDimitry Andric SizeArgExpr Size, AnyArgExpr First, 297bdd1243dSDimitry Andric AnyArgExpr Second, 298bdd1243dSDimitry Andric CharKind CK = CharKind::Regular) const; 2990b57cec5SDimitry Andric void emitOverlapBug(CheckerContext &C, 3000b57cec5SDimitry Andric ProgramStateRef state, 3010b57cec5SDimitry Andric const Stmt *First, 3020b57cec5SDimitry Andric const Stmt *Second) const; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S, 3050b57cec5SDimitry Andric StringRef WarningMsg) const; 3060b57cec5SDimitry Andric void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State, 3070b57cec5SDimitry Andric const Stmt *S, StringRef WarningMsg) const; 3080b57cec5SDimitry Andric void emitNotCStringBug(CheckerContext &C, ProgramStateRef State, 3090b57cec5SDimitry Andric const Stmt *S, StringRef WarningMsg) const; 3100b57cec5SDimitry Andric void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const; 31181ad6265SDimitry Andric void emitUninitializedReadBug(CheckerContext &C, ProgramStateRef State, 31281ad6265SDimitry Andric const Expr *E) const; 3130b57cec5SDimitry Andric ProgramStateRef checkAdditionOverflow(CheckerContext &C, 3140b57cec5SDimitry Andric ProgramStateRef state, 3150b57cec5SDimitry Andric NonLoc left, 3160b57cec5SDimitry Andric NonLoc right) const; 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric // Return true if the destination buffer of the copy function may be in bound. 3190b57cec5SDimitry Andric // Expects SVal of Size to be positive and unsigned. 3200b57cec5SDimitry Andric // Expects SVal of FirstBuf to be a FieldRegion. 32106c3fb27SDimitry Andric static bool isFirstBufInBound(CheckerContext &C, ProgramStateRef State, 32206c3fb27SDimitry Andric SVal BufVal, QualType BufTy, SVal LengthVal, 32306c3fb27SDimitry Andric QualType LengthTy); 3240b57cec5SDimitry Andric }; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric } //end anonymous namespace 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal) 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3310b57cec5SDimitry Andric // Individual checks and utility methods. 3320b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef > 3350b57cec5SDimitry Andric CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V, 3360b57cec5SDimitry Andric QualType Ty) { 337bdd1243dSDimitry Andric std::optional<DefinedSVal> val = V.getAs<DefinedSVal>(); 3380b57cec5SDimitry Andric if (!val) 3390b57cec5SDimitry Andric return std::pair<ProgramStateRef , ProgramStateRef >(state, state); 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 3420b57cec5SDimitry Andric DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); 3430b57cec5SDimitry Andric return state->assume(svalBuilder.evalEQ(state, *val, zero)); 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C, 3475ffd83dbSDimitry Andric ProgramStateRef State, 3485ffd83dbSDimitry Andric AnyArgExpr Arg, SVal l) const { 3490b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 3505ffd83dbSDimitry Andric if (!State) 3510b57cec5SDimitry Andric return nullptr; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric ProgramStateRef stateNull, stateNonNull; 3545ffd83dbSDimitry Andric std::tie(stateNull, stateNonNull) = 3555ffd83dbSDimitry Andric assumeZero(C, State, l, Arg.Expression->getType()); 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric if (stateNull && !stateNonNull) { 3580b57cec5SDimitry Andric if (Filter.CheckCStringNullArg) { 3590b57cec5SDimitry Andric SmallString<80> buf; 360a7dea167SDimitry Andric llvm::raw_svector_ostream OS(buf); 3610b57cec5SDimitry Andric assert(CurrentFunctionDescription); 3625ffd83dbSDimitry Andric OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1) 3635ffd83dbSDimitry Andric << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to " 364480093f4SDimitry Andric << CurrentFunctionDescription; 3650b57cec5SDimitry Andric 3665ffd83dbSDimitry Andric emitNullArgBug(C, stateNull, Arg.Expression, OS.str()); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric return nullptr; 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric // From here on, assume that the value is non-null. 3720b57cec5SDimitry Andric assert(stateNonNull); 3730b57cec5SDimitry Andric return stateNonNull; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? 3770b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, 3780b57cec5SDimitry Andric ProgramStateRef state, 3795ffd83dbSDimitry Andric AnyArgExpr Buffer, SVal Element, 380972a253aSDimitry Andric AccessKind Access, 381bdd1243dSDimitry Andric CharKind CK) const { 3825ffd83dbSDimitry Andric 3830b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 3840b57cec5SDimitry Andric if (!state) 3850b57cec5SDimitry Andric return nullptr; 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // Check for out of bound array element access. 3885ffd83dbSDimitry Andric const MemRegion *R = Element.getAsRegion(); 3890b57cec5SDimitry Andric if (!R) 3900b57cec5SDimitry Andric return state; 3910b57cec5SDimitry Andric 3925ffd83dbSDimitry Andric const auto *ER = dyn_cast<ElementRegion>(R); 3930b57cec5SDimitry Andric if (!ER) 3940b57cec5SDimitry Andric return state; 3950b57cec5SDimitry Andric 396972a253aSDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 397972a253aSDimitry Andric ASTContext &Ctx = svalBuilder.getContext(); 398972a253aSDimitry Andric 399972a253aSDimitry Andric // Get the index of the accessed element. 400972a253aSDimitry Andric NonLoc Idx = ER->getIndex(); 401972a253aSDimitry Andric 402bdd1243dSDimitry Andric if (CK == CharKind::Regular) { 403972a253aSDimitry Andric if (ER->getValueType() != Ctx.CharTy) 4040b57cec5SDimitry Andric return state; 405972a253aSDimitry Andric } else { 406972a253aSDimitry Andric if (ER->getValueType() != Ctx.WideCharTy) 407972a253aSDimitry Andric return state; 408972a253aSDimitry Andric 409972a253aSDimitry Andric QualType SizeTy = Ctx.getSizeType(); 410972a253aSDimitry Andric NonLoc WideSize = 411972a253aSDimitry Andric svalBuilder 412972a253aSDimitry Andric .makeIntVal(Ctx.getTypeSizeInChars(Ctx.WideCharTy).getQuantity(), 413972a253aSDimitry Andric SizeTy) 414972a253aSDimitry Andric .castAs<NonLoc>(); 415972a253aSDimitry Andric SVal Offset = svalBuilder.evalBinOpNN(state, BO_Mul, Idx, WideSize, SizeTy); 416972a253aSDimitry Andric if (Offset.isUnknown()) 417972a253aSDimitry Andric return state; 418972a253aSDimitry Andric Idx = Offset.castAs<NonLoc>(); 419972a253aSDimitry Andric } 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Get the size of the array. 4225ffd83dbSDimitry Andric const auto *superReg = cast<SubRegion>(ER->getSuperRegion()); 4235ffd83dbSDimitry Andric DefinedOrUnknownSVal Size = 424fe6060f1SDimitry Andric getDynamicExtent(state, superReg, C.getSValBuilder()); 4250b57cec5SDimitry Andric 42681ad6265SDimitry Andric ProgramStateRef StInBound, StOutBound; 42781ad6265SDimitry Andric std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, Size); 4280b57cec5SDimitry Andric if (StOutBound && !StInBound) { 4290b57cec5SDimitry Andric // These checks are either enabled by the CString out-of-bounds checker 4300b57cec5SDimitry Andric // explicitly or implicitly by the Malloc checker. 4310b57cec5SDimitry Andric // In the latter case we only do modeling but do not emit warning. 4320b57cec5SDimitry Andric if (!Filter.CheckCStringOutOfBounds) 4330b57cec5SDimitry Andric return nullptr; 4340b57cec5SDimitry Andric 4355ffd83dbSDimitry Andric // Emit a bug report. 4365ffd83dbSDimitry Andric ErrorMessage Message = 4375ffd83dbSDimitry Andric createOutOfBoundErrorMsg(CurrentFunctionDescription, Access); 4385ffd83dbSDimitry Andric emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message); 4390b57cec5SDimitry Andric return nullptr; 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric 44281ad6265SDimitry Andric // Ensure that we wouldn't read uninitialized value. 44381ad6265SDimitry Andric if (Access == AccessKind::read) { 44481ad6265SDimitry Andric if (Filter.CheckCStringUninitializedRead && 44581ad6265SDimitry Andric StInBound->getSVal(ER).isUndef()) { 44681ad6265SDimitry Andric emitUninitializedReadBug(C, StInBound, Buffer.Expression); 44781ad6265SDimitry Andric return nullptr; 44881ad6265SDimitry Andric } 44981ad6265SDimitry Andric } 45081ad6265SDimitry Andric 4510b57cec5SDimitry Andric // Array bound check succeeded. From this point forward the array bound 4520b57cec5SDimitry Andric // should always succeed. 4530b57cec5SDimitry Andric return StInBound; 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 456972a253aSDimitry Andric ProgramStateRef 457972a253aSDimitry Andric CStringChecker::CheckBufferAccess(CheckerContext &C, ProgramStateRef State, 458972a253aSDimitry Andric AnyArgExpr Buffer, SizeArgExpr Size, 459bdd1243dSDimitry Andric AccessKind Access, CharKind CK) const { 4600b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 4615ffd83dbSDimitry Andric if (!State) 4620b57cec5SDimitry Andric return nullptr; 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 4650b57cec5SDimitry Andric ASTContext &Ctx = svalBuilder.getContext(); 4660b57cec5SDimitry Andric 4675ffd83dbSDimitry Andric QualType SizeTy = Size.Expression->getType(); 468bdd1243dSDimitry Andric QualType PtrTy = getCharPtrType(Ctx, CK); 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric // Check that the first buffer is non-null. 4715ffd83dbSDimitry Andric SVal BufVal = C.getSVal(Buffer.Expression); 4725ffd83dbSDimitry Andric State = checkNonNull(C, State, Buffer, BufVal); 4735ffd83dbSDimitry Andric if (!State) 4740b57cec5SDimitry Andric return nullptr; 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric // If out-of-bounds checking is turned off, skip the rest. 4770b57cec5SDimitry Andric if (!Filter.CheckCStringOutOfBounds) 4785ffd83dbSDimitry Andric return State; 4790b57cec5SDimitry Andric 4805f757f3fSDimitry Andric SVal BufStart = 4815f757f3fSDimitry Andric svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType()); 4825f757f3fSDimitry Andric 4835f757f3fSDimitry Andric // Check if the first byte of the buffer is accessible. 4845f757f3fSDimitry Andric State = CheckLocation(C, State, Buffer, BufStart, Access, CK); 4855f757f3fSDimitry Andric if (!State) 4865f757f3fSDimitry Andric return nullptr; 4875f757f3fSDimitry Andric 4880b57cec5SDimitry Andric // Get the access length and make sure it is known. 4890b57cec5SDimitry Andric // FIXME: This assumes the caller has already checked that the access length 4900b57cec5SDimitry Andric // is positive. And that it's unsigned. 4915ffd83dbSDimitry Andric SVal LengthVal = C.getSVal(Size.Expression); 492bdd1243dSDimitry Andric std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 4930b57cec5SDimitry Andric if (!Length) 4945ffd83dbSDimitry Andric return State; 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric // Compute the offset of the last element to be accessed: size-1. 4975ffd83dbSDimitry Andric NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>(); 4985ffd83dbSDimitry Andric SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy); 4990b57cec5SDimitry Andric if (Offset.isUnknown()) 5000b57cec5SDimitry Andric return nullptr; 5010b57cec5SDimitry Andric NonLoc LastOffset = Offset.castAs<NonLoc>(); 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric // Check that the first buffer is sufficiently long. 504bdd1243dSDimitry Andric if (std::optional<Loc> BufLoc = BufStart.getAs<Loc>()) { 5050b57cec5SDimitry Andric 5065ffd83dbSDimitry Andric SVal BufEnd = 5075ffd83dbSDimitry Andric svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy); 508bdd1243dSDimitry Andric State = CheckLocation(C, State, Buffer, BufEnd, Access, CK); 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric // If the buffer isn't large enough, abort. 5115ffd83dbSDimitry Andric if (!State) 5120b57cec5SDimitry Andric return nullptr; 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric // Large enough or not, return this state! 5165ffd83dbSDimitry Andric return State; 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C, 5200b57cec5SDimitry Andric ProgramStateRef state, 5215ffd83dbSDimitry Andric SizeArgExpr Size, AnyArgExpr First, 522972a253aSDimitry Andric AnyArgExpr Second, 523bdd1243dSDimitry Andric CharKind CK) const { 5240b57cec5SDimitry Andric if (!Filter.CheckCStringBufferOverlap) 5250b57cec5SDimitry Andric return state; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric // Do a simple check for overlap: if the two arguments are from the same 5280b57cec5SDimitry Andric // buffer, see if the end of the first is greater than the start of the second 5290b57cec5SDimitry Andric // or vice versa. 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 5320b57cec5SDimitry Andric if (!state) 5330b57cec5SDimitry Andric return nullptr; 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric ProgramStateRef stateTrue, stateFalse; 5360b57cec5SDimitry Andric 53781ad6265SDimitry Andric // Assume different address spaces cannot overlap. 53881ad6265SDimitry Andric if (First.Expression->getType()->getPointeeType().getAddressSpace() != 53981ad6265SDimitry Andric Second.Expression->getType()->getPointeeType().getAddressSpace()) 54081ad6265SDimitry Andric return state; 54181ad6265SDimitry Andric 5420b57cec5SDimitry Andric // Get the buffer values and make sure they're known locations. 5430b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 5445ffd83dbSDimitry Andric SVal firstVal = state->getSVal(First.Expression, LCtx); 5455ffd83dbSDimitry Andric SVal secondVal = state->getSVal(Second.Expression, LCtx); 5460b57cec5SDimitry Andric 547bdd1243dSDimitry Andric std::optional<Loc> firstLoc = firstVal.getAs<Loc>(); 5480b57cec5SDimitry Andric if (!firstLoc) 5490b57cec5SDimitry Andric return state; 5500b57cec5SDimitry Andric 551bdd1243dSDimitry Andric std::optional<Loc> secondLoc = secondVal.getAs<Loc>(); 5520b57cec5SDimitry Andric if (!secondLoc) 5530b57cec5SDimitry Andric return state; 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // Are the two values the same? 5560b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 5570b57cec5SDimitry Andric std::tie(stateTrue, stateFalse) = 5580b57cec5SDimitry Andric state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric if (stateTrue && !stateFalse) { 5610b57cec5SDimitry Andric // If the values are known to be equal, that's automatically an overlap. 5625ffd83dbSDimitry Andric emitOverlapBug(C, stateTrue, First.Expression, Second.Expression); 5630b57cec5SDimitry Andric return nullptr; 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric // assume the two expressions are not equal. 5670b57cec5SDimitry Andric assert(stateFalse); 5680b57cec5SDimitry Andric state = stateFalse; 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric // Which value comes first? 5710b57cec5SDimitry Andric QualType cmpTy = svalBuilder.getConditionType(); 5725ffd83dbSDimitry Andric SVal reverse = 5735ffd83dbSDimitry Andric svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy); 574bdd1243dSDimitry Andric std::optional<DefinedOrUnknownSVal> reverseTest = 5750b57cec5SDimitry Andric reverse.getAs<DefinedOrUnknownSVal>(); 5760b57cec5SDimitry Andric if (!reverseTest) 5770b57cec5SDimitry Andric return state; 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric std::tie(stateTrue, stateFalse) = state->assume(*reverseTest); 5800b57cec5SDimitry Andric if (stateTrue) { 5810b57cec5SDimitry Andric if (stateFalse) { 5820b57cec5SDimitry Andric // If we don't know which one comes first, we can't perform this test. 5830b57cec5SDimitry Andric return state; 5840b57cec5SDimitry Andric } else { 5850b57cec5SDimitry Andric // Switch the values so that firstVal is before secondVal. 5860b57cec5SDimitry Andric std::swap(firstLoc, secondLoc); 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric // Switch the Exprs as well, so that they still correspond. 5890b57cec5SDimitry Andric std::swap(First, Second); 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric } 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric // Get the length, and make sure it too is known. 5945ffd83dbSDimitry Andric SVal LengthVal = state->getSVal(Size.Expression, LCtx); 595bdd1243dSDimitry Andric std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 5960b57cec5SDimitry Andric if (!Length) 5970b57cec5SDimitry Andric return state; 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric // Convert the first buffer's start address to char*. 6000b57cec5SDimitry Andric // Bail out if the cast fails. 6010b57cec5SDimitry Andric ASTContext &Ctx = svalBuilder.getContext(); 602bdd1243dSDimitry Andric QualType CharPtrTy = getCharPtrType(Ctx, CK); 6035ffd83dbSDimitry Andric SVal FirstStart = 6045ffd83dbSDimitry Andric svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType()); 605bdd1243dSDimitry Andric std::optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>(); 6060b57cec5SDimitry Andric if (!FirstStartLoc) 6070b57cec5SDimitry Andric return state; 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric // Compute the end of the first buffer. Bail out if THAT fails. 6105ffd83dbSDimitry Andric SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc, 6115ffd83dbSDimitry Andric *Length, CharPtrTy); 612bdd1243dSDimitry Andric std::optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>(); 6130b57cec5SDimitry Andric if (!FirstEndLoc) 6140b57cec5SDimitry Andric return state; 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric // Is the end of the first buffer past the start of the second buffer? 6175ffd83dbSDimitry Andric SVal Overlap = 6185ffd83dbSDimitry Andric svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy); 619bdd1243dSDimitry Andric std::optional<DefinedOrUnknownSVal> OverlapTest = 6200b57cec5SDimitry Andric Overlap.getAs<DefinedOrUnknownSVal>(); 6210b57cec5SDimitry Andric if (!OverlapTest) 6220b57cec5SDimitry Andric return state; 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric if (stateTrue && !stateFalse) { 6270b57cec5SDimitry Andric // Overlap! 6285ffd83dbSDimitry Andric emitOverlapBug(C, stateTrue, First.Expression, Second.Expression); 6290b57cec5SDimitry Andric return nullptr; 6300b57cec5SDimitry Andric } 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric // assume the two expressions don't overlap. 6330b57cec5SDimitry Andric assert(stateFalse); 6340b57cec5SDimitry Andric return stateFalse; 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state, 6380b57cec5SDimitry Andric const Stmt *First, const Stmt *Second) const { 6390b57cec5SDimitry Andric ExplodedNode *N = C.generateErrorNode(state); 6400b57cec5SDimitry Andric if (!N) 6410b57cec5SDimitry Andric return; 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric if (!BT_Overlap) 6440b57cec5SDimitry Andric BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap, 6450b57cec5SDimitry Andric categories::UnixAPI, "Improper arguments")); 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric // Generate a report for this bug. 648a7dea167SDimitry Andric auto report = std::make_unique<PathSensitiveBugReport>( 6490b57cec5SDimitry Andric *BT_Overlap, "Arguments must not be overlapping buffers", N); 6500b57cec5SDimitry Andric report->addRange(First->getSourceRange()); 6510b57cec5SDimitry Andric report->addRange(Second->getSourceRange()); 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric C.emitReport(std::move(report)); 6540b57cec5SDimitry Andric } 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State, 6570b57cec5SDimitry Andric const Stmt *S, StringRef WarningMsg) const { 6580b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 6595f757f3fSDimitry Andric if (!BT_Null) { 6605f757f3fSDimitry Andric // FIXME: This call uses the string constant 'categories::UnixAPI' as the 6615f757f3fSDimitry Andric // description of the bug; it should be replaced by a real description. 6625f757f3fSDimitry Andric BT_Null.reset( 6635f757f3fSDimitry Andric new BugType(Filter.CheckNameCStringNullArg, categories::UnixAPI)); 6645f757f3fSDimitry Andric } 6650b57cec5SDimitry Andric 6665f757f3fSDimitry Andric auto Report = 6675f757f3fSDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_Null, WarningMsg, N); 6680b57cec5SDimitry Andric Report->addRange(S->getSourceRange()); 6690b57cec5SDimitry Andric if (const auto *Ex = dyn_cast<Expr>(S)) 6700b57cec5SDimitry Andric bugreporter::trackExpressionValue(N, Ex, *Report); 6710b57cec5SDimitry Andric C.emitReport(std::move(Report)); 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric 67581ad6265SDimitry Andric void CStringChecker::emitUninitializedReadBug(CheckerContext &C, 67681ad6265SDimitry Andric ProgramStateRef State, 67781ad6265SDimitry Andric const Expr *E) const { 67881ad6265SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 67981ad6265SDimitry Andric const char *Msg = 68081ad6265SDimitry Andric "Bytes string function accesses uninitialized/garbage values"; 68181ad6265SDimitry Andric if (!BT_UninitRead) 6825f757f3fSDimitry Andric BT_UninitRead.reset(new BugType(Filter.CheckNameCStringUninitializedRead, 6835f757f3fSDimitry Andric "Accessing unitialized/garbage values")); 68481ad6265SDimitry Andric 6855f757f3fSDimitry Andric auto Report = 6865f757f3fSDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_UninitRead, Msg, N); 68781ad6265SDimitry Andric Report->addRange(E->getSourceRange()); 68881ad6265SDimitry Andric bugreporter::trackExpressionValue(N, E, *Report); 68981ad6265SDimitry Andric C.emitReport(std::move(Report)); 69081ad6265SDimitry Andric } 69181ad6265SDimitry Andric } 69281ad6265SDimitry Andric 6930b57cec5SDimitry Andric void CStringChecker::emitOutOfBoundsBug(CheckerContext &C, 6940b57cec5SDimitry Andric ProgramStateRef State, const Stmt *S, 6950b57cec5SDimitry Andric StringRef WarningMsg) const { 6960b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 6970b57cec5SDimitry Andric if (!BT_Bounds) 6985f757f3fSDimitry Andric BT_Bounds.reset(new BugType(Filter.CheckCStringOutOfBounds 6995f757f3fSDimitry Andric ? Filter.CheckNameCStringOutOfBounds 7000b57cec5SDimitry Andric : Filter.CheckNameCStringNullArg, 7015f757f3fSDimitry Andric "Out-of-bound array access")); 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric // FIXME: It would be nice to eventually make this diagnostic more clear, 7040b57cec5SDimitry Andric // e.g., by referencing the original declaration or by saying *why* this 7050b57cec5SDimitry Andric // reference is outside the range. 7065f757f3fSDimitry Andric auto Report = 7075f757f3fSDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_Bounds, WarningMsg, N); 7080b57cec5SDimitry Andric Report->addRange(S->getSourceRange()); 7090b57cec5SDimitry Andric C.emitReport(std::move(Report)); 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State, 7140b57cec5SDimitry Andric const Stmt *S, 7150b57cec5SDimitry Andric StringRef WarningMsg) const { 7160b57cec5SDimitry Andric if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) { 7175f757f3fSDimitry Andric if (!BT_NotCString) { 7185f757f3fSDimitry Andric // FIXME: This call uses the string constant 'categories::UnixAPI' as the 7195f757f3fSDimitry Andric // description of the bug; it should be replaced by a real description. 7205f757f3fSDimitry Andric BT_NotCString.reset( 7215f757f3fSDimitry Andric new BugType(Filter.CheckNameCStringNotNullTerm, categories::UnixAPI)); 7225f757f3fSDimitry Andric } 7230b57cec5SDimitry Andric 724a7dea167SDimitry Andric auto Report = 725a7dea167SDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N); 7260b57cec5SDimitry Andric 7270b57cec5SDimitry Andric Report->addRange(S->getSourceRange()); 7280b57cec5SDimitry Andric C.emitReport(std::move(Report)); 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric void CStringChecker::emitAdditionOverflowBug(CheckerContext &C, 7330b57cec5SDimitry Andric ProgramStateRef State) const { 7340b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 7355f757f3fSDimitry Andric if (!BT_AdditionOverflow) { 7365f757f3fSDimitry Andric // FIXME: This call uses the word "API" as the description of the bug; 7375f757f3fSDimitry Andric // it should be replaced by a better error message (if this unlikely 7385f757f3fSDimitry Andric // situation continues to exist as a separate bug type). 73981ad6265SDimitry Andric BT_AdditionOverflow.reset( 7405f757f3fSDimitry Andric new BugType(Filter.CheckNameCStringOutOfBounds, "API")); 7415f757f3fSDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric // This isn't a great error message, but this should never occur in real 7440b57cec5SDimitry Andric // code anyway -- you'd have to create a buffer longer than a size_t can 7450b57cec5SDimitry Andric // represent, which is sort of a contradiction. 7460b57cec5SDimitry Andric const char *WarningMsg = 7470b57cec5SDimitry Andric "This expression will create a string whose length is too big to " 7480b57cec5SDimitry Andric "be represented as a size_t"; 7490b57cec5SDimitry Andric 75081ad6265SDimitry Andric auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow, 75181ad6265SDimitry Andric WarningMsg, N); 7520b57cec5SDimitry Andric C.emitReport(std::move(Report)); 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C, 7570b57cec5SDimitry Andric ProgramStateRef state, 7580b57cec5SDimitry Andric NonLoc left, 7590b57cec5SDimitry Andric NonLoc right) const { 7600b57cec5SDimitry Andric // If out-of-bounds checking is turned off, skip the rest. 7610b57cec5SDimitry Andric if (!Filter.CheckCStringOutOfBounds) 7620b57cec5SDimitry Andric return state; 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 7650b57cec5SDimitry Andric if (!state) 7660b57cec5SDimitry Andric return nullptr; 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 7690b57cec5SDimitry Andric BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 7720b57cec5SDimitry Andric const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); 7730b57cec5SDimitry Andric NonLoc maxVal = svalBuilder.makeIntVal(maxValInt); 7740b57cec5SDimitry Andric 7750b57cec5SDimitry Andric SVal maxMinusRight; 77681ad6265SDimitry Andric if (isa<nonloc::ConcreteInt>(right)) { 7770b57cec5SDimitry Andric maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right, 7780b57cec5SDimitry Andric sizeTy); 7790b57cec5SDimitry Andric } else { 7800b57cec5SDimitry Andric // Try switching the operands. (The order of these two assignments is 7810b57cec5SDimitry Andric // important!) 7820b57cec5SDimitry Andric maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left, 7830b57cec5SDimitry Andric sizeTy); 7840b57cec5SDimitry Andric left = right; 7850b57cec5SDimitry Andric } 7860b57cec5SDimitry Andric 787bdd1243dSDimitry Andric if (std::optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) { 7880b57cec5SDimitry Andric QualType cmpTy = svalBuilder.getConditionType(); 7890b57cec5SDimitry Andric // If left > max - right, we have an overflow. 7900b57cec5SDimitry Andric SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left, 7910b57cec5SDimitry Andric *maxMinusRightNL, cmpTy); 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric ProgramStateRef stateOverflow, stateOkay; 7940b57cec5SDimitry Andric std::tie(stateOverflow, stateOkay) = 7950b57cec5SDimitry Andric state->assume(willOverflow.castAs<DefinedOrUnknownSVal>()); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric if (stateOverflow && !stateOkay) { 7980b57cec5SDimitry Andric // We have an overflow. Emit a bug report. 7990b57cec5SDimitry Andric emitAdditionOverflowBug(C, stateOverflow); 8000b57cec5SDimitry Andric return nullptr; 8010b57cec5SDimitry Andric } 8020b57cec5SDimitry Andric 8030b57cec5SDimitry Andric // From now on, assume an overflow didn't occur. 8040b57cec5SDimitry Andric assert(stateOkay); 8050b57cec5SDimitry Andric state = stateOkay; 8060b57cec5SDimitry Andric } 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric return state; 8090b57cec5SDimitry Andric } 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state, 8120b57cec5SDimitry Andric const MemRegion *MR, 8130b57cec5SDimitry Andric SVal strLength) { 8140b57cec5SDimitry Andric assert(!strLength.isUndef() && "Attempt to set an undefined string length"); 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric MR = MR->StripCasts(); 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric switch (MR->getKind()) { 8190b57cec5SDimitry Andric case MemRegion::StringRegionKind: 8200b57cec5SDimitry Andric // FIXME: This can happen if we strcpy() into a string region. This is 8210b57cec5SDimitry Andric // undefined [C99 6.4.5p6], but we should still warn about it. 8220b57cec5SDimitry Andric return state; 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andric case MemRegion::SymbolicRegionKind: 8250b57cec5SDimitry Andric case MemRegion::AllocaRegionKind: 8265ffd83dbSDimitry Andric case MemRegion::NonParamVarRegionKind: 8275ffd83dbSDimitry Andric case MemRegion::ParamVarRegionKind: 8280b57cec5SDimitry Andric case MemRegion::FieldRegionKind: 8290b57cec5SDimitry Andric case MemRegion::ObjCIvarRegionKind: 8300b57cec5SDimitry Andric // These are the types we can currently track string lengths for. 8310b57cec5SDimitry Andric break; 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric case MemRegion::ElementRegionKind: 8340b57cec5SDimitry Andric // FIXME: Handle element regions by upper-bounding the parent region's 8350b57cec5SDimitry Andric // string length. 8360b57cec5SDimitry Andric return state; 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric default: 8390b57cec5SDimitry Andric // Other regions (mostly non-data) can't have a reliable C string length. 8400b57cec5SDimitry Andric // For now, just ignore the change. 8410b57cec5SDimitry Andric // FIXME: These are rare but not impossible. We should output some kind of 8420b57cec5SDimitry Andric // warning for things like strcpy((char[]){'a', 0}, "b"); 8430b57cec5SDimitry Andric return state; 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric if (strLength.isUnknown()) 8470b57cec5SDimitry Andric return state->remove<CStringLength>(MR); 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric return state->set<CStringLength>(MR, strLength); 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, 8530b57cec5SDimitry Andric ProgramStateRef &state, 8540b57cec5SDimitry Andric const Expr *Ex, 8550b57cec5SDimitry Andric const MemRegion *MR, 8560b57cec5SDimitry Andric bool hypothetical) { 8570b57cec5SDimitry Andric if (!hypothetical) { 8580b57cec5SDimitry Andric // If there's a recorded length, go ahead and return it. 8590b57cec5SDimitry Andric const SVal *Recorded = state->get<CStringLength>(MR); 8600b57cec5SDimitry Andric if (Recorded) 8610b57cec5SDimitry Andric return *Recorded; 8620b57cec5SDimitry Andric } 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric // Otherwise, get a new symbol and update the state. 8650b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 8660b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 8670b57cec5SDimitry Andric SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), 8680b57cec5SDimitry Andric MR, Ex, sizeTy, 8690b57cec5SDimitry Andric C.getLocationContext(), 8700b57cec5SDimitry Andric C.blockCount()); 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric if (!hypothetical) { 873bdd1243dSDimitry Andric if (std::optional<NonLoc> strLn = strLength.getAs<NonLoc>()) { 8740b57cec5SDimitry Andric // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4 8750b57cec5SDimitry Andric BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 8760b57cec5SDimitry Andric const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); 8770b57cec5SDimitry Andric llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4); 8780b57cec5SDimitry Andric const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt, 8790b57cec5SDimitry Andric fourInt); 8800b57cec5SDimitry Andric NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt); 8815f757f3fSDimitry Andric SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn, maxLength, 8825f757f3fSDimitry Andric svalBuilder.getConditionType()); 8830b57cec5SDimitry Andric state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true); 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric state = state->set<CStringLength>(MR, strLength); 8860b57cec5SDimitry Andric } 8870b57cec5SDimitry Andric 8880b57cec5SDimitry Andric return strLength; 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state, 8920b57cec5SDimitry Andric const Expr *Ex, SVal Buf, 8930b57cec5SDimitry Andric bool hypothetical) const { 8940b57cec5SDimitry Andric const MemRegion *MR = Buf.getAsRegion(); 8950b57cec5SDimitry Andric if (!MR) { 8960b57cec5SDimitry Andric // If we can't get a region, see if it's something we /know/ isn't a 8970b57cec5SDimitry Andric // C string. In the context of locations, the only time we can issue such 8980b57cec5SDimitry Andric // a warning is for labels. 899bdd1243dSDimitry Andric if (std::optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) { 9000b57cec5SDimitry Andric if (Filter.CheckCStringNotNullTerm) { 9010b57cec5SDimitry Andric SmallString<120> buf; 9020b57cec5SDimitry Andric llvm::raw_svector_ostream os(buf); 9030b57cec5SDimitry Andric assert(CurrentFunctionDescription); 9040b57cec5SDimitry Andric os << "Argument to " << CurrentFunctionDescription 9050b57cec5SDimitry Andric << " is the address of the label '" << Label->getLabel()->getName() 9060b57cec5SDimitry Andric << "', which is not a null-terminated string"; 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric emitNotCStringBug(C, state, Ex, os.str()); 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric return UndefinedVal(); 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric // If it's not a region and not a label, give up. 9140b57cec5SDimitry Andric return UnknownVal(); 9150b57cec5SDimitry Andric } 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric // If we have a region, strip casts from it and see if we can figure out 9180b57cec5SDimitry Andric // its length. For anything we can't figure out, just return UnknownVal. 9190b57cec5SDimitry Andric MR = MR->StripCasts(); 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric switch (MR->getKind()) { 9220b57cec5SDimitry Andric case MemRegion::StringRegionKind: { 9230b57cec5SDimitry Andric // Modifying the contents of string regions is undefined [C99 6.4.5p6], 9240b57cec5SDimitry Andric // so we can assume that the byte length is the correct C string length. 9250b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 9260b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 9270b57cec5SDimitry Andric const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); 928753f127fSDimitry Andric return svalBuilder.makeIntVal(strLit->getLength(), sizeTy); 9290b57cec5SDimitry Andric } 9305f757f3fSDimitry Andric case MemRegion::NonParamVarRegionKind: { 9315f757f3fSDimitry Andric // If we have a global constant with a string literal initializer, 9325f757f3fSDimitry Andric // compute the initializer's length. 9335f757f3fSDimitry Andric const VarDecl *Decl = cast<NonParamVarRegion>(MR)->getDecl(); 9345f757f3fSDimitry Andric if (Decl->getType().isConstQualified() && Decl->hasGlobalStorage()) { 9355f757f3fSDimitry Andric if (const Expr *Init = Decl->getInit()) { 9365f757f3fSDimitry Andric if (auto *StrLit = dyn_cast<StringLiteral>(Init)) { 9375f757f3fSDimitry Andric SValBuilder &SvalBuilder = C.getSValBuilder(); 9385f757f3fSDimitry Andric QualType SizeTy = SvalBuilder.getContext().getSizeType(); 9395f757f3fSDimitry Andric return SvalBuilder.makeIntVal(StrLit->getLength(), SizeTy); 9405f757f3fSDimitry Andric } 9415f757f3fSDimitry Andric } 9425f757f3fSDimitry Andric } 9435f757f3fSDimitry Andric [[fallthrough]]; 9445f757f3fSDimitry Andric } 9450b57cec5SDimitry Andric case MemRegion::SymbolicRegionKind: 9460b57cec5SDimitry Andric case MemRegion::AllocaRegionKind: 9475ffd83dbSDimitry Andric case MemRegion::ParamVarRegionKind: 9480b57cec5SDimitry Andric case MemRegion::FieldRegionKind: 9490b57cec5SDimitry Andric case MemRegion::ObjCIvarRegionKind: 9500b57cec5SDimitry Andric return getCStringLengthForRegion(C, state, Ex, MR, hypothetical); 9510b57cec5SDimitry Andric case MemRegion::CompoundLiteralRegionKind: 9520b57cec5SDimitry Andric // FIXME: Can we track this? Is it necessary? 9530b57cec5SDimitry Andric return UnknownVal(); 9540b57cec5SDimitry Andric case MemRegion::ElementRegionKind: 9550b57cec5SDimitry Andric // FIXME: How can we handle this? It's not good enough to subtract the 9560b57cec5SDimitry Andric // offset from the base string length; consider "123\x00567" and &a[5]. 9570b57cec5SDimitry Andric return UnknownVal(); 9580b57cec5SDimitry Andric default: 9590b57cec5SDimitry Andric // Other regions (mostly non-data) can't have a reliable C string length. 9600b57cec5SDimitry Andric // In this case, an error is emitted and UndefinedVal is returned. 9610b57cec5SDimitry Andric // The caller should always be prepared to handle this case. 9620b57cec5SDimitry Andric if (Filter.CheckCStringNotNullTerm) { 9630b57cec5SDimitry Andric SmallString<120> buf; 9640b57cec5SDimitry Andric llvm::raw_svector_ostream os(buf); 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric assert(CurrentFunctionDescription); 9670b57cec5SDimitry Andric os << "Argument to " << CurrentFunctionDescription << " is "; 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric if (SummarizeRegion(os, C.getASTContext(), MR)) 9700b57cec5SDimitry Andric os << ", which is not a null-terminated string"; 9710b57cec5SDimitry Andric else 9720b57cec5SDimitry Andric os << "not a null-terminated string"; 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric emitNotCStringBug(C, state, Ex, os.str()); 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric return UndefinedVal(); 9770b57cec5SDimitry Andric } 9780b57cec5SDimitry Andric } 9790b57cec5SDimitry Andric 9800b57cec5SDimitry Andric const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, 9810b57cec5SDimitry Andric ProgramStateRef &state, const Expr *expr, SVal val) const { 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric // Get the memory region pointed to by the val. 9840b57cec5SDimitry Andric const MemRegion *bufRegion = val.getAsRegion(); 9850b57cec5SDimitry Andric if (!bufRegion) 9860b57cec5SDimitry Andric return nullptr; 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric // Strip casts off the memory region. 9890b57cec5SDimitry Andric bufRegion = bufRegion->StripCasts(); 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric // Cast the memory region to a string region. 9920b57cec5SDimitry Andric const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); 9930b57cec5SDimitry Andric if (!strRegion) 9940b57cec5SDimitry Andric return nullptr; 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric // Return the actual string in the string region. 9970b57cec5SDimitry Andric return strRegion->getStringLiteral(); 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric 100006c3fb27SDimitry Andric bool CStringChecker::isFirstBufInBound(CheckerContext &C, ProgramStateRef State, 100106c3fb27SDimitry Andric SVal BufVal, QualType BufTy, 100206c3fb27SDimitry Andric SVal LengthVal, QualType LengthTy) { 10030b57cec5SDimitry Andric // If we do not know that the buffer is long enough we return 'true'. 10040b57cec5SDimitry Andric // Otherwise the parent region of this field region would also get 10050b57cec5SDimitry Andric // invalidated, which would lead to warnings based on an unknown state. 10060b57cec5SDimitry Andric 100706c3fb27SDimitry Andric if (LengthVal.isUnknown()) 100806c3fb27SDimitry Andric return false; 100906c3fb27SDimitry Andric 10100b57cec5SDimitry Andric // Originally copied from CheckBufferAccess and CheckLocation. 101106c3fb27SDimitry Andric SValBuilder &SB = C.getSValBuilder(); 101206c3fb27SDimitry Andric ASTContext &Ctx = C.getASTContext(); 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); 10150b57cec5SDimitry Andric 1016bdd1243dSDimitry Andric std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 10170b57cec5SDimitry Andric if (!Length) 10180b57cec5SDimitry Andric return true; // cf top comment. 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric // Compute the offset of the last element to be accessed: size-1. 102106c3fb27SDimitry Andric NonLoc One = SB.makeIntVal(1, LengthTy).castAs<NonLoc>(); 102206c3fb27SDimitry Andric SVal Offset = SB.evalBinOpNN(State, BO_Sub, *Length, One, LengthTy); 10230b57cec5SDimitry Andric if (Offset.isUnknown()) 10240b57cec5SDimitry Andric return true; // cf top comment 10250b57cec5SDimitry Andric NonLoc LastOffset = Offset.castAs<NonLoc>(); 10260b57cec5SDimitry Andric 10270b57cec5SDimitry Andric // Check that the first buffer is sufficiently long. 102806c3fb27SDimitry Andric SVal BufStart = SB.evalCast(BufVal, PtrTy, BufTy); 1029bdd1243dSDimitry Andric std::optional<Loc> BufLoc = BufStart.getAs<Loc>(); 10300b57cec5SDimitry Andric if (!BufLoc) 10310b57cec5SDimitry Andric return true; // cf top comment. 10320b57cec5SDimitry Andric 103306c3fb27SDimitry Andric SVal BufEnd = SB.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy); 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric // Check for out of bound array element access. 10360b57cec5SDimitry Andric const MemRegion *R = BufEnd.getAsRegion(); 10370b57cec5SDimitry Andric if (!R) 10380b57cec5SDimitry Andric return true; // cf top comment. 10390b57cec5SDimitry Andric 10400b57cec5SDimitry Andric const ElementRegion *ER = dyn_cast<ElementRegion>(R); 10410b57cec5SDimitry Andric if (!ER) 10420b57cec5SDimitry Andric return true; // cf top comment. 10430b57cec5SDimitry Andric 10440b57cec5SDimitry Andric // FIXME: Does this crash when a non-standard definition 10450b57cec5SDimitry Andric // of a library function is encountered? 10460b57cec5SDimitry Andric assert(ER->getValueType() == C.getASTContext().CharTy && 104706c3fb27SDimitry Andric "isFirstBufInBound should only be called with char* ElementRegions"); 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric // Get the size of the array. 10500b57cec5SDimitry Andric const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); 105106c3fb27SDimitry Andric DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, superReg, SB); 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric // Get the index of the accessed element. 10540b57cec5SDimitry Andric DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); 10550b57cec5SDimitry Andric 105606c3fb27SDimitry Andric ProgramStateRef StInBound = State->assumeInBound(Idx, SizeDV, true); 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andric return static_cast<bool>(StInBound); 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric 106106c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateDestinationBufferBySize( 106206c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV, 106306c3fb27SDimitry Andric SVal SizeV, QualType SizeTy) { 106406c3fb27SDimitry Andric auto InvalidationTraitOperations = 106506c3fb27SDimitry Andric [&C, S, BufTy = BufE->getType(), BufV, SizeV, 106606c3fb27SDimitry Andric SizeTy](RegionAndSymbolInvalidationTraits &ITraits, const MemRegion *R) { 106706c3fb27SDimitry Andric // If destination buffer is a field region and access is in bound, do 106806c3fb27SDimitry Andric // not invalidate its super region. 106906c3fb27SDimitry Andric if (MemRegion::FieldRegionKind == R->getKind() && 107006c3fb27SDimitry Andric isFirstBufInBound(C, S, BufV, BufTy, SizeV, SizeTy)) { 107106c3fb27SDimitry Andric ITraits.setTrait( 107206c3fb27SDimitry Andric R, 107306c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 107406c3fb27SDimitry Andric } 107506c3fb27SDimitry Andric return false; 107606c3fb27SDimitry Andric }; 107706c3fb27SDimitry Andric 107806c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 107906c3fb27SDimitry Andric } 108006c3fb27SDimitry Andric 108106c3fb27SDimitry Andric ProgramStateRef 108206c3fb27SDimitry Andric CStringChecker::invalidateDestinationBufferAlwaysEscapeSuperRegion( 108306c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV) { 108406c3fb27SDimitry Andric auto InvalidationTraitOperations = [](RegionAndSymbolInvalidationTraits &, 108506c3fb27SDimitry Andric const MemRegion *R) { 108606c3fb27SDimitry Andric return isa<FieldRegion>(R); 108706c3fb27SDimitry Andric }; 108806c3fb27SDimitry Andric 108906c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 109006c3fb27SDimitry Andric } 109106c3fb27SDimitry Andric 109206c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateDestinationBufferNeverOverflows( 109306c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV) { 109406c3fb27SDimitry Andric auto InvalidationTraitOperations = 109506c3fb27SDimitry Andric [](RegionAndSymbolInvalidationTraits &ITraits, const MemRegion *R) { 109606c3fb27SDimitry Andric if (MemRegion::FieldRegionKind == R->getKind()) 109706c3fb27SDimitry Andric ITraits.setTrait( 109806c3fb27SDimitry Andric R, 109906c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 110006c3fb27SDimitry Andric return false; 110106c3fb27SDimitry Andric }; 110206c3fb27SDimitry Andric 110306c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 110406c3fb27SDimitry Andric } 110506c3fb27SDimitry Andric 110606c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateSourceBuffer(CheckerContext &C, 110706c3fb27SDimitry Andric ProgramStateRef S, 110806c3fb27SDimitry Andric const Expr *BufE, 110906c3fb27SDimitry Andric SVal BufV) { 111006c3fb27SDimitry Andric auto InvalidationTraitOperations = 111106c3fb27SDimitry Andric [](RegionAndSymbolInvalidationTraits &ITraits, const MemRegion *R) { 111206c3fb27SDimitry Andric ITraits.setTrait( 111306c3fb27SDimitry Andric R->getBaseRegion(), 111406c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_PreserveContents); 111506c3fb27SDimitry Andric ITraits.setTrait(R, 111606c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_SuppressEscape); 111706c3fb27SDimitry Andric return true; 111806c3fb27SDimitry Andric }; 111906c3fb27SDimitry Andric 112006c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 112106c3fb27SDimitry Andric } 112206c3fb27SDimitry Andric 112306c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateBufferAux( 112406c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef State, const Expr *E, SVal V, 112506c3fb27SDimitry Andric llvm::function_ref<bool(RegionAndSymbolInvalidationTraits &, 112606c3fb27SDimitry Andric const MemRegion *)> 112706c3fb27SDimitry Andric InvalidationTraitOperations) { 1128bdd1243dSDimitry Andric std::optional<Loc> L = V.getAs<Loc>(); 11290b57cec5SDimitry Andric if (!L) 113006c3fb27SDimitry Andric return State; 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes 11330b57cec5SDimitry Andric // some assumptions about the value that CFRefCount can't. Even so, it should 11340b57cec5SDimitry Andric // probably be refactored. 1135bdd1243dSDimitry Andric if (std::optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) { 11360b57cec5SDimitry Andric const MemRegion *R = MR->getRegion()->StripCasts(); 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric // Are we dealing with an ElementRegion? If so, we should be invalidating 11390b57cec5SDimitry Andric // the super-region. 11400b57cec5SDimitry Andric if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 11410b57cec5SDimitry Andric R = ER->getSuperRegion(); 11420b57cec5SDimitry Andric // FIXME: What about layers of ElementRegions? 11430b57cec5SDimitry Andric } 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric // Invalidate this region. 11460b57cec5SDimitry Andric const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); 11470b57cec5SDimitry Andric RegionAndSymbolInvalidationTraits ITraits; 114806c3fb27SDimitry Andric bool CausesPointerEscape = InvalidationTraitOperations(ITraits, R); 11490b57cec5SDimitry Andric 115006c3fb27SDimitry Andric return State->invalidateRegions(R, E, C.blockCount(), LCtx, 11510b57cec5SDimitry Andric CausesPointerEscape, nullptr, nullptr, 11520b57cec5SDimitry Andric &ITraits); 11530b57cec5SDimitry Andric } 11540b57cec5SDimitry Andric 11550b57cec5SDimitry Andric // If we have a non-region value by chance, just remove the binding. 11560b57cec5SDimitry Andric // FIXME: is this necessary or correct? This handles the non-Region 11570b57cec5SDimitry Andric // cases. Is it ever valid to store to these? 115806c3fb27SDimitry Andric return State->killBinding(*L); 11590b57cec5SDimitry Andric } 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx, 11620b57cec5SDimitry Andric const MemRegion *MR) { 11630b57cec5SDimitry Andric switch (MR->getKind()) { 11640b57cec5SDimitry Andric case MemRegion::FunctionCodeRegionKind: { 1165480093f4SDimitry Andric if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl()) 11660b57cec5SDimitry Andric os << "the address of the function '" << *FD << '\''; 11670b57cec5SDimitry Andric else 11680b57cec5SDimitry Andric os << "the address of a function"; 11690b57cec5SDimitry Andric return true; 11700b57cec5SDimitry Andric } 11710b57cec5SDimitry Andric case MemRegion::BlockCodeRegionKind: 11720b57cec5SDimitry Andric os << "block text"; 11730b57cec5SDimitry Andric return true; 11740b57cec5SDimitry Andric case MemRegion::BlockDataRegionKind: 11750b57cec5SDimitry Andric os << "a block"; 11760b57cec5SDimitry Andric return true; 11770b57cec5SDimitry Andric case MemRegion::CXXThisRegionKind: 11780b57cec5SDimitry Andric case MemRegion::CXXTempObjectRegionKind: 1179480093f4SDimitry Andric os << "a C++ temp object of type " 118081ad6265SDimitry Andric << cast<TypedValueRegion>(MR)->getValueType(); 11810b57cec5SDimitry Andric return true; 11825ffd83dbSDimitry Andric case MemRegion::NonParamVarRegionKind: 118381ad6265SDimitry Andric os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType(); 11840b57cec5SDimitry Andric return true; 11855ffd83dbSDimitry Andric case MemRegion::ParamVarRegionKind: 118681ad6265SDimitry Andric os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType(); 11875ffd83dbSDimitry Andric return true; 11880b57cec5SDimitry Andric case MemRegion::FieldRegionKind: 118981ad6265SDimitry Andric os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType(); 11900b57cec5SDimitry Andric return true; 11910b57cec5SDimitry Andric case MemRegion::ObjCIvarRegionKind: 1192480093f4SDimitry Andric os << "an instance variable of type " 119381ad6265SDimitry Andric << cast<TypedValueRegion>(MR)->getValueType(); 11940b57cec5SDimitry Andric return true; 11950b57cec5SDimitry Andric default: 11960b57cec5SDimitry Andric return false; 11970b57cec5SDimitry Andric } 11980b57cec5SDimitry Andric } 11990b57cec5SDimitry Andric 12000b57cec5SDimitry Andric bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal, 12010b57cec5SDimitry Andric const Expr *Size, CheckerContext &C, 12020b57cec5SDimitry Andric ProgramStateRef &State) { 12030b57cec5SDimitry Andric SVal MemVal = C.getSVal(DstBuffer); 12040b57cec5SDimitry Andric SVal SizeVal = C.getSVal(Size); 12050b57cec5SDimitry Andric const MemRegion *MR = MemVal.getAsRegion(); 12060b57cec5SDimitry Andric if (!MR) 12070b57cec5SDimitry Andric return false; 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric // We're about to model memset by producing a "default binding" in the Store. 12100b57cec5SDimitry Andric // Our current implementation - RegionStore - doesn't support default bindings 12110b57cec5SDimitry Andric // that don't cover the whole base region. So we should first get the offset 12120b57cec5SDimitry Andric // and the base region to figure out whether the offset of buffer is 0. 12130b57cec5SDimitry Andric RegionOffset Offset = MR->getAsOffset(); 12140b57cec5SDimitry Andric const MemRegion *BR = Offset.getRegion(); 12150b57cec5SDimitry Andric 1216bdd1243dSDimitry Andric std::optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>(); 12170b57cec5SDimitry Andric if (!SizeNL) 12180b57cec5SDimitry Andric return false; 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 12210b57cec5SDimitry Andric ASTContext &Ctx = C.getASTContext(); 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric // void *memset(void *dest, int ch, size_t count); 12240b57cec5SDimitry Andric // For now we can only handle the case of offset is 0 and concrete char value. 12250b57cec5SDimitry Andric if (Offset.isValid() && !Offset.hasSymbolicOffset() && 12260b57cec5SDimitry Andric Offset.getOffset() == 0) { 12275ffd83dbSDimitry Andric // Get the base region's size. 1228fe6060f1SDimitry Andric DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder); 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric ProgramStateRef StateWholeReg, StateNotWholeReg; 12310b57cec5SDimitry Andric std::tie(StateWholeReg, StateNotWholeReg) = 12325ffd83dbSDimitry Andric State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL)); 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric // With the semantic of 'memset()', we should convert the CharVal to 12350b57cec5SDimitry Andric // unsigned char. 12360b57cec5SDimitry Andric CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy); 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric ProgramStateRef StateNullChar, StateNonNullChar; 12390b57cec5SDimitry Andric std::tie(StateNullChar, StateNonNullChar) = 12400b57cec5SDimitry Andric assumeZero(C, State, CharVal, Ctx.UnsignedCharTy); 12410b57cec5SDimitry Andric 12420b57cec5SDimitry Andric if (StateWholeReg && !StateNotWholeReg && StateNullChar && 12430b57cec5SDimitry Andric !StateNonNullChar) { 12440b57cec5SDimitry Andric // If the 'memset()' acts on the whole region of destination buffer and 12450b57cec5SDimitry Andric // the value of the second argument of 'memset()' is zero, bind the second 12460b57cec5SDimitry Andric // argument's value to the destination buffer with 'default binding'. 12470b57cec5SDimitry Andric // FIXME: Since there is no perfect way to bind the non-zero character, we 12480b57cec5SDimitry Andric // can only deal with zero value here. In the future, we need to deal with 12490b57cec5SDimitry Andric // the binding of non-zero value in the case of whole region. 12500b57cec5SDimitry Andric State = State->bindDefaultZero(svalBuilder.makeLoc(BR), 12510b57cec5SDimitry Andric C.getLocationContext()); 12520b57cec5SDimitry Andric } else { 12530b57cec5SDimitry Andric // If the destination buffer's extent is not equal to the value of 12540b57cec5SDimitry Andric // third argument, just invalidate buffer. 125506c3fb27SDimitry Andric State = invalidateDestinationBufferBySize(C, State, DstBuffer, MemVal, 125606c3fb27SDimitry Andric SizeVal, Size->getType()); 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric if (StateNullChar && !StateNonNullChar) { 12600b57cec5SDimitry Andric // If the value of the second argument of 'memset()' is zero, set the 12610b57cec5SDimitry Andric // string length of destination buffer to 0 directly. 12620b57cec5SDimitry Andric State = setCStringLength(State, MR, 12630b57cec5SDimitry Andric svalBuilder.makeZeroVal(Ctx.getSizeType())); 12640b57cec5SDimitry Andric } else if (!StateNullChar && StateNonNullChar) { 12650b57cec5SDimitry Andric SVal NewStrLen = svalBuilder.getMetadataSymbolVal( 12660b57cec5SDimitry Andric CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(), 12670b57cec5SDimitry Andric C.getLocationContext(), C.blockCount()); 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric // If the value of second argument is not zero, then the string length 12700b57cec5SDimitry Andric // is at least the size argument. 12710b57cec5SDimitry Andric SVal NewStrLenGESize = svalBuilder.evalBinOp( 12720b57cec5SDimitry Andric State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType()); 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andric State = setCStringLength( 12750b57cec5SDimitry Andric State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true), 12760b57cec5SDimitry Andric MR, NewStrLen); 12770b57cec5SDimitry Andric } 12780b57cec5SDimitry Andric } else { 12790b57cec5SDimitry Andric // If the offset is not zero and char value is not concrete, we can do 12800b57cec5SDimitry Andric // nothing but invalidate the buffer. 128106c3fb27SDimitry Andric State = invalidateDestinationBufferBySize(C, State, DstBuffer, MemVal, 128206c3fb27SDimitry Andric SizeVal, Size->getType()); 12830b57cec5SDimitry Andric } 12840b57cec5SDimitry Andric return true; 12850b57cec5SDimitry Andric } 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12880b57cec5SDimitry Andric // evaluation of individual function calls. 12890b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12900b57cec5SDimitry Andric 1291*647cbc5dSDimitry Andric void CStringChecker::evalCopyCommon(CheckerContext &C, const CallEvent &Call, 12925ffd83dbSDimitry Andric ProgramStateRef state, SizeArgExpr Size, 12935ffd83dbSDimitry Andric DestinationArgExpr Dest, 12945ffd83dbSDimitry Andric SourceArgExpr Source, bool Restricted, 1295bdd1243dSDimitry Andric bool IsMempcpy, CharKind CK) const { 12960b57cec5SDimitry Andric CurrentFunctionDescription = "memory copy function"; 12970b57cec5SDimitry Andric 12980b57cec5SDimitry Andric // See if the size argument is zero. 12990b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 13005ffd83dbSDimitry Andric SVal sizeVal = state->getSVal(Size.Expression, LCtx); 13015ffd83dbSDimitry Andric QualType sizeTy = Size.Expression->getType(); 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric ProgramStateRef stateZeroSize, stateNonZeroSize; 13040b57cec5SDimitry Andric std::tie(stateZeroSize, stateNonZeroSize) = 13050b57cec5SDimitry Andric assumeZero(C, state, sizeVal, sizeTy); 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andric // Get the value of the Dest. 13085ffd83dbSDimitry Andric SVal destVal = state->getSVal(Dest.Expression, LCtx); 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric // If the size is zero, there won't be any actual memory access, so 13110b57cec5SDimitry Andric // just bind the return value to the destination buffer and return. 13120b57cec5SDimitry Andric if (stateZeroSize && !stateNonZeroSize) { 1313*647cbc5dSDimitry Andric stateZeroSize = 1314*647cbc5dSDimitry Andric stateZeroSize->BindExpr(Call.getOriginExpr(), LCtx, destVal); 13150b57cec5SDimitry Andric C.addTransition(stateZeroSize); 13160b57cec5SDimitry Andric return; 13170b57cec5SDimitry Andric } 13180b57cec5SDimitry Andric 13190b57cec5SDimitry Andric // If the size can be nonzero, we have to check the other arguments. 13200b57cec5SDimitry Andric if (stateNonZeroSize) { 13210b57cec5SDimitry Andric state = stateNonZeroSize; 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andric // Ensure the destination is not null. If it is NULL there will be a 13240b57cec5SDimitry Andric // NULL pointer dereference. 13255ffd83dbSDimitry Andric state = checkNonNull(C, state, Dest, destVal); 13260b57cec5SDimitry Andric if (!state) 13270b57cec5SDimitry Andric return; 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric // Get the value of the Src. 13305ffd83dbSDimitry Andric SVal srcVal = state->getSVal(Source.Expression, LCtx); 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric // Ensure the source is not null. If it is NULL there will be a 13330b57cec5SDimitry Andric // NULL pointer dereference. 13345ffd83dbSDimitry Andric state = checkNonNull(C, state, Source, srcVal); 13350b57cec5SDimitry Andric if (!state) 13360b57cec5SDimitry Andric return; 13370b57cec5SDimitry Andric 13380b57cec5SDimitry Andric // Ensure the accesses are valid and that the buffers do not overlap. 1339bdd1243dSDimitry Andric state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write, CK); 1340bdd1243dSDimitry Andric state = CheckBufferAccess(C, state, Source, Size, AccessKind::read, CK); 13415ffd83dbSDimitry Andric 13420b57cec5SDimitry Andric if (Restricted) 1343bdd1243dSDimitry Andric state = CheckOverlap(C, state, Size, Dest, Source, CK); 13440b57cec5SDimitry Andric 13450b57cec5SDimitry Andric if (!state) 13460b57cec5SDimitry Andric return; 13470b57cec5SDimitry Andric 13480b57cec5SDimitry Andric // If this is mempcpy, get the byte after the last byte copied and 13490b57cec5SDimitry Andric // bind the expr. 13500b57cec5SDimitry Andric if (IsMempcpy) { 13510b57cec5SDimitry Andric // Get the byte after the last byte copied. 13520b57cec5SDimitry Andric SValBuilder &SvalBuilder = C.getSValBuilder(); 13530b57cec5SDimitry Andric ASTContext &Ctx = SvalBuilder.getContext(); 1354bdd1243dSDimitry Andric QualType CharPtrTy = getCharPtrType(Ctx, CK); 13550b57cec5SDimitry Andric SVal DestRegCharVal = 13565ffd83dbSDimitry Andric SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType()); 13570b57cec5SDimitry Andric SVal lastElement = C.getSValBuilder().evalBinOp( 13585ffd83dbSDimitry Andric state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType()); 13590b57cec5SDimitry Andric // If we don't know how much we copied, we can at least 13600b57cec5SDimitry Andric // conjure a return value for later. 13610b57cec5SDimitry Andric if (lastElement.isUnknown()) 1362*647cbc5dSDimitry Andric lastElement = C.getSValBuilder().conjureSymbolVal( 1363*647cbc5dSDimitry Andric nullptr, Call.getOriginExpr(), LCtx, C.blockCount()); 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric // The byte after the last byte copied is the return value. 1366*647cbc5dSDimitry Andric state = state->BindExpr(Call.getOriginExpr(), LCtx, lastElement); 13670b57cec5SDimitry Andric } else { 13680b57cec5SDimitry Andric // All other copies return the destination buffer. 13690b57cec5SDimitry Andric // (Well, bcopy() has a void return type, but this won't hurt.) 1370*647cbc5dSDimitry Andric state = state->BindExpr(Call.getOriginExpr(), LCtx, destVal); 13710b57cec5SDimitry Andric } 13720b57cec5SDimitry Andric 13730b57cec5SDimitry Andric // Invalidate the destination (regular invalidation without pointer-escaping 13740b57cec5SDimitry Andric // the address of the top-level region). 13750b57cec5SDimitry Andric // FIXME: Even if we can't perfectly model the copy, we should see if we 13760b57cec5SDimitry Andric // can use LazyCompoundVals to copy the source values into the destination. 13770b57cec5SDimitry Andric // This would probably remove any existing bindings past the end of the 13780b57cec5SDimitry Andric // copied region, but that's still an improvement over blank invalidation. 137906c3fb27SDimitry Andric state = invalidateDestinationBufferBySize( 138006c3fb27SDimitry Andric C, state, Dest.Expression, C.getSVal(Dest.Expression), sizeVal, 138106c3fb27SDimitry Andric Size.Expression->getType()); 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric // Invalidate the source (const-invalidation without const-pointer-escaping 13840b57cec5SDimitry Andric // the address of the top-level region). 138506c3fb27SDimitry Andric state = invalidateSourceBuffer(C, state, Source.Expression, 138606c3fb27SDimitry Andric C.getSVal(Source.Expression)); 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric C.addTransition(state); 13890b57cec5SDimitry Andric } 13900b57cec5SDimitry Andric } 13910b57cec5SDimitry Andric 1392*647cbc5dSDimitry Andric void CStringChecker::evalMemcpy(CheckerContext &C, const CallEvent &Call, 1393bdd1243dSDimitry Andric CharKind CK) const { 13940b57cec5SDimitry Andric // void *memcpy(void *restrict dst, const void *restrict src, size_t n); 13950b57cec5SDimitry Andric // The return value is the address of the destination buffer. 1396*647cbc5dSDimitry Andric DestinationArgExpr Dest = {{Call.getArgExpr(0), 0}}; 1397*647cbc5dSDimitry Andric SourceArgExpr Src = {{Call.getArgExpr(1), 1}}; 1398*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(2), 2}}; 13990b57cec5SDimitry Andric 14005ffd83dbSDimitry Andric ProgramStateRef State = C.getState(); 14015ffd83dbSDimitry Andric 14025ffd83dbSDimitry Andric constexpr bool IsRestricted = true; 14035ffd83dbSDimitry Andric constexpr bool IsMempcpy = false; 1404*647cbc5dSDimitry Andric evalCopyCommon(C, Call, State, Size, Dest, Src, IsRestricted, IsMempcpy, CK); 14050b57cec5SDimitry Andric } 14060b57cec5SDimitry Andric 1407*647cbc5dSDimitry Andric void CStringChecker::evalMempcpy(CheckerContext &C, const CallEvent &Call, 1408bdd1243dSDimitry Andric CharKind CK) const { 14090b57cec5SDimitry Andric // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); 14100b57cec5SDimitry Andric // The return value is a pointer to the byte following the last written byte. 1411*647cbc5dSDimitry Andric DestinationArgExpr Dest = {{Call.getArgExpr(0), 0}}; 1412*647cbc5dSDimitry Andric SourceArgExpr Src = {{Call.getArgExpr(1), 1}}; 1413*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(2), 2}}; 14140b57cec5SDimitry Andric 14155ffd83dbSDimitry Andric constexpr bool IsRestricted = true; 14165ffd83dbSDimitry Andric constexpr bool IsMempcpy = true; 1417*647cbc5dSDimitry Andric evalCopyCommon(C, Call, C.getState(), Size, Dest, Src, IsRestricted, 1418*647cbc5dSDimitry Andric IsMempcpy, CK); 14190b57cec5SDimitry Andric } 14200b57cec5SDimitry Andric 1421*647cbc5dSDimitry Andric void CStringChecker::evalMemmove(CheckerContext &C, const CallEvent &Call, 1422bdd1243dSDimitry Andric CharKind CK) const { 14230b57cec5SDimitry Andric // void *memmove(void *dst, const void *src, size_t n); 14240b57cec5SDimitry Andric // The return value is the address of the destination buffer. 1425*647cbc5dSDimitry Andric DestinationArgExpr Dest = {{Call.getArgExpr(0), 0}}; 1426*647cbc5dSDimitry Andric SourceArgExpr Src = {{Call.getArgExpr(1), 1}}; 1427*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(2), 2}}; 14280b57cec5SDimitry Andric 14295ffd83dbSDimitry Andric constexpr bool IsRestricted = false; 14305ffd83dbSDimitry Andric constexpr bool IsMempcpy = false; 1431*647cbc5dSDimitry Andric evalCopyCommon(C, Call, C.getState(), Size, Dest, Src, IsRestricted, 1432*647cbc5dSDimitry Andric IsMempcpy, CK); 14330b57cec5SDimitry Andric } 14340b57cec5SDimitry Andric 1435*647cbc5dSDimitry Andric void CStringChecker::evalBcopy(CheckerContext &C, const CallEvent &Call) const { 14360b57cec5SDimitry Andric // void bcopy(const void *src, void *dst, size_t n); 1437*647cbc5dSDimitry Andric SourceArgExpr Src{{Call.getArgExpr(0), 0}}; 1438*647cbc5dSDimitry Andric DestinationArgExpr Dest = {{Call.getArgExpr(1), 1}}; 1439*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(2), 2}}; 14405ffd83dbSDimitry Andric 14415ffd83dbSDimitry Andric constexpr bool IsRestricted = false; 14425ffd83dbSDimitry Andric constexpr bool IsMempcpy = false; 1443*647cbc5dSDimitry Andric evalCopyCommon(C, Call, C.getState(), Size, Dest, Src, IsRestricted, 1444*647cbc5dSDimitry Andric IsMempcpy, CharKind::Regular); 14450b57cec5SDimitry Andric } 14460b57cec5SDimitry Andric 1447*647cbc5dSDimitry Andric void CStringChecker::evalMemcmp(CheckerContext &C, const CallEvent &Call, 1448bdd1243dSDimitry Andric CharKind CK) const { 14490b57cec5SDimitry Andric // int memcmp(const void *s1, const void *s2, size_t n); 14500b57cec5SDimitry Andric CurrentFunctionDescription = "memory comparison function"; 14510b57cec5SDimitry Andric 1452*647cbc5dSDimitry Andric AnyArgExpr Left = {Call.getArgExpr(0), 0}; 1453*647cbc5dSDimitry Andric AnyArgExpr Right = {Call.getArgExpr(1), 1}; 1454*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(2), 2}}; 14550b57cec5SDimitry Andric 14565ffd83dbSDimitry Andric ProgramStateRef State = C.getState(); 14575ffd83dbSDimitry Andric SValBuilder &Builder = C.getSValBuilder(); 14585ffd83dbSDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 14590b57cec5SDimitry Andric 14600b57cec5SDimitry Andric // See if the size argument is zero. 14615ffd83dbSDimitry Andric SVal sizeVal = State->getSVal(Size.Expression, LCtx); 14625ffd83dbSDimitry Andric QualType sizeTy = Size.Expression->getType(); 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric ProgramStateRef stateZeroSize, stateNonZeroSize; 14650b57cec5SDimitry Andric std::tie(stateZeroSize, stateNonZeroSize) = 14665ffd83dbSDimitry Andric assumeZero(C, State, sizeVal, sizeTy); 14670b57cec5SDimitry Andric 14680b57cec5SDimitry Andric // If the size can be zero, the result will be 0 in that case, and we don't 14690b57cec5SDimitry Andric // have to check either of the buffers. 14700b57cec5SDimitry Andric if (stateZeroSize) { 14715ffd83dbSDimitry Andric State = stateZeroSize; 1472*647cbc5dSDimitry Andric State = State->BindExpr(Call.getOriginExpr(), LCtx, 1473*647cbc5dSDimitry Andric Builder.makeZeroVal(Call.getResultType())); 14745ffd83dbSDimitry Andric C.addTransition(State); 14750b57cec5SDimitry Andric } 14760b57cec5SDimitry Andric 14770b57cec5SDimitry Andric // If the size can be nonzero, we have to check the other arguments. 14780b57cec5SDimitry Andric if (stateNonZeroSize) { 14795ffd83dbSDimitry Andric State = stateNonZeroSize; 14800b57cec5SDimitry Andric // If we know the two buffers are the same, we know the result is 0. 14810b57cec5SDimitry Andric // First, get the two buffers' addresses. Another checker will have already 14820b57cec5SDimitry Andric // made sure they're not undefined. 14830b57cec5SDimitry Andric DefinedOrUnknownSVal LV = 14845ffd83dbSDimitry Andric State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>(); 14850b57cec5SDimitry Andric DefinedOrUnknownSVal RV = 14865ffd83dbSDimitry Andric State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>(); 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric // See if they are the same. 14895ffd83dbSDimitry Andric ProgramStateRef SameBuffer, NotSameBuffer; 14905ffd83dbSDimitry Andric std::tie(SameBuffer, NotSameBuffer) = 14915ffd83dbSDimitry Andric State->assume(Builder.evalEQ(State, LV, RV)); 14920b57cec5SDimitry Andric 1493480093f4SDimitry Andric // If the two arguments are the same buffer, we know the result is 0, 14940b57cec5SDimitry Andric // and we only need to check one size. 14955ffd83dbSDimitry Andric if (SameBuffer && !NotSameBuffer) { 14965ffd83dbSDimitry Andric State = SameBuffer; 14975ffd83dbSDimitry Andric State = CheckBufferAccess(C, State, Left, Size, AccessKind::read); 14985ffd83dbSDimitry Andric if (State) { 1499*647cbc5dSDimitry Andric State = SameBuffer->BindExpr(Call.getOriginExpr(), LCtx, 1500*647cbc5dSDimitry Andric Builder.makeZeroVal(Call.getResultType())); 15015ffd83dbSDimitry Andric C.addTransition(State); 15020b57cec5SDimitry Andric } 1503480093f4SDimitry Andric return; 15040b57cec5SDimitry Andric } 15050b57cec5SDimitry Andric 1506480093f4SDimitry Andric // If the two arguments might be different buffers, we have to check 1507480093f4SDimitry Andric // the size of both of them. 15085ffd83dbSDimitry Andric assert(NotSameBuffer); 1509bdd1243dSDimitry Andric State = CheckBufferAccess(C, State, Right, Size, AccessKind::read, CK); 1510bdd1243dSDimitry Andric State = CheckBufferAccess(C, State, Left, Size, AccessKind::read, CK); 15115ffd83dbSDimitry Andric if (State) { 15120b57cec5SDimitry Andric // The return value is the comparison result, which we don't know. 1513*647cbc5dSDimitry Andric SVal CmpV = Builder.conjureSymbolVal(nullptr, Call.getOriginExpr(), LCtx, 1514*647cbc5dSDimitry Andric C.blockCount()); 1515*647cbc5dSDimitry Andric State = State->BindExpr(Call.getOriginExpr(), LCtx, CmpV); 15165ffd83dbSDimitry Andric C.addTransition(State); 15170b57cec5SDimitry Andric } 15180b57cec5SDimitry Andric } 15190b57cec5SDimitry Andric } 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric void CStringChecker::evalstrLength(CheckerContext &C, 1522*647cbc5dSDimitry Andric const CallEvent &Call) const { 15230b57cec5SDimitry Andric // size_t strlen(const char *s); 1524*647cbc5dSDimitry Andric evalstrLengthCommon(C, Call, /* IsStrnlen = */ false); 15250b57cec5SDimitry Andric } 15260b57cec5SDimitry Andric 15270b57cec5SDimitry Andric void CStringChecker::evalstrnLength(CheckerContext &C, 1528*647cbc5dSDimitry Andric const CallEvent &Call) const { 15290b57cec5SDimitry Andric // size_t strnlen(const char *s, size_t maxlen); 1530*647cbc5dSDimitry Andric evalstrLengthCommon(C, Call, /* IsStrnlen = */ true); 15310b57cec5SDimitry Andric } 15320b57cec5SDimitry Andric 1533*647cbc5dSDimitry Andric void CStringChecker::evalstrLengthCommon(CheckerContext &C, 1534*647cbc5dSDimitry Andric const CallEvent &Call, 15350b57cec5SDimitry Andric bool IsStrnlen) const { 15360b57cec5SDimitry Andric CurrentFunctionDescription = "string length function"; 15370b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 15380b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 15390b57cec5SDimitry Andric 15400b57cec5SDimitry Andric if (IsStrnlen) { 1541*647cbc5dSDimitry Andric const Expr *maxlenExpr = Call.getArgExpr(1); 15420b57cec5SDimitry Andric SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); 15430b57cec5SDimitry Andric 15440b57cec5SDimitry Andric ProgramStateRef stateZeroSize, stateNonZeroSize; 15450b57cec5SDimitry Andric std::tie(stateZeroSize, stateNonZeroSize) = 15460b57cec5SDimitry Andric assumeZero(C, state, maxlenVal, maxlenExpr->getType()); 15470b57cec5SDimitry Andric 15480b57cec5SDimitry Andric // If the size can be zero, the result will be 0 in that case, and we don't 15490b57cec5SDimitry Andric // have to check the string itself. 15500b57cec5SDimitry Andric if (stateZeroSize) { 1551*647cbc5dSDimitry Andric SVal zero = C.getSValBuilder().makeZeroVal(Call.getResultType()); 1552*647cbc5dSDimitry Andric stateZeroSize = stateZeroSize->BindExpr(Call.getOriginExpr(), LCtx, zero); 15530b57cec5SDimitry Andric C.addTransition(stateZeroSize); 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric // If the size is GUARANTEED to be zero, we're done! 15570b57cec5SDimitry Andric if (!stateNonZeroSize) 15580b57cec5SDimitry Andric return; 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric // Otherwise, record the assumption that the size is nonzero. 15610b57cec5SDimitry Andric state = stateNonZeroSize; 15620b57cec5SDimitry Andric } 15630b57cec5SDimitry Andric 15640b57cec5SDimitry Andric // Check that the string argument is non-null. 1565*647cbc5dSDimitry Andric AnyArgExpr Arg = {Call.getArgExpr(0), 0}; 15665ffd83dbSDimitry Andric SVal ArgVal = state->getSVal(Arg.Expression, LCtx); 15675ffd83dbSDimitry Andric state = checkNonNull(C, state, Arg, ArgVal); 15680b57cec5SDimitry Andric 15690b57cec5SDimitry Andric if (!state) 15700b57cec5SDimitry Andric return; 15710b57cec5SDimitry Andric 15725ffd83dbSDimitry Andric SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal); 15730b57cec5SDimitry Andric 15740b57cec5SDimitry Andric // If the argument isn't a valid C string, there's no valid state to 15750b57cec5SDimitry Andric // transition to. 15760b57cec5SDimitry Andric if (strLength.isUndef()) 15770b57cec5SDimitry Andric return; 15780b57cec5SDimitry Andric 15790b57cec5SDimitry Andric DefinedOrUnknownSVal result = UnknownVal(); 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric // If the check is for strnlen() then bind the return value to no more than 15820b57cec5SDimitry Andric // the maxlen value. 15830b57cec5SDimitry Andric if (IsStrnlen) { 15840b57cec5SDimitry Andric QualType cmpTy = C.getSValBuilder().getConditionType(); 15850b57cec5SDimitry Andric 15860b57cec5SDimitry Andric // It's a little unfortunate to be getting this again, 15870b57cec5SDimitry Andric // but it's not that expensive... 1588*647cbc5dSDimitry Andric const Expr *maxlenExpr = Call.getArgExpr(1); 15890b57cec5SDimitry Andric SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); 15900b57cec5SDimitry Andric 1591bdd1243dSDimitry Andric std::optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); 1592bdd1243dSDimitry Andric std::optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>(); 15930b57cec5SDimitry Andric 15940b57cec5SDimitry Andric if (strLengthNL && maxlenValNL) { 15950b57cec5SDimitry Andric ProgramStateRef stateStringTooLong, stateStringNotTooLong; 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andric // Check if the strLength is greater than the maxlen. 15980b57cec5SDimitry Andric std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume( 15990b57cec5SDimitry Andric C.getSValBuilder() 16000b57cec5SDimitry Andric .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy) 16010b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>()); 16020b57cec5SDimitry Andric 16030b57cec5SDimitry Andric if (stateStringTooLong && !stateStringNotTooLong) { 16040b57cec5SDimitry Andric // If the string is longer than maxlen, return maxlen. 16050b57cec5SDimitry Andric result = *maxlenValNL; 16060b57cec5SDimitry Andric } else if (stateStringNotTooLong && !stateStringTooLong) { 16070b57cec5SDimitry Andric // If the string is shorter than maxlen, return its length. 16080b57cec5SDimitry Andric result = *strLengthNL; 16090b57cec5SDimitry Andric } 16100b57cec5SDimitry Andric } 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric if (result.isUnknown()) { 16130b57cec5SDimitry Andric // If we don't have enough information for a comparison, there's 16140b57cec5SDimitry Andric // no guarantee the full string length will actually be returned. 16150b57cec5SDimitry Andric // All we know is the return value is the min of the string length 16160b57cec5SDimitry Andric // and the limit. This is better than nothing. 1617*647cbc5dSDimitry Andric result = C.getSValBuilder().conjureSymbolVal( 1618*647cbc5dSDimitry Andric nullptr, Call.getOriginExpr(), LCtx, C.blockCount()); 16190b57cec5SDimitry Andric NonLoc resultNL = result.castAs<NonLoc>(); 16200b57cec5SDimitry Andric 16210b57cec5SDimitry Andric if (strLengthNL) { 16220b57cec5SDimitry Andric state = state->assume(C.getSValBuilder().evalBinOpNN( 16230b57cec5SDimitry Andric state, BO_LE, resultNL, *strLengthNL, cmpTy) 16240b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>(), true); 16250b57cec5SDimitry Andric } 16260b57cec5SDimitry Andric 16270b57cec5SDimitry Andric if (maxlenValNL) { 16280b57cec5SDimitry Andric state = state->assume(C.getSValBuilder().evalBinOpNN( 16290b57cec5SDimitry Andric state, BO_LE, resultNL, *maxlenValNL, cmpTy) 16300b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>(), true); 16310b57cec5SDimitry Andric } 16320b57cec5SDimitry Andric } 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric } else { 16350b57cec5SDimitry Andric // This is a plain strlen(), not strnlen(). 16360b57cec5SDimitry Andric result = strLength.castAs<DefinedOrUnknownSVal>(); 16370b57cec5SDimitry Andric 16380b57cec5SDimitry Andric // If we don't know the length of the string, conjure a return 16390b57cec5SDimitry Andric // value, so it can be used in constraints, at least. 16400b57cec5SDimitry Andric if (result.isUnknown()) { 1641*647cbc5dSDimitry Andric result = C.getSValBuilder().conjureSymbolVal( 1642*647cbc5dSDimitry Andric nullptr, Call.getOriginExpr(), LCtx, C.blockCount()); 16430b57cec5SDimitry Andric } 16440b57cec5SDimitry Andric } 16450b57cec5SDimitry Andric 16460b57cec5SDimitry Andric // Bind the return value. 16470b57cec5SDimitry Andric assert(!result.isUnknown() && "Should have conjured a value by now"); 1648*647cbc5dSDimitry Andric state = state->BindExpr(Call.getOriginExpr(), LCtx, result); 16490b57cec5SDimitry Andric C.addTransition(state); 16500b57cec5SDimitry Andric } 16510b57cec5SDimitry Andric 1652*647cbc5dSDimitry Andric void CStringChecker::evalStrcpy(CheckerContext &C, 1653*647cbc5dSDimitry Andric const CallEvent &Call) const { 16540b57cec5SDimitry Andric // char *strcpy(char *restrict dst, const char *restrict src); 1655*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1656480093f4SDimitry Andric /* ReturnEnd = */ false, 1657480093f4SDimitry Andric /* IsBounded = */ false, 1658480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none); 16590b57cec5SDimitry Andric } 16600b57cec5SDimitry Andric 1661*647cbc5dSDimitry Andric void CStringChecker::evalStrncpy(CheckerContext &C, 1662*647cbc5dSDimitry Andric const CallEvent &Call) const { 16630b57cec5SDimitry Andric // char *strncpy(char *restrict dst, const char *restrict src, size_t n); 1664*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1665480093f4SDimitry Andric /* ReturnEnd = */ false, 1666480093f4SDimitry Andric /* IsBounded = */ true, 1667480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none); 16680b57cec5SDimitry Andric } 16690b57cec5SDimitry Andric 1670*647cbc5dSDimitry Andric void CStringChecker::evalStpcpy(CheckerContext &C, 1671*647cbc5dSDimitry Andric const CallEvent &Call) const { 16720b57cec5SDimitry Andric // char *stpcpy(char *restrict dst, const char *restrict src); 1673*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1674480093f4SDimitry Andric /* ReturnEnd = */ true, 1675480093f4SDimitry Andric /* IsBounded = */ false, 1676480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none); 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric 1679*647cbc5dSDimitry Andric void CStringChecker::evalStrlcpy(CheckerContext &C, 1680*647cbc5dSDimitry Andric const CallEvent &Call) const { 1681480093f4SDimitry Andric // size_t strlcpy(char *dest, const char *src, size_t size); 1682*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1683480093f4SDimitry Andric /* ReturnEnd = */ true, 1684480093f4SDimitry Andric /* IsBounded = */ true, 1685480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none, 16860b57cec5SDimitry Andric /* returnPtr = */ false); 16870b57cec5SDimitry Andric } 16880b57cec5SDimitry Andric 1689*647cbc5dSDimitry Andric void CStringChecker::evalStrcat(CheckerContext &C, 1690*647cbc5dSDimitry Andric const CallEvent &Call) const { 16910b57cec5SDimitry Andric // char *strcat(char *restrict s1, const char *restrict s2); 1692*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1693480093f4SDimitry Andric /* ReturnEnd = */ false, 1694480093f4SDimitry Andric /* IsBounded = */ false, 1695480093f4SDimitry Andric /* appendK = */ ConcatFnKind::strcat); 16960b57cec5SDimitry Andric } 16970b57cec5SDimitry Andric 1698*647cbc5dSDimitry Andric void CStringChecker::evalStrncat(CheckerContext &C, 1699*647cbc5dSDimitry Andric const CallEvent &Call) const { 17000b57cec5SDimitry Andric // char *strncat(char *restrict s1, const char *restrict s2, size_t n); 1701*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1702480093f4SDimitry Andric /* ReturnEnd = */ false, 1703480093f4SDimitry Andric /* IsBounded = */ true, 1704480093f4SDimitry Andric /* appendK = */ ConcatFnKind::strcat); 17050b57cec5SDimitry Andric } 17060b57cec5SDimitry Andric 1707*647cbc5dSDimitry Andric void CStringChecker::evalStrlcat(CheckerContext &C, 1708*647cbc5dSDimitry Andric const CallEvent &Call) const { 1709480093f4SDimitry Andric // size_t strlcat(char *dst, const char *src, size_t size); 1710480093f4SDimitry Andric // It will append at most size - strlen(dst) - 1 bytes, 1711480093f4SDimitry Andric // NULL-terminating the result. 1712*647cbc5dSDimitry Andric evalStrcpyCommon(C, Call, 1713480093f4SDimitry Andric /* ReturnEnd = */ false, 1714480093f4SDimitry Andric /* IsBounded = */ true, 1715480093f4SDimitry Andric /* appendK = */ ConcatFnKind::strlcat, 17160b57cec5SDimitry Andric /* returnPtr = */ false); 17170b57cec5SDimitry Andric } 17180b57cec5SDimitry Andric 1719*647cbc5dSDimitry Andric void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallEvent &Call, 1720480093f4SDimitry Andric bool ReturnEnd, bool IsBounded, 1721480093f4SDimitry Andric ConcatFnKind appendK, 1722480093f4SDimitry Andric bool returnPtr) const { 1723480093f4SDimitry Andric if (appendK == ConcatFnKind::none) 17240b57cec5SDimitry Andric CurrentFunctionDescription = "string copy function"; 1725480093f4SDimitry Andric else 1726480093f4SDimitry Andric CurrentFunctionDescription = "string concatenation function"; 17275ffd83dbSDimitry Andric 17280b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 17290b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric // Check that the destination is non-null. 1732*647cbc5dSDimitry Andric DestinationArgExpr Dst = {{Call.getArgExpr(0), 0}}; 17335ffd83dbSDimitry Andric SVal DstVal = state->getSVal(Dst.Expression, LCtx); 17345ffd83dbSDimitry Andric state = checkNonNull(C, state, Dst, DstVal); 17350b57cec5SDimitry Andric if (!state) 17360b57cec5SDimitry Andric return; 17370b57cec5SDimitry Andric 17380b57cec5SDimitry Andric // Check that the source is non-null. 1739*647cbc5dSDimitry Andric SourceArgExpr srcExpr = {{Call.getArgExpr(1), 1}}; 17405ffd83dbSDimitry Andric SVal srcVal = state->getSVal(srcExpr.Expression, LCtx); 17415ffd83dbSDimitry Andric state = checkNonNull(C, state, srcExpr, srcVal); 17420b57cec5SDimitry Andric if (!state) 17430b57cec5SDimitry Andric return; 17440b57cec5SDimitry Andric 17450b57cec5SDimitry Andric // Get the string length of the source. 17465ffd83dbSDimitry Andric SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal); 1747bdd1243dSDimitry Andric std::optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); 1748480093f4SDimitry Andric 1749480093f4SDimitry Andric // Get the string length of the destination buffer. 17505ffd83dbSDimitry Andric SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal); 1751bdd1243dSDimitry Andric std::optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>(); 17520b57cec5SDimitry Andric 17530b57cec5SDimitry Andric // If the source isn't a valid C string, give up. 17540b57cec5SDimitry Andric if (strLength.isUndef()) 17550b57cec5SDimitry Andric return; 17560b57cec5SDimitry Andric 17570b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 17580b57cec5SDimitry Andric QualType cmpTy = svalBuilder.getConditionType(); 17590b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric // These two values allow checking two kinds of errors: 17620b57cec5SDimitry Andric // - actual overflows caused by a source that doesn't fit in the destination 17630b57cec5SDimitry Andric // - potential overflows caused by a bound that could exceed the destination 17640b57cec5SDimitry Andric SVal amountCopied = UnknownVal(); 17650b57cec5SDimitry Andric SVal maxLastElementIndex = UnknownVal(); 17660b57cec5SDimitry Andric const char *boundWarning = nullptr; 17670b57cec5SDimitry Andric 17685ffd83dbSDimitry Andric // FIXME: Why do we choose the srcExpr if the access has no size? 17695ffd83dbSDimitry Andric // Note that the 3rd argument of the call would be the size parameter. 177006c3fb27SDimitry Andric SizeArgExpr SrcExprAsSizeDummy = { 177106c3fb27SDimitry Andric {srcExpr.Expression, srcExpr.ArgumentIndex}}; 17725ffd83dbSDimitry Andric state = CheckOverlap( 17735ffd83dbSDimitry Andric C, state, 1774*647cbc5dSDimitry Andric (IsBounded ? SizeArgExpr{{Call.getArgExpr(2), 2}} : SrcExprAsSizeDummy), 1775*647cbc5dSDimitry Andric Dst, srcExpr); 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric if (!state) 17780b57cec5SDimitry Andric return; 17790b57cec5SDimitry Andric 17800b57cec5SDimitry Andric // If the function is strncpy, strncat, etc... it is bounded. 1781480093f4SDimitry Andric if (IsBounded) { 17820b57cec5SDimitry Andric // Get the max number of characters to copy. 1783*647cbc5dSDimitry Andric SizeArgExpr lenExpr = {{Call.getArgExpr(2), 2}}; 17845ffd83dbSDimitry Andric SVal lenVal = state->getSVal(lenExpr.Expression, LCtx); 17850b57cec5SDimitry Andric 17860b57cec5SDimitry Andric // Protect against misdeclared strncpy(). 17875ffd83dbSDimitry Andric lenVal = 17885ffd83dbSDimitry Andric svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType()); 17890b57cec5SDimitry Andric 1790bdd1243dSDimitry Andric std::optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>(); 17910b57cec5SDimitry Andric 17920b57cec5SDimitry Andric // If we know both values, we might be able to figure out how much 17930b57cec5SDimitry Andric // we're copying. 17940b57cec5SDimitry Andric if (strLengthNL && lenValNL) { 1795480093f4SDimitry Andric switch (appendK) { 1796480093f4SDimitry Andric case ConcatFnKind::none: 1797480093f4SDimitry Andric case ConcatFnKind::strcat: { 17980b57cec5SDimitry Andric ProgramStateRef stateSourceTooLong, stateSourceNotTooLong; 17990b57cec5SDimitry Andric // Check if the max number to copy is less than the length of the src. 18000b57cec5SDimitry Andric // If the bound is equal to the source length, strncpy won't null- 18010b57cec5SDimitry Andric // terminate the result! 18020b57cec5SDimitry Andric std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume( 1803480093f4SDimitry Andric svalBuilder 1804480093f4SDimitry Andric .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy) 18050b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>()); 18060b57cec5SDimitry Andric 18070b57cec5SDimitry Andric if (stateSourceTooLong && !stateSourceNotTooLong) { 1808480093f4SDimitry Andric // Max number to copy is less than the length of the src, so the 1809480093f4SDimitry Andric // actual strLength copied is the max number arg. 18100b57cec5SDimitry Andric state = stateSourceTooLong; 18110b57cec5SDimitry Andric amountCopied = lenVal; 18120b57cec5SDimitry Andric 18130b57cec5SDimitry Andric } else if (!stateSourceTooLong && stateSourceNotTooLong) { 18140b57cec5SDimitry Andric // The source buffer entirely fits in the bound. 18150b57cec5SDimitry Andric state = stateSourceNotTooLong; 18160b57cec5SDimitry Andric amountCopied = strLength; 18170b57cec5SDimitry Andric } 1818480093f4SDimitry Andric break; 1819480093f4SDimitry Andric } 1820480093f4SDimitry Andric case ConcatFnKind::strlcat: 1821480093f4SDimitry Andric if (!dstStrLengthNL) 1822480093f4SDimitry Andric return; 1823480093f4SDimitry Andric 1824480093f4SDimitry Andric // amountCopied = min (size - dstLen - 1 , srcLen) 1825480093f4SDimitry Andric SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, 1826480093f4SDimitry Andric *dstStrLengthNL, sizeTy); 182781ad6265SDimitry Andric if (!isa<NonLoc>(freeSpace)) 1828480093f4SDimitry Andric return; 1829480093f4SDimitry Andric freeSpace = 1830480093f4SDimitry Andric svalBuilder.evalBinOp(state, BO_Sub, freeSpace, 1831480093f4SDimitry Andric svalBuilder.makeIntVal(1, sizeTy), sizeTy); 1832bdd1243dSDimitry Andric std::optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>(); 1833480093f4SDimitry Andric 1834480093f4SDimitry Andric // While unlikely, it is possible that the subtraction is 1835480093f4SDimitry Andric // too complex to compute, let's check whether it succeeded. 1836480093f4SDimitry Andric if (!freeSpaceNL) 1837480093f4SDimitry Andric return; 1838480093f4SDimitry Andric SVal hasEnoughSpace = svalBuilder.evalBinOpNN( 1839480093f4SDimitry Andric state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy); 1840480093f4SDimitry Andric 1841480093f4SDimitry Andric ProgramStateRef TrueState, FalseState; 1842480093f4SDimitry Andric std::tie(TrueState, FalseState) = 1843480093f4SDimitry Andric state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>()); 1844480093f4SDimitry Andric 1845480093f4SDimitry Andric // srcStrLength <= size - dstStrLength -1 1846480093f4SDimitry Andric if (TrueState && !FalseState) { 1847480093f4SDimitry Andric amountCopied = strLength; 18480b57cec5SDimitry Andric } 18490b57cec5SDimitry Andric 1850480093f4SDimitry Andric // srcStrLength > size - dstStrLength -1 1851480093f4SDimitry Andric if (!TrueState && FalseState) { 1852480093f4SDimitry Andric amountCopied = freeSpace; 1853480093f4SDimitry Andric } 1854480093f4SDimitry Andric 1855480093f4SDimitry Andric if (TrueState && FalseState) 1856480093f4SDimitry Andric amountCopied = UnknownVal(); 1857480093f4SDimitry Andric break; 1858480093f4SDimitry Andric } 1859480093f4SDimitry Andric } 18600b57cec5SDimitry Andric // We still want to know if the bound is known to be too large. 18610b57cec5SDimitry Andric if (lenValNL) { 1862480093f4SDimitry Andric switch (appendK) { 1863480093f4SDimitry Andric case ConcatFnKind::strcat: 18640b57cec5SDimitry Andric // For strncat, the check is strlen(dst) + lenVal < sizeof(dst) 18650b57cec5SDimitry Andric 18660b57cec5SDimitry Andric // Get the string length of the destination. If the destination is 18670b57cec5SDimitry Andric // memory that can't have a string length, we shouldn't be copying 18680b57cec5SDimitry Andric // into it anyway. 18690b57cec5SDimitry Andric if (dstStrLength.isUndef()) 18700b57cec5SDimitry Andric return; 18710b57cec5SDimitry Andric 1872480093f4SDimitry Andric if (dstStrLengthNL) { 1873480093f4SDimitry Andric maxLastElementIndex = svalBuilder.evalBinOpNN( 1874480093f4SDimitry Andric state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy); 1875480093f4SDimitry Andric 18760b57cec5SDimitry Andric boundWarning = "Size argument is greater than the free space in the " 18770b57cec5SDimitry Andric "destination buffer"; 18780b57cec5SDimitry Andric } 1879480093f4SDimitry Andric break; 1880480093f4SDimitry Andric case ConcatFnKind::none: 1881480093f4SDimitry Andric case ConcatFnKind::strlcat: 1882480093f4SDimitry Andric // For strncpy and strlcat, this is just checking 1883480093f4SDimitry Andric // that lenVal <= sizeof(dst). 18840b57cec5SDimitry Andric // (Yes, strncpy and strncat differ in how they treat termination. 18850b57cec5SDimitry Andric // strncat ALWAYS terminates, but strncpy doesn't.) 18860b57cec5SDimitry Andric 18870b57cec5SDimitry Andric // We need a special case for when the copy size is zero, in which 18880b57cec5SDimitry Andric // case strncpy will do no work at all. Our bounds check uses n-1 18890b57cec5SDimitry Andric // as the last element accessed, so n == 0 is problematic. 18900b57cec5SDimitry Andric ProgramStateRef StateZeroSize, StateNonZeroSize; 18910b57cec5SDimitry Andric std::tie(StateZeroSize, StateNonZeroSize) = 18920b57cec5SDimitry Andric assumeZero(C, state, *lenValNL, sizeTy); 18930b57cec5SDimitry Andric 18940b57cec5SDimitry Andric // If the size is known to be zero, we're done. 18950b57cec5SDimitry Andric if (StateZeroSize && !StateNonZeroSize) { 18960b57cec5SDimitry Andric if (returnPtr) { 1897*647cbc5dSDimitry Andric StateZeroSize = 1898*647cbc5dSDimitry Andric StateZeroSize->BindExpr(Call.getOriginExpr(), LCtx, DstVal); 18990b57cec5SDimitry Andric } else { 1900480093f4SDimitry Andric if (appendK == ConcatFnKind::none) { 1901480093f4SDimitry Andric // strlcpy returns strlen(src) 1902*647cbc5dSDimitry Andric StateZeroSize = StateZeroSize->BindExpr(Call.getOriginExpr(), 1903*647cbc5dSDimitry Andric LCtx, strLength); 1904480093f4SDimitry Andric } else { 1905480093f4SDimitry Andric // strlcat returns strlen(src) + strlen(dst) 1906480093f4SDimitry Andric SVal retSize = svalBuilder.evalBinOp( 1907480093f4SDimitry Andric state, BO_Add, strLength, dstStrLength, sizeTy); 1908*647cbc5dSDimitry Andric StateZeroSize = 1909*647cbc5dSDimitry Andric StateZeroSize->BindExpr(Call.getOriginExpr(), LCtx, retSize); 1910480093f4SDimitry Andric } 19110b57cec5SDimitry Andric } 19120b57cec5SDimitry Andric C.addTransition(StateZeroSize); 19130b57cec5SDimitry Andric return; 19140b57cec5SDimitry Andric } 19150b57cec5SDimitry Andric 19160b57cec5SDimitry Andric // Otherwise, go ahead and figure out the last element we'll touch. 19170b57cec5SDimitry Andric // We don't record the non-zero assumption here because we can't 19180b57cec5SDimitry Andric // be sure. We won't warn on a possible zero. 19190b57cec5SDimitry Andric NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); 1920480093f4SDimitry Andric maxLastElementIndex = 1921480093f4SDimitry Andric svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy); 19220b57cec5SDimitry Andric boundWarning = "Size argument is greater than the length of the " 19230b57cec5SDimitry Andric "destination buffer"; 1924480093f4SDimitry Andric break; 19250b57cec5SDimitry Andric } 19260b57cec5SDimitry Andric } 19270b57cec5SDimitry Andric } else { 19280b57cec5SDimitry Andric // The function isn't bounded. The amount copied should match the length 19290b57cec5SDimitry Andric // of the source buffer. 19300b57cec5SDimitry Andric amountCopied = strLength; 19310b57cec5SDimitry Andric } 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric assert(state); 19340b57cec5SDimitry Andric 19350b57cec5SDimitry Andric // This represents the number of characters copied into the destination 19360b57cec5SDimitry Andric // buffer. (It may not actually be the strlen if the destination buffer 19370b57cec5SDimitry Andric // is not terminated.) 19380b57cec5SDimitry Andric SVal finalStrLength = UnknownVal(); 1939480093f4SDimitry Andric SVal strlRetVal = UnknownVal(); 1940480093f4SDimitry Andric 1941480093f4SDimitry Andric if (appendK == ConcatFnKind::none && !returnPtr) { 1942480093f4SDimitry Andric // strlcpy returns the sizeof(src) 1943480093f4SDimitry Andric strlRetVal = strLength; 1944480093f4SDimitry Andric } 19450b57cec5SDimitry Andric 19460b57cec5SDimitry Andric // If this is an appending function (strcat, strncat...) then set the 19470b57cec5SDimitry Andric // string length to strlen(src) + strlen(dst) since the buffer will 19480b57cec5SDimitry Andric // ultimately contain both. 1949480093f4SDimitry Andric if (appendK != ConcatFnKind::none) { 19500b57cec5SDimitry Andric // Get the string length of the destination. If the destination is memory 19510b57cec5SDimitry Andric // that can't have a string length, we shouldn't be copying into it anyway. 19520b57cec5SDimitry Andric if (dstStrLength.isUndef()) 19530b57cec5SDimitry Andric return; 19540b57cec5SDimitry Andric 1955480093f4SDimitry Andric if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) { 1956480093f4SDimitry Andric strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL, 1957480093f4SDimitry Andric *dstStrLengthNL, sizeTy); 1958480093f4SDimitry Andric } 1959480093f4SDimitry Andric 1960bdd1243dSDimitry Andric std::optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>(); 19610b57cec5SDimitry Andric 19620b57cec5SDimitry Andric // If we know both string lengths, we might know the final string length. 1963480093f4SDimitry Andric if (amountCopiedNL && dstStrLengthNL) { 19640b57cec5SDimitry Andric // Make sure the two lengths together don't overflow a size_t. 1965480093f4SDimitry Andric state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL); 19660b57cec5SDimitry Andric if (!state) 19670b57cec5SDimitry Andric return; 19680b57cec5SDimitry Andric 1969480093f4SDimitry Andric finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL, 19700b57cec5SDimitry Andric *dstStrLengthNL, sizeTy); 19710b57cec5SDimitry Andric } 19720b57cec5SDimitry Andric 19730b57cec5SDimitry Andric // If we couldn't get a single value for the final string length, 19740b57cec5SDimitry Andric // we can at least bound it by the individual lengths. 19750b57cec5SDimitry Andric if (finalStrLength.isUnknown()) { 19760b57cec5SDimitry Andric // Try to get a "hypothetical" string length symbol, which we can later 19770b57cec5SDimitry Andric // set as a real value if that turns out to be the case. 1978*647cbc5dSDimitry Andric finalStrLength = 1979*647cbc5dSDimitry Andric getCStringLength(C, state, Call.getOriginExpr(), DstVal, true); 19800b57cec5SDimitry Andric assert(!finalStrLength.isUndef()); 19810b57cec5SDimitry Andric 1982bdd1243dSDimitry Andric if (std::optional<NonLoc> finalStrLengthNL = 1983bdd1243dSDimitry Andric finalStrLength.getAs<NonLoc>()) { 1984480093f4SDimitry Andric if (amountCopiedNL && appendK == ConcatFnKind::none) { 1985480093f4SDimitry Andric // we overwrite dst string with the src 19860b57cec5SDimitry Andric // finalStrLength >= srcStrLength 1987480093f4SDimitry Andric SVal sourceInResult = svalBuilder.evalBinOpNN( 1988480093f4SDimitry Andric state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy); 19890b57cec5SDimitry Andric state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(), 19900b57cec5SDimitry Andric true); 19910b57cec5SDimitry Andric if (!state) 19920b57cec5SDimitry Andric return; 19930b57cec5SDimitry Andric } 19940b57cec5SDimitry Andric 1995480093f4SDimitry Andric if (dstStrLengthNL && appendK != ConcatFnKind::none) { 1996480093f4SDimitry Andric // we extend the dst string with the src 19970b57cec5SDimitry Andric // finalStrLength >= dstStrLength 19980b57cec5SDimitry Andric SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE, 19990b57cec5SDimitry Andric *finalStrLengthNL, 20000b57cec5SDimitry Andric *dstStrLengthNL, 20010b57cec5SDimitry Andric cmpTy); 20020b57cec5SDimitry Andric state = 20030b57cec5SDimitry Andric state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true); 20040b57cec5SDimitry Andric if (!state) 20050b57cec5SDimitry Andric return; 20060b57cec5SDimitry Andric } 20070b57cec5SDimitry Andric } 20080b57cec5SDimitry Andric } 20090b57cec5SDimitry Andric 20100b57cec5SDimitry Andric } else { 20110b57cec5SDimitry Andric // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and 20120b57cec5SDimitry Andric // the final string length will match the input string length. 20130b57cec5SDimitry Andric finalStrLength = amountCopied; 20140b57cec5SDimitry Andric } 20150b57cec5SDimitry Andric 20160b57cec5SDimitry Andric SVal Result; 20170b57cec5SDimitry Andric 20180b57cec5SDimitry Andric if (returnPtr) { 20190b57cec5SDimitry Andric // The final result of the function will either be a pointer past the last 20200b57cec5SDimitry Andric // copied element, or a pointer to the start of the destination buffer. 2021480093f4SDimitry Andric Result = (ReturnEnd ? UnknownVal() : DstVal); 20220b57cec5SDimitry Andric } else { 2023480093f4SDimitry Andric if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none) 2024480093f4SDimitry Andric //strlcpy, strlcat 2025480093f4SDimitry Andric Result = strlRetVal; 2026480093f4SDimitry Andric else 20270b57cec5SDimitry Andric Result = finalStrLength; 20280b57cec5SDimitry Andric } 20290b57cec5SDimitry Andric 20300b57cec5SDimitry Andric assert(state); 20310b57cec5SDimitry Andric 20320b57cec5SDimitry Andric // If the destination is a MemRegion, try to check for a buffer overflow and 20330b57cec5SDimitry Andric // record the new string length. 2034bdd1243dSDimitry Andric if (std::optional<loc::MemRegionVal> dstRegVal = 20350b57cec5SDimitry Andric DstVal.getAs<loc::MemRegionVal>()) { 20365ffd83dbSDimitry Andric QualType ptrTy = Dst.Expression->getType(); 20370b57cec5SDimitry Andric 20380b57cec5SDimitry Andric // If we have an exact value on a bounded copy, use that to check for 20390b57cec5SDimitry Andric // overflows, rather than our estimate about how much is actually copied. 2040bdd1243dSDimitry Andric if (std::optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) { 20415ffd83dbSDimitry Andric SVal maxLastElement = 20425ffd83dbSDimitry Andric svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy); 20435ffd83dbSDimitry Andric 20445f757f3fSDimitry Andric // Check if the first byte of the destination is writable. 20455f757f3fSDimitry Andric state = CheckLocation(C, state, Dst, DstVal, AccessKind::write); 20465f757f3fSDimitry Andric if (!state) 20475f757f3fSDimitry Andric return; 20485f757f3fSDimitry Andric // Check if the last byte of the destination is writable. 20495ffd83dbSDimitry Andric state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write); 20500b57cec5SDimitry Andric if (!state) 20510b57cec5SDimitry Andric return; 20520b57cec5SDimitry Andric } 20530b57cec5SDimitry Andric 20540b57cec5SDimitry Andric // Then, if the final length is known... 2055bdd1243dSDimitry Andric if (std::optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) { 20560b57cec5SDimitry Andric SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, 20570b57cec5SDimitry Andric *knownStrLength, ptrTy); 20580b57cec5SDimitry Andric 20590b57cec5SDimitry Andric // ...and we haven't checked the bound, we'll check the actual copy. 20600b57cec5SDimitry Andric if (!boundWarning) { 20615f757f3fSDimitry Andric // Check if the first byte of the destination is writable. 20625f757f3fSDimitry Andric state = CheckLocation(C, state, Dst, DstVal, AccessKind::write); 20635f757f3fSDimitry Andric if (!state) 20645f757f3fSDimitry Andric return; 20655f757f3fSDimitry Andric // Check if the last byte of the destination is writable. 20665ffd83dbSDimitry Andric state = CheckLocation(C, state, Dst, lastElement, AccessKind::write); 20670b57cec5SDimitry Andric if (!state) 20680b57cec5SDimitry Andric return; 20690b57cec5SDimitry Andric } 20700b57cec5SDimitry Andric 20710b57cec5SDimitry Andric // If this is a stpcpy-style copy, the last element is the return value. 2072480093f4SDimitry Andric if (returnPtr && ReturnEnd) 20730b57cec5SDimitry Andric Result = lastElement; 20740b57cec5SDimitry Andric } 20750b57cec5SDimitry Andric 20760b57cec5SDimitry Andric // Invalidate the destination (regular invalidation without pointer-escaping 20770b57cec5SDimitry Andric // the address of the top-level region). This must happen before we set the 20780b57cec5SDimitry Andric // C string length because invalidation will clear the length. 20790b57cec5SDimitry Andric // FIXME: Even if we can't perfectly model the copy, we should see if we 20800b57cec5SDimitry Andric // can use LazyCompoundVals to copy the source values into the destination. 20810b57cec5SDimitry Andric // This would probably remove any existing bindings past the end of the 20820b57cec5SDimitry Andric // string, but that's still an improvement over blank invalidation. 208306c3fb27SDimitry Andric state = invalidateDestinationBufferBySize(C, state, Dst.Expression, 208406c3fb27SDimitry Andric *dstRegVal, amountCopied, 208506c3fb27SDimitry Andric C.getASTContext().getSizeType()); 20860b57cec5SDimitry Andric 20870b57cec5SDimitry Andric // Invalidate the source (const-invalidation without const-pointer-escaping 20880b57cec5SDimitry Andric // the address of the top-level region). 208906c3fb27SDimitry Andric state = invalidateSourceBuffer(C, state, srcExpr.Expression, srcVal); 20900b57cec5SDimitry Andric 20910b57cec5SDimitry Andric // Set the C string length of the destination, if we know it. 2092480093f4SDimitry Andric if (IsBounded && (appendK == ConcatFnKind::none)) { 20930b57cec5SDimitry Andric // strncpy is annoying in that it doesn't guarantee to null-terminate 20940b57cec5SDimitry Andric // the result string. If the original string didn't fit entirely inside 20950b57cec5SDimitry Andric // the bound (including the null-terminator), we don't know how long the 20960b57cec5SDimitry Andric // result is. 20970b57cec5SDimitry Andric if (amountCopied != strLength) 20980b57cec5SDimitry Andric finalStrLength = UnknownVal(); 20990b57cec5SDimitry Andric } 21000b57cec5SDimitry Andric state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength); 21010b57cec5SDimitry Andric } 21020b57cec5SDimitry Andric 21030b57cec5SDimitry Andric assert(state); 21040b57cec5SDimitry Andric 21050b57cec5SDimitry Andric if (returnPtr) { 21060b57cec5SDimitry Andric // If this is a stpcpy-style copy, but we were unable to check for a buffer 21070b57cec5SDimitry Andric // overflow, we still need a result. Conjure a return value. 2108480093f4SDimitry Andric if (ReturnEnd && Result.isUnknown()) { 2109*647cbc5dSDimitry Andric Result = svalBuilder.conjureSymbolVal(nullptr, Call.getOriginExpr(), LCtx, 2110*647cbc5dSDimitry Andric C.blockCount()); 21110b57cec5SDimitry Andric } 21120b57cec5SDimitry Andric } 21130b57cec5SDimitry Andric // Set the return value. 2114*647cbc5dSDimitry Andric state = state->BindExpr(Call.getOriginExpr(), LCtx, Result); 21150b57cec5SDimitry Andric C.addTransition(state); 21160b57cec5SDimitry Andric } 21170b57cec5SDimitry Andric 2118*647cbc5dSDimitry Andric void CStringChecker::evalStrcmp(CheckerContext &C, 2119*647cbc5dSDimitry Andric const CallEvent &Call) const { 21200b57cec5SDimitry Andric //int strcmp(const char *s1, const char *s2); 2121*647cbc5dSDimitry Andric evalStrcmpCommon(C, Call, /* IsBounded = */ false, /* IgnoreCase = */ false); 21220b57cec5SDimitry Andric } 21230b57cec5SDimitry Andric 2124*647cbc5dSDimitry Andric void CStringChecker::evalStrncmp(CheckerContext &C, 2125*647cbc5dSDimitry Andric const CallEvent &Call) const { 21260b57cec5SDimitry Andric //int strncmp(const char *s1, const char *s2, size_t n); 2127*647cbc5dSDimitry Andric evalStrcmpCommon(C, Call, /* IsBounded = */ true, /* IgnoreCase = */ false); 21280b57cec5SDimitry Andric } 21290b57cec5SDimitry Andric 21300b57cec5SDimitry Andric void CStringChecker::evalStrcasecmp(CheckerContext &C, 2131*647cbc5dSDimitry Andric const CallEvent &Call) const { 21320b57cec5SDimitry Andric //int strcasecmp(const char *s1, const char *s2); 2133*647cbc5dSDimitry Andric evalStrcmpCommon(C, Call, /* IsBounded = */ false, /* IgnoreCase = */ true); 21340b57cec5SDimitry Andric } 21350b57cec5SDimitry Andric 21360b57cec5SDimitry Andric void CStringChecker::evalStrncasecmp(CheckerContext &C, 2137*647cbc5dSDimitry Andric const CallEvent &Call) const { 21380b57cec5SDimitry Andric //int strncasecmp(const char *s1, const char *s2, size_t n); 2139*647cbc5dSDimitry Andric evalStrcmpCommon(C, Call, /* IsBounded = */ true, /* IgnoreCase = */ true); 21400b57cec5SDimitry Andric } 21410b57cec5SDimitry Andric 2142*647cbc5dSDimitry Andric void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallEvent &Call, 2143480093f4SDimitry Andric bool IsBounded, bool IgnoreCase) const { 21440b57cec5SDimitry Andric CurrentFunctionDescription = "string comparison function"; 21450b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 21460b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 21470b57cec5SDimitry Andric 21480b57cec5SDimitry Andric // Check that the first string is non-null 2149*647cbc5dSDimitry Andric AnyArgExpr Left = {Call.getArgExpr(0), 0}; 21505ffd83dbSDimitry Andric SVal LeftVal = state->getSVal(Left.Expression, LCtx); 21515ffd83dbSDimitry Andric state = checkNonNull(C, state, Left, LeftVal); 21520b57cec5SDimitry Andric if (!state) 21530b57cec5SDimitry Andric return; 21540b57cec5SDimitry Andric 21550b57cec5SDimitry Andric // Check that the second string is non-null. 2156*647cbc5dSDimitry Andric AnyArgExpr Right = {Call.getArgExpr(1), 1}; 21575ffd83dbSDimitry Andric SVal RightVal = state->getSVal(Right.Expression, LCtx); 21585ffd83dbSDimitry Andric state = checkNonNull(C, state, Right, RightVal); 21590b57cec5SDimitry Andric if (!state) 21600b57cec5SDimitry Andric return; 21610b57cec5SDimitry Andric 21620b57cec5SDimitry Andric // Get the string length of the first string or give up. 21635ffd83dbSDimitry Andric SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal); 21645ffd83dbSDimitry Andric if (LeftLength.isUndef()) 21650b57cec5SDimitry Andric return; 21660b57cec5SDimitry Andric 21670b57cec5SDimitry Andric // Get the string length of the second string or give up. 21685ffd83dbSDimitry Andric SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal); 21695ffd83dbSDimitry Andric if (RightLength.isUndef()) 21700b57cec5SDimitry Andric return; 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric // If we know the two buffers are the same, we know the result is 0. 21730b57cec5SDimitry Andric // First, get the two buffers' addresses. Another checker will have already 21740b57cec5SDimitry Andric // made sure they're not undefined. 21755ffd83dbSDimitry Andric DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>(); 21765ffd83dbSDimitry Andric DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>(); 21770b57cec5SDimitry Andric 21780b57cec5SDimitry Andric // See if they are the same. 21790b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 21800b57cec5SDimitry Andric DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); 21810b57cec5SDimitry Andric ProgramStateRef StSameBuf, StNotSameBuf; 21820b57cec5SDimitry Andric std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); 21830b57cec5SDimitry Andric 21840b57cec5SDimitry Andric // If the two arguments might be the same buffer, we know the result is 0, 21850b57cec5SDimitry Andric // and we only need to check one size. 21860b57cec5SDimitry Andric if (StSameBuf) { 2187*647cbc5dSDimitry Andric StSameBuf = 2188*647cbc5dSDimitry Andric StSameBuf->BindExpr(Call.getOriginExpr(), LCtx, 2189*647cbc5dSDimitry Andric svalBuilder.makeZeroVal(Call.getResultType())); 21900b57cec5SDimitry Andric C.addTransition(StSameBuf); 21910b57cec5SDimitry Andric 21920b57cec5SDimitry Andric // If the two arguments are GUARANTEED to be the same, we're done! 21930b57cec5SDimitry Andric if (!StNotSameBuf) 21940b57cec5SDimitry Andric return; 21950b57cec5SDimitry Andric } 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric assert(StNotSameBuf); 21980b57cec5SDimitry Andric state = StNotSameBuf; 21990b57cec5SDimitry Andric 22000b57cec5SDimitry Andric // At this point we can go about comparing the two buffers. 22010b57cec5SDimitry Andric // For now, we only do this if they're both known string literals. 22020b57cec5SDimitry Andric 22030b57cec5SDimitry Andric // Attempt to extract string literals from both expressions. 22045ffd83dbSDimitry Andric const StringLiteral *LeftStrLiteral = 22055ffd83dbSDimitry Andric getCStringLiteral(C, state, Left.Expression, LeftVal); 22065ffd83dbSDimitry Andric const StringLiteral *RightStrLiteral = 22075ffd83dbSDimitry Andric getCStringLiteral(C, state, Right.Expression, RightVal); 22080b57cec5SDimitry Andric bool canComputeResult = false; 2209*647cbc5dSDimitry Andric SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, Call.getOriginExpr(), 2210*647cbc5dSDimitry Andric LCtx, C.blockCount()); 22110b57cec5SDimitry Andric 22125ffd83dbSDimitry Andric if (LeftStrLiteral && RightStrLiteral) { 22135ffd83dbSDimitry Andric StringRef LeftStrRef = LeftStrLiteral->getString(); 22145ffd83dbSDimitry Andric StringRef RightStrRef = RightStrLiteral->getString(); 22150b57cec5SDimitry Andric 2216480093f4SDimitry Andric if (IsBounded) { 22170b57cec5SDimitry Andric // Get the max number of characters to compare. 2218*647cbc5dSDimitry Andric const Expr *lenExpr = Call.getArgExpr(2); 22190b57cec5SDimitry Andric SVal lenVal = state->getSVal(lenExpr, LCtx); 22200b57cec5SDimitry Andric 22210b57cec5SDimitry Andric // If the length is known, we can get the right substrings. 22220b57cec5SDimitry Andric if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) { 22230b57cec5SDimitry Andric // Create substrings of each to compare the prefix. 22245ffd83dbSDimitry Andric LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue()); 22255ffd83dbSDimitry Andric RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue()); 22260b57cec5SDimitry Andric canComputeResult = true; 22270b57cec5SDimitry Andric } 22280b57cec5SDimitry Andric } else { 22290b57cec5SDimitry Andric // This is a normal, unbounded strcmp. 22300b57cec5SDimitry Andric canComputeResult = true; 22310b57cec5SDimitry Andric } 22320b57cec5SDimitry Andric 22330b57cec5SDimitry Andric if (canComputeResult) { 22340b57cec5SDimitry Andric // Real strcmp stops at null characters. 22355ffd83dbSDimitry Andric size_t s1Term = LeftStrRef.find('\0'); 22360b57cec5SDimitry Andric if (s1Term != StringRef::npos) 22375ffd83dbSDimitry Andric LeftStrRef = LeftStrRef.substr(0, s1Term); 22380b57cec5SDimitry Andric 22395ffd83dbSDimitry Andric size_t s2Term = RightStrRef.find('\0'); 22400b57cec5SDimitry Andric if (s2Term != StringRef::npos) 22415ffd83dbSDimitry Andric RightStrRef = RightStrRef.substr(0, s2Term); 22420b57cec5SDimitry Andric 22430b57cec5SDimitry Andric // Use StringRef's comparison methods to compute the actual result. 2244fe6060f1SDimitry Andric int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef) 22455ffd83dbSDimitry Andric : LeftStrRef.compare(RightStrRef); 22460b57cec5SDimitry Andric 22470b57cec5SDimitry Andric // The strcmp function returns an integer greater than, equal to, or less 22480b57cec5SDimitry Andric // than zero, [c11, p7.24.4.2]. 22490b57cec5SDimitry Andric if (compareRes == 0) { 2250*647cbc5dSDimitry Andric resultVal = svalBuilder.makeIntVal(compareRes, Call.getResultType()); 22510b57cec5SDimitry Andric } 22520b57cec5SDimitry Andric else { 2253*647cbc5dSDimitry Andric DefinedSVal zeroVal = svalBuilder.makeIntVal(0, Call.getResultType()); 22540b57cec5SDimitry Andric // Constrain strcmp's result range based on the result of StringRef's 22550b57cec5SDimitry Andric // comparison methods. 2256bdd1243dSDimitry Andric BinaryOperatorKind op = (compareRes > 0) ? BO_GT : BO_LT; 22570b57cec5SDimitry Andric SVal compareWithZero = 22580b57cec5SDimitry Andric svalBuilder.evalBinOp(state, op, resultVal, zeroVal, 22590b57cec5SDimitry Andric svalBuilder.getConditionType()); 22600b57cec5SDimitry Andric DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>(); 22610b57cec5SDimitry Andric state = state->assume(compareWithZeroVal, true); 22620b57cec5SDimitry Andric } 22630b57cec5SDimitry Andric } 22640b57cec5SDimitry Andric } 22650b57cec5SDimitry Andric 2266*647cbc5dSDimitry Andric state = state->BindExpr(Call.getOriginExpr(), LCtx, resultVal); 22670b57cec5SDimitry Andric 22680b57cec5SDimitry Andric // Record this as a possible path. 22690b57cec5SDimitry Andric C.addTransition(state); 22700b57cec5SDimitry Andric } 22710b57cec5SDimitry Andric 2272*647cbc5dSDimitry Andric void CStringChecker::evalStrsep(CheckerContext &C, 2273*647cbc5dSDimitry Andric const CallEvent &Call) const { 22740b57cec5SDimitry Andric // char *strsep(char **stringp, const char *delim); 22755e801ac6SDimitry Andric // Verify whether the search string parameter matches the return type. 2276*647cbc5dSDimitry Andric SourceArgExpr SearchStrPtr = {{Call.getArgExpr(0), 0}}; 22775ffd83dbSDimitry Andric 22785ffd83dbSDimitry Andric QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType(); 2279*647cbc5dSDimitry Andric if (CharPtrTy.isNull() || Call.getResultType().getUnqualifiedType() != 2280*647cbc5dSDimitry Andric CharPtrTy.getUnqualifiedType()) 22810b57cec5SDimitry Andric return; 22820b57cec5SDimitry Andric 22830b57cec5SDimitry Andric CurrentFunctionDescription = "strsep()"; 22840b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 22850b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 22860b57cec5SDimitry Andric 22870b57cec5SDimitry Andric // Check that the search string pointer is non-null (though it may point to 22880b57cec5SDimitry Andric // a null string). 22895ffd83dbSDimitry Andric SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx); 22905ffd83dbSDimitry Andric State = checkNonNull(C, State, SearchStrPtr, SearchStrVal); 22910b57cec5SDimitry Andric if (!State) 22920b57cec5SDimitry Andric return; 22930b57cec5SDimitry Andric 22940b57cec5SDimitry Andric // Check that the delimiter string is non-null. 2295*647cbc5dSDimitry Andric AnyArgExpr DelimStr = {Call.getArgExpr(1), 1}; 22965ffd83dbSDimitry Andric SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx); 22975ffd83dbSDimitry Andric State = checkNonNull(C, State, DelimStr, DelimStrVal); 22980b57cec5SDimitry Andric if (!State) 22990b57cec5SDimitry Andric return; 23000b57cec5SDimitry Andric 23010b57cec5SDimitry Andric SValBuilder &SVB = C.getSValBuilder(); 23020b57cec5SDimitry Andric SVal Result; 2303bdd1243dSDimitry Andric if (std::optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) { 23040b57cec5SDimitry Andric // Get the current value of the search string pointer, as a char*. 23050b57cec5SDimitry Andric Result = State->getSVal(*SearchStrLoc, CharPtrTy); 23060b57cec5SDimitry Andric 23070b57cec5SDimitry Andric // Invalidate the search string, representing the change of one delimiter 23080b57cec5SDimitry Andric // character to NUL. 230906c3fb27SDimitry Andric // As the replacement never overflows, do not invalidate its super region. 231006c3fb27SDimitry Andric State = invalidateDestinationBufferNeverOverflows( 231106c3fb27SDimitry Andric C, State, SearchStrPtr.Expression, Result); 23120b57cec5SDimitry Andric 23130b57cec5SDimitry Andric // Overwrite the search string pointer. The new value is either an address 23140b57cec5SDimitry Andric // further along in the same string, or NULL if there are no more tokens. 2315*647cbc5dSDimitry Andric State = 2316*647cbc5dSDimitry Andric State->bindLoc(*SearchStrLoc, 2317*647cbc5dSDimitry Andric SVB.conjureSymbolVal(getTag(), Call.getOriginExpr(), 2318*647cbc5dSDimitry Andric LCtx, CharPtrTy, C.blockCount()), 23190b57cec5SDimitry Andric LCtx); 23200b57cec5SDimitry Andric } else { 23210b57cec5SDimitry Andric assert(SearchStrVal.isUnknown()); 23220b57cec5SDimitry Andric // Conjure a symbolic value. It's the best we can do. 2323*647cbc5dSDimitry Andric Result = SVB.conjureSymbolVal(nullptr, Call.getOriginExpr(), LCtx, 2324*647cbc5dSDimitry Andric C.blockCount()); 23250b57cec5SDimitry Andric } 23260b57cec5SDimitry Andric 23270b57cec5SDimitry Andric // Set the return value, and finish. 2328*647cbc5dSDimitry Andric State = State->BindExpr(Call.getOriginExpr(), LCtx, Result); 23290b57cec5SDimitry Andric C.addTransition(State); 23300b57cec5SDimitry Andric } 23310b57cec5SDimitry Andric 23320b57cec5SDimitry Andric // These should probably be moved into a C++ standard library checker. 2333*647cbc5dSDimitry Andric void CStringChecker::evalStdCopy(CheckerContext &C, 2334*647cbc5dSDimitry Andric const CallEvent &Call) const { 2335*647cbc5dSDimitry Andric evalStdCopyCommon(C, Call); 23360b57cec5SDimitry Andric } 23370b57cec5SDimitry Andric 23380b57cec5SDimitry Andric void CStringChecker::evalStdCopyBackward(CheckerContext &C, 2339*647cbc5dSDimitry Andric const CallEvent &Call) const { 2340*647cbc5dSDimitry Andric evalStdCopyCommon(C, Call); 23410b57cec5SDimitry Andric } 23420b57cec5SDimitry Andric 23430b57cec5SDimitry Andric void CStringChecker::evalStdCopyCommon(CheckerContext &C, 2344*647cbc5dSDimitry Andric const CallEvent &Call) const { 2345*647cbc5dSDimitry Andric if (!Call.getArgExpr(2)->getType()->isPointerType()) 23460b57cec5SDimitry Andric return; 23470b57cec5SDimitry Andric 23480b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 23490b57cec5SDimitry Andric 23500b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 23510b57cec5SDimitry Andric 23520b57cec5SDimitry Andric // template <class _InputIterator, class _OutputIterator> 23530b57cec5SDimitry Andric // _OutputIterator 23540b57cec5SDimitry Andric // copy(_InputIterator __first, _InputIterator __last, 23550b57cec5SDimitry Andric // _OutputIterator __result) 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric // Invalidate the destination buffer 2358*647cbc5dSDimitry Andric const Expr *Dst = Call.getArgExpr(2); 23590b57cec5SDimitry Andric SVal DstVal = State->getSVal(Dst, LCtx); 236006c3fb27SDimitry Andric // FIXME: As we do not know how many items are copied, we also invalidate the 236106c3fb27SDimitry Andric // super region containing the target location. 236206c3fb27SDimitry Andric State = 236306c3fb27SDimitry Andric invalidateDestinationBufferAlwaysEscapeSuperRegion(C, State, Dst, DstVal); 23640b57cec5SDimitry Andric 23650b57cec5SDimitry Andric SValBuilder &SVB = C.getSValBuilder(); 23660b57cec5SDimitry Andric 2367*647cbc5dSDimitry Andric SVal ResultVal = 2368*647cbc5dSDimitry Andric SVB.conjureSymbolVal(nullptr, Call.getOriginExpr(), LCtx, C.blockCount()); 2369*647cbc5dSDimitry Andric State = State->BindExpr(Call.getOriginExpr(), LCtx, ResultVal); 23700b57cec5SDimitry Andric 23710b57cec5SDimitry Andric C.addTransition(State); 23720b57cec5SDimitry Andric } 23730b57cec5SDimitry Andric 2374*647cbc5dSDimitry Andric void CStringChecker::evalMemset(CheckerContext &C, 2375*647cbc5dSDimitry Andric const CallEvent &Call) const { 23765ffd83dbSDimitry Andric // void *memset(void *s, int c, size_t n); 23770b57cec5SDimitry Andric CurrentFunctionDescription = "memory set function"; 23780b57cec5SDimitry Andric 2379*647cbc5dSDimitry Andric DestinationArgExpr Buffer = {{Call.getArgExpr(0), 0}}; 2380*647cbc5dSDimitry Andric AnyArgExpr CharE = {Call.getArgExpr(1), 1}; 2381*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(2), 2}}; 23825ffd83dbSDimitry Andric 23830b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric // See if the size argument is zero. 23860b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 23875ffd83dbSDimitry Andric SVal SizeVal = C.getSVal(Size.Expression); 23885ffd83dbSDimitry Andric QualType SizeTy = Size.Expression->getType(); 23890b57cec5SDimitry Andric 23905ffd83dbSDimitry Andric ProgramStateRef ZeroSize, NonZeroSize; 23915ffd83dbSDimitry Andric std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy); 23920b57cec5SDimitry Andric 23930b57cec5SDimitry Andric // Get the value of the memory area. 23945ffd83dbSDimitry Andric SVal BufferPtrVal = C.getSVal(Buffer.Expression); 23950b57cec5SDimitry Andric 23960b57cec5SDimitry Andric // If the size is zero, there won't be any actual memory access, so 23975ffd83dbSDimitry Andric // just bind the return value to the buffer and return. 23985ffd83dbSDimitry Andric if (ZeroSize && !NonZeroSize) { 2399*647cbc5dSDimitry Andric ZeroSize = ZeroSize->BindExpr(Call.getOriginExpr(), LCtx, BufferPtrVal); 24005ffd83dbSDimitry Andric C.addTransition(ZeroSize); 24010b57cec5SDimitry Andric return; 24020b57cec5SDimitry Andric } 24030b57cec5SDimitry Andric 24040b57cec5SDimitry Andric // Ensure the memory area is not null. 24050b57cec5SDimitry Andric // If it is NULL there will be a NULL pointer dereference. 24065ffd83dbSDimitry Andric State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal); 24070b57cec5SDimitry Andric if (!State) 24080b57cec5SDimitry Andric return; 24090b57cec5SDimitry Andric 24105ffd83dbSDimitry Andric State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write); 24110b57cec5SDimitry Andric if (!State) 24120b57cec5SDimitry Andric return; 24130b57cec5SDimitry Andric 24140b57cec5SDimitry Andric // According to the values of the arguments, bind the value of the second 24150b57cec5SDimitry Andric // argument to the destination buffer and set string length, or just 24160b57cec5SDimitry Andric // invalidate the destination buffer. 24175ffd83dbSDimitry Andric if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression), 24185ffd83dbSDimitry Andric Size.Expression, C, State)) 24190b57cec5SDimitry Andric return; 24200b57cec5SDimitry Andric 2421*647cbc5dSDimitry Andric State = State->BindExpr(Call.getOriginExpr(), LCtx, BufferPtrVal); 24220b57cec5SDimitry Andric C.addTransition(State); 24230b57cec5SDimitry Andric } 24240b57cec5SDimitry Andric 2425*647cbc5dSDimitry Andric void CStringChecker::evalBzero(CheckerContext &C, const CallEvent &Call) const { 24260b57cec5SDimitry Andric CurrentFunctionDescription = "memory clearance function"; 24270b57cec5SDimitry Andric 2428*647cbc5dSDimitry Andric DestinationArgExpr Buffer = {{Call.getArgExpr(0), 0}}; 2429*647cbc5dSDimitry Andric SizeArgExpr Size = {{Call.getArgExpr(1), 1}}; 24300b57cec5SDimitry Andric SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy); 24310b57cec5SDimitry Andric 24320b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 24330b57cec5SDimitry Andric 24340b57cec5SDimitry Andric // See if the size argument is zero. 24355ffd83dbSDimitry Andric SVal SizeVal = C.getSVal(Size.Expression); 24365ffd83dbSDimitry Andric QualType SizeTy = Size.Expression->getType(); 24370b57cec5SDimitry Andric 24380b57cec5SDimitry Andric ProgramStateRef StateZeroSize, StateNonZeroSize; 24390b57cec5SDimitry Andric std::tie(StateZeroSize, StateNonZeroSize) = 24400b57cec5SDimitry Andric assumeZero(C, State, SizeVal, SizeTy); 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric // If the size is zero, there won't be any actual memory access, 24430b57cec5SDimitry Andric // In this case we just return. 24440b57cec5SDimitry Andric if (StateZeroSize && !StateNonZeroSize) { 24450b57cec5SDimitry Andric C.addTransition(StateZeroSize); 24460b57cec5SDimitry Andric return; 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric 24490b57cec5SDimitry Andric // Get the value of the memory area. 24505ffd83dbSDimitry Andric SVal MemVal = C.getSVal(Buffer.Expression); 24510b57cec5SDimitry Andric 24520b57cec5SDimitry Andric // Ensure the memory area is not null. 24530b57cec5SDimitry Andric // If it is NULL there will be a NULL pointer dereference. 24545ffd83dbSDimitry Andric State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal); 24550b57cec5SDimitry Andric if (!State) 24560b57cec5SDimitry Andric return; 24570b57cec5SDimitry Andric 24585ffd83dbSDimitry Andric State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write); 24590b57cec5SDimitry Andric if (!State) 24600b57cec5SDimitry Andric return; 24610b57cec5SDimitry Andric 24625ffd83dbSDimitry Andric if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State)) 24630b57cec5SDimitry Andric return; 24640b57cec5SDimitry Andric 24650b57cec5SDimitry Andric C.addTransition(State); 24660b57cec5SDimitry Andric } 24670b57cec5SDimitry Andric 2468*647cbc5dSDimitry Andric void CStringChecker::evalSprintf(CheckerContext &C, 2469*647cbc5dSDimitry Andric const CallEvent &Call) const { 247006c3fb27SDimitry Andric CurrentFunctionDescription = "'sprintf'"; 2471*647cbc5dSDimitry Andric const auto *CE = cast<CallExpr>(Call.getOriginExpr()); 247206c3fb27SDimitry Andric bool IsBI = CE->getBuiltinCallee() == Builtin::BI__builtin___sprintf_chk; 2473*647cbc5dSDimitry Andric evalSprintfCommon(C, Call, /* IsBounded */ false, IsBI); 247406c3fb27SDimitry Andric } 247506c3fb27SDimitry Andric 2476*647cbc5dSDimitry Andric void CStringChecker::evalSnprintf(CheckerContext &C, 2477*647cbc5dSDimitry Andric const CallEvent &Call) const { 247806c3fb27SDimitry Andric CurrentFunctionDescription = "'snprintf'"; 2479*647cbc5dSDimitry Andric const auto *CE = cast<CallExpr>(Call.getOriginExpr()); 248006c3fb27SDimitry Andric bool IsBI = CE->getBuiltinCallee() == Builtin::BI__builtin___snprintf_chk; 2481*647cbc5dSDimitry Andric evalSprintfCommon(C, Call, /* IsBounded */ true, IsBI); 248206c3fb27SDimitry Andric } 248306c3fb27SDimitry Andric 2484*647cbc5dSDimitry Andric void CStringChecker::evalSprintfCommon(CheckerContext &C, const CallEvent &Call, 248506c3fb27SDimitry Andric bool IsBounded, bool IsBuiltin) const { 248606c3fb27SDimitry Andric ProgramStateRef State = C.getState(); 2487*647cbc5dSDimitry Andric const auto *CE = cast<CallExpr>(Call.getOriginExpr()); 2488*647cbc5dSDimitry Andric DestinationArgExpr Dest = {{Call.getArgExpr(0), 0}}; 248906c3fb27SDimitry Andric 2490*647cbc5dSDimitry Andric const auto NumParams = Call.parameters().size(); 249106c3fb27SDimitry Andric assert(CE->getNumArgs() >= NumParams); 249206c3fb27SDimitry Andric 249306c3fb27SDimitry Andric const auto AllArguments = 249406c3fb27SDimitry Andric llvm::make_range(CE->getArgs(), CE->getArgs() + CE->getNumArgs()); 249506c3fb27SDimitry Andric const auto VariadicArguments = drop_begin(enumerate(AllArguments), NumParams); 249606c3fb27SDimitry Andric 249706c3fb27SDimitry Andric for (const auto &[ArgIdx, ArgExpr] : VariadicArguments) { 249806c3fb27SDimitry Andric // We consider only string buffers 249906c3fb27SDimitry Andric if (const QualType type = ArgExpr->getType(); 250006c3fb27SDimitry Andric !type->isAnyPointerType() || 250106c3fb27SDimitry Andric !type->getPointeeType()->isAnyCharacterType()) 250206c3fb27SDimitry Andric continue; 250306c3fb27SDimitry Andric SourceArgExpr Source = {{ArgExpr, unsigned(ArgIdx)}}; 250406c3fb27SDimitry Andric 250506c3fb27SDimitry Andric // Ensure the buffers do not overlap. 250606c3fb27SDimitry Andric SizeArgExpr SrcExprAsSizeDummy = { 250706c3fb27SDimitry Andric {Source.Expression, Source.ArgumentIndex}}; 250806c3fb27SDimitry Andric State = CheckOverlap( 250906c3fb27SDimitry Andric C, State, 2510*647cbc5dSDimitry Andric (IsBounded ? SizeArgExpr{{Call.getArgExpr(1), 1}} : SrcExprAsSizeDummy), 251106c3fb27SDimitry Andric Dest, Source); 251206c3fb27SDimitry Andric if (!State) 251306c3fb27SDimitry Andric return; 251406c3fb27SDimitry Andric } 251506c3fb27SDimitry Andric 251606c3fb27SDimitry Andric C.addTransition(State); 251706c3fb27SDimitry Andric } 251806c3fb27SDimitry Andric 25190b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 25200b57cec5SDimitry Andric // The driver method, and other Checker callbacks. 25210b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 25220b57cec5SDimitry Andric 25230b57cec5SDimitry Andric CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call, 25240b57cec5SDimitry Andric CheckerContext &C) const { 25250b57cec5SDimitry Andric const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr()); 25260b57cec5SDimitry Andric if (!CE) 25270b57cec5SDimitry Andric return nullptr; 25280b57cec5SDimitry Andric 25290b57cec5SDimitry Andric const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl()); 25300b57cec5SDimitry Andric if (!FD) 25310b57cec5SDimitry Andric return nullptr; 25320b57cec5SDimitry Andric 2533349cc55cSDimitry Andric if (StdCopy.matches(Call)) 25340b57cec5SDimitry Andric return &CStringChecker::evalStdCopy; 2535349cc55cSDimitry Andric if (StdCopyBackward.matches(Call)) 25360b57cec5SDimitry Andric return &CStringChecker::evalStdCopyBackward; 25370b57cec5SDimitry Andric 25380b57cec5SDimitry Andric // Pro-actively check that argument types are safe to do arithmetic upon. 25390b57cec5SDimitry Andric // We do not want to crash if someone accidentally passes a structure 25400b57cec5SDimitry Andric // into, say, a C++ overload of any of these functions. We could not check 25410b57cec5SDimitry Andric // that for std::copy because they may have arguments of other types. 25420b57cec5SDimitry Andric for (auto I : CE->arguments()) { 25430b57cec5SDimitry Andric QualType T = I->getType(); 25440b57cec5SDimitry Andric if (!T->isIntegralOrEnumerationType() && !T->isPointerType()) 25450b57cec5SDimitry Andric return nullptr; 25460b57cec5SDimitry Andric } 25470b57cec5SDimitry Andric 25480b57cec5SDimitry Andric const FnCheck *Callback = Callbacks.lookup(Call); 25490b57cec5SDimitry Andric if (Callback) 25500b57cec5SDimitry Andric return *Callback; 25510b57cec5SDimitry Andric 25520b57cec5SDimitry Andric return nullptr; 25530b57cec5SDimitry Andric } 25540b57cec5SDimitry Andric 25550b57cec5SDimitry Andric bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const { 25560b57cec5SDimitry Andric FnCheck Callback = identifyCall(Call, C); 25570b57cec5SDimitry Andric 25580b57cec5SDimitry Andric // If the callee isn't a string function, let another checker handle it. 25590b57cec5SDimitry Andric if (!Callback) 25600b57cec5SDimitry Andric return false; 25610b57cec5SDimitry Andric 25620b57cec5SDimitry Andric // Check and evaluate the call. 2563*647cbc5dSDimitry Andric assert(isa<CallExpr>(Call.getOriginExpr())); 2564*647cbc5dSDimitry Andric Callback(this, C, Call); 25650b57cec5SDimitry Andric 25660b57cec5SDimitry Andric // If the evaluate call resulted in no change, chain to the next eval call 25670b57cec5SDimitry Andric // handler. 25680b57cec5SDimitry Andric // Note, the custom CString evaluation calls assume that basic safety 25690b57cec5SDimitry Andric // properties are held. However, if the user chooses to turn off some of these 25700b57cec5SDimitry Andric // checks, we ignore the issues and leave the call evaluation to a generic 25710b57cec5SDimitry Andric // handler. 25720b57cec5SDimitry Andric return C.isDifferent(); 25730b57cec5SDimitry Andric } 25740b57cec5SDimitry Andric 25750b57cec5SDimitry Andric void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { 25760b57cec5SDimitry Andric // Record string length for char a[] = "abc"; 25770b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 25780b57cec5SDimitry Andric 25790b57cec5SDimitry Andric for (const auto *I : DS->decls()) { 25800b57cec5SDimitry Andric const VarDecl *D = dyn_cast<VarDecl>(I); 25810b57cec5SDimitry Andric if (!D) 25820b57cec5SDimitry Andric continue; 25830b57cec5SDimitry Andric 25840b57cec5SDimitry Andric // FIXME: Handle array fields of structs. 25850b57cec5SDimitry Andric if (!D->getType()->isArrayType()) 25860b57cec5SDimitry Andric continue; 25870b57cec5SDimitry Andric 25880b57cec5SDimitry Andric const Expr *Init = D->getInit(); 25890b57cec5SDimitry Andric if (!Init) 25900b57cec5SDimitry Andric continue; 25910b57cec5SDimitry Andric if (!isa<StringLiteral>(Init)) 25920b57cec5SDimitry Andric continue; 25930b57cec5SDimitry Andric 25940b57cec5SDimitry Andric Loc VarLoc = state->getLValue(D, C.getLocationContext()); 25950b57cec5SDimitry Andric const MemRegion *MR = VarLoc.getAsRegion(); 25960b57cec5SDimitry Andric if (!MR) 25970b57cec5SDimitry Andric continue; 25980b57cec5SDimitry Andric 25990b57cec5SDimitry Andric SVal StrVal = C.getSVal(Init); 26000b57cec5SDimitry Andric assert(StrVal.isValid() && "Initializer string is unknown or undefined"); 26010b57cec5SDimitry Andric DefinedOrUnknownSVal strLength = 26020b57cec5SDimitry Andric getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>(); 26030b57cec5SDimitry Andric 26040b57cec5SDimitry Andric state = state->set<CStringLength>(MR, strLength); 26050b57cec5SDimitry Andric } 26060b57cec5SDimitry Andric 26070b57cec5SDimitry Andric C.addTransition(state); 26080b57cec5SDimitry Andric } 26090b57cec5SDimitry Andric 26100b57cec5SDimitry Andric ProgramStateRef 26110b57cec5SDimitry Andric CStringChecker::checkRegionChanges(ProgramStateRef state, 26120b57cec5SDimitry Andric const InvalidatedSymbols *, 26130b57cec5SDimitry Andric ArrayRef<const MemRegion *> ExplicitRegions, 26140b57cec5SDimitry Andric ArrayRef<const MemRegion *> Regions, 26150b57cec5SDimitry Andric const LocationContext *LCtx, 26160b57cec5SDimitry Andric const CallEvent *Call) const { 26170b57cec5SDimitry Andric CStringLengthTy Entries = state->get<CStringLength>(); 26180b57cec5SDimitry Andric if (Entries.isEmpty()) 26190b57cec5SDimitry Andric return state; 26200b57cec5SDimitry Andric 26210b57cec5SDimitry Andric llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; 26220b57cec5SDimitry Andric llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; 26230b57cec5SDimitry Andric 26240b57cec5SDimitry Andric // First build sets for the changed regions and their super-regions. 262506c3fb27SDimitry Andric for (const MemRegion *MR : Regions) { 26260b57cec5SDimitry Andric Invalidated.insert(MR); 26270b57cec5SDimitry Andric 26280b57cec5SDimitry Andric SuperRegions.insert(MR); 26290b57cec5SDimitry Andric while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { 26300b57cec5SDimitry Andric MR = SR->getSuperRegion(); 26310b57cec5SDimitry Andric SuperRegions.insert(MR); 26320b57cec5SDimitry Andric } 26330b57cec5SDimitry Andric } 26340b57cec5SDimitry Andric 26350b57cec5SDimitry Andric CStringLengthTy::Factory &F = state->get_context<CStringLength>(); 26360b57cec5SDimitry Andric 26370b57cec5SDimitry Andric // Then loop over the entries in the current state. 263806c3fb27SDimitry Andric for (const MemRegion *MR : llvm::make_first_range(Entries)) { 26390b57cec5SDimitry Andric // Is this entry for a super-region of a changed region? 26400b57cec5SDimitry Andric if (SuperRegions.count(MR)) { 26410b57cec5SDimitry Andric Entries = F.remove(Entries, MR); 26420b57cec5SDimitry Andric continue; 26430b57cec5SDimitry Andric } 26440b57cec5SDimitry Andric 26450b57cec5SDimitry Andric // Is this entry for a sub-region of a changed region? 26460b57cec5SDimitry Andric const MemRegion *Super = MR; 26470b57cec5SDimitry Andric while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { 26480b57cec5SDimitry Andric Super = SR->getSuperRegion(); 26490b57cec5SDimitry Andric if (Invalidated.count(Super)) { 26500b57cec5SDimitry Andric Entries = F.remove(Entries, MR); 26510b57cec5SDimitry Andric break; 26520b57cec5SDimitry Andric } 26530b57cec5SDimitry Andric } 26540b57cec5SDimitry Andric } 26550b57cec5SDimitry Andric 26560b57cec5SDimitry Andric return state->set<CStringLength>(Entries); 26570b57cec5SDimitry Andric } 26580b57cec5SDimitry Andric 26590b57cec5SDimitry Andric void CStringChecker::checkLiveSymbols(ProgramStateRef state, 26600b57cec5SDimitry Andric SymbolReaper &SR) const { 26610b57cec5SDimitry Andric // Mark all symbols in our string length map as valid. 26620b57cec5SDimitry Andric CStringLengthTy Entries = state->get<CStringLength>(); 26630b57cec5SDimitry Andric 266406c3fb27SDimitry Andric for (SVal Len : llvm::make_second_range(Entries)) { 266506c3fb27SDimitry Andric for (SymbolRef Sym : Len.symbols()) 266606c3fb27SDimitry Andric SR.markInUse(Sym); 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric } 26690b57cec5SDimitry Andric 26700b57cec5SDimitry Andric void CStringChecker::checkDeadSymbols(SymbolReaper &SR, 26710b57cec5SDimitry Andric CheckerContext &C) const { 26720b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 26730b57cec5SDimitry Andric CStringLengthTy Entries = state->get<CStringLength>(); 26740b57cec5SDimitry Andric if (Entries.isEmpty()) 26750b57cec5SDimitry Andric return; 26760b57cec5SDimitry Andric 26770b57cec5SDimitry Andric CStringLengthTy::Factory &F = state->get_context<CStringLength>(); 267806c3fb27SDimitry Andric for (auto [Reg, Len] : Entries) { 26790b57cec5SDimitry Andric if (SymbolRef Sym = Len.getAsSymbol()) { 26800b57cec5SDimitry Andric if (SR.isDead(Sym)) 268106c3fb27SDimitry Andric Entries = F.remove(Entries, Reg); 26820b57cec5SDimitry Andric } 26830b57cec5SDimitry Andric } 26840b57cec5SDimitry Andric 26850b57cec5SDimitry Andric state = state->set<CStringLength>(Entries); 26860b57cec5SDimitry Andric C.addTransition(state); 26870b57cec5SDimitry Andric } 26880b57cec5SDimitry Andric 26890b57cec5SDimitry Andric void ento::registerCStringModeling(CheckerManager &Mgr) { 26900b57cec5SDimitry Andric Mgr.registerChecker<CStringChecker>(); 26910b57cec5SDimitry Andric } 26920b57cec5SDimitry Andric 26935ffd83dbSDimitry Andric bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) { 26940b57cec5SDimitry Andric return true; 26950b57cec5SDimitry Andric } 26960b57cec5SDimitry Andric 26970b57cec5SDimitry Andric #define REGISTER_CHECKER(name) \ 26980b57cec5SDimitry Andric void ento::register##name(CheckerManager &mgr) { \ 26990b57cec5SDimitry Andric CStringChecker *checker = mgr.getChecker<CStringChecker>(); \ 27000b57cec5SDimitry Andric checker->Filter.Check##name = true; \ 2701a7dea167SDimitry Andric checker->Filter.CheckName##name = mgr.getCurrentCheckerName(); \ 27020b57cec5SDimitry Andric } \ 27030b57cec5SDimitry Andric \ 27045ffd83dbSDimitry Andric bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; } 27050b57cec5SDimitry Andric 27060b57cec5SDimitry Andric REGISTER_CHECKER(CStringNullArg) 27070b57cec5SDimitry Andric REGISTER_CHECKER(CStringOutOfBounds) 27080b57cec5SDimitry Andric REGISTER_CHECKER(CStringBufferOverlap) 27090b57cec5SDimitry Andric REGISTER_CHECKER(CStringNotNullTerm) 271081ad6265SDimitry Andric REGISTER_CHECKER(CStringUninitializedRead) 2711