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 &, 124972a253aSDimitry Andric const CallExpr *)>; 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; 176bdd1243dSDimitry Andric void evalMemcpy(CheckerContext &C, const CallExpr *CE, CharKind CK) const; 177bdd1243dSDimitry Andric void evalMempcpy(CheckerContext &C, const CallExpr *CE, CharKind CK) const; 178bdd1243dSDimitry Andric void evalMemmove(CheckerContext &C, const CallExpr *CE, CharKind CK) const; 1790b57cec5SDimitry Andric void evalBcopy(CheckerContext &C, const CallExpr *CE) const; 1800b57cec5SDimitry Andric void evalCopyCommon(CheckerContext &C, const CallExpr *CE, 1815ffd83dbSDimitry Andric ProgramStateRef state, SizeArgExpr Size, 1825ffd83dbSDimitry Andric DestinationArgExpr Dest, SourceArgExpr Source, 183bdd1243dSDimitry Andric bool Restricted, bool IsMempcpy, CharKind CK) const; 1840b57cec5SDimitry Andric 185bdd1243dSDimitry Andric void evalMemcmp(CheckerContext &C, const CallExpr *CE, CharKind CK) const; 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric void evalstrLength(CheckerContext &C, const CallExpr *CE) const; 1880b57cec5SDimitry Andric void evalstrnLength(CheckerContext &C, const CallExpr *CE) const; 1890b57cec5SDimitry Andric void evalstrLengthCommon(CheckerContext &C, 1900b57cec5SDimitry Andric const CallExpr *CE, 1910b57cec5SDimitry Andric bool IsStrnlen = false) const; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric void evalStrcpy(CheckerContext &C, const CallExpr *CE) const; 1940b57cec5SDimitry Andric void evalStrncpy(CheckerContext &C, const CallExpr *CE) const; 1950b57cec5SDimitry Andric void evalStpcpy(CheckerContext &C, const CallExpr *CE) const; 1960b57cec5SDimitry Andric void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const; 197480093f4SDimitry Andric void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd, 198480093f4SDimitry Andric bool IsBounded, ConcatFnKind appendK, 1990b57cec5SDimitry Andric bool returnPtr = true) const; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric void evalStrcat(CheckerContext &C, const CallExpr *CE) const; 2020b57cec5SDimitry Andric void evalStrncat(CheckerContext &C, const CallExpr *CE) const; 2030b57cec5SDimitry Andric void evalStrlcat(CheckerContext &C, const CallExpr *CE) const; 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric void evalStrcmp(CheckerContext &C, const CallExpr *CE) const; 2060b57cec5SDimitry Andric void evalStrncmp(CheckerContext &C, const CallExpr *CE) const; 2070b57cec5SDimitry Andric void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const; 2080b57cec5SDimitry Andric void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const; 2090b57cec5SDimitry Andric void evalStrcmpCommon(CheckerContext &C, 2100b57cec5SDimitry Andric const CallExpr *CE, 211480093f4SDimitry Andric bool IsBounded = false, 212480093f4SDimitry Andric bool IgnoreCase = false) const; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric void evalStrsep(CheckerContext &C, const CallExpr *CE) const; 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric void evalStdCopy(CheckerContext &C, const CallExpr *CE) const; 2170b57cec5SDimitry Andric void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const; 2180b57cec5SDimitry Andric void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const; 2190b57cec5SDimitry Andric void evalMemset(CheckerContext &C, const CallExpr *CE) const; 2200b57cec5SDimitry Andric void evalBzero(CheckerContext &C, const CallExpr *CE) const; 2210b57cec5SDimitry Andric 22206c3fb27SDimitry Andric void evalSprintf(CheckerContext &C, const CallExpr *CE) const; 22306c3fb27SDimitry Andric void evalSnprintf(CheckerContext &C, const CallExpr *CE) const; 22406c3fb27SDimitry Andric void evalSprintfCommon(CheckerContext &C, const CallExpr *CE, bool IsBounded, 22506c3fb27SDimitry Andric bool IsBuiltin) const; 22606c3fb27SDimitry Andric 2270b57cec5SDimitry Andric // Utility methods 2280b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef > 2290b57cec5SDimitry Andric static assumeZero(CheckerContext &C, 2300b57cec5SDimitry Andric ProgramStateRef state, SVal V, QualType Ty); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric static ProgramStateRef setCStringLength(ProgramStateRef state, 2330b57cec5SDimitry Andric const MemRegion *MR, 2340b57cec5SDimitry Andric SVal strLength); 2350b57cec5SDimitry Andric static SVal getCStringLengthForRegion(CheckerContext &C, 2360b57cec5SDimitry Andric ProgramStateRef &state, 2370b57cec5SDimitry Andric const Expr *Ex, 2380b57cec5SDimitry Andric const MemRegion *MR, 2390b57cec5SDimitry Andric bool hypothetical); 2400b57cec5SDimitry Andric SVal getCStringLength(CheckerContext &C, 2410b57cec5SDimitry Andric ProgramStateRef &state, 2420b57cec5SDimitry Andric const Expr *Ex, 2430b57cec5SDimitry Andric SVal Buf, 2440b57cec5SDimitry Andric bool hypothetical = false) const; 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric const StringLiteral *getCStringLiteral(CheckerContext &C, 2470b57cec5SDimitry Andric ProgramStateRef &state, 2480b57cec5SDimitry Andric const Expr *expr, 2490b57cec5SDimitry Andric SVal val) const; 2500b57cec5SDimitry Andric 25106c3fb27SDimitry Andric /// Invalidate the destination buffer determined by characters copied. 25206c3fb27SDimitry Andric static ProgramStateRef 25306c3fb27SDimitry Andric invalidateDestinationBufferBySize(CheckerContext &C, ProgramStateRef S, 25406c3fb27SDimitry Andric const Expr *BufE, SVal BufV, SVal SizeV, 25506c3fb27SDimitry Andric QualType SizeTy); 25606c3fb27SDimitry Andric 25706c3fb27SDimitry Andric /// Operation never overflows, do not invalidate the super region. 25806c3fb27SDimitry Andric static ProgramStateRef invalidateDestinationBufferNeverOverflows( 25906c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV); 26006c3fb27SDimitry Andric 26106c3fb27SDimitry Andric /// We do not know whether the operation can overflow (e.g. size is unknown), 26206c3fb27SDimitry Andric /// invalidate the super region and escape related pointers. 26306c3fb27SDimitry Andric static ProgramStateRef invalidateDestinationBufferAlwaysEscapeSuperRegion( 26406c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV); 26506c3fb27SDimitry Andric 26606c3fb27SDimitry Andric /// Invalidate the source buffer for escaping pointers. 26706c3fb27SDimitry Andric static ProgramStateRef invalidateSourceBuffer(CheckerContext &C, 26806c3fb27SDimitry Andric ProgramStateRef S, 26906c3fb27SDimitry Andric const Expr *BufE, SVal BufV); 27006c3fb27SDimitry Andric 27106c3fb27SDimitry Andric /// @param InvalidationTraitOperations Determine how to invlidate the 27206c3fb27SDimitry Andric /// MemRegion by setting the invalidation traits. Return true to cause pointer 27306c3fb27SDimitry Andric /// escape, or false otherwise. 27406c3fb27SDimitry Andric static ProgramStateRef invalidateBufferAux( 27506c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef State, const Expr *Ex, SVal V, 27606c3fb27SDimitry Andric llvm::function_ref<bool(RegionAndSymbolInvalidationTraits &, 27706c3fb27SDimitry Andric const MemRegion *)> 27806c3fb27SDimitry Andric InvalidationTraitOperations); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx, 2810b57cec5SDimitry Andric const MemRegion *MR); 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric static bool memsetAux(const Expr *DstBuffer, SVal CharE, 2840b57cec5SDimitry Andric const Expr *Size, CheckerContext &C, 2850b57cec5SDimitry Andric ProgramStateRef &State); 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric // Re-usable checks 2885ffd83dbSDimitry Andric ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State, 2895ffd83dbSDimitry Andric AnyArgExpr Arg, SVal l) const; 2905ffd83dbSDimitry Andric ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state, 2915ffd83dbSDimitry Andric AnyArgExpr Buffer, SVal Element, 292bdd1243dSDimitry Andric AccessKind Access, 293bdd1243dSDimitry Andric CharKind CK = CharKind::Regular) const; 2945ffd83dbSDimitry Andric ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State, 2955ffd83dbSDimitry Andric AnyArgExpr Buffer, SizeArgExpr Size, 296972a253aSDimitry Andric AccessKind Access, 297bdd1243dSDimitry Andric CharKind CK = CharKind::Regular) const; 2985ffd83dbSDimitry Andric ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state, 2995ffd83dbSDimitry Andric SizeArgExpr Size, AnyArgExpr First, 300bdd1243dSDimitry Andric AnyArgExpr Second, 301bdd1243dSDimitry Andric CharKind CK = CharKind::Regular) const; 3020b57cec5SDimitry Andric void emitOverlapBug(CheckerContext &C, 3030b57cec5SDimitry Andric ProgramStateRef state, 3040b57cec5SDimitry Andric const Stmt *First, 3050b57cec5SDimitry Andric const Stmt *Second) const; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S, 3080b57cec5SDimitry Andric StringRef WarningMsg) const; 3090b57cec5SDimitry Andric void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State, 3100b57cec5SDimitry Andric const Stmt *S, StringRef WarningMsg) const; 3110b57cec5SDimitry Andric void emitNotCStringBug(CheckerContext &C, ProgramStateRef State, 3120b57cec5SDimitry Andric const Stmt *S, StringRef WarningMsg) const; 3130b57cec5SDimitry Andric void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const; 31481ad6265SDimitry Andric void emitUninitializedReadBug(CheckerContext &C, ProgramStateRef State, 31581ad6265SDimitry Andric const Expr *E) const; 3160b57cec5SDimitry Andric ProgramStateRef checkAdditionOverflow(CheckerContext &C, 3170b57cec5SDimitry Andric ProgramStateRef state, 3180b57cec5SDimitry Andric NonLoc left, 3190b57cec5SDimitry Andric NonLoc right) const; 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Return true if the destination buffer of the copy function may be in bound. 3220b57cec5SDimitry Andric // Expects SVal of Size to be positive and unsigned. 3230b57cec5SDimitry Andric // Expects SVal of FirstBuf to be a FieldRegion. 32406c3fb27SDimitry Andric static bool isFirstBufInBound(CheckerContext &C, ProgramStateRef State, 32506c3fb27SDimitry Andric SVal BufVal, QualType BufTy, SVal LengthVal, 32606c3fb27SDimitry Andric QualType LengthTy); 3270b57cec5SDimitry Andric }; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric } //end anonymous namespace 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal) 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3340b57cec5SDimitry Andric // Individual checks and utility methods. 3350b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef > 3380b57cec5SDimitry Andric CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V, 3390b57cec5SDimitry Andric QualType Ty) { 340bdd1243dSDimitry Andric std::optional<DefinedSVal> val = V.getAs<DefinedSVal>(); 3410b57cec5SDimitry Andric if (!val) 3420b57cec5SDimitry Andric return std::pair<ProgramStateRef , ProgramStateRef >(state, state); 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 3450b57cec5SDimitry Andric DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); 3460b57cec5SDimitry Andric return state->assume(svalBuilder.evalEQ(state, *val, zero)); 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C, 3505ffd83dbSDimitry Andric ProgramStateRef State, 3515ffd83dbSDimitry Andric AnyArgExpr Arg, SVal l) const { 3520b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 3535ffd83dbSDimitry Andric if (!State) 3540b57cec5SDimitry Andric return nullptr; 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric ProgramStateRef stateNull, stateNonNull; 3575ffd83dbSDimitry Andric std::tie(stateNull, stateNonNull) = 3585ffd83dbSDimitry Andric assumeZero(C, State, l, Arg.Expression->getType()); 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric if (stateNull && !stateNonNull) { 3610b57cec5SDimitry Andric if (Filter.CheckCStringNullArg) { 3620b57cec5SDimitry Andric SmallString<80> buf; 363a7dea167SDimitry Andric llvm::raw_svector_ostream OS(buf); 3640b57cec5SDimitry Andric assert(CurrentFunctionDescription); 3655ffd83dbSDimitry Andric OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1) 3665ffd83dbSDimitry Andric << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to " 367480093f4SDimitry Andric << CurrentFunctionDescription; 3680b57cec5SDimitry Andric 3695ffd83dbSDimitry Andric emitNullArgBug(C, stateNull, Arg.Expression, OS.str()); 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric return nullptr; 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // From here on, assume that the value is non-null. 3750b57cec5SDimitry Andric assert(stateNonNull); 3760b57cec5SDimitry Andric return stateNonNull; 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? 3800b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, 3810b57cec5SDimitry Andric ProgramStateRef state, 3825ffd83dbSDimitry Andric AnyArgExpr Buffer, SVal Element, 383972a253aSDimitry Andric AccessKind Access, 384bdd1243dSDimitry Andric CharKind CK) const { 3855ffd83dbSDimitry Andric 3860b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 3870b57cec5SDimitry Andric if (!state) 3880b57cec5SDimitry Andric return nullptr; 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric // Check for out of bound array element access. 3915ffd83dbSDimitry Andric const MemRegion *R = Element.getAsRegion(); 3920b57cec5SDimitry Andric if (!R) 3930b57cec5SDimitry Andric return state; 3940b57cec5SDimitry Andric 3955ffd83dbSDimitry Andric const auto *ER = dyn_cast<ElementRegion>(R); 3960b57cec5SDimitry Andric if (!ER) 3970b57cec5SDimitry Andric return state; 3980b57cec5SDimitry Andric 399972a253aSDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 400972a253aSDimitry Andric ASTContext &Ctx = svalBuilder.getContext(); 401972a253aSDimitry Andric 402972a253aSDimitry Andric // Get the index of the accessed element. 403972a253aSDimitry Andric NonLoc Idx = ER->getIndex(); 404972a253aSDimitry Andric 405bdd1243dSDimitry Andric if (CK == CharKind::Regular) { 406972a253aSDimitry Andric if (ER->getValueType() != Ctx.CharTy) 4070b57cec5SDimitry Andric return state; 408972a253aSDimitry Andric } else { 409972a253aSDimitry Andric if (ER->getValueType() != Ctx.WideCharTy) 410972a253aSDimitry Andric return state; 411972a253aSDimitry Andric 412972a253aSDimitry Andric QualType SizeTy = Ctx.getSizeType(); 413972a253aSDimitry Andric NonLoc WideSize = 414972a253aSDimitry Andric svalBuilder 415972a253aSDimitry Andric .makeIntVal(Ctx.getTypeSizeInChars(Ctx.WideCharTy).getQuantity(), 416972a253aSDimitry Andric SizeTy) 417972a253aSDimitry Andric .castAs<NonLoc>(); 418972a253aSDimitry Andric SVal Offset = svalBuilder.evalBinOpNN(state, BO_Mul, Idx, WideSize, SizeTy); 419972a253aSDimitry Andric if (Offset.isUnknown()) 420972a253aSDimitry Andric return state; 421972a253aSDimitry Andric Idx = Offset.castAs<NonLoc>(); 422972a253aSDimitry Andric } 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric // Get the size of the array. 4255ffd83dbSDimitry Andric const auto *superReg = cast<SubRegion>(ER->getSuperRegion()); 4265ffd83dbSDimitry Andric DefinedOrUnknownSVal Size = 427fe6060f1SDimitry Andric getDynamicExtent(state, superReg, C.getSValBuilder()); 4280b57cec5SDimitry Andric 42981ad6265SDimitry Andric ProgramStateRef StInBound, StOutBound; 43081ad6265SDimitry Andric std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, Size); 4310b57cec5SDimitry Andric if (StOutBound && !StInBound) { 4320b57cec5SDimitry Andric // These checks are either enabled by the CString out-of-bounds checker 4330b57cec5SDimitry Andric // explicitly or implicitly by the Malloc checker. 4340b57cec5SDimitry Andric // In the latter case we only do modeling but do not emit warning. 4350b57cec5SDimitry Andric if (!Filter.CheckCStringOutOfBounds) 4360b57cec5SDimitry Andric return nullptr; 4370b57cec5SDimitry Andric 4385ffd83dbSDimitry Andric // Emit a bug report. 4395ffd83dbSDimitry Andric ErrorMessage Message = 4405ffd83dbSDimitry Andric createOutOfBoundErrorMsg(CurrentFunctionDescription, Access); 4415ffd83dbSDimitry Andric emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message); 4420b57cec5SDimitry Andric return nullptr; 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 44581ad6265SDimitry Andric // Ensure that we wouldn't read uninitialized value. 44681ad6265SDimitry Andric if (Access == AccessKind::read) { 44781ad6265SDimitry Andric if (Filter.CheckCStringUninitializedRead && 44881ad6265SDimitry Andric StInBound->getSVal(ER).isUndef()) { 44981ad6265SDimitry Andric emitUninitializedReadBug(C, StInBound, Buffer.Expression); 45081ad6265SDimitry Andric return nullptr; 45181ad6265SDimitry Andric } 45281ad6265SDimitry Andric } 45381ad6265SDimitry Andric 4540b57cec5SDimitry Andric // Array bound check succeeded. From this point forward the array bound 4550b57cec5SDimitry Andric // should always succeed. 4560b57cec5SDimitry Andric return StInBound; 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric 459972a253aSDimitry Andric ProgramStateRef 460972a253aSDimitry Andric CStringChecker::CheckBufferAccess(CheckerContext &C, ProgramStateRef State, 461972a253aSDimitry Andric AnyArgExpr Buffer, SizeArgExpr Size, 462bdd1243dSDimitry Andric AccessKind Access, CharKind CK) const { 4630b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 4645ffd83dbSDimitry Andric if (!State) 4650b57cec5SDimitry Andric return nullptr; 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 4680b57cec5SDimitry Andric ASTContext &Ctx = svalBuilder.getContext(); 4690b57cec5SDimitry Andric 4705ffd83dbSDimitry Andric QualType SizeTy = Size.Expression->getType(); 471bdd1243dSDimitry Andric QualType PtrTy = getCharPtrType(Ctx, CK); 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric // Check that the first buffer is non-null. 4745ffd83dbSDimitry Andric SVal BufVal = C.getSVal(Buffer.Expression); 4755ffd83dbSDimitry Andric State = checkNonNull(C, State, Buffer, BufVal); 4765ffd83dbSDimitry Andric if (!State) 4770b57cec5SDimitry Andric return nullptr; 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric // If out-of-bounds checking is turned off, skip the rest. 4800b57cec5SDimitry Andric if (!Filter.CheckCStringOutOfBounds) 4815ffd83dbSDimitry Andric return State; 4820b57cec5SDimitry Andric 483*5f757f3fSDimitry Andric SVal BufStart = 484*5f757f3fSDimitry Andric svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType()); 485*5f757f3fSDimitry Andric 486*5f757f3fSDimitry Andric // Check if the first byte of the buffer is accessible. 487*5f757f3fSDimitry Andric State = CheckLocation(C, State, Buffer, BufStart, Access, CK); 488*5f757f3fSDimitry Andric if (!State) 489*5f757f3fSDimitry Andric return nullptr; 490*5f757f3fSDimitry Andric 4910b57cec5SDimitry Andric // Get the access length and make sure it is known. 4920b57cec5SDimitry Andric // FIXME: This assumes the caller has already checked that the access length 4930b57cec5SDimitry Andric // is positive. And that it's unsigned. 4945ffd83dbSDimitry Andric SVal LengthVal = C.getSVal(Size.Expression); 495bdd1243dSDimitry Andric std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 4960b57cec5SDimitry Andric if (!Length) 4975ffd83dbSDimitry Andric return State; 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // Compute the offset of the last element to be accessed: size-1. 5005ffd83dbSDimitry Andric NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>(); 5015ffd83dbSDimitry Andric SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy); 5020b57cec5SDimitry Andric if (Offset.isUnknown()) 5030b57cec5SDimitry Andric return nullptr; 5040b57cec5SDimitry Andric NonLoc LastOffset = Offset.castAs<NonLoc>(); 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric // Check that the first buffer is sufficiently long. 507bdd1243dSDimitry Andric if (std::optional<Loc> BufLoc = BufStart.getAs<Loc>()) { 5080b57cec5SDimitry Andric 5095ffd83dbSDimitry Andric SVal BufEnd = 5105ffd83dbSDimitry Andric svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy); 511bdd1243dSDimitry Andric State = CheckLocation(C, State, Buffer, BufEnd, Access, CK); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // If the buffer isn't large enough, abort. 5145ffd83dbSDimitry Andric if (!State) 5150b57cec5SDimitry Andric return nullptr; 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric // Large enough or not, return this state! 5195ffd83dbSDimitry Andric return State; 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C, 5230b57cec5SDimitry Andric ProgramStateRef state, 5245ffd83dbSDimitry Andric SizeArgExpr Size, AnyArgExpr First, 525972a253aSDimitry Andric AnyArgExpr Second, 526bdd1243dSDimitry Andric CharKind CK) const { 5270b57cec5SDimitry Andric if (!Filter.CheckCStringBufferOverlap) 5280b57cec5SDimitry Andric return state; 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric // Do a simple check for overlap: if the two arguments are from the same 5310b57cec5SDimitry Andric // buffer, see if the end of the first is greater than the start of the second 5320b57cec5SDimitry Andric // or vice versa. 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 5350b57cec5SDimitry Andric if (!state) 5360b57cec5SDimitry Andric return nullptr; 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric ProgramStateRef stateTrue, stateFalse; 5390b57cec5SDimitry Andric 54081ad6265SDimitry Andric // Assume different address spaces cannot overlap. 54181ad6265SDimitry Andric if (First.Expression->getType()->getPointeeType().getAddressSpace() != 54281ad6265SDimitry Andric Second.Expression->getType()->getPointeeType().getAddressSpace()) 54381ad6265SDimitry Andric return state; 54481ad6265SDimitry Andric 5450b57cec5SDimitry Andric // Get the buffer values and make sure they're known locations. 5460b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 5475ffd83dbSDimitry Andric SVal firstVal = state->getSVal(First.Expression, LCtx); 5485ffd83dbSDimitry Andric SVal secondVal = state->getSVal(Second.Expression, LCtx); 5490b57cec5SDimitry Andric 550bdd1243dSDimitry Andric std::optional<Loc> firstLoc = firstVal.getAs<Loc>(); 5510b57cec5SDimitry Andric if (!firstLoc) 5520b57cec5SDimitry Andric return state; 5530b57cec5SDimitry Andric 554bdd1243dSDimitry Andric std::optional<Loc> secondLoc = secondVal.getAs<Loc>(); 5550b57cec5SDimitry Andric if (!secondLoc) 5560b57cec5SDimitry Andric return state; 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric // Are the two values the same? 5590b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 5600b57cec5SDimitry Andric std::tie(stateTrue, stateFalse) = 5610b57cec5SDimitry Andric state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric if (stateTrue && !stateFalse) { 5640b57cec5SDimitry Andric // If the values are known to be equal, that's automatically an overlap. 5655ffd83dbSDimitry Andric emitOverlapBug(C, stateTrue, First.Expression, Second.Expression); 5660b57cec5SDimitry Andric return nullptr; 5670b57cec5SDimitry Andric } 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // assume the two expressions are not equal. 5700b57cec5SDimitry Andric assert(stateFalse); 5710b57cec5SDimitry Andric state = stateFalse; 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric // Which value comes first? 5740b57cec5SDimitry Andric QualType cmpTy = svalBuilder.getConditionType(); 5755ffd83dbSDimitry Andric SVal reverse = 5765ffd83dbSDimitry Andric svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy); 577bdd1243dSDimitry Andric std::optional<DefinedOrUnknownSVal> reverseTest = 5780b57cec5SDimitry Andric reverse.getAs<DefinedOrUnknownSVal>(); 5790b57cec5SDimitry Andric if (!reverseTest) 5800b57cec5SDimitry Andric return state; 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric std::tie(stateTrue, stateFalse) = state->assume(*reverseTest); 5830b57cec5SDimitry Andric if (stateTrue) { 5840b57cec5SDimitry Andric if (stateFalse) { 5850b57cec5SDimitry Andric // If we don't know which one comes first, we can't perform this test. 5860b57cec5SDimitry Andric return state; 5870b57cec5SDimitry Andric } else { 5880b57cec5SDimitry Andric // Switch the values so that firstVal is before secondVal. 5890b57cec5SDimitry Andric std::swap(firstLoc, secondLoc); 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric // Switch the Exprs as well, so that they still correspond. 5920b57cec5SDimitry Andric std::swap(First, Second); 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric // Get the length, and make sure it too is known. 5975ffd83dbSDimitry Andric SVal LengthVal = state->getSVal(Size.Expression, LCtx); 598bdd1243dSDimitry Andric std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 5990b57cec5SDimitry Andric if (!Length) 6000b57cec5SDimitry Andric return state; 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // Convert the first buffer's start address to char*. 6030b57cec5SDimitry Andric // Bail out if the cast fails. 6040b57cec5SDimitry Andric ASTContext &Ctx = svalBuilder.getContext(); 605bdd1243dSDimitry Andric QualType CharPtrTy = getCharPtrType(Ctx, CK); 6065ffd83dbSDimitry Andric SVal FirstStart = 6075ffd83dbSDimitry Andric svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType()); 608bdd1243dSDimitry Andric std::optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>(); 6090b57cec5SDimitry Andric if (!FirstStartLoc) 6100b57cec5SDimitry Andric return state; 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric // Compute the end of the first buffer. Bail out if THAT fails. 6135ffd83dbSDimitry Andric SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc, 6145ffd83dbSDimitry Andric *Length, CharPtrTy); 615bdd1243dSDimitry Andric std::optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>(); 6160b57cec5SDimitry Andric if (!FirstEndLoc) 6170b57cec5SDimitry Andric return state; 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric // Is the end of the first buffer past the start of the second buffer? 6205ffd83dbSDimitry Andric SVal Overlap = 6215ffd83dbSDimitry Andric svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy); 622bdd1243dSDimitry Andric std::optional<DefinedOrUnknownSVal> OverlapTest = 6230b57cec5SDimitry Andric Overlap.getAs<DefinedOrUnknownSVal>(); 6240b57cec5SDimitry Andric if (!OverlapTest) 6250b57cec5SDimitry Andric return state; 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric if (stateTrue && !stateFalse) { 6300b57cec5SDimitry Andric // Overlap! 6315ffd83dbSDimitry Andric emitOverlapBug(C, stateTrue, First.Expression, Second.Expression); 6320b57cec5SDimitry Andric return nullptr; 6330b57cec5SDimitry Andric } 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric // assume the two expressions don't overlap. 6360b57cec5SDimitry Andric assert(stateFalse); 6370b57cec5SDimitry Andric return stateFalse; 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state, 6410b57cec5SDimitry Andric const Stmt *First, const Stmt *Second) const { 6420b57cec5SDimitry Andric ExplodedNode *N = C.generateErrorNode(state); 6430b57cec5SDimitry Andric if (!N) 6440b57cec5SDimitry Andric return; 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric if (!BT_Overlap) 6470b57cec5SDimitry Andric BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap, 6480b57cec5SDimitry Andric categories::UnixAPI, "Improper arguments")); 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric // Generate a report for this bug. 651a7dea167SDimitry Andric auto report = std::make_unique<PathSensitiveBugReport>( 6520b57cec5SDimitry Andric *BT_Overlap, "Arguments must not be overlapping buffers", N); 6530b57cec5SDimitry Andric report->addRange(First->getSourceRange()); 6540b57cec5SDimitry Andric report->addRange(Second->getSourceRange()); 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric C.emitReport(std::move(report)); 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State, 6600b57cec5SDimitry Andric const Stmt *S, StringRef WarningMsg) const { 6610b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 662*5f757f3fSDimitry Andric if (!BT_Null) { 663*5f757f3fSDimitry Andric // FIXME: This call uses the string constant 'categories::UnixAPI' as the 664*5f757f3fSDimitry Andric // description of the bug; it should be replaced by a real description. 665*5f757f3fSDimitry Andric BT_Null.reset( 666*5f757f3fSDimitry Andric new BugType(Filter.CheckNameCStringNullArg, categories::UnixAPI)); 667*5f757f3fSDimitry Andric } 6680b57cec5SDimitry Andric 669*5f757f3fSDimitry Andric auto Report = 670*5f757f3fSDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_Null, WarningMsg, N); 6710b57cec5SDimitry Andric Report->addRange(S->getSourceRange()); 6720b57cec5SDimitry Andric if (const auto *Ex = dyn_cast<Expr>(S)) 6730b57cec5SDimitry Andric bugreporter::trackExpressionValue(N, Ex, *Report); 6740b57cec5SDimitry Andric C.emitReport(std::move(Report)); 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric } 6770b57cec5SDimitry Andric 67881ad6265SDimitry Andric void CStringChecker::emitUninitializedReadBug(CheckerContext &C, 67981ad6265SDimitry Andric ProgramStateRef State, 68081ad6265SDimitry Andric const Expr *E) const { 68181ad6265SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 68281ad6265SDimitry Andric const char *Msg = 68381ad6265SDimitry Andric "Bytes string function accesses uninitialized/garbage values"; 68481ad6265SDimitry Andric if (!BT_UninitRead) 685*5f757f3fSDimitry Andric BT_UninitRead.reset(new BugType(Filter.CheckNameCStringUninitializedRead, 686*5f757f3fSDimitry Andric "Accessing unitialized/garbage values")); 68781ad6265SDimitry Andric 688*5f757f3fSDimitry Andric auto Report = 689*5f757f3fSDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_UninitRead, Msg, N); 69081ad6265SDimitry Andric Report->addRange(E->getSourceRange()); 69181ad6265SDimitry Andric bugreporter::trackExpressionValue(N, E, *Report); 69281ad6265SDimitry Andric C.emitReport(std::move(Report)); 69381ad6265SDimitry Andric } 69481ad6265SDimitry Andric } 69581ad6265SDimitry Andric 6960b57cec5SDimitry Andric void CStringChecker::emitOutOfBoundsBug(CheckerContext &C, 6970b57cec5SDimitry Andric ProgramStateRef State, const Stmt *S, 6980b57cec5SDimitry Andric StringRef WarningMsg) const { 6990b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 7000b57cec5SDimitry Andric if (!BT_Bounds) 701*5f757f3fSDimitry Andric BT_Bounds.reset(new BugType(Filter.CheckCStringOutOfBounds 702*5f757f3fSDimitry Andric ? Filter.CheckNameCStringOutOfBounds 7030b57cec5SDimitry Andric : Filter.CheckNameCStringNullArg, 704*5f757f3fSDimitry Andric "Out-of-bound array access")); 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric // FIXME: It would be nice to eventually make this diagnostic more clear, 7070b57cec5SDimitry Andric // e.g., by referencing the original declaration or by saying *why* this 7080b57cec5SDimitry Andric // reference is outside the range. 709*5f757f3fSDimitry Andric auto Report = 710*5f757f3fSDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_Bounds, WarningMsg, N); 7110b57cec5SDimitry Andric Report->addRange(S->getSourceRange()); 7120b57cec5SDimitry Andric C.emitReport(std::move(Report)); 7130b57cec5SDimitry Andric } 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State, 7170b57cec5SDimitry Andric const Stmt *S, 7180b57cec5SDimitry Andric StringRef WarningMsg) const { 7190b57cec5SDimitry Andric if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) { 720*5f757f3fSDimitry Andric if (!BT_NotCString) { 721*5f757f3fSDimitry Andric // FIXME: This call uses the string constant 'categories::UnixAPI' as the 722*5f757f3fSDimitry Andric // description of the bug; it should be replaced by a real description. 723*5f757f3fSDimitry Andric BT_NotCString.reset( 724*5f757f3fSDimitry Andric new BugType(Filter.CheckNameCStringNotNullTerm, categories::UnixAPI)); 725*5f757f3fSDimitry Andric } 7260b57cec5SDimitry Andric 727a7dea167SDimitry Andric auto Report = 728a7dea167SDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N); 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric Report->addRange(S->getSourceRange()); 7310b57cec5SDimitry Andric C.emitReport(std::move(Report)); 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric } 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric void CStringChecker::emitAdditionOverflowBug(CheckerContext &C, 7360b57cec5SDimitry Andric ProgramStateRef State) const { 7370b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode(State)) { 738*5f757f3fSDimitry Andric if (!BT_AdditionOverflow) { 739*5f757f3fSDimitry Andric // FIXME: This call uses the word "API" as the description of the bug; 740*5f757f3fSDimitry Andric // it should be replaced by a better error message (if this unlikely 741*5f757f3fSDimitry Andric // situation continues to exist as a separate bug type). 74281ad6265SDimitry Andric BT_AdditionOverflow.reset( 743*5f757f3fSDimitry Andric new BugType(Filter.CheckNameCStringOutOfBounds, "API")); 744*5f757f3fSDimitry Andric } 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric // This isn't a great error message, but this should never occur in real 7470b57cec5SDimitry Andric // code anyway -- you'd have to create a buffer longer than a size_t can 7480b57cec5SDimitry Andric // represent, which is sort of a contradiction. 7490b57cec5SDimitry Andric const char *WarningMsg = 7500b57cec5SDimitry Andric "This expression will create a string whose length is too big to " 7510b57cec5SDimitry Andric "be represented as a size_t"; 7520b57cec5SDimitry Andric 75381ad6265SDimitry Andric auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow, 75481ad6265SDimitry Andric WarningMsg, N); 7550b57cec5SDimitry Andric C.emitReport(std::move(Report)); 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C, 7600b57cec5SDimitry Andric ProgramStateRef state, 7610b57cec5SDimitry Andric NonLoc left, 7620b57cec5SDimitry Andric NonLoc right) const { 7630b57cec5SDimitry Andric // If out-of-bounds checking is turned off, skip the rest. 7640b57cec5SDimitry Andric if (!Filter.CheckCStringOutOfBounds) 7650b57cec5SDimitry Andric return state; 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric // If a previous check has failed, propagate the failure. 7680b57cec5SDimitry Andric if (!state) 7690b57cec5SDimitry Andric return nullptr; 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 7720b57cec5SDimitry Andric BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 7750b57cec5SDimitry Andric const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); 7760b57cec5SDimitry Andric NonLoc maxVal = svalBuilder.makeIntVal(maxValInt); 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric SVal maxMinusRight; 77981ad6265SDimitry Andric if (isa<nonloc::ConcreteInt>(right)) { 7800b57cec5SDimitry Andric maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right, 7810b57cec5SDimitry Andric sizeTy); 7820b57cec5SDimitry Andric } else { 7830b57cec5SDimitry Andric // Try switching the operands. (The order of these two assignments is 7840b57cec5SDimitry Andric // important!) 7850b57cec5SDimitry Andric maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left, 7860b57cec5SDimitry Andric sizeTy); 7870b57cec5SDimitry Andric left = right; 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 790bdd1243dSDimitry Andric if (std::optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) { 7910b57cec5SDimitry Andric QualType cmpTy = svalBuilder.getConditionType(); 7920b57cec5SDimitry Andric // If left > max - right, we have an overflow. 7930b57cec5SDimitry Andric SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left, 7940b57cec5SDimitry Andric *maxMinusRightNL, cmpTy); 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric ProgramStateRef stateOverflow, stateOkay; 7970b57cec5SDimitry Andric std::tie(stateOverflow, stateOkay) = 7980b57cec5SDimitry Andric state->assume(willOverflow.castAs<DefinedOrUnknownSVal>()); 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric if (stateOverflow && !stateOkay) { 8010b57cec5SDimitry Andric // We have an overflow. Emit a bug report. 8020b57cec5SDimitry Andric emitAdditionOverflowBug(C, stateOverflow); 8030b57cec5SDimitry Andric return nullptr; 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric // From now on, assume an overflow didn't occur. 8070b57cec5SDimitry Andric assert(stateOkay); 8080b57cec5SDimitry Andric state = stateOkay; 8090b57cec5SDimitry Andric } 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric return state; 8120b57cec5SDimitry Andric } 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state, 8150b57cec5SDimitry Andric const MemRegion *MR, 8160b57cec5SDimitry Andric SVal strLength) { 8170b57cec5SDimitry Andric assert(!strLength.isUndef() && "Attempt to set an undefined string length"); 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric MR = MR->StripCasts(); 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric switch (MR->getKind()) { 8220b57cec5SDimitry Andric case MemRegion::StringRegionKind: 8230b57cec5SDimitry Andric // FIXME: This can happen if we strcpy() into a string region. This is 8240b57cec5SDimitry Andric // undefined [C99 6.4.5p6], but we should still warn about it. 8250b57cec5SDimitry Andric return state; 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric case MemRegion::SymbolicRegionKind: 8280b57cec5SDimitry Andric case MemRegion::AllocaRegionKind: 8295ffd83dbSDimitry Andric case MemRegion::NonParamVarRegionKind: 8305ffd83dbSDimitry Andric case MemRegion::ParamVarRegionKind: 8310b57cec5SDimitry Andric case MemRegion::FieldRegionKind: 8320b57cec5SDimitry Andric case MemRegion::ObjCIvarRegionKind: 8330b57cec5SDimitry Andric // These are the types we can currently track string lengths for. 8340b57cec5SDimitry Andric break; 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric case MemRegion::ElementRegionKind: 8370b57cec5SDimitry Andric // FIXME: Handle element regions by upper-bounding the parent region's 8380b57cec5SDimitry Andric // string length. 8390b57cec5SDimitry Andric return state; 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric default: 8420b57cec5SDimitry Andric // Other regions (mostly non-data) can't have a reliable C string length. 8430b57cec5SDimitry Andric // For now, just ignore the change. 8440b57cec5SDimitry Andric // FIXME: These are rare but not impossible. We should output some kind of 8450b57cec5SDimitry Andric // warning for things like strcpy((char[]){'a', 0}, "b"); 8460b57cec5SDimitry Andric return state; 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric if (strLength.isUnknown()) 8500b57cec5SDimitry Andric return state->remove<CStringLength>(MR); 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric return state->set<CStringLength>(MR, strLength); 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, 8560b57cec5SDimitry Andric ProgramStateRef &state, 8570b57cec5SDimitry Andric const Expr *Ex, 8580b57cec5SDimitry Andric const MemRegion *MR, 8590b57cec5SDimitry Andric bool hypothetical) { 8600b57cec5SDimitry Andric if (!hypothetical) { 8610b57cec5SDimitry Andric // If there's a recorded length, go ahead and return it. 8620b57cec5SDimitry Andric const SVal *Recorded = state->get<CStringLength>(MR); 8630b57cec5SDimitry Andric if (Recorded) 8640b57cec5SDimitry Andric return *Recorded; 8650b57cec5SDimitry Andric } 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric // Otherwise, get a new symbol and update the state. 8680b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 8690b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 8700b57cec5SDimitry Andric SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), 8710b57cec5SDimitry Andric MR, Ex, sizeTy, 8720b57cec5SDimitry Andric C.getLocationContext(), 8730b57cec5SDimitry Andric C.blockCount()); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric if (!hypothetical) { 876bdd1243dSDimitry Andric if (std::optional<NonLoc> strLn = strLength.getAs<NonLoc>()) { 8770b57cec5SDimitry Andric // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4 8780b57cec5SDimitry Andric BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 8790b57cec5SDimitry Andric const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); 8800b57cec5SDimitry Andric llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4); 8810b57cec5SDimitry Andric const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt, 8820b57cec5SDimitry Andric fourInt); 8830b57cec5SDimitry Andric NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt); 884*5f757f3fSDimitry Andric SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn, maxLength, 885*5f757f3fSDimitry Andric svalBuilder.getConditionType()); 8860b57cec5SDimitry Andric state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true); 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric state = state->set<CStringLength>(MR, strLength); 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric return strLength; 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state, 8950b57cec5SDimitry Andric const Expr *Ex, SVal Buf, 8960b57cec5SDimitry Andric bool hypothetical) const { 8970b57cec5SDimitry Andric const MemRegion *MR = Buf.getAsRegion(); 8980b57cec5SDimitry Andric if (!MR) { 8990b57cec5SDimitry Andric // If we can't get a region, see if it's something we /know/ isn't a 9000b57cec5SDimitry Andric // C string. In the context of locations, the only time we can issue such 9010b57cec5SDimitry Andric // a warning is for labels. 902bdd1243dSDimitry Andric if (std::optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) { 9030b57cec5SDimitry Andric if (Filter.CheckCStringNotNullTerm) { 9040b57cec5SDimitry Andric SmallString<120> buf; 9050b57cec5SDimitry Andric llvm::raw_svector_ostream os(buf); 9060b57cec5SDimitry Andric assert(CurrentFunctionDescription); 9070b57cec5SDimitry Andric os << "Argument to " << CurrentFunctionDescription 9080b57cec5SDimitry Andric << " is the address of the label '" << Label->getLabel()->getName() 9090b57cec5SDimitry Andric << "', which is not a null-terminated string"; 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric emitNotCStringBug(C, state, Ex, os.str()); 9120b57cec5SDimitry Andric } 9130b57cec5SDimitry Andric return UndefinedVal(); 9140b57cec5SDimitry Andric } 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric // If it's not a region and not a label, give up. 9170b57cec5SDimitry Andric return UnknownVal(); 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric // If we have a region, strip casts from it and see if we can figure out 9210b57cec5SDimitry Andric // its length. For anything we can't figure out, just return UnknownVal. 9220b57cec5SDimitry Andric MR = MR->StripCasts(); 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric switch (MR->getKind()) { 9250b57cec5SDimitry Andric case MemRegion::StringRegionKind: { 9260b57cec5SDimitry Andric // Modifying the contents of string regions is undefined [C99 6.4.5p6], 9270b57cec5SDimitry Andric // so we can assume that the byte length is the correct C string length. 9280b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 9290b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 9300b57cec5SDimitry Andric const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); 931753f127fSDimitry Andric return svalBuilder.makeIntVal(strLit->getLength(), sizeTy); 9320b57cec5SDimitry Andric } 933*5f757f3fSDimitry Andric case MemRegion::NonParamVarRegionKind: { 934*5f757f3fSDimitry Andric // If we have a global constant with a string literal initializer, 935*5f757f3fSDimitry Andric // compute the initializer's length. 936*5f757f3fSDimitry Andric const VarDecl *Decl = cast<NonParamVarRegion>(MR)->getDecl(); 937*5f757f3fSDimitry Andric if (Decl->getType().isConstQualified() && Decl->hasGlobalStorage()) { 938*5f757f3fSDimitry Andric if (const Expr *Init = Decl->getInit()) { 939*5f757f3fSDimitry Andric if (auto *StrLit = dyn_cast<StringLiteral>(Init)) { 940*5f757f3fSDimitry Andric SValBuilder &SvalBuilder = C.getSValBuilder(); 941*5f757f3fSDimitry Andric QualType SizeTy = SvalBuilder.getContext().getSizeType(); 942*5f757f3fSDimitry Andric return SvalBuilder.makeIntVal(StrLit->getLength(), SizeTy); 943*5f757f3fSDimitry Andric } 944*5f757f3fSDimitry Andric } 945*5f757f3fSDimitry Andric } 946*5f757f3fSDimitry Andric [[fallthrough]]; 947*5f757f3fSDimitry Andric } 9480b57cec5SDimitry Andric case MemRegion::SymbolicRegionKind: 9490b57cec5SDimitry Andric case MemRegion::AllocaRegionKind: 9505ffd83dbSDimitry Andric case MemRegion::ParamVarRegionKind: 9510b57cec5SDimitry Andric case MemRegion::FieldRegionKind: 9520b57cec5SDimitry Andric case MemRegion::ObjCIvarRegionKind: 9530b57cec5SDimitry Andric return getCStringLengthForRegion(C, state, Ex, MR, hypothetical); 9540b57cec5SDimitry Andric case MemRegion::CompoundLiteralRegionKind: 9550b57cec5SDimitry Andric // FIXME: Can we track this? Is it necessary? 9560b57cec5SDimitry Andric return UnknownVal(); 9570b57cec5SDimitry Andric case MemRegion::ElementRegionKind: 9580b57cec5SDimitry Andric // FIXME: How can we handle this? It's not good enough to subtract the 9590b57cec5SDimitry Andric // offset from the base string length; consider "123\x00567" and &a[5]. 9600b57cec5SDimitry Andric return UnknownVal(); 9610b57cec5SDimitry Andric default: 9620b57cec5SDimitry Andric // Other regions (mostly non-data) can't have a reliable C string length. 9630b57cec5SDimitry Andric // In this case, an error is emitted and UndefinedVal is returned. 9640b57cec5SDimitry Andric // The caller should always be prepared to handle this case. 9650b57cec5SDimitry Andric if (Filter.CheckCStringNotNullTerm) { 9660b57cec5SDimitry Andric SmallString<120> buf; 9670b57cec5SDimitry Andric llvm::raw_svector_ostream os(buf); 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric assert(CurrentFunctionDescription); 9700b57cec5SDimitry Andric os << "Argument to " << CurrentFunctionDescription << " is "; 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric if (SummarizeRegion(os, C.getASTContext(), MR)) 9730b57cec5SDimitry Andric os << ", which is not a null-terminated string"; 9740b57cec5SDimitry Andric else 9750b57cec5SDimitry Andric os << "not a null-terminated string"; 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric emitNotCStringBug(C, state, Ex, os.str()); 9780b57cec5SDimitry Andric } 9790b57cec5SDimitry Andric return UndefinedVal(); 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, 9840b57cec5SDimitry Andric ProgramStateRef &state, const Expr *expr, SVal val) const { 9850b57cec5SDimitry Andric 9860b57cec5SDimitry Andric // Get the memory region pointed to by the val. 9870b57cec5SDimitry Andric const MemRegion *bufRegion = val.getAsRegion(); 9880b57cec5SDimitry Andric if (!bufRegion) 9890b57cec5SDimitry Andric return nullptr; 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric // Strip casts off the memory region. 9920b57cec5SDimitry Andric bufRegion = bufRegion->StripCasts(); 9930b57cec5SDimitry Andric 9940b57cec5SDimitry Andric // Cast the memory region to a string region. 9950b57cec5SDimitry Andric const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); 9960b57cec5SDimitry Andric if (!strRegion) 9970b57cec5SDimitry Andric return nullptr; 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric // Return the actual string in the string region. 10000b57cec5SDimitry Andric return strRegion->getStringLiteral(); 10010b57cec5SDimitry Andric } 10020b57cec5SDimitry Andric 100306c3fb27SDimitry Andric bool CStringChecker::isFirstBufInBound(CheckerContext &C, ProgramStateRef State, 100406c3fb27SDimitry Andric SVal BufVal, QualType BufTy, 100506c3fb27SDimitry Andric SVal LengthVal, QualType LengthTy) { 10060b57cec5SDimitry Andric // If we do not know that the buffer is long enough we return 'true'. 10070b57cec5SDimitry Andric // Otherwise the parent region of this field region would also get 10080b57cec5SDimitry Andric // invalidated, which would lead to warnings based on an unknown state. 10090b57cec5SDimitry Andric 101006c3fb27SDimitry Andric if (LengthVal.isUnknown()) 101106c3fb27SDimitry Andric return false; 101206c3fb27SDimitry Andric 10130b57cec5SDimitry Andric // Originally copied from CheckBufferAccess and CheckLocation. 101406c3fb27SDimitry Andric SValBuilder &SB = C.getSValBuilder(); 101506c3fb27SDimitry Andric ASTContext &Ctx = C.getASTContext(); 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); 10180b57cec5SDimitry Andric 1019bdd1243dSDimitry Andric std::optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 10200b57cec5SDimitry Andric if (!Length) 10210b57cec5SDimitry Andric return true; // cf top comment. 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric // Compute the offset of the last element to be accessed: size-1. 102406c3fb27SDimitry Andric NonLoc One = SB.makeIntVal(1, LengthTy).castAs<NonLoc>(); 102506c3fb27SDimitry Andric SVal Offset = SB.evalBinOpNN(State, BO_Sub, *Length, One, LengthTy); 10260b57cec5SDimitry Andric if (Offset.isUnknown()) 10270b57cec5SDimitry Andric return true; // cf top comment 10280b57cec5SDimitry Andric NonLoc LastOffset = Offset.castAs<NonLoc>(); 10290b57cec5SDimitry Andric 10300b57cec5SDimitry Andric // Check that the first buffer is sufficiently long. 103106c3fb27SDimitry Andric SVal BufStart = SB.evalCast(BufVal, PtrTy, BufTy); 1032bdd1243dSDimitry Andric std::optional<Loc> BufLoc = BufStart.getAs<Loc>(); 10330b57cec5SDimitry Andric if (!BufLoc) 10340b57cec5SDimitry Andric return true; // cf top comment. 10350b57cec5SDimitry Andric 103606c3fb27SDimitry Andric SVal BufEnd = SB.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy); 10370b57cec5SDimitry Andric 10380b57cec5SDimitry Andric // Check for out of bound array element access. 10390b57cec5SDimitry Andric const MemRegion *R = BufEnd.getAsRegion(); 10400b57cec5SDimitry Andric if (!R) 10410b57cec5SDimitry Andric return true; // cf top comment. 10420b57cec5SDimitry Andric 10430b57cec5SDimitry Andric const ElementRegion *ER = dyn_cast<ElementRegion>(R); 10440b57cec5SDimitry Andric if (!ER) 10450b57cec5SDimitry Andric return true; // cf top comment. 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric // FIXME: Does this crash when a non-standard definition 10480b57cec5SDimitry Andric // of a library function is encountered? 10490b57cec5SDimitry Andric assert(ER->getValueType() == C.getASTContext().CharTy && 105006c3fb27SDimitry Andric "isFirstBufInBound should only be called with char* ElementRegions"); 10510b57cec5SDimitry Andric 10520b57cec5SDimitry Andric // Get the size of the array. 10530b57cec5SDimitry Andric const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); 105406c3fb27SDimitry Andric DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, superReg, SB); 10550b57cec5SDimitry Andric 10560b57cec5SDimitry Andric // Get the index of the accessed element. 10570b57cec5SDimitry Andric DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); 10580b57cec5SDimitry Andric 105906c3fb27SDimitry Andric ProgramStateRef StInBound = State->assumeInBound(Idx, SizeDV, true); 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric return static_cast<bool>(StInBound); 10620b57cec5SDimitry Andric } 10630b57cec5SDimitry Andric 106406c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateDestinationBufferBySize( 106506c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV, 106606c3fb27SDimitry Andric SVal SizeV, QualType SizeTy) { 106706c3fb27SDimitry Andric auto InvalidationTraitOperations = 106806c3fb27SDimitry Andric [&C, S, BufTy = BufE->getType(), BufV, SizeV, 106906c3fb27SDimitry Andric SizeTy](RegionAndSymbolInvalidationTraits &ITraits, const MemRegion *R) { 107006c3fb27SDimitry Andric // If destination buffer is a field region and access is in bound, do 107106c3fb27SDimitry Andric // not invalidate its super region. 107206c3fb27SDimitry Andric if (MemRegion::FieldRegionKind == R->getKind() && 107306c3fb27SDimitry Andric isFirstBufInBound(C, S, BufV, BufTy, SizeV, SizeTy)) { 107406c3fb27SDimitry Andric ITraits.setTrait( 107506c3fb27SDimitry Andric R, 107606c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 107706c3fb27SDimitry Andric } 107806c3fb27SDimitry Andric return false; 107906c3fb27SDimitry Andric }; 108006c3fb27SDimitry Andric 108106c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 108206c3fb27SDimitry Andric } 108306c3fb27SDimitry Andric 108406c3fb27SDimitry Andric ProgramStateRef 108506c3fb27SDimitry Andric CStringChecker::invalidateDestinationBufferAlwaysEscapeSuperRegion( 108606c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV) { 108706c3fb27SDimitry Andric auto InvalidationTraitOperations = [](RegionAndSymbolInvalidationTraits &, 108806c3fb27SDimitry Andric const MemRegion *R) { 108906c3fb27SDimitry Andric return isa<FieldRegion>(R); 109006c3fb27SDimitry Andric }; 109106c3fb27SDimitry Andric 109206c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 109306c3fb27SDimitry Andric } 109406c3fb27SDimitry Andric 109506c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateDestinationBufferNeverOverflows( 109606c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef S, const Expr *BufE, SVal BufV) { 109706c3fb27SDimitry Andric auto InvalidationTraitOperations = 109806c3fb27SDimitry Andric [](RegionAndSymbolInvalidationTraits &ITraits, const MemRegion *R) { 109906c3fb27SDimitry Andric if (MemRegion::FieldRegionKind == R->getKind()) 110006c3fb27SDimitry Andric ITraits.setTrait( 110106c3fb27SDimitry Andric R, 110206c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 110306c3fb27SDimitry Andric return false; 110406c3fb27SDimitry Andric }; 110506c3fb27SDimitry Andric 110606c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 110706c3fb27SDimitry Andric } 110806c3fb27SDimitry Andric 110906c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateSourceBuffer(CheckerContext &C, 111006c3fb27SDimitry Andric ProgramStateRef S, 111106c3fb27SDimitry Andric const Expr *BufE, 111206c3fb27SDimitry Andric SVal BufV) { 111306c3fb27SDimitry Andric auto InvalidationTraitOperations = 111406c3fb27SDimitry Andric [](RegionAndSymbolInvalidationTraits &ITraits, const MemRegion *R) { 111506c3fb27SDimitry Andric ITraits.setTrait( 111606c3fb27SDimitry Andric R->getBaseRegion(), 111706c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_PreserveContents); 111806c3fb27SDimitry Andric ITraits.setTrait(R, 111906c3fb27SDimitry Andric RegionAndSymbolInvalidationTraits::TK_SuppressEscape); 112006c3fb27SDimitry Andric return true; 112106c3fb27SDimitry Andric }; 112206c3fb27SDimitry Andric 112306c3fb27SDimitry Andric return invalidateBufferAux(C, S, BufE, BufV, InvalidationTraitOperations); 112406c3fb27SDimitry Andric } 112506c3fb27SDimitry Andric 112606c3fb27SDimitry Andric ProgramStateRef CStringChecker::invalidateBufferAux( 112706c3fb27SDimitry Andric CheckerContext &C, ProgramStateRef State, const Expr *E, SVal V, 112806c3fb27SDimitry Andric llvm::function_ref<bool(RegionAndSymbolInvalidationTraits &, 112906c3fb27SDimitry Andric const MemRegion *)> 113006c3fb27SDimitry Andric InvalidationTraitOperations) { 1131bdd1243dSDimitry Andric std::optional<Loc> L = V.getAs<Loc>(); 11320b57cec5SDimitry Andric if (!L) 113306c3fb27SDimitry Andric return State; 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes 11360b57cec5SDimitry Andric // some assumptions about the value that CFRefCount can't. Even so, it should 11370b57cec5SDimitry Andric // probably be refactored. 1138bdd1243dSDimitry Andric if (std::optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) { 11390b57cec5SDimitry Andric const MemRegion *R = MR->getRegion()->StripCasts(); 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric // Are we dealing with an ElementRegion? If so, we should be invalidating 11420b57cec5SDimitry Andric // the super-region. 11430b57cec5SDimitry Andric if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 11440b57cec5SDimitry Andric R = ER->getSuperRegion(); 11450b57cec5SDimitry Andric // FIXME: What about layers of ElementRegions? 11460b57cec5SDimitry Andric } 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // Invalidate this region. 11490b57cec5SDimitry Andric const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); 11500b57cec5SDimitry Andric RegionAndSymbolInvalidationTraits ITraits; 115106c3fb27SDimitry Andric bool CausesPointerEscape = InvalidationTraitOperations(ITraits, R); 11520b57cec5SDimitry Andric 115306c3fb27SDimitry Andric return State->invalidateRegions(R, E, C.blockCount(), LCtx, 11540b57cec5SDimitry Andric CausesPointerEscape, nullptr, nullptr, 11550b57cec5SDimitry Andric &ITraits); 11560b57cec5SDimitry Andric } 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric // If we have a non-region value by chance, just remove the binding. 11590b57cec5SDimitry Andric // FIXME: is this necessary or correct? This handles the non-Region 11600b57cec5SDimitry Andric // cases. Is it ever valid to store to these? 116106c3fb27SDimitry Andric return State->killBinding(*L); 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx, 11650b57cec5SDimitry Andric const MemRegion *MR) { 11660b57cec5SDimitry Andric switch (MR->getKind()) { 11670b57cec5SDimitry Andric case MemRegion::FunctionCodeRegionKind: { 1168480093f4SDimitry Andric if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl()) 11690b57cec5SDimitry Andric os << "the address of the function '" << *FD << '\''; 11700b57cec5SDimitry Andric else 11710b57cec5SDimitry Andric os << "the address of a function"; 11720b57cec5SDimitry Andric return true; 11730b57cec5SDimitry Andric } 11740b57cec5SDimitry Andric case MemRegion::BlockCodeRegionKind: 11750b57cec5SDimitry Andric os << "block text"; 11760b57cec5SDimitry Andric return true; 11770b57cec5SDimitry Andric case MemRegion::BlockDataRegionKind: 11780b57cec5SDimitry Andric os << "a block"; 11790b57cec5SDimitry Andric return true; 11800b57cec5SDimitry Andric case MemRegion::CXXThisRegionKind: 11810b57cec5SDimitry Andric case MemRegion::CXXTempObjectRegionKind: 1182480093f4SDimitry Andric os << "a C++ temp object of type " 118381ad6265SDimitry Andric << cast<TypedValueRegion>(MR)->getValueType(); 11840b57cec5SDimitry Andric return true; 11855ffd83dbSDimitry Andric case MemRegion::NonParamVarRegionKind: 118681ad6265SDimitry Andric os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType(); 11870b57cec5SDimitry Andric return true; 11885ffd83dbSDimitry Andric case MemRegion::ParamVarRegionKind: 118981ad6265SDimitry Andric os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType(); 11905ffd83dbSDimitry Andric return true; 11910b57cec5SDimitry Andric case MemRegion::FieldRegionKind: 119281ad6265SDimitry Andric os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType(); 11930b57cec5SDimitry Andric return true; 11940b57cec5SDimitry Andric case MemRegion::ObjCIvarRegionKind: 1195480093f4SDimitry Andric os << "an instance variable of type " 119681ad6265SDimitry Andric << cast<TypedValueRegion>(MR)->getValueType(); 11970b57cec5SDimitry Andric return true; 11980b57cec5SDimitry Andric default: 11990b57cec5SDimitry Andric return false; 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal, 12040b57cec5SDimitry Andric const Expr *Size, CheckerContext &C, 12050b57cec5SDimitry Andric ProgramStateRef &State) { 12060b57cec5SDimitry Andric SVal MemVal = C.getSVal(DstBuffer); 12070b57cec5SDimitry Andric SVal SizeVal = C.getSVal(Size); 12080b57cec5SDimitry Andric const MemRegion *MR = MemVal.getAsRegion(); 12090b57cec5SDimitry Andric if (!MR) 12100b57cec5SDimitry Andric return false; 12110b57cec5SDimitry Andric 12120b57cec5SDimitry Andric // We're about to model memset by producing a "default binding" in the Store. 12130b57cec5SDimitry Andric // Our current implementation - RegionStore - doesn't support default bindings 12140b57cec5SDimitry Andric // that don't cover the whole base region. So we should first get the offset 12150b57cec5SDimitry Andric // and the base region to figure out whether the offset of buffer is 0. 12160b57cec5SDimitry Andric RegionOffset Offset = MR->getAsOffset(); 12170b57cec5SDimitry Andric const MemRegion *BR = Offset.getRegion(); 12180b57cec5SDimitry Andric 1219bdd1243dSDimitry Andric std::optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>(); 12200b57cec5SDimitry Andric if (!SizeNL) 12210b57cec5SDimitry Andric return false; 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 12240b57cec5SDimitry Andric ASTContext &Ctx = C.getASTContext(); 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric // void *memset(void *dest, int ch, size_t count); 12270b57cec5SDimitry Andric // For now we can only handle the case of offset is 0 and concrete char value. 12280b57cec5SDimitry Andric if (Offset.isValid() && !Offset.hasSymbolicOffset() && 12290b57cec5SDimitry Andric Offset.getOffset() == 0) { 12305ffd83dbSDimitry Andric // Get the base region's size. 1231fe6060f1SDimitry Andric DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder); 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric ProgramStateRef StateWholeReg, StateNotWholeReg; 12340b57cec5SDimitry Andric std::tie(StateWholeReg, StateNotWholeReg) = 12355ffd83dbSDimitry Andric State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL)); 12360b57cec5SDimitry Andric 12370b57cec5SDimitry Andric // With the semantic of 'memset()', we should convert the CharVal to 12380b57cec5SDimitry Andric // unsigned char. 12390b57cec5SDimitry Andric CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy); 12400b57cec5SDimitry Andric 12410b57cec5SDimitry Andric ProgramStateRef StateNullChar, StateNonNullChar; 12420b57cec5SDimitry Andric std::tie(StateNullChar, StateNonNullChar) = 12430b57cec5SDimitry Andric assumeZero(C, State, CharVal, Ctx.UnsignedCharTy); 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric if (StateWholeReg && !StateNotWholeReg && StateNullChar && 12460b57cec5SDimitry Andric !StateNonNullChar) { 12470b57cec5SDimitry Andric // If the 'memset()' acts on the whole region of destination buffer and 12480b57cec5SDimitry Andric // the value of the second argument of 'memset()' is zero, bind the second 12490b57cec5SDimitry Andric // argument's value to the destination buffer with 'default binding'. 12500b57cec5SDimitry Andric // FIXME: Since there is no perfect way to bind the non-zero character, we 12510b57cec5SDimitry Andric // can only deal with zero value here. In the future, we need to deal with 12520b57cec5SDimitry Andric // the binding of non-zero value in the case of whole region. 12530b57cec5SDimitry Andric State = State->bindDefaultZero(svalBuilder.makeLoc(BR), 12540b57cec5SDimitry Andric C.getLocationContext()); 12550b57cec5SDimitry Andric } else { 12560b57cec5SDimitry Andric // If the destination buffer's extent is not equal to the value of 12570b57cec5SDimitry Andric // third argument, just invalidate buffer. 125806c3fb27SDimitry Andric State = invalidateDestinationBufferBySize(C, State, DstBuffer, MemVal, 125906c3fb27SDimitry Andric SizeVal, Size->getType()); 12600b57cec5SDimitry Andric } 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric if (StateNullChar && !StateNonNullChar) { 12630b57cec5SDimitry Andric // If the value of the second argument of 'memset()' is zero, set the 12640b57cec5SDimitry Andric // string length of destination buffer to 0 directly. 12650b57cec5SDimitry Andric State = setCStringLength(State, MR, 12660b57cec5SDimitry Andric svalBuilder.makeZeroVal(Ctx.getSizeType())); 12670b57cec5SDimitry Andric } else if (!StateNullChar && StateNonNullChar) { 12680b57cec5SDimitry Andric SVal NewStrLen = svalBuilder.getMetadataSymbolVal( 12690b57cec5SDimitry Andric CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(), 12700b57cec5SDimitry Andric C.getLocationContext(), C.blockCount()); 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric // If the value of second argument is not zero, then the string length 12730b57cec5SDimitry Andric // is at least the size argument. 12740b57cec5SDimitry Andric SVal NewStrLenGESize = svalBuilder.evalBinOp( 12750b57cec5SDimitry Andric State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType()); 12760b57cec5SDimitry Andric 12770b57cec5SDimitry Andric State = setCStringLength( 12780b57cec5SDimitry Andric State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true), 12790b57cec5SDimitry Andric MR, NewStrLen); 12800b57cec5SDimitry Andric } 12810b57cec5SDimitry Andric } else { 12820b57cec5SDimitry Andric // If the offset is not zero and char value is not concrete, we can do 12830b57cec5SDimitry Andric // nothing but invalidate the buffer. 128406c3fb27SDimitry Andric State = invalidateDestinationBufferBySize(C, State, DstBuffer, MemVal, 128506c3fb27SDimitry Andric SizeVal, Size->getType()); 12860b57cec5SDimitry Andric } 12870b57cec5SDimitry Andric return true; 12880b57cec5SDimitry Andric } 12890b57cec5SDimitry Andric 12900b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12910b57cec5SDimitry Andric // evaluation of individual function calls. 12920b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12930b57cec5SDimitry Andric 12945ffd83dbSDimitry Andric void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE, 12955ffd83dbSDimitry Andric ProgramStateRef state, SizeArgExpr Size, 12965ffd83dbSDimitry Andric DestinationArgExpr Dest, 12975ffd83dbSDimitry Andric SourceArgExpr Source, bool Restricted, 1298bdd1243dSDimitry Andric bool IsMempcpy, CharKind CK) const { 12990b57cec5SDimitry Andric CurrentFunctionDescription = "memory copy function"; 13000b57cec5SDimitry Andric 13010b57cec5SDimitry Andric // See if the size argument is zero. 13020b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 13035ffd83dbSDimitry Andric SVal sizeVal = state->getSVal(Size.Expression, LCtx); 13045ffd83dbSDimitry Andric QualType sizeTy = Size.Expression->getType(); 13050b57cec5SDimitry Andric 13060b57cec5SDimitry Andric ProgramStateRef stateZeroSize, stateNonZeroSize; 13070b57cec5SDimitry Andric std::tie(stateZeroSize, stateNonZeroSize) = 13080b57cec5SDimitry Andric assumeZero(C, state, sizeVal, sizeTy); 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric // Get the value of the Dest. 13115ffd83dbSDimitry Andric SVal destVal = state->getSVal(Dest.Expression, LCtx); 13120b57cec5SDimitry Andric 13130b57cec5SDimitry Andric // If the size is zero, there won't be any actual memory access, so 13140b57cec5SDimitry Andric // just bind the return value to the destination buffer and return. 13150b57cec5SDimitry Andric if (stateZeroSize && !stateNonZeroSize) { 13160b57cec5SDimitry Andric stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal); 13170b57cec5SDimitry Andric C.addTransition(stateZeroSize); 13180b57cec5SDimitry Andric return; 13190b57cec5SDimitry Andric } 13200b57cec5SDimitry Andric 13210b57cec5SDimitry Andric // If the size can be nonzero, we have to check the other arguments. 13220b57cec5SDimitry Andric if (stateNonZeroSize) { 13230b57cec5SDimitry Andric state = stateNonZeroSize; 13240b57cec5SDimitry Andric 13250b57cec5SDimitry Andric // Ensure the destination is not null. If it is NULL there will be a 13260b57cec5SDimitry Andric // NULL pointer dereference. 13275ffd83dbSDimitry Andric state = checkNonNull(C, state, Dest, destVal); 13280b57cec5SDimitry Andric if (!state) 13290b57cec5SDimitry Andric return; 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andric // Get the value of the Src. 13325ffd83dbSDimitry Andric SVal srcVal = state->getSVal(Source.Expression, LCtx); 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric // Ensure the source is not null. If it is NULL there will be a 13350b57cec5SDimitry Andric // NULL pointer dereference. 13365ffd83dbSDimitry Andric state = checkNonNull(C, state, Source, srcVal); 13370b57cec5SDimitry Andric if (!state) 13380b57cec5SDimitry Andric return; 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric // Ensure the accesses are valid and that the buffers do not overlap. 1341bdd1243dSDimitry Andric state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write, CK); 1342bdd1243dSDimitry Andric state = CheckBufferAccess(C, state, Source, Size, AccessKind::read, CK); 13435ffd83dbSDimitry Andric 13440b57cec5SDimitry Andric if (Restricted) 1345bdd1243dSDimitry Andric state = CheckOverlap(C, state, Size, Dest, Source, CK); 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric if (!state) 13480b57cec5SDimitry Andric return; 13490b57cec5SDimitry Andric 13500b57cec5SDimitry Andric // If this is mempcpy, get the byte after the last byte copied and 13510b57cec5SDimitry Andric // bind the expr. 13520b57cec5SDimitry Andric if (IsMempcpy) { 13530b57cec5SDimitry Andric // Get the byte after the last byte copied. 13540b57cec5SDimitry Andric SValBuilder &SvalBuilder = C.getSValBuilder(); 13550b57cec5SDimitry Andric ASTContext &Ctx = SvalBuilder.getContext(); 1356bdd1243dSDimitry Andric QualType CharPtrTy = getCharPtrType(Ctx, CK); 13570b57cec5SDimitry Andric SVal DestRegCharVal = 13585ffd83dbSDimitry Andric SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType()); 13590b57cec5SDimitry Andric SVal lastElement = C.getSValBuilder().evalBinOp( 13605ffd83dbSDimitry Andric state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType()); 13610b57cec5SDimitry Andric // If we don't know how much we copied, we can at least 13620b57cec5SDimitry Andric // conjure a return value for later. 13630b57cec5SDimitry Andric if (lastElement.isUnknown()) 13640b57cec5SDimitry Andric lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, 13650b57cec5SDimitry Andric C.blockCount()); 13660b57cec5SDimitry Andric 13670b57cec5SDimitry Andric // The byte after the last byte copied is the return value. 13680b57cec5SDimitry Andric state = state->BindExpr(CE, LCtx, lastElement); 13690b57cec5SDimitry Andric } else { 13700b57cec5SDimitry Andric // All other copies return the destination buffer. 13710b57cec5SDimitry Andric // (Well, bcopy() has a void return type, but this won't hurt.) 13720b57cec5SDimitry Andric state = state->BindExpr(CE, LCtx, destVal); 13730b57cec5SDimitry Andric } 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric // Invalidate the destination (regular invalidation without pointer-escaping 13760b57cec5SDimitry Andric // the address of the top-level region). 13770b57cec5SDimitry Andric // FIXME: Even if we can't perfectly model the copy, we should see if we 13780b57cec5SDimitry Andric // can use LazyCompoundVals to copy the source values into the destination. 13790b57cec5SDimitry Andric // This would probably remove any existing bindings past the end of the 13800b57cec5SDimitry Andric // copied region, but that's still an improvement over blank invalidation. 138106c3fb27SDimitry Andric state = invalidateDestinationBufferBySize( 138206c3fb27SDimitry Andric C, state, Dest.Expression, C.getSVal(Dest.Expression), sizeVal, 138306c3fb27SDimitry Andric Size.Expression->getType()); 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric // Invalidate the source (const-invalidation without const-pointer-escaping 13860b57cec5SDimitry Andric // the address of the top-level region). 138706c3fb27SDimitry Andric state = invalidateSourceBuffer(C, state, Source.Expression, 138806c3fb27SDimitry Andric C.getSVal(Source.Expression)); 13890b57cec5SDimitry Andric 13900b57cec5SDimitry Andric C.addTransition(state); 13910b57cec5SDimitry Andric } 13920b57cec5SDimitry Andric } 13930b57cec5SDimitry Andric 1394972a253aSDimitry Andric void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE, 1395bdd1243dSDimitry Andric CharKind CK) const { 13960b57cec5SDimitry Andric // void *memcpy(void *restrict dst, const void *restrict src, size_t n); 13970b57cec5SDimitry Andric // The return value is the address of the destination buffer. 139806c3fb27SDimitry Andric DestinationArgExpr Dest = {{CE->getArg(0), 0}}; 139906c3fb27SDimitry Andric SourceArgExpr Src = {{CE->getArg(1), 1}}; 140006c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(2), 2}}; 14010b57cec5SDimitry Andric 14025ffd83dbSDimitry Andric ProgramStateRef State = C.getState(); 14035ffd83dbSDimitry Andric 14045ffd83dbSDimitry Andric constexpr bool IsRestricted = true; 14055ffd83dbSDimitry Andric constexpr bool IsMempcpy = false; 1406bdd1243dSDimitry Andric evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy, CK); 14070b57cec5SDimitry Andric } 14080b57cec5SDimitry Andric 1409bdd1243dSDimitry Andric void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE, 1410bdd1243dSDimitry Andric CharKind CK) const { 14110b57cec5SDimitry Andric // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); 14120b57cec5SDimitry Andric // The return value is a pointer to the byte following the last written byte. 141306c3fb27SDimitry Andric DestinationArgExpr Dest = {{CE->getArg(0), 0}}; 141406c3fb27SDimitry Andric SourceArgExpr Src = {{CE->getArg(1), 1}}; 141506c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(2), 2}}; 14160b57cec5SDimitry Andric 14175ffd83dbSDimitry Andric constexpr bool IsRestricted = true; 14185ffd83dbSDimitry Andric constexpr bool IsMempcpy = true; 1419972a253aSDimitry Andric evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy, 1420bdd1243dSDimitry Andric CK); 14210b57cec5SDimitry Andric } 14220b57cec5SDimitry Andric 1423bdd1243dSDimitry Andric void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE, 1424bdd1243dSDimitry Andric CharKind CK) const { 14250b57cec5SDimitry Andric // void *memmove(void *dst, const void *src, size_t n); 14260b57cec5SDimitry Andric // The return value is the address of the destination buffer. 142706c3fb27SDimitry Andric DestinationArgExpr Dest = {{CE->getArg(0), 0}}; 142806c3fb27SDimitry Andric SourceArgExpr Src = {{CE->getArg(1), 1}}; 142906c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(2), 2}}; 14300b57cec5SDimitry Andric 14315ffd83dbSDimitry Andric constexpr bool IsRestricted = false; 14325ffd83dbSDimitry Andric constexpr bool IsMempcpy = false; 1433972a253aSDimitry Andric evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy, 1434bdd1243dSDimitry Andric CK); 14350b57cec5SDimitry Andric } 14360b57cec5SDimitry Andric 14370b57cec5SDimitry Andric void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const { 14380b57cec5SDimitry Andric // void bcopy(const void *src, void *dst, size_t n); 143906c3fb27SDimitry Andric SourceArgExpr Src{{CE->getArg(0), 0}}; 144006c3fb27SDimitry Andric DestinationArgExpr Dest = {{CE->getArg(1), 1}}; 144106c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(2), 2}}; 14425ffd83dbSDimitry Andric 14435ffd83dbSDimitry Andric constexpr bool IsRestricted = false; 14445ffd83dbSDimitry Andric constexpr bool IsMempcpy = false; 1445972a253aSDimitry Andric evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy, 1446bdd1243dSDimitry Andric CharKind::Regular); 14470b57cec5SDimitry Andric } 14480b57cec5SDimitry Andric 1449bdd1243dSDimitry Andric void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE, 1450bdd1243dSDimitry Andric CharKind CK) const { 14510b57cec5SDimitry Andric // int memcmp(const void *s1, const void *s2, size_t n); 14520b57cec5SDimitry Andric CurrentFunctionDescription = "memory comparison function"; 14530b57cec5SDimitry Andric 14545ffd83dbSDimitry Andric AnyArgExpr Left = {CE->getArg(0), 0}; 14555ffd83dbSDimitry Andric AnyArgExpr Right = {CE->getArg(1), 1}; 145606c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(2), 2}}; 14570b57cec5SDimitry Andric 14585ffd83dbSDimitry Andric ProgramStateRef State = C.getState(); 14595ffd83dbSDimitry Andric SValBuilder &Builder = C.getSValBuilder(); 14605ffd83dbSDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 14610b57cec5SDimitry Andric 14620b57cec5SDimitry Andric // See if the size argument is zero. 14635ffd83dbSDimitry Andric SVal sizeVal = State->getSVal(Size.Expression, LCtx); 14645ffd83dbSDimitry Andric QualType sizeTy = Size.Expression->getType(); 14650b57cec5SDimitry Andric 14660b57cec5SDimitry Andric ProgramStateRef stateZeroSize, stateNonZeroSize; 14670b57cec5SDimitry Andric std::tie(stateZeroSize, stateNonZeroSize) = 14685ffd83dbSDimitry Andric assumeZero(C, State, sizeVal, sizeTy); 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric // If the size can be zero, the result will be 0 in that case, and we don't 14710b57cec5SDimitry Andric // have to check either of the buffers. 14720b57cec5SDimitry Andric if (stateZeroSize) { 14735ffd83dbSDimitry Andric State = stateZeroSize; 14745ffd83dbSDimitry Andric State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType())); 14755ffd83dbSDimitry Andric C.addTransition(State); 14760b57cec5SDimitry Andric } 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andric // If the size can be nonzero, we have to check the other arguments. 14790b57cec5SDimitry Andric if (stateNonZeroSize) { 14805ffd83dbSDimitry Andric State = stateNonZeroSize; 14810b57cec5SDimitry Andric // If we know the two buffers are the same, we know the result is 0. 14820b57cec5SDimitry Andric // First, get the two buffers' addresses. Another checker will have already 14830b57cec5SDimitry Andric // made sure they're not undefined. 14840b57cec5SDimitry Andric DefinedOrUnknownSVal LV = 14855ffd83dbSDimitry Andric State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>(); 14860b57cec5SDimitry Andric DefinedOrUnknownSVal RV = 14875ffd83dbSDimitry Andric State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>(); 14880b57cec5SDimitry Andric 14890b57cec5SDimitry Andric // See if they are the same. 14905ffd83dbSDimitry Andric ProgramStateRef SameBuffer, NotSameBuffer; 14915ffd83dbSDimitry Andric std::tie(SameBuffer, NotSameBuffer) = 14925ffd83dbSDimitry Andric State->assume(Builder.evalEQ(State, LV, RV)); 14930b57cec5SDimitry Andric 1494480093f4SDimitry Andric // If the two arguments are the same buffer, we know the result is 0, 14950b57cec5SDimitry Andric // and we only need to check one size. 14965ffd83dbSDimitry Andric if (SameBuffer && !NotSameBuffer) { 14975ffd83dbSDimitry Andric State = SameBuffer; 14985ffd83dbSDimitry Andric State = CheckBufferAccess(C, State, Left, Size, AccessKind::read); 14995ffd83dbSDimitry Andric if (State) { 15005ffd83dbSDimitry Andric State = 15015ffd83dbSDimitry Andric SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType())); 15025ffd83dbSDimitry Andric C.addTransition(State); 15030b57cec5SDimitry Andric } 1504480093f4SDimitry Andric return; 15050b57cec5SDimitry Andric } 15060b57cec5SDimitry Andric 1507480093f4SDimitry Andric // If the two arguments might be different buffers, we have to check 1508480093f4SDimitry Andric // the size of both of them. 15095ffd83dbSDimitry Andric assert(NotSameBuffer); 1510bdd1243dSDimitry Andric State = CheckBufferAccess(C, State, Right, Size, AccessKind::read, CK); 1511bdd1243dSDimitry Andric State = CheckBufferAccess(C, State, Left, Size, AccessKind::read, CK); 15125ffd83dbSDimitry Andric if (State) { 15130b57cec5SDimitry Andric // The return value is the comparison result, which we don't know. 15145ffd83dbSDimitry Andric SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 15155ffd83dbSDimitry Andric State = State->BindExpr(CE, LCtx, CmpV); 15165ffd83dbSDimitry Andric C.addTransition(State); 15170b57cec5SDimitry Andric } 15180b57cec5SDimitry Andric } 15190b57cec5SDimitry Andric } 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric void CStringChecker::evalstrLength(CheckerContext &C, 15220b57cec5SDimitry Andric const CallExpr *CE) const { 15230b57cec5SDimitry Andric // size_t strlen(const char *s); 15240b57cec5SDimitry Andric evalstrLengthCommon(C, CE, /* IsStrnlen = */ false); 15250b57cec5SDimitry Andric } 15260b57cec5SDimitry Andric 15270b57cec5SDimitry Andric void CStringChecker::evalstrnLength(CheckerContext &C, 15280b57cec5SDimitry Andric const CallExpr *CE) const { 15290b57cec5SDimitry Andric // size_t strnlen(const char *s, size_t maxlen); 15300b57cec5SDimitry Andric evalstrLengthCommon(C, CE, /* IsStrnlen = */ true); 15310b57cec5SDimitry Andric } 15320b57cec5SDimitry Andric 15330b57cec5SDimitry Andric void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, 15340b57cec5SDimitry Andric bool IsStrnlen) const { 15350b57cec5SDimitry Andric CurrentFunctionDescription = "string length function"; 15360b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 15370b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 15380b57cec5SDimitry Andric 15390b57cec5SDimitry Andric if (IsStrnlen) { 15400b57cec5SDimitry Andric const Expr *maxlenExpr = CE->getArg(1); 15410b57cec5SDimitry Andric SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); 15420b57cec5SDimitry Andric 15430b57cec5SDimitry Andric ProgramStateRef stateZeroSize, stateNonZeroSize; 15440b57cec5SDimitry Andric std::tie(stateZeroSize, stateNonZeroSize) = 15450b57cec5SDimitry Andric assumeZero(C, state, maxlenVal, maxlenExpr->getType()); 15460b57cec5SDimitry Andric 15470b57cec5SDimitry Andric // If the size can be zero, the result will be 0 in that case, and we don't 15480b57cec5SDimitry Andric // have to check the string itself. 15490b57cec5SDimitry Andric if (stateZeroSize) { 15500b57cec5SDimitry Andric SVal zero = C.getSValBuilder().makeZeroVal(CE->getType()); 15510b57cec5SDimitry Andric stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero); 15520b57cec5SDimitry Andric C.addTransition(stateZeroSize); 15530b57cec5SDimitry Andric } 15540b57cec5SDimitry Andric 15550b57cec5SDimitry Andric // If the size is GUARANTEED to be zero, we're done! 15560b57cec5SDimitry Andric if (!stateNonZeroSize) 15570b57cec5SDimitry Andric return; 15580b57cec5SDimitry Andric 15590b57cec5SDimitry Andric // Otherwise, record the assumption that the size is nonzero. 15600b57cec5SDimitry Andric state = stateNonZeroSize; 15610b57cec5SDimitry Andric } 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric // Check that the string argument is non-null. 15645ffd83dbSDimitry Andric AnyArgExpr Arg = {CE->getArg(0), 0}; 15655ffd83dbSDimitry Andric SVal ArgVal = state->getSVal(Arg.Expression, LCtx); 15665ffd83dbSDimitry Andric state = checkNonNull(C, state, Arg, ArgVal); 15670b57cec5SDimitry Andric 15680b57cec5SDimitry Andric if (!state) 15690b57cec5SDimitry Andric return; 15700b57cec5SDimitry Andric 15715ffd83dbSDimitry Andric SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal); 15720b57cec5SDimitry Andric 15730b57cec5SDimitry Andric // If the argument isn't a valid C string, there's no valid state to 15740b57cec5SDimitry Andric // transition to. 15750b57cec5SDimitry Andric if (strLength.isUndef()) 15760b57cec5SDimitry Andric return; 15770b57cec5SDimitry Andric 15780b57cec5SDimitry Andric DefinedOrUnknownSVal result = UnknownVal(); 15790b57cec5SDimitry Andric 15800b57cec5SDimitry Andric // If the check is for strnlen() then bind the return value to no more than 15810b57cec5SDimitry Andric // the maxlen value. 15820b57cec5SDimitry Andric if (IsStrnlen) { 15830b57cec5SDimitry Andric QualType cmpTy = C.getSValBuilder().getConditionType(); 15840b57cec5SDimitry Andric 15850b57cec5SDimitry Andric // It's a little unfortunate to be getting this again, 15860b57cec5SDimitry Andric // but it's not that expensive... 15870b57cec5SDimitry Andric const Expr *maxlenExpr = CE->getArg(1); 15880b57cec5SDimitry Andric SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); 15890b57cec5SDimitry Andric 1590bdd1243dSDimitry Andric std::optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); 1591bdd1243dSDimitry Andric std::optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>(); 15920b57cec5SDimitry Andric 15930b57cec5SDimitry Andric if (strLengthNL && maxlenValNL) { 15940b57cec5SDimitry Andric ProgramStateRef stateStringTooLong, stateStringNotTooLong; 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andric // Check if the strLength is greater than the maxlen. 15970b57cec5SDimitry Andric std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume( 15980b57cec5SDimitry Andric C.getSValBuilder() 15990b57cec5SDimitry Andric .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy) 16000b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>()); 16010b57cec5SDimitry Andric 16020b57cec5SDimitry Andric if (stateStringTooLong && !stateStringNotTooLong) { 16030b57cec5SDimitry Andric // If the string is longer than maxlen, return maxlen. 16040b57cec5SDimitry Andric result = *maxlenValNL; 16050b57cec5SDimitry Andric } else if (stateStringNotTooLong && !stateStringTooLong) { 16060b57cec5SDimitry Andric // If the string is shorter than maxlen, return its length. 16070b57cec5SDimitry Andric result = *strLengthNL; 16080b57cec5SDimitry Andric } 16090b57cec5SDimitry Andric } 16100b57cec5SDimitry Andric 16110b57cec5SDimitry Andric if (result.isUnknown()) { 16120b57cec5SDimitry Andric // If we don't have enough information for a comparison, there's 16130b57cec5SDimitry Andric // no guarantee the full string length will actually be returned. 16140b57cec5SDimitry Andric // All we know is the return value is the min of the string length 16150b57cec5SDimitry Andric // and the limit. This is better than nothing. 16160b57cec5SDimitry Andric result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, 16170b57cec5SDimitry Andric C.blockCount()); 16180b57cec5SDimitry Andric NonLoc resultNL = result.castAs<NonLoc>(); 16190b57cec5SDimitry Andric 16200b57cec5SDimitry Andric if (strLengthNL) { 16210b57cec5SDimitry Andric state = state->assume(C.getSValBuilder().evalBinOpNN( 16220b57cec5SDimitry Andric state, BO_LE, resultNL, *strLengthNL, cmpTy) 16230b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>(), true); 16240b57cec5SDimitry Andric } 16250b57cec5SDimitry Andric 16260b57cec5SDimitry Andric if (maxlenValNL) { 16270b57cec5SDimitry Andric state = state->assume(C.getSValBuilder().evalBinOpNN( 16280b57cec5SDimitry Andric state, BO_LE, resultNL, *maxlenValNL, cmpTy) 16290b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>(), true); 16300b57cec5SDimitry Andric } 16310b57cec5SDimitry Andric } 16320b57cec5SDimitry Andric 16330b57cec5SDimitry Andric } else { 16340b57cec5SDimitry Andric // This is a plain strlen(), not strnlen(). 16350b57cec5SDimitry Andric result = strLength.castAs<DefinedOrUnknownSVal>(); 16360b57cec5SDimitry Andric 16370b57cec5SDimitry Andric // If we don't know the length of the string, conjure a return 16380b57cec5SDimitry Andric // value, so it can be used in constraints, at least. 16390b57cec5SDimitry Andric if (result.isUnknown()) { 16400b57cec5SDimitry Andric result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, 16410b57cec5SDimitry Andric C.blockCount()); 16420b57cec5SDimitry Andric } 16430b57cec5SDimitry Andric } 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric // Bind the return value. 16460b57cec5SDimitry Andric assert(!result.isUnknown() && "Should have conjured a value by now"); 16470b57cec5SDimitry Andric state = state->BindExpr(CE, LCtx, result); 16480b57cec5SDimitry Andric C.addTransition(state); 16490b57cec5SDimitry Andric } 16500b57cec5SDimitry Andric 16510b57cec5SDimitry Andric void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const { 16520b57cec5SDimitry Andric // char *strcpy(char *restrict dst, const char *restrict src); 16530b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1654480093f4SDimitry Andric /* ReturnEnd = */ false, 1655480093f4SDimitry Andric /* IsBounded = */ false, 1656480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none); 16570b57cec5SDimitry Andric } 16580b57cec5SDimitry Andric 16590b57cec5SDimitry Andric void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const { 16600b57cec5SDimitry Andric // char *strncpy(char *restrict dst, const char *restrict src, size_t n); 16610b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1662480093f4SDimitry Andric /* ReturnEnd = */ false, 1663480093f4SDimitry Andric /* IsBounded = */ true, 1664480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none); 16650b57cec5SDimitry Andric } 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const { 16680b57cec5SDimitry Andric // char *stpcpy(char *restrict dst, const char *restrict src); 16690b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1670480093f4SDimitry Andric /* ReturnEnd = */ true, 1671480093f4SDimitry Andric /* IsBounded = */ false, 1672480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none); 16730b57cec5SDimitry Andric } 16740b57cec5SDimitry Andric 16750b57cec5SDimitry Andric void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const { 1676480093f4SDimitry Andric // size_t strlcpy(char *dest, const char *src, size_t size); 16770b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1678480093f4SDimitry Andric /* ReturnEnd = */ true, 1679480093f4SDimitry Andric /* IsBounded = */ true, 1680480093f4SDimitry Andric /* appendK = */ ConcatFnKind::none, 16810b57cec5SDimitry Andric /* returnPtr = */ false); 16820b57cec5SDimitry Andric } 16830b57cec5SDimitry Andric 16840b57cec5SDimitry Andric void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const { 16850b57cec5SDimitry Andric // char *strcat(char *restrict s1, const char *restrict s2); 16860b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1687480093f4SDimitry Andric /* ReturnEnd = */ false, 1688480093f4SDimitry Andric /* IsBounded = */ false, 1689480093f4SDimitry Andric /* appendK = */ ConcatFnKind::strcat); 16900b57cec5SDimitry Andric } 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const { 16930b57cec5SDimitry Andric // char *strncat(char *restrict s1, const char *restrict s2, size_t n); 16940b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1695480093f4SDimitry Andric /* ReturnEnd = */ false, 1696480093f4SDimitry Andric /* IsBounded = */ true, 1697480093f4SDimitry Andric /* appendK = */ ConcatFnKind::strcat); 16980b57cec5SDimitry Andric } 16990b57cec5SDimitry Andric 17000b57cec5SDimitry Andric void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const { 1701480093f4SDimitry Andric // size_t strlcat(char *dst, const char *src, size_t size); 1702480093f4SDimitry Andric // It will append at most size - strlen(dst) - 1 bytes, 1703480093f4SDimitry Andric // NULL-terminating the result. 17040b57cec5SDimitry Andric evalStrcpyCommon(C, CE, 1705480093f4SDimitry Andric /* ReturnEnd = */ false, 1706480093f4SDimitry Andric /* IsBounded = */ true, 1707480093f4SDimitry Andric /* appendK = */ ConcatFnKind::strlcat, 17080b57cec5SDimitry Andric /* returnPtr = */ false); 17090b57cec5SDimitry Andric } 17100b57cec5SDimitry Andric 17110b57cec5SDimitry Andric void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, 1712480093f4SDimitry Andric bool ReturnEnd, bool IsBounded, 1713480093f4SDimitry Andric ConcatFnKind appendK, 1714480093f4SDimitry Andric bool returnPtr) const { 1715480093f4SDimitry Andric if (appendK == ConcatFnKind::none) 17160b57cec5SDimitry Andric CurrentFunctionDescription = "string copy function"; 1717480093f4SDimitry Andric else 1718480093f4SDimitry Andric CurrentFunctionDescription = "string concatenation function"; 17195ffd83dbSDimitry Andric 17200b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 17210b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric // Check that the destination is non-null. 172406c3fb27SDimitry Andric DestinationArgExpr Dst = {{CE->getArg(0), 0}}; 17255ffd83dbSDimitry Andric SVal DstVal = state->getSVal(Dst.Expression, LCtx); 17265ffd83dbSDimitry Andric state = checkNonNull(C, state, Dst, DstVal); 17270b57cec5SDimitry Andric if (!state) 17280b57cec5SDimitry Andric return; 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andric // Check that the source is non-null. 173106c3fb27SDimitry Andric SourceArgExpr srcExpr = {{CE->getArg(1), 1}}; 17325ffd83dbSDimitry Andric SVal srcVal = state->getSVal(srcExpr.Expression, LCtx); 17335ffd83dbSDimitry Andric state = checkNonNull(C, state, srcExpr, srcVal); 17340b57cec5SDimitry Andric if (!state) 17350b57cec5SDimitry Andric return; 17360b57cec5SDimitry Andric 17370b57cec5SDimitry Andric // Get the string length of the source. 17385ffd83dbSDimitry Andric SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal); 1739bdd1243dSDimitry Andric std::optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); 1740480093f4SDimitry Andric 1741480093f4SDimitry Andric // Get the string length of the destination buffer. 17425ffd83dbSDimitry Andric SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal); 1743bdd1243dSDimitry Andric std::optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>(); 17440b57cec5SDimitry Andric 17450b57cec5SDimitry Andric // If the source isn't a valid C string, give up. 17460b57cec5SDimitry Andric if (strLength.isUndef()) 17470b57cec5SDimitry Andric return; 17480b57cec5SDimitry Andric 17490b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 17500b57cec5SDimitry Andric QualType cmpTy = svalBuilder.getConditionType(); 17510b57cec5SDimitry Andric QualType sizeTy = svalBuilder.getContext().getSizeType(); 17520b57cec5SDimitry Andric 17530b57cec5SDimitry Andric // These two values allow checking two kinds of errors: 17540b57cec5SDimitry Andric // - actual overflows caused by a source that doesn't fit in the destination 17550b57cec5SDimitry Andric // - potential overflows caused by a bound that could exceed the destination 17560b57cec5SDimitry Andric SVal amountCopied = UnknownVal(); 17570b57cec5SDimitry Andric SVal maxLastElementIndex = UnknownVal(); 17580b57cec5SDimitry Andric const char *boundWarning = nullptr; 17590b57cec5SDimitry Andric 17605ffd83dbSDimitry Andric // FIXME: Why do we choose the srcExpr if the access has no size? 17615ffd83dbSDimitry Andric // Note that the 3rd argument of the call would be the size parameter. 176206c3fb27SDimitry Andric SizeArgExpr SrcExprAsSizeDummy = { 176306c3fb27SDimitry Andric {srcExpr.Expression, srcExpr.ArgumentIndex}}; 17645ffd83dbSDimitry Andric state = CheckOverlap( 17655ffd83dbSDimitry Andric C, state, 176606c3fb27SDimitry Andric (IsBounded ? SizeArgExpr{{CE->getArg(2), 2}} : SrcExprAsSizeDummy), Dst, 1767480093f4SDimitry Andric srcExpr); 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric if (!state) 17700b57cec5SDimitry Andric return; 17710b57cec5SDimitry Andric 17720b57cec5SDimitry Andric // If the function is strncpy, strncat, etc... it is bounded. 1773480093f4SDimitry Andric if (IsBounded) { 17740b57cec5SDimitry Andric // Get the max number of characters to copy. 177506c3fb27SDimitry Andric SizeArgExpr lenExpr = {{CE->getArg(2), 2}}; 17765ffd83dbSDimitry Andric SVal lenVal = state->getSVal(lenExpr.Expression, LCtx); 17770b57cec5SDimitry Andric 17780b57cec5SDimitry Andric // Protect against misdeclared strncpy(). 17795ffd83dbSDimitry Andric lenVal = 17805ffd83dbSDimitry Andric svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType()); 17810b57cec5SDimitry Andric 1782bdd1243dSDimitry Andric std::optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>(); 17830b57cec5SDimitry Andric 17840b57cec5SDimitry Andric // If we know both values, we might be able to figure out how much 17850b57cec5SDimitry Andric // we're copying. 17860b57cec5SDimitry Andric if (strLengthNL && lenValNL) { 1787480093f4SDimitry Andric switch (appendK) { 1788480093f4SDimitry Andric case ConcatFnKind::none: 1789480093f4SDimitry Andric case ConcatFnKind::strcat: { 17900b57cec5SDimitry Andric ProgramStateRef stateSourceTooLong, stateSourceNotTooLong; 17910b57cec5SDimitry Andric // Check if the max number to copy is less than the length of the src. 17920b57cec5SDimitry Andric // If the bound is equal to the source length, strncpy won't null- 17930b57cec5SDimitry Andric // terminate the result! 17940b57cec5SDimitry Andric std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume( 1795480093f4SDimitry Andric svalBuilder 1796480093f4SDimitry Andric .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy) 17970b57cec5SDimitry Andric .castAs<DefinedOrUnknownSVal>()); 17980b57cec5SDimitry Andric 17990b57cec5SDimitry Andric if (stateSourceTooLong && !stateSourceNotTooLong) { 1800480093f4SDimitry Andric // Max number to copy is less than the length of the src, so the 1801480093f4SDimitry Andric // actual strLength copied is the max number arg. 18020b57cec5SDimitry Andric state = stateSourceTooLong; 18030b57cec5SDimitry Andric amountCopied = lenVal; 18040b57cec5SDimitry Andric 18050b57cec5SDimitry Andric } else if (!stateSourceTooLong && stateSourceNotTooLong) { 18060b57cec5SDimitry Andric // The source buffer entirely fits in the bound. 18070b57cec5SDimitry Andric state = stateSourceNotTooLong; 18080b57cec5SDimitry Andric amountCopied = strLength; 18090b57cec5SDimitry Andric } 1810480093f4SDimitry Andric break; 1811480093f4SDimitry Andric } 1812480093f4SDimitry Andric case ConcatFnKind::strlcat: 1813480093f4SDimitry Andric if (!dstStrLengthNL) 1814480093f4SDimitry Andric return; 1815480093f4SDimitry Andric 1816480093f4SDimitry Andric // amountCopied = min (size - dstLen - 1 , srcLen) 1817480093f4SDimitry Andric SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, 1818480093f4SDimitry Andric *dstStrLengthNL, sizeTy); 181981ad6265SDimitry Andric if (!isa<NonLoc>(freeSpace)) 1820480093f4SDimitry Andric return; 1821480093f4SDimitry Andric freeSpace = 1822480093f4SDimitry Andric svalBuilder.evalBinOp(state, BO_Sub, freeSpace, 1823480093f4SDimitry Andric svalBuilder.makeIntVal(1, sizeTy), sizeTy); 1824bdd1243dSDimitry Andric std::optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>(); 1825480093f4SDimitry Andric 1826480093f4SDimitry Andric // While unlikely, it is possible that the subtraction is 1827480093f4SDimitry Andric // too complex to compute, let's check whether it succeeded. 1828480093f4SDimitry Andric if (!freeSpaceNL) 1829480093f4SDimitry Andric return; 1830480093f4SDimitry Andric SVal hasEnoughSpace = svalBuilder.evalBinOpNN( 1831480093f4SDimitry Andric state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy); 1832480093f4SDimitry Andric 1833480093f4SDimitry Andric ProgramStateRef TrueState, FalseState; 1834480093f4SDimitry Andric std::tie(TrueState, FalseState) = 1835480093f4SDimitry Andric state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>()); 1836480093f4SDimitry Andric 1837480093f4SDimitry Andric // srcStrLength <= size - dstStrLength -1 1838480093f4SDimitry Andric if (TrueState && !FalseState) { 1839480093f4SDimitry Andric amountCopied = strLength; 18400b57cec5SDimitry Andric } 18410b57cec5SDimitry Andric 1842480093f4SDimitry Andric // srcStrLength > size - dstStrLength -1 1843480093f4SDimitry Andric if (!TrueState && FalseState) { 1844480093f4SDimitry Andric amountCopied = freeSpace; 1845480093f4SDimitry Andric } 1846480093f4SDimitry Andric 1847480093f4SDimitry Andric if (TrueState && FalseState) 1848480093f4SDimitry Andric amountCopied = UnknownVal(); 1849480093f4SDimitry Andric break; 1850480093f4SDimitry Andric } 1851480093f4SDimitry Andric } 18520b57cec5SDimitry Andric // We still want to know if the bound is known to be too large. 18530b57cec5SDimitry Andric if (lenValNL) { 1854480093f4SDimitry Andric switch (appendK) { 1855480093f4SDimitry Andric case ConcatFnKind::strcat: 18560b57cec5SDimitry Andric // For strncat, the check is strlen(dst) + lenVal < sizeof(dst) 18570b57cec5SDimitry Andric 18580b57cec5SDimitry Andric // Get the string length of the destination. If the destination is 18590b57cec5SDimitry Andric // memory that can't have a string length, we shouldn't be copying 18600b57cec5SDimitry Andric // into it anyway. 18610b57cec5SDimitry Andric if (dstStrLength.isUndef()) 18620b57cec5SDimitry Andric return; 18630b57cec5SDimitry Andric 1864480093f4SDimitry Andric if (dstStrLengthNL) { 1865480093f4SDimitry Andric maxLastElementIndex = svalBuilder.evalBinOpNN( 1866480093f4SDimitry Andric state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy); 1867480093f4SDimitry Andric 18680b57cec5SDimitry Andric boundWarning = "Size argument is greater than the free space in the " 18690b57cec5SDimitry Andric "destination buffer"; 18700b57cec5SDimitry Andric } 1871480093f4SDimitry Andric break; 1872480093f4SDimitry Andric case ConcatFnKind::none: 1873480093f4SDimitry Andric case ConcatFnKind::strlcat: 1874480093f4SDimitry Andric // For strncpy and strlcat, this is just checking 1875480093f4SDimitry Andric // that lenVal <= sizeof(dst). 18760b57cec5SDimitry Andric // (Yes, strncpy and strncat differ in how they treat termination. 18770b57cec5SDimitry Andric // strncat ALWAYS terminates, but strncpy doesn't.) 18780b57cec5SDimitry Andric 18790b57cec5SDimitry Andric // We need a special case for when the copy size is zero, in which 18800b57cec5SDimitry Andric // case strncpy will do no work at all. Our bounds check uses n-1 18810b57cec5SDimitry Andric // as the last element accessed, so n == 0 is problematic. 18820b57cec5SDimitry Andric ProgramStateRef StateZeroSize, StateNonZeroSize; 18830b57cec5SDimitry Andric std::tie(StateZeroSize, StateNonZeroSize) = 18840b57cec5SDimitry Andric assumeZero(C, state, *lenValNL, sizeTy); 18850b57cec5SDimitry Andric 18860b57cec5SDimitry Andric // If the size is known to be zero, we're done. 18870b57cec5SDimitry Andric if (StateZeroSize && !StateNonZeroSize) { 18880b57cec5SDimitry Andric if (returnPtr) { 18890b57cec5SDimitry Andric StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal); 18900b57cec5SDimitry Andric } else { 1891480093f4SDimitry Andric if (appendK == ConcatFnKind::none) { 1892480093f4SDimitry Andric // strlcpy returns strlen(src) 1893480093f4SDimitry Andric StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength); 1894480093f4SDimitry Andric } else { 1895480093f4SDimitry Andric // strlcat returns strlen(src) + strlen(dst) 1896480093f4SDimitry Andric SVal retSize = svalBuilder.evalBinOp( 1897480093f4SDimitry Andric state, BO_Add, strLength, dstStrLength, sizeTy); 1898480093f4SDimitry Andric StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize); 1899480093f4SDimitry Andric } 19000b57cec5SDimitry Andric } 19010b57cec5SDimitry Andric C.addTransition(StateZeroSize); 19020b57cec5SDimitry Andric return; 19030b57cec5SDimitry Andric } 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric // Otherwise, go ahead and figure out the last element we'll touch. 19060b57cec5SDimitry Andric // We don't record the non-zero assumption here because we can't 19070b57cec5SDimitry Andric // be sure. We won't warn on a possible zero. 19080b57cec5SDimitry Andric NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); 1909480093f4SDimitry Andric maxLastElementIndex = 1910480093f4SDimitry Andric svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy); 19110b57cec5SDimitry Andric boundWarning = "Size argument is greater than the length of the " 19120b57cec5SDimitry Andric "destination buffer"; 1913480093f4SDimitry Andric break; 19140b57cec5SDimitry Andric } 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric } else { 19170b57cec5SDimitry Andric // The function isn't bounded. The amount copied should match the length 19180b57cec5SDimitry Andric // of the source buffer. 19190b57cec5SDimitry Andric amountCopied = strLength; 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric assert(state); 19230b57cec5SDimitry Andric 19240b57cec5SDimitry Andric // This represents the number of characters copied into the destination 19250b57cec5SDimitry Andric // buffer. (It may not actually be the strlen if the destination buffer 19260b57cec5SDimitry Andric // is not terminated.) 19270b57cec5SDimitry Andric SVal finalStrLength = UnknownVal(); 1928480093f4SDimitry Andric SVal strlRetVal = UnknownVal(); 1929480093f4SDimitry Andric 1930480093f4SDimitry Andric if (appendK == ConcatFnKind::none && !returnPtr) { 1931480093f4SDimitry Andric // strlcpy returns the sizeof(src) 1932480093f4SDimitry Andric strlRetVal = strLength; 1933480093f4SDimitry Andric } 19340b57cec5SDimitry Andric 19350b57cec5SDimitry Andric // If this is an appending function (strcat, strncat...) then set the 19360b57cec5SDimitry Andric // string length to strlen(src) + strlen(dst) since the buffer will 19370b57cec5SDimitry Andric // ultimately contain both. 1938480093f4SDimitry Andric if (appendK != ConcatFnKind::none) { 19390b57cec5SDimitry Andric // Get the string length of the destination. If the destination is memory 19400b57cec5SDimitry Andric // that can't have a string length, we shouldn't be copying into it anyway. 19410b57cec5SDimitry Andric if (dstStrLength.isUndef()) 19420b57cec5SDimitry Andric return; 19430b57cec5SDimitry Andric 1944480093f4SDimitry Andric if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) { 1945480093f4SDimitry Andric strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL, 1946480093f4SDimitry Andric *dstStrLengthNL, sizeTy); 1947480093f4SDimitry Andric } 1948480093f4SDimitry Andric 1949bdd1243dSDimitry Andric std::optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>(); 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric // If we know both string lengths, we might know the final string length. 1952480093f4SDimitry Andric if (amountCopiedNL && dstStrLengthNL) { 19530b57cec5SDimitry Andric // Make sure the two lengths together don't overflow a size_t. 1954480093f4SDimitry Andric state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL); 19550b57cec5SDimitry Andric if (!state) 19560b57cec5SDimitry Andric return; 19570b57cec5SDimitry Andric 1958480093f4SDimitry Andric finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL, 19590b57cec5SDimitry Andric *dstStrLengthNL, sizeTy); 19600b57cec5SDimitry Andric } 19610b57cec5SDimitry Andric 19620b57cec5SDimitry Andric // If we couldn't get a single value for the final string length, 19630b57cec5SDimitry Andric // we can at least bound it by the individual lengths. 19640b57cec5SDimitry Andric if (finalStrLength.isUnknown()) { 19650b57cec5SDimitry Andric // Try to get a "hypothetical" string length symbol, which we can later 19660b57cec5SDimitry Andric // set as a real value if that turns out to be the case. 19670b57cec5SDimitry Andric finalStrLength = getCStringLength(C, state, CE, DstVal, true); 19680b57cec5SDimitry Andric assert(!finalStrLength.isUndef()); 19690b57cec5SDimitry Andric 1970bdd1243dSDimitry Andric if (std::optional<NonLoc> finalStrLengthNL = 1971bdd1243dSDimitry Andric finalStrLength.getAs<NonLoc>()) { 1972480093f4SDimitry Andric if (amountCopiedNL && appendK == ConcatFnKind::none) { 1973480093f4SDimitry Andric // we overwrite dst string with the src 19740b57cec5SDimitry Andric // finalStrLength >= srcStrLength 1975480093f4SDimitry Andric SVal sourceInResult = svalBuilder.evalBinOpNN( 1976480093f4SDimitry Andric state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy); 19770b57cec5SDimitry Andric state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(), 19780b57cec5SDimitry Andric true); 19790b57cec5SDimitry Andric if (!state) 19800b57cec5SDimitry Andric return; 19810b57cec5SDimitry Andric } 19820b57cec5SDimitry Andric 1983480093f4SDimitry Andric if (dstStrLengthNL && appendK != ConcatFnKind::none) { 1984480093f4SDimitry Andric // we extend the dst string with the src 19850b57cec5SDimitry Andric // finalStrLength >= dstStrLength 19860b57cec5SDimitry Andric SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE, 19870b57cec5SDimitry Andric *finalStrLengthNL, 19880b57cec5SDimitry Andric *dstStrLengthNL, 19890b57cec5SDimitry Andric cmpTy); 19900b57cec5SDimitry Andric state = 19910b57cec5SDimitry Andric state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true); 19920b57cec5SDimitry Andric if (!state) 19930b57cec5SDimitry Andric return; 19940b57cec5SDimitry Andric } 19950b57cec5SDimitry Andric } 19960b57cec5SDimitry Andric } 19970b57cec5SDimitry Andric 19980b57cec5SDimitry Andric } else { 19990b57cec5SDimitry Andric // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and 20000b57cec5SDimitry Andric // the final string length will match the input string length. 20010b57cec5SDimitry Andric finalStrLength = amountCopied; 20020b57cec5SDimitry Andric } 20030b57cec5SDimitry Andric 20040b57cec5SDimitry Andric SVal Result; 20050b57cec5SDimitry Andric 20060b57cec5SDimitry Andric if (returnPtr) { 20070b57cec5SDimitry Andric // The final result of the function will either be a pointer past the last 20080b57cec5SDimitry Andric // copied element, or a pointer to the start of the destination buffer. 2009480093f4SDimitry Andric Result = (ReturnEnd ? UnknownVal() : DstVal); 20100b57cec5SDimitry Andric } else { 2011480093f4SDimitry Andric if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none) 2012480093f4SDimitry Andric //strlcpy, strlcat 2013480093f4SDimitry Andric Result = strlRetVal; 2014480093f4SDimitry Andric else 20150b57cec5SDimitry Andric Result = finalStrLength; 20160b57cec5SDimitry Andric } 20170b57cec5SDimitry Andric 20180b57cec5SDimitry Andric assert(state); 20190b57cec5SDimitry Andric 20200b57cec5SDimitry Andric // If the destination is a MemRegion, try to check for a buffer overflow and 20210b57cec5SDimitry Andric // record the new string length. 2022bdd1243dSDimitry Andric if (std::optional<loc::MemRegionVal> dstRegVal = 20230b57cec5SDimitry Andric DstVal.getAs<loc::MemRegionVal>()) { 20245ffd83dbSDimitry Andric QualType ptrTy = Dst.Expression->getType(); 20250b57cec5SDimitry Andric 20260b57cec5SDimitry Andric // If we have an exact value on a bounded copy, use that to check for 20270b57cec5SDimitry Andric // overflows, rather than our estimate about how much is actually copied. 2028bdd1243dSDimitry Andric if (std::optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) { 20295ffd83dbSDimitry Andric SVal maxLastElement = 20305ffd83dbSDimitry Andric svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy); 20315ffd83dbSDimitry Andric 2032*5f757f3fSDimitry Andric // Check if the first byte of the destination is writable. 2033*5f757f3fSDimitry Andric state = CheckLocation(C, state, Dst, DstVal, AccessKind::write); 2034*5f757f3fSDimitry Andric if (!state) 2035*5f757f3fSDimitry Andric return; 2036*5f757f3fSDimitry Andric // Check if the last byte of the destination is writable. 20375ffd83dbSDimitry Andric state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write); 20380b57cec5SDimitry Andric if (!state) 20390b57cec5SDimitry Andric return; 20400b57cec5SDimitry Andric } 20410b57cec5SDimitry Andric 20420b57cec5SDimitry Andric // Then, if the final length is known... 2043bdd1243dSDimitry Andric if (std::optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) { 20440b57cec5SDimitry Andric SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, 20450b57cec5SDimitry Andric *knownStrLength, ptrTy); 20460b57cec5SDimitry Andric 20470b57cec5SDimitry Andric // ...and we haven't checked the bound, we'll check the actual copy. 20480b57cec5SDimitry Andric if (!boundWarning) { 2049*5f757f3fSDimitry Andric // Check if the first byte of the destination is writable. 2050*5f757f3fSDimitry Andric state = CheckLocation(C, state, Dst, DstVal, AccessKind::write); 2051*5f757f3fSDimitry Andric if (!state) 2052*5f757f3fSDimitry Andric return; 2053*5f757f3fSDimitry Andric // Check if the last byte of the destination is writable. 20545ffd83dbSDimitry Andric state = CheckLocation(C, state, Dst, lastElement, AccessKind::write); 20550b57cec5SDimitry Andric if (!state) 20560b57cec5SDimitry Andric return; 20570b57cec5SDimitry Andric } 20580b57cec5SDimitry Andric 20590b57cec5SDimitry Andric // If this is a stpcpy-style copy, the last element is the return value. 2060480093f4SDimitry Andric if (returnPtr && ReturnEnd) 20610b57cec5SDimitry Andric Result = lastElement; 20620b57cec5SDimitry Andric } 20630b57cec5SDimitry Andric 20640b57cec5SDimitry Andric // Invalidate the destination (regular invalidation without pointer-escaping 20650b57cec5SDimitry Andric // the address of the top-level region). This must happen before we set the 20660b57cec5SDimitry Andric // C string length because invalidation will clear the length. 20670b57cec5SDimitry Andric // FIXME: Even if we can't perfectly model the copy, we should see if we 20680b57cec5SDimitry Andric // can use LazyCompoundVals to copy the source values into the destination. 20690b57cec5SDimitry Andric // This would probably remove any existing bindings past the end of the 20700b57cec5SDimitry Andric // string, but that's still an improvement over blank invalidation. 207106c3fb27SDimitry Andric state = invalidateDestinationBufferBySize(C, state, Dst.Expression, 207206c3fb27SDimitry Andric *dstRegVal, amountCopied, 207306c3fb27SDimitry Andric C.getASTContext().getSizeType()); 20740b57cec5SDimitry Andric 20750b57cec5SDimitry Andric // Invalidate the source (const-invalidation without const-pointer-escaping 20760b57cec5SDimitry Andric // the address of the top-level region). 207706c3fb27SDimitry Andric state = invalidateSourceBuffer(C, state, srcExpr.Expression, srcVal); 20780b57cec5SDimitry Andric 20790b57cec5SDimitry Andric // Set the C string length of the destination, if we know it. 2080480093f4SDimitry Andric if (IsBounded && (appendK == ConcatFnKind::none)) { 20810b57cec5SDimitry Andric // strncpy is annoying in that it doesn't guarantee to null-terminate 20820b57cec5SDimitry Andric // the result string. If the original string didn't fit entirely inside 20830b57cec5SDimitry Andric // the bound (including the null-terminator), we don't know how long the 20840b57cec5SDimitry Andric // result is. 20850b57cec5SDimitry Andric if (amountCopied != strLength) 20860b57cec5SDimitry Andric finalStrLength = UnknownVal(); 20870b57cec5SDimitry Andric } 20880b57cec5SDimitry Andric state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength); 20890b57cec5SDimitry Andric } 20900b57cec5SDimitry Andric 20910b57cec5SDimitry Andric assert(state); 20920b57cec5SDimitry Andric 20930b57cec5SDimitry Andric if (returnPtr) { 20940b57cec5SDimitry Andric // If this is a stpcpy-style copy, but we were unable to check for a buffer 20950b57cec5SDimitry Andric // overflow, we still need a result. Conjure a return value. 2096480093f4SDimitry Andric if (ReturnEnd && Result.isUnknown()) { 20970b57cec5SDimitry Andric Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 20980b57cec5SDimitry Andric } 20990b57cec5SDimitry Andric } 21000b57cec5SDimitry Andric // Set the return value. 21010b57cec5SDimitry Andric state = state->BindExpr(CE, LCtx, Result); 21020b57cec5SDimitry Andric C.addTransition(state); 21030b57cec5SDimitry Andric } 21040b57cec5SDimitry Andric 21050b57cec5SDimitry Andric void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const { 21060b57cec5SDimitry Andric //int strcmp(const char *s1, const char *s2); 2107480093f4SDimitry Andric evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false); 21080b57cec5SDimitry Andric } 21090b57cec5SDimitry Andric 21100b57cec5SDimitry Andric void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const { 21110b57cec5SDimitry Andric //int strncmp(const char *s1, const char *s2, size_t n); 2112480093f4SDimitry Andric evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false); 21130b57cec5SDimitry Andric } 21140b57cec5SDimitry Andric 21150b57cec5SDimitry Andric void CStringChecker::evalStrcasecmp(CheckerContext &C, 21160b57cec5SDimitry Andric const CallExpr *CE) const { 21170b57cec5SDimitry Andric //int strcasecmp(const char *s1, const char *s2); 2118480093f4SDimitry Andric evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true); 21190b57cec5SDimitry Andric } 21200b57cec5SDimitry Andric 21210b57cec5SDimitry Andric void CStringChecker::evalStrncasecmp(CheckerContext &C, 21220b57cec5SDimitry Andric const CallExpr *CE) const { 21230b57cec5SDimitry Andric //int strncasecmp(const char *s1, const char *s2, size_t n); 2124480093f4SDimitry Andric evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true); 21250b57cec5SDimitry Andric } 21260b57cec5SDimitry Andric 21270b57cec5SDimitry Andric void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, 2128480093f4SDimitry Andric bool IsBounded, bool IgnoreCase) const { 21290b57cec5SDimitry Andric CurrentFunctionDescription = "string comparison function"; 21300b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 21310b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 21320b57cec5SDimitry Andric 21330b57cec5SDimitry Andric // Check that the first string is non-null 21345ffd83dbSDimitry Andric AnyArgExpr Left = {CE->getArg(0), 0}; 21355ffd83dbSDimitry Andric SVal LeftVal = state->getSVal(Left.Expression, LCtx); 21365ffd83dbSDimitry Andric state = checkNonNull(C, state, Left, LeftVal); 21370b57cec5SDimitry Andric if (!state) 21380b57cec5SDimitry Andric return; 21390b57cec5SDimitry Andric 21400b57cec5SDimitry Andric // Check that the second string is non-null. 21415ffd83dbSDimitry Andric AnyArgExpr Right = {CE->getArg(1), 1}; 21425ffd83dbSDimitry Andric SVal RightVal = state->getSVal(Right.Expression, LCtx); 21435ffd83dbSDimitry Andric state = checkNonNull(C, state, Right, RightVal); 21440b57cec5SDimitry Andric if (!state) 21450b57cec5SDimitry Andric return; 21460b57cec5SDimitry Andric 21470b57cec5SDimitry Andric // Get the string length of the first string or give up. 21485ffd83dbSDimitry Andric SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal); 21495ffd83dbSDimitry Andric if (LeftLength.isUndef()) 21500b57cec5SDimitry Andric return; 21510b57cec5SDimitry Andric 21520b57cec5SDimitry Andric // Get the string length of the second string or give up. 21535ffd83dbSDimitry Andric SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal); 21545ffd83dbSDimitry Andric if (RightLength.isUndef()) 21550b57cec5SDimitry Andric return; 21560b57cec5SDimitry Andric 21570b57cec5SDimitry Andric // If we know the two buffers are the same, we know the result is 0. 21580b57cec5SDimitry Andric // First, get the two buffers' addresses. Another checker will have already 21590b57cec5SDimitry Andric // made sure they're not undefined. 21605ffd83dbSDimitry Andric DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>(); 21615ffd83dbSDimitry Andric DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>(); 21620b57cec5SDimitry Andric 21630b57cec5SDimitry Andric // See if they are the same. 21640b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 21650b57cec5SDimitry Andric DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); 21660b57cec5SDimitry Andric ProgramStateRef StSameBuf, StNotSameBuf; 21670b57cec5SDimitry Andric std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); 21680b57cec5SDimitry Andric 21690b57cec5SDimitry Andric // If the two arguments might be the same buffer, we know the result is 0, 21700b57cec5SDimitry Andric // and we only need to check one size. 21710b57cec5SDimitry Andric if (StSameBuf) { 21720b57cec5SDimitry Andric StSameBuf = StSameBuf->BindExpr(CE, LCtx, 21730b57cec5SDimitry Andric svalBuilder.makeZeroVal(CE->getType())); 21740b57cec5SDimitry Andric C.addTransition(StSameBuf); 21750b57cec5SDimitry Andric 21760b57cec5SDimitry Andric // If the two arguments are GUARANTEED to be the same, we're done! 21770b57cec5SDimitry Andric if (!StNotSameBuf) 21780b57cec5SDimitry Andric return; 21790b57cec5SDimitry Andric } 21800b57cec5SDimitry Andric 21810b57cec5SDimitry Andric assert(StNotSameBuf); 21820b57cec5SDimitry Andric state = StNotSameBuf; 21830b57cec5SDimitry Andric 21840b57cec5SDimitry Andric // At this point we can go about comparing the two buffers. 21850b57cec5SDimitry Andric // For now, we only do this if they're both known string literals. 21860b57cec5SDimitry Andric 21870b57cec5SDimitry Andric // Attempt to extract string literals from both expressions. 21885ffd83dbSDimitry Andric const StringLiteral *LeftStrLiteral = 21895ffd83dbSDimitry Andric getCStringLiteral(C, state, Left.Expression, LeftVal); 21905ffd83dbSDimitry Andric const StringLiteral *RightStrLiteral = 21915ffd83dbSDimitry Andric getCStringLiteral(C, state, Right.Expression, RightVal); 21920b57cec5SDimitry Andric bool canComputeResult = false; 21930b57cec5SDimitry Andric SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, 21940b57cec5SDimitry Andric C.blockCount()); 21950b57cec5SDimitry Andric 21965ffd83dbSDimitry Andric if (LeftStrLiteral && RightStrLiteral) { 21975ffd83dbSDimitry Andric StringRef LeftStrRef = LeftStrLiteral->getString(); 21985ffd83dbSDimitry Andric StringRef RightStrRef = RightStrLiteral->getString(); 21990b57cec5SDimitry Andric 2200480093f4SDimitry Andric if (IsBounded) { 22010b57cec5SDimitry Andric // Get the max number of characters to compare. 22020b57cec5SDimitry Andric const Expr *lenExpr = CE->getArg(2); 22030b57cec5SDimitry Andric SVal lenVal = state->getSVal(lenExpr, LCtx); 22040b57cec5SDimitry Andric 22050b57cec5SDimitry Andric // If the length is known, we can get the right substrings. 22060b57cec5SDimitry Andric if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) { 22070b57cec5SDimitry Andric // Create substrings of each to compare the prefix. 22085ffd83dbSDimitry Andric LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue()); 22095ffd83dbSDimitry Andric RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue()); 22100b57cec5SDimitry Andric canComputeResult = true; 22110b57cec5SDimitry Andric } 22120b57cec5SDimitry Andric } else { 22130b57cec5SDimitry Andric // This is a normal, unbounded strcmp. 22140b57cec5SDimitry Andric canComputeResult = true; 22150b57cec5SDimitry Andric } 22160b57cec5SDimitry Andric 22170b57cec5SDimitry Andric if (canComputeResult) { 22180b57cec5SDimitry Andric // Real strcmp stops at null characters. 22195ffd83dbSDimitry Andric size_t s1Term = LeftStrRef.find('\0'); 22200b57cec5SDimitry Andric if (s1Term != StringRef::npos) 22215ffd83dbSDimitry Andric LeftStrRef = LeftStrRef.substr(0, s1Term); 22220b57cec5SDimitry Andric 22235ffd83dbSDimitry Andric size_t s2Term = RightStrRef.find('\0'); 22240b57cec5SDimitry Andric if (s2Term != StringRef::npos) 22255ffd83dbSDimitry Andric RightStrRef = RightStrRef.substr(0, s2Term); 22260b57cec5SDimitry Andric 22270b57cec5SDimitry Andric // Use StringRef's comparison methods to compute the actual result. 2228fe6060f1SDimitry Andric int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef) 22295ffd83dbSDimitry Andric : LeftStrRef.compare(RightStrRef); 22300b57cec5SDimitry Andric 22310b57cec5SDimitry Andric // The strcmp function returns an integer greater than, equal to, or less 22320b57cec5SDimitry Andric // than zero, [c11, p7.24.4.2]. 22330b57cec5SDimitry Andric if (compareRes == 0) { 22340b57cec5SDimitry Andric resultVal = svalBuilder.makeIntVal(compareRes, CE->getType()); 22350b57cec5SDimitry Andric } 22360b57cec5SDimitry Andric else { 22370b57cec5SDimitry Andric DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType()); 22380b57cec5SDimitry Andric // Constrain strcmp's result range based on the result of StringRef's 22390b57cec5SDimitry Andric // comparison methods. 2240bdd1243dSDimitry Andric BinaryOperatorKind op = (compareRes > 0) ? BO_GT : BO_LT; 22410b57cec5SDimitry Andric SVal compareWithZero = 22420b57cec5SDimitry Andric svalBuilder.evalBinOp(state, op, resultVal, zeroVal, 22430b57cec5SDimitry Andric svalBuilder.getConditionType()); 22440b57cec5SDimitry Andric DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>(); 22450b57cec5SDimitry Andric state = state->assume(compareWithZeroVal, true); 22460b57cec5SDimitry Andric } 22470b57cec5SDimitry Andric } 22480b57cec5SDimitry Andric } 22490b57cec5SDimitry Andric 22500b57cec5SDimitry Andric state = state->BindExpr(CE, LCtx, resultVal); 22510b57cec5SDimitry Andric 22520b57cec5SDimitry Andric // Record this as a possible path. 22530b57cec5SDimitry Andric C.addTransition(state); 22540b57cec5SDimitry Andric } 22550b57cec5SDimitry Andric 22560b57cec5SDimitry Andric void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const { 22570b57cec5SDimitry Andric // char *strsep(char **stringp, const char *delim); 22585e801ac6SDimitry Andric // Verify whether the search string parameter matches the return type. 225906c3fb27SDimitry Andric SourceArgExpr SearchStrPtr = {{CE->getArg(0), 0}}; 22605ffd83dbSDimitry Andric 22615ffd83dbSDimitry Andric QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType(); 22620b57cec5SDimitry Andric if (CharPtrTy.isNull() || 22630b57cec5SDimitry Andric CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType()) 22640b57cec5SDimitry Andric return; 22650b57cec5SDimitry Andric 22660b57cec5SDimitry Andric CurrentFunctionDescription = "strsep()"; 22670b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 22680b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 22690b57cec5SDimitry Andric 22700b57cec5SDimitry Andric // Check that the search string pointer is non-null (though it may point to 22710b57cec5SDimitry Andric // a null string). 22725ffd83dbSDimitry Andric SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx); 22735ffd83dbSDimitry Andric State = checkNonNull(C, State, SearchStrPtr, SearchStrVal); 22740b57cec5SDimitry Andric if (!State) 22750b57cec5SDimitry Andric return; 22760b57cec5SDimitry Andric 22770b57cec5SDimitry Andric // Check that the delimiter string is non-null. 22785ffd83dbSDimitry Andric AnyArgExpr DelimStr = {CE->getArg(1), 1}; 22795ffd83dbSDimitry Andric SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx); 22805ffd83dbSDimitry Andric State = checkNonNull(C, State, DelimStr, DelimStrVal); 22810b57cec5SDimitry Andric if (!State) 22820b57cec5SDimitry Andric return; 22830b57cec5SDimitry Andric 22840b57cec5SDimitry Andric SValBuilder &SVB = C.getSValBuilder(); 22850b57cec5SDimitry Andric SVal Result; 2286bdd1243dSDimitry Andric if (std::optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) { 22870b57cec5SDimitry Andric // Get the current value of the search string pointer, as a char*. 22880b57cec5SDimitry Andric Result = State->getSVal(*SearchStrLoc, CharPtrTy); 22890b57cec5SDimitry Andric 22900b57cec5SDimitry Andric // Invalidate the search string, representing the change of one delimiter 22910b57cec5SDimitry Andric // character to NUL. 229206c3fb27SDimitry Andric // As the replacement never overflows, do not invalidate its super region. 229306c3fb27SDimitry Andric State = invalidateDestinationBufferNeverOverflows( 229406c3fb27SDimitry Andric C, State, SearchStrPtr.Expression, Result); 22950b57cec5SDimitry Andric 22960b57cec5SDimitry Andric // Overwrite the search string pointer. The new value is either an address 22970b57cec5SDimitry Andric // further along in the same string, or NULL if there are no more tokens. 22980b57cec5SDimitry Andric State = State->bindLoc(*SearchStrLoc, 22990b57cec5SDimitry Andric SVB.conjureSymbolVal(getTag(), 23000b57cec5SDimitry Andric CE, 23010b57cec5SDimitry Andric LCtx, 23020b57cec5SDimitry Andric CharPtrTy, 23030b57cec5SDimitry Andric C.blockCount()), 23040b57cec5SDimitry Andric LCtx); 23050b57cec5SDimitry Andric } else { 23060b57cec5SDimitry Andric assert(SearchStrVal.isUnknown()); 23070b57cec5SDimitry Andric // Conjure a symbolic value. It's the best we can do. 23080b57cec5SDimitry Andric Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 23090b57cec5SDimitry Andric } 23100b57cec5SDimitry Andric 23110b57cec5SDimitry Andric // Set the return value, and finish. 23120b57cec5SDimitry Andric State = State->BindExpr(CE, LCtx, Result); 23130b57cec5SDimitry Andric C.addTransition(State); 23140b57cec5SDimitry Andric } 23150b57cec5SDimitry Andric 23160b57cec5SDimitry Andric // These should probably be moved into a C++ standard library checker. 23170b57cec5SDimitry Andric void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const { 23180b57cec5SDimitry Andric evalStdCopyCommon(C, CE); 23190b57cec5SDimitry Andric } 23200b57cec5SDimitry Andric 23210b57cec5SDimitry Andric void CStringChecker::evalStdCopyBackward(CheckerContext &C, 23220b57cec5SDimitry Andric const CallExpr *CE) const { 23230b57cec5SDimitry Andric evalStdCopyCommon(C, CE); 23240b57cec5SDimitry Andric } 23250b57cec5SDimitry Andric 23260b57cec5SDimitry Andric void CStringChecker::evalStdCopyCommon(CheckerContext &C, 23270b57cec5SDimitry Andric const CallExpr *CE) const { 23280b57cec5SDimitry Andric if (!CE->getArg(2)->getType()->isPointerType()) 23290b57cec5SDimitry Andric return; 23300b57cec5SDimitry Andric 23310b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 23320b57cec5SDimitry Andric 23330b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 23340b57cec5SDimitry Andric 23350b57cec5SDimitry Andric // template <class _InputIterator, class _OutputIterator> 23360b57cec5SDimitry Andric // _OutputIterator 23370b57cec5SDimitry Andric // copy(_InputIterator __first, _InputIterator __last, 23380b57cec5SDimitry Andric // _OutputIterator __result) 23390b57cec5SDimitry Andric 23400b57cec5SDimitry Andric // Invalidate the destination buffer 23410b57cec5SDimitry Andric const Expr *Dst = CE->getArg(2); 23420b57cec5SDimitry Andric SVal DstVal = State->getSVal(Dst, LCtx); 234306c3fb27SDimitry Andric // FIXME: As we do not know how many items are copied, we also invalidate the 234406c3fb27SDimitry Andric // super region containing the target location. 234506c3fb27SDimitry Andric State = 234606c3fb27SDimitry Andric invalidateDestinationBufferAlwaysEscapeSuperRegion(C, State, Dst, DstVal); 23470b57cec5SDimitry Andric 23480b57cec5SDimitry Andric SValBuilder &SVB = C.getSValBuilder(); 23490b57cec5SDimitry Andric 23500b57cec5SDimitry Andric SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 23510b57cec5SDimitry Andric State = State->BindExpr(CE, LCtx, ResultVal); 23520b57cec5SDimitry Andric 23530b57cec5SDimitry Andric C.addTransition(State); 23540b57cec5SDimitry Andric } 23550b57cec5SDimitry Andric 23560b57cec5SDimitry Andric void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const { 23575ffd83dbSDimitry Andric // void *memset(void *s, int c, size_t n); 23580b57cec5SDimitry Andric CurrentFunctionDescription = "memory set function"; 23590b57cec5SDimitry Andric 236006c3fb27SDimitry Andric DestinationArgExpr Buffer = {{CE->getArg(0), 0}}; 23615ffd83dbSDimitry Andric AnyArgExpr CharE = {CE->getArg(1), 1}; 236206c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(2), 2}}; 23635ffd83dbSDimitry Andric 23640b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 23650b57cec5SDimitry Andric 23660b57cec5SDimitry Andric // See if the size argument is zero. 23670b57cec5SDimitry Andric const LocationContext *LCtx = C.getLocationContext(); 23685ffd83dbSDimitry Andric SVal SizeVal = C.getSVal(Size.Expression); 23695ffd83dbSDimitry Andric QualType SizeTy = Size.Expression->getType(); 23700b57cec5SDimitry Andric 23715ffd83dbSDimitry Andric ProgramStateRef ZeroSize, NonZeroSize; 23725ffd83dbSDimitry Andric std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy); 23730b57cec5SDimitry Andric 23740b57cec5SDimitry Andric // Get the value of the memory area. 23755ffd83dbSDimitry Andric SVal BufferPtrVal = C.getSVal(Buffer.Expression); 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric // If the size is zero, there won't be any actual memory access, so 23785ffd83dbSDimitry Andric // just bind the return value to the buffer and return. 23795ffd83dbSDimitry Andric if (ZeroSize && !NonZeroSize) { 23805ffd83dbSDimitry Andric ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal); 23815ffd83dbSDimitry Andric C.addTransition(ZeroSize); 23820b57cec5SDimitry Andric return; 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric // Ensure the memory area is not null. 23860b57cec5SDimitry Andric // If it is NULL there will be a NULL pointer dereference. 23875ffd83dbSDimitry Andric State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal); 23880b57cec5SDimitry Andric if (!State) 23890b57cec5SDimitry Andric return; 23900b57cec5SDimitry Andric 23915ffd83dbSDimitry Andric State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write); 23920b57cec5SDimitry Andric if (!State) 23930b57cec5SDimitry Andric return; 23940b57cec5SDimitry Andric 23950b57cec5SDimitry Andric // According to the values of the arguments, bind the value of the second 23960b57cec5SDimitry Andric // argument to the destination buffer and set string length, or just 23970b57cec5SDimitry Andric // invalidate the destination buffer. 23985ffd83dbSDimitry Andric if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression), 23995ffd83dbSDimitry Andric Size.Expression, C, State)) 24000b57cec5SDimitry Andric return; 24010b57cec5SDimitry Andric 24025ffd83dbSDimitry Andric State = State->BindExpr(CE, LCtx, BufferPtrVal); 24030b57cec5SDimitry Andric C.addTransition(State); 24040b57cec5SDimitry Andric } 24050b57cec5SDimitry Andric 24060b57cec5SDimitry Andric void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const { 24070b57cec5SDimitry Andric CurrentFunctionDescription = "memory clearance function"; 24080b57cec5SDimitry Andric 240906c3fb27SDimitry Andric DestinationArgExpr Buffer = {{CE->getArg(0), 0}}; 241006c3fb27SDimitry Andric SizeArgExpr Size = {{CE->getArg(1), 1}}; 24110b57cec5SDimitry Andric SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy); 24120b57cec5SDimitry Andric 24130b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 24140b57cec5SDimitry Andric 24150b57cec5SDimitry Andric // See if the size argument is zero. 24165ffd83dbSDimitry Andric SVal SizeVal = C.getSVal(Size.Expression); 24175ffd83dbSDimitry Andric QualType SizeTy = Size.Expression->getType(); 24180b57cec5SDimitry Andric 24190b57cec5SDimitry Andric ProgramStateRef StateZeroSize, StateNonZeroSize; 24200b57cec5SDimitry Andric std::tie(StateZeroSize, StateNonZeroSize) = 24210b57cec5SDimitry Andric assumeZero(C, State, SizeVal, SizeTy); 24220b57cec5SDimitry Andric 24230b57cec5SDimitry Andric // If the size is zero, there won't be any actual memory access, 24240b57cec5SDimitry Andric // In this case we just return. 24250b57cec5SDimitry Andric if (StateZeroSize && !StateNonZeroSize) { 24260b57cec5SDimitry Andric C.addTransition(StateZeroSize); 24270b57cec5SDimitry Andric return; 24280b57cec5SDimitry Andric } 24290b57cec5SDimitry Andric 24300b57cec5SDimitry Andric // Get the value of the memory area. 24315ffd83dbSDimitry Andric SVal MemVal = C.getSVal(Buffer.Expression); 24320b57cec5SDimitry Andric 24330b57cec5SDimitry Andric // Ensure the memory area is not null. 24340b57cec5SDimitry Andric // If it is NULL there will be a NULL pointer dereference. 24355ffd83dbSDimitry Andric State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal); 24360b57cec5SDimitry Andric if (!State) 24370b57cec5SDimitry Andric return; 24380b57cec5SDimitry Andric 24395ffd83dbSDimitry Andric State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write); 24400b57cec5SDimitry Andric if (!State) 24410b57cec5SDimitry Andric return; 24420b57cec5SDimitry Andric 24435ffd83dbSDimitry Andric if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State)) 24440b57cec5SDimitry Andric return; 24450b57cec5SDimitry Andric 24460b57cec5SDimitry Andric C.addTransition(State); 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric 244906c3fb27SDimitry Andric void CStringChecker::evalSprintf(CheckerContext &C, const CallExpr *CE) const { 245006c3fb27SDimitry Andric CurrentFunctionDescription = "'sprintf'"; 245106c3fb27SDimitry Andric bool IsBI = CE->getBuiltinCallee() == Builtin::BI__builtin___sprintf_chk; 245206c3fb27SDimitry Andric evalSprintfCommon(C, CE, /* IsBounded */ false, IsBI); 245306c3fb27SDimitry Andric } 245406c3fb27SDimitry Andric 245506c3fb27SDimitry Andric void CStringChecker::evalSnprintf(CheckerContext &C, const CallExpr *CE) const { 245606c3fb27SDimitry Andric CurrentFunctionDescription = "'snprintf'"; 245706c3fb27SDimitry Andric bool IsBI = CE->getBuiltinCallee() == Builtin::BI__builtin___snprintf_chk; 245806c3fb27SDimitry Andric evalSprintfCommon(C, CE, /* IsBounded */ true, IsBI); 245906c3fb27SDimitry Andric } 246006c3fb27SDimitry Andric 246106c3fb27SDimitry Andric void CStringChecker::evalSprintfCommon(CheckerContext &C, const CallExpr *CE, 246206c3fb27SDimitry Andric bool IsBounded, bool IsBuiltin) const { 246306c3fb27SDimitry Andric ProgramStateRef State = C.getState(); 246406c3fb27SDimitry Andric DestinationArgExpr Dest = {{CE->getArg(0), 0}}; 246506c3fb27SDimitry Andric 246606c3fb27SDimitry Andric const auto NumParams = CE->getCalleeDecl()->getAsFunction()->getNumParams(); 246706c3fb27SDimitry Andric assert(CE->getNumArgs() >= NumParams); 246806c3fb27SDimitry Andric 246906c3fb27SDimitry Andric const auto AllArguments = 247006c3fb27SDimitry Andric llvm::make_range(CE->getArgs(), CE->getArgs() + CE->getNumArgs()); 247106c3fb27SDimitry Andric const auto VariadicArguments = drop_begin(enumerate(AllArguments), NumParams); 247206c3fb27SDimitry Andric 247306c3fb27SDimitry Andric for (const auto &[ArgIdx, ArgExpr] : VariadicArguments) { 247406c3fb27SDimitry Andric // We consider only string buffers 247506c3fb27SDimitry Andric if (const QualType type = ArgExpr->getType(); 247606c3fb27SDimitry Andric !type->isAnyPointerType() || 247706c3fb27SDimitry Andric !type->getPointeeType()->isAnyCharacterType()) 247806c3fb27SDimitry Andric continue; 247906c3fb27SDimitry Andric SourceArgExpr Source = {{ArgExpr, unsigned(ArgIdx)}}; 248006c3fb27SDimitry Andric 248106c3fb27SDimitry Andric // Ensure the buffers do not overlap. 248206c3fb27SDimitry Andric SizeArgExpr SrcExprAsSizeDummy = { 248306c3fb27SDimitry Andric {Source.Expression, Source.ArgumentIndex}}; 248406c3fb27SDimitry Andric State = CheckOverlap( 248506c3fb27SDimitry Andric C, State, 248606c3fb27SDimitry Andric (IsBounded ? SizeArgExpr{{CE->getArg(1), 1}} : SrcExprAsSizeDummy), 248706c3fb27SDimitry Andric Dest, Source); 248806c3fb27SDimitry Andric if (!State) 248906c3fb27SDimitry Andric return; 249006c3fb27SDimitry Andric } 249106c3fb27SDimitry Andric 249206c3fb27SDimitry Andric C.addTransition(State); 249306c3fb27SDimitry Andric } 249406c3fb27SDimitry Andric 24950b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 24960b57cec5SDimitry Andric // The driver method, and other Checker callbacks. 24970b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 24980b57cec5SDimitry Andric 24990b57cec5SDimitry Andric CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call, 25000b57cec5SDimitry Andric CheckerContext &C) const { 25010b57cec5SDimitry Andric const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr()); 25020b57cec5SDimitry Andric if (!CE) 25030b57cec5SDimitry Andric return nullptr; 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl()); 25060b57cec5SDimitry Andric if (!FD) 25070b57cec5SDimitry Andric return nullptr; 25080b57cec5SDimitry Andric 2509349cc55cSDimitry Andric if (StdCopy.matches(Call)) 25100b57cec5SDimitry Andric return &CStringChecker::evalStdCopy; 2511349cc55cSDimitry Andric if (StdCopyBackward.matches(Call)) 25120b57cec5SDimitry Andric return &CStringChecker::evalStdCopyBackward; 25130b57cec5SDimitry Andric 25140b57cec5SDimitry Andric // Pro-actively check that argument types are safe to do arithmetic upon. 25150b57cec5SDimitry Andric // We do not want to crash if someone accidentally passes a structure 25160b57cec5SDimitry Andric // into, say, a C++ overload of any of these functions. We could not check 25170b57cec5SDimitry Andric // that for std::copy because they may have arguments of other types. 25180b57cec5SDimitry Andric for (auto I : CE->arguments()) { 25190b57cec5SDimitry Andric QualType T = I->getType(); 25200b57cec5SDimitry Andric if (!T->isIntegralOrEnumerationType() && !T->isPointerType()) 25210b57cec5SDimitry Andric return nullptr; 25220b57cec5SDimitry Andric } 25230b57cec5SDimitry Andric 25240b57cec5SDimitry Andric const FnCheck *Callback = Callbacks.lookup(Call); 25250b57cec5SDimitry Andric if (Callback) 25260b57cec5SDimitry Andric return *Callback; 25270b57cec5SDimitry Andric 25280b57cec5SDimitry Andric return nullptr; 25290b57cec5SDimitry Andric } 25300b57cec5SDimitry Andric 25310b57cec5SDimitry Andric bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const { 25320b57cec5SDimitry Andric FnCheck Callback = identifyCall(Call, C); 25330b57cec5SDimitry Andric 25340b57cec5SDimitry Andric // If the callee isn't a string function, let another checker handle it. 25350b57cec5SDimitry Andric if (!Callback) 25360b57cec5SDimitry Andric return false; 25370b57cec5SDimitry Andric 25380b57cec5SDimitry Andric // Check and evaluate the call. 25390b57cec5SDimitry Andric const auto *CE = cast<CallExpr>(Call.getOriginExpr()); 2540972a253aSDimitry Andric Callback(this, C, CE); 25410b57cec5SDimitry Andric 25420b57cec5SDimitry Andric // If the evaluate call resulted in no change, chain to the next eval call 25430b57cec5SDimitry Andric // handler. 25440b57cec5SDimitry Andric // Note, the custom CString evaluation calls assume that basic safety 25450b57cec5SDimitry Andric // properties are held. However, if the user chooses to turn off some of these 25460b57cec5SDimitry Andric // checks, we ignore the issues and leave the call evaluation to a generic 25470b57cec5SDimitry Andric // handler. 25480b57cec5SDimitry Andric return C.isDifferent(); 25490b57cec5SDimitry Andric } 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { 25520b57cec5SDimitry Andric // Record string length for char a[] = "abc"; 25530b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 25540b57cec5SDimitry Andric 25550b57cec5SDimitry Andric for (const auto *I : DS->decls()) { 25560b57cec5SDimitry Andric const VarDecl *D = dyn_cast<VarDecl>(I); 25570b57cec5SDimitry Andric if (!D) 25580b57cec5SDimitry Andric continue; 25590b57cec5SDimitry Andric 25600b57cec5SDimitry Andric // FIXME: Handle array fields of structs. 25610b57cec5SDimitry Andric if (!D->getType()->isArrayType()) 25620b57cec5SDimitry Andric continue; 25630b57cec5SDimitry Andric 25640b57cec5SDimitry Andric const Expr *Init = D->getInit(); 25650b57cec5SDimitry Andric if (!Init) 25660b57cec5SDimitry Andric continue; 25670b57cec5SDimitry Andric if (!isa<StringLiteral>(Init)) 25680b57cec5SDimitry Andric continue; 25690b57cec5SDimitry Andric 25700b57cec5SDimitry Andric Loc VarLoc = state->getLValue(D, C.getLocationContext()); 25710b57cec5SDimitry Andric const MemRegion *MR = VarLoc.getAsRegion(); 25720b57cec5SDimitry Andric if (!MR) 25730b57cec5SDimitry Andric continue; 25740b57cec5SDimitry Andric 25750b57cec5SDimitry Andric SVal StrVal = C.getSVal(Init); 25760b57cec5SDimitry Andric assert(StrVal.isValid() && "Initializer string is unknown or undefined"); 25770b57cec5SDimitry Andric DefinedOrUnknownSVal strLength = 25780b57cec5SDimitry Andric getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>(); 25790b57cec5SDimitry Andric 25800b57cec5SDimitry Andric state = state->set<CStringLength>(MR, strLength); 25810b57cec5SDimitry Andric } 25820b57cec5SDimitry Andric 25830b57cec5SDimitry Andric C.addTransition(state); 25840b57cec5SDimitry Andric } 25850b57cec5SDimitry Andric 25860b57cec5SDimitry Andric ProgramStateRef 25870b57cec5SDimitry Andric CStringChecker::checkRegionChanges(ProgramStateRef state, 25880b57cec5SDimitry Andric const InvalidatedSymbols *, 25890b57cec5SDimitry Andric ArrayRef<const MemRegion *> ExplicitRegions, 25900b57cec5SDimitry Andric ArrayRef<const MemRegion *> Regions, 25910b57cec5SDimitry Andric const LocationContext *LCtx, 25920b57cec5SDimitry Andric const CallEvent *Call) const { 25930b57cec5SDimitry Andric CStringLengthTy Entries = state->get<CStringLength>(); 25940b57cec5SDimitry Andric if (Entries.isEmpty()) 25950b57cec5SDimitry Andric return state; 25960b57cec5SDimitry Andric 25970b57cec5SDimitry Andric llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; 25980b57cec5SDimitry Andric llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; 25990b57cec5SDimitry Andric 26000b57cec5SDimitry Andric // First build sets for the changed regions and their super-regions. 260106c3fb27SDimitry Andric for (const MemRegion *MR : Regions) { 26020b57cec5SDimitry Andric Invalidated.insert(MR); 26030b57cec5SDimitry Andric 26040b57cec5SDimitry Andric SuperRegions.insert(MR); 26050b57cec5SDimitry Andric while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { 26060b57cec5SDimitry Andric MR = SR->getSuperRegion(); 26070b57cec5SDimitry Andric SuperRegions.insert(MR); 26080b57cec5SDimitry Andric } 26090b57cec5SDimitry Andric } 26100b57cec5SDimitry Andric 26110b57cec5SDimitry Andric CStringLengthTy::Factory &F = state->get_context<CStringLength>(); 26120b57cec5SDimitry Andric 26130b57cec5SDimitry Andric // Then loop over the entries in the current state. 261406c3fb27SDimitry Andric for (const MemRegion *MR : llvm::make_first_range(Entries)) { 26150b57cec5SDimitry Andric // Is this entry for a super-region of a changed region? 26160b57cec5SDimitry Andric if (SuperRegions.count(MR)) { 26170b57cec5SDimitry Andric Entries = F.remove(Entries, MR); 26180b57cec5SDimitry Andric continue; 26190b57cec5SDimitry Andric } 26200b57cec5SDimitry Andric 26210b57cec5SDimitry Andric // Is this entry for a sub-region of a changed region? 26220b57cec5SDimitry Andric const MemRegion *Super = MR; 26230b57cec5SDimitry Andric while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { 26240b57cec5SDimitry Andric Super = SR->getSuperRegion(); 26250b57cec5SDimitry Andric if (Invalidated.count(Super)) { 26260b57cec5SDimitry Andric Entries = F.remove(Entries, MR); 26270b57cec5SDimitry Andric break; 26280b57cec5SDimitry Andric } 26290b57cec5SDimitry Andric } 26300b57cec5SDimitry Andric } 26310b57cec5SDimitry Andric 26320b57cec5SDimitry Andric return state->set<CStringLength>(Entries); 26330b57cec5SDimitry Andric } 26340b57cec5SDimitry Andric 26350b57cec5SDimitry Andric void CStringChecker::checkLiveSymbols(ProgramStateRef state, 26360b57cec5SDimitry Andric SymbolReaper &SR) const { 26370b57cec5SDimitry Andric // Mark all symbols in our string length map as valid. 26380b57cec5SDimitry Andric CStringLengthTy Entries = state->get<CStringLength>(); 26390b57cec5SDimitry Andric 264006c3fb27SDimitry Andric for (SVal Len : llvm::make_second_range(Entries)) { 264106c3fb27SDimitry Andric for (SymbolRef Sym : Len.symbols()) 264206c3fb27SDimitry Andric SR.markInUse(Sym); 26430b57cec5SDimitry Andric } 26440b57cec5SDimitry Andric } 26450b57cec5SDimitry Andric 26460b57cec5SDimitry Andric void CStringChecker::checkDeadSymbols(SymbolReaper &SR, 26470b57cec5SDimitry Andric CheckerContext &C) const { 26480b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 26490b57cec5SDimitry Andric CStringLengthTy Entries = state->get<CStringLength>(); 26500b57cec5SDimitry Andric if (Entries.isEmpty()) 26510b57cec5SDimitry Andric return; 26520b57cec5SDimitry Andric 26530b57cec5SDimitry Andric CStringLengthTy::Factory &F = state->get_context<CStringLength>(); 265406c3fb27SDimitry Andric for (auto [Reg, Len] : Entries) { 26550b57cec5SDimitry Andric if (SymbolRef Sym = Len.getAsSymbol()) { 26560b57cec5SDimitry Andric if (SR.isDead(Sym)) 265706c3fb27SDimitry Andric Entries = F.remove(Entries, Reg); 26580b57cec5SDimitry Andric } 26590b57cec5SDimitry Andric } 26600b57cec5SDimitry Andric 26610b57cec5SDimitry Andric state = state->set<CStringLength>(Entries); 26620b57cec5SDimitry Andric C.addTransition(state); 26630b57cec5SDimitry Andric } 26640b57cec5SDimitry Andric 26650b57cec5SDimitry Andric void ento::registerCStringModeling(CheckerManager &Mgr) { 26660b57cec5SDimitry Andric Mgr.registerChecker<CStringChecker>(); 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric 26695ffd83dbSDimitry Andric bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) { 26700b57cec5SDimitry Andric return true; 26710b57cec5SDimitry Andric } 26720b57cec5SDimitry Andric 26730b57cec5SDimitry Andric #define REGISTER_CHECKER(name) \ 26740b57cec5SDimitry Andric void ento::register##name(CheckerManager &mgr) { \ 26750b57cec5SDimitry Andric CStringChecker *checker = mgr.getChecker<CStringChecker>(); \ 26760b57cec5SDimitry Andric checker->Filter.Check##name = true; \ 2677a7dea167SDimitry Andric checker->Filter.CheckName##name = mgr.getCurrentCheckerName(); \ 26780b57cec5SDimitry Andric } \ 26790b57cec5SDimitry Andric \ 26805ffd83dbSDimitry Andric bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; } 26810b57cec5SDimitry Andric 26820b57cec5SDimitry Andric REGISTER_CHECKER(CStringNullArg) 26830b57cec5SDimitry Andric REGISTER_CHECKER(CStringOutOfBounds) 26840b57cec5SDimitry Andric REGISTER_CHECKER(CStringBufferOverlap) 26850b57cec5SDimitry Andric REGISTER_CHECKER(CStringNotNullTerm) 268681ad6265SDimitry Andric REGISTER_CHECKER(CStringUninitializedRead) 2687