1 //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This defines CStringChecker, which is an assortment of checks on calls 10 // to functions in <string.h>. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "InterCheckerAPI.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 18 #include "clang/StaticAnalyzer/Core/Checker.h" 19 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 22 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h" 23 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/SmallString.h" 26 #include "llvm/Support/raw_ostream.h" 27 28 using namespace clang; 29 using namespace ento; 30 31 namespace { 32 enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 }; 33 class CStringChecker : public Checker< eval::Call, 34 check::PreStmt<DeclStmt>, 35 check::LiveSymbols, 36 check::DeadSymbols, 37 check::RegionChanges 38 > { 39 mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap, 40 BT_NotCString, BT_AdditionOverflow; 41 42 mutable const char *CurrentFunctionDescription; 43 44 public: 45 /// The filter is used to filter out the diagnostics which are not enabled by 46 /// the user. 47 struct CStringChecksFilter { 48 DefaultBool CheckCStringNullArg; 49 DefaultBool CheckCStringOutOfBounds; 50 DefaultBool CheckCStringBufferOverlap; 51 DefaultBool CheckCStringNotNullTerm; 52 53 CheckerNameRef CheckNameCStringNullArg; 54 CheckerNameRef CheckNameCStringOutOfBounds; 55 CheckerNameRef CheckNameCStringBufferOverlap; 56 CheckerNameRef CheckNameCStringNotNullTerm; 57 }; 58 59 CStringChecksFilter Filter; 60 61 static void *getTag() { static int tag; return &tag; } 62 63 bool evalCall(const CallEvent &Call, CheckerContext &C) const; 64 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; 65 void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const; 66 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; 67 68 ProgramStateRef 69 checkRegionChanges(ProgramStateRef state, 70 const InvalidatedSymbols *, 71 ArrayRef<const MemRegion *> ExplicitRegions, 72 ArrayRef<const MemRegion *> Regions, 73 const LocationContext *LCtx, 74 const CallEvent *Call) const; 75 76 typedef void (CStringChecker::*FnCheck)(CheckerContext &, 77 const CallExpr *) const; 78 CallDescriptionMap<FnCheck> Callbacks = { 79 {{CDF_MaybeBuiltin, "memcpy", 3}, &CStringChecker::evalMemcpy}, 80 {{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy}, 81 {{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp}, 82 {{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove}, 83 {{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset}, 84 {{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset}, 85 {{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy}, 86 {{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy}, 87 {{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy}, 88 {{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy}, 89 {{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat}, 90 {{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat}, 91 {{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat}, 92 {{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength}, 93 {{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength}, 94 {{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp}, 95 {{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp}, 96 {{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp}, 97 {{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp}, 98 {{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep}, 99 {{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy}, 100 {{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp}, 101 {{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero}, 102 {{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero}, 103 }; 104 105 // These require a bit of special handling. 106 CallDescription StdCopy{{"std", "copy"}, 3}, 107 StdCopyBackward{{"std", "copy_backward"}, 3}; 108 109 FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const; 110 void evalMemcpy(CheckerContext &C, const CallExpr *CE) const; 111 void evalMempcpy(CheckerContext &C, const CallExpr *CE) const; 112 void evalMemmove(CheckerContext &C, const CallExpr *CE) const; 113 void evalBcopy(CheckerContext &C, const CallExpr *CE) const; 114 void evalCopyCommon(CheckerContext &C, const CallExpr *CE, 115 ProgramStateRef state, 116 const Expr *Size, 117 const Expr *Source, 118 const Expr *Dest, 119 bool Restricted = false, 120 bool IsMempcpy = false) const; 121 122 void evalMemcmp(CheckerContext &C, const CallExpr *CE) const; 123 124 void evalstrLength(CheckerContext &C, const CallExpr *CE) const; 125 void evalstrnLength(CheckerContext &C, const CallExpr *CE) const; 126 void evalstrLengthCommon(CheckerContext &C, 127 const CallExpr *CE, 128 bool IsStrnlen = false) const; 129 130 void evalStrcpy(CheckerContext &C, const CallExpr *CE) const; 131 void evalStrncpy(CheckerContext &C, const CallExpr *CE) const; 132 void evalStpcpy(CheckerContext &C, const CallExpr *CE) const; 133 void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const; 134 void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd, 135 bool IsBounded, ConcatFnKind appendK, 136 bool returnPtr = true) const; 137 138 void evalStrcat(CheckerContext &C, const CallExpr *CE) const; 139 void evalStrncat(CheckerContext &C, const CallExpr *CE) const; 140 void evalStrlcat(CheckerContext &C, const CallExpr *CE) const; 141 142 void evalStrcmp(CheckerContext &C, const CallExpr *CE) const; 143 void evalStrncmp(CheckerContext &C, const CallExpr *CE) const; 144 void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const; 145 void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const; 146 void evalStrcmpCommon(CheckerContext &C, 147 const CallExpr *CE, 148 bool IsBounded = false, 149 bool IgnoreCase = false) const; 150 151 void evalStrsep(CheckerContext &C, const CallExpr *CE) const; 152 153 void evalStdCopy(CheckerContext &C, const CallExpr *CE) const; 154 void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const; 155 void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const; 156 void evalMemset(CheckerContext &C, const CallExpr *CE) const; 157 void evalBzero(CheckerContext &C, const CallExpr *CE) const; 158 159 // Utility methods 160 std::pair<ProgramStateRef , ProgramStateRef > 161 static assumeZero(CheckerContext &C, 162 ProgramStateRef state, SVal V, QualType Ty); 163 164 static ProgramStateRef setCStringLength(ProgramStateRef state, 165 const MemRegion *MR, 166 SVal strLength); 167 static SVal getCStringLengthForRegion(CheckerContext &C, 168 ProgramStateRef &state, 169 const Expr *Ex, 170 const MemRegion *MR, 171 bool hypothetical); 172 SVal getCStringLength(CheckerContext &C, 173 ProgramStateRef &state, 174 const Expr *Ex, 175 SVal Buf, 176 bool hypothetical = false) const; 177 178 const StringLiteral *getCStringLiteral(CheckerContext &C, 179 ProgramStateRef &state, 180 const Expr *expr, 181 SVal val) const; 182 183 static ProgramStateRef InvalidateBuffer(CheckerContext &C, 184 ProgramStateRef state, 185 const Expr *Ex, SVal V, 186 bool IsSourceBuffer, 187 const Expr *Size); 188 189 static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx, 190 const MemRegion *MR); 191 192 static bool memsetAux(const Expr *DstBuffer, SVal CharE, 193 const Expr *Size, CheckerContext &C, 194 ProgramStateRef &State); 195 196 // Re-usable checks 197 ProgramStateRef checkNonNull(CheckerContext &C, 198 ProgramStateRef state, 199 const Expr *S, 200 SVal l, 201 unsigned IdxOfArg) const; 202 ProgramStateRef CheckLocation(CheckerContext &C, 203 ProgramStateRef state, 204 const Expr *S, 205 SVal l, 206 const char *message = nullptr) const; 207 ProgramStateRef CheckBufferAccess(CheckerContext &C, 208 ProgramStateRef state, 209 const Expr *Size, 210 const Expr *FirstBuf, 211 const Expr *SecondBuf, 212 const char *firstMessage = nullptr, 213 const char *secondMessage = nullptr, 214 bool WarnAboutSize = false) const; 215 216 ProgramStateRef CheckBufferAccess(CheckerContext &C, 217 ProgramStateRef state, 218 const Expr *Size, 219 const Expr *Buf, 220 const char *message = nullptr, 221 bool WarnAboutSize = false) const { 222 // This is a convenience overload. 223 return CheckBufferAccess(C, state, Size, Buf, nullptr, message, nullptr, 224 WarnAboutSize); 225 } 226 ProgramStateRef CheckOverlap(CheckerContext &C, 227 ProgramStateRef state, 228 const Expr *Size, 229 const Expr *First, 230 const Expr *Second) const; 231 void emitOverlapBug(CheckerContext &C, 232 ProgramStateRef state, 233 const Stmt *First, 234 const Stmt *Second) const; 235 236 void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S, 237 StringRef WarningMsg) const; 238 void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State, 239 const Stmt *S, StringRef WarningMsg) const; 240 void emitNotCStringBug(CheckerContext &C, ProgramStateRef State, 241 const Stmt *S, StringRef WarningMsg) const; 242 void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const; 243 244 ProgramStateRef checkAdditionOverflow(CheckerContext &C, 245 ProgramStateRef state, 246 NonLoc left, 247 NonLoc right) const; 248 249 // Return true if the destination buffer of the copy function may be in bound. 250 // Expects SVal of Size to be positive and unsigned. 251 // Expects SVal of FirstBuf to be a FieldRegion. 252 static bool IsFirstBufInBound(CheckerContext &C, 253 ProgramStateRef state, 254 const Expr *FirstBuf, 255 const Expr *Size); 256 }; 257 258 } //end anonymous namespace 259 260 REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal) 261 262 //===----------------------------------------------------------------------===// 263 // Individual checks and utility methods. 264 //===----------------------------------------------------------------------===// 265 266 std::pair<ProgramStateRef , ProgramStateRef > 267 CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V, 268 QualType Ty) { 269 Optional<DefinedSVal> val = V.getAs<DefinedSVal>(); 270 if (!val) 271 return std::pair<ProgramStateRef , ProgramStateRef >(state, state); 272 273 SValBuilder &svalBuilder = C.getSValBuilder(); 274 DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); 275 return state->assume(svalBuilder.evalEQ(state, *val, zero)); 276 } 277 278 ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C, 279 ProgramStateRef state, 280 const Expr *S, SVal l, 281 unsigned IdxOfArg) const { 282 // If a previous check has failed, propagate the failure. 283 if (!state) 284 return nullptr; 285 286 ProgramStateRef stateNull, stateNonNull; 287 std::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType()); 288 289 if (stateNull && !stateNonNull) { 290 if (Filter.CheckCStringNullArg) { 291 SmallString<80> buf; 292 llvm::raw_svector_ostream OS(buf); 293 assert(CurrentFunctionDescription); 294 OS << "Null pointer passed as " << IdxOfArg 295 << llvm::getOrdinalSuffix(IdxOfArg) << " argument to " 296 << CurrentFunctionDescription; 297 298 emitNullArgBug(C, stateNull, S, OS.str()); 299 } 300 return nullptr; 301 } 302 303 // From here on, assume that the value is non-null. 304 assert(stateNonNull); 305 return stateNonNull; 306 } 307 308 // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? 309 ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, 310 ProgramStateRef state, 311 const Expr *S, SVal l, 312 const char *warningMsg) const { 313 // If a previous check has failed, propagate the failure. 314 if (!state) 315 return nullptr; 316 317 // Check for out of bound array element access. 318 const MemRegion *R = l.getAsRegion(); 319 if (!R) 320 return state; 321 322 const ElementRegion *ER = dyn_cast<ElementRegion>(R); 323 if (!ER) 324 return state; 325 326 if (ER->getValueType() != C.getASTContext().CharTy) 327 return state; 328 329 // Get the size of the array. 330 const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); 331 DefinedOrUnknownSVal Size = 332 getDynamicSize(state, superReg, C.getSValBuilder()); 333 334 // Get the index of the accessed element. 335 DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); 336 337 ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true); 338 ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false); 339 if (StOutBound && !StInBound) { 340 // These checks are either enabled by the CString out-of-bounds checker 341 // explicitly or implicitly by the Malloc checker. 342 // In the latter case we only do modeling but do not emit warning. 343 if (!Filter.CheckCStringOutOfBounds) 344 return nullptr; 345 // Emit a bug report. 346 if (warningMsg) { 347 emitOutOfBoundsBug(C, StOutBound, S, warningMsg); 348 } else { 349 assert(CurrentFunctionDescription); 350 assert(CurrentFunctionDescription[0] != '\0'); 351 352 SmallString<80> buf; 353 llvm::raw_svector_ostream os(buf); 354 os << toUppercase(CurrentFunctionDescription[0]) 355 << &CurrentFunctionDescription[1] 356 << " accesses out-of-bound array element"; 357 emitOutOfBoundsBug(C, StOutBound, S, os.str()); 358 } 359 return nullptr; 360 } 361 362 // Array bound check succeeded. From this point forward the array bound 363 // should always succeed. 364 return StInBound; 365 } 366 367 ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C, 368 ProgramStateRef state, 369 const Expr *Size, 370 const Expr *FirstBuf, 371 const Expr *SecondBuf, 372 const char *firstMessage, 373 const char *secondMessage, 374 bool WarnAboutSize) const { 375 // If a previous check has failed, propagate the failure. 376 if (!state) 377 return nullptr; 378 379 SValBuilder &svalBuilder = C.getSValBuilder(); 380 ASTContext &Ctx = svalBuilder.getContext(); 381 const LocationContext *LCtx = C.getLocationContext(); 382 383 QualType sizeTy = Size->getType(); 384 QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); 385 386 // Check that the first buffer is non-null. 387 SVal BufVal = C.getSVal(FirstBuf); 388 state = checkNonNull(C, state, FirstBuf, BufVal, 1); 389 if (!state) 390 return nullptr; 391 392 // If out-of-bounds checking is turned off, skip the rest. 393 if (!Filter.CheckCStringOutOfBounds) 394 return state; 395 396 // Get the access length and make sure it is known. 397 // FIXME: This assumes the caller has already checked that the access length 398 // is positive. And that it's unsigned. 399 SVal LengthVal = C.getSVal(Size); 400 Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 401 if (!Length) 402 return state; 403 404 // Compute the offset of the last element to be accessed: size-1. 405 NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); 406 SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy); 407 if (Offset.isUnknown()) 408 return nullptr; 409 NonLoc LastOffset = Offset.castAs<NonLoc>(); 410 411 // Check that the first buffer is sufficiently long. 412 SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType()); 413 if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) { 414 const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf); 415 416 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, 417 LastOffset, PtrTy); 418 state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage); 419 420 // If the buffer isn't large enough, abort. 421 if (!state) 422 return nullptr; 423 } 424 425 // If there's a second buffer, check it as well. 426 if (SecondBuf) { 427 BufVal = state->getSVal(SecondBuf, LCtx); 428 state = checkNonNull(C, state, SecondBuf, BufVal, 2); 429 if (!state) 430 return nullptr; 431 432 BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType()); 433 if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) { 434 const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf); 435 436 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, 437 LastOffset, PtrTy); 438 state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage); 439 } 440 } 441 442 // Large enough or not, return this state! 443 return state; 444 } 445 446 ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C, 447 ProgramStateRef state, 448 const Expr *Size, 449 const Expr *First, 450 const Expr *Second) const { 451 if (!Filter.CheckCStringBufferOverlap) 452 return state; 453 454 // Do a simple check for overlap: if the two arguments are from the same 455 // buffer, see if the end of the first is greater than the start of the second 456 // or vice versa. 457 458 // If a previous check has failed, propagate the failure. 459 if (!state) 460 return nullptr; 461 462 ProgramStateRef stateTrue, stateFalse; 463 464 // Get the buffer values and make sure they're known locations. 465 const LocationContext *LCtx = C.getLocationContext(); 466 SVal firstVal = state->getSVal(First, LCtx); 467 SVal secondVal = state->getSVal(Second, LCtx); 468 469 Optional<Loc> firstLoc = firstVal.getAs<Loc>(); 470 if (!firstLoc) 471 return state; 472 473 Optional<Loc> secondLoc = secondVal.getAs<Loc>(); 474 if (!secondLoc) 475 return state; 476 477 // Are the two values the same? 478 SValBuilder &svalBuilder = C.getSValBuilder(); 479 std::tie(stateTrue, stateFalse) = 480 state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); 481 482 if (stateTrue && !stateFalse) { 483 // If the values are known to be equal, that's automatically an overlap. 484 emitOverlapBug(C, stateTrue, First, Second); 485 return nullptr; 486 } 487 488 // assume the two expressions are not equal. 489 assert(stateFalse); 490 state = stateFalse; 491 492 // Which value comes first? 493 QualType cmpTy = svalBuilder.getConditionType(); 494 SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT, 495 *firstLoc, *secondLoc, cmpTy); 496 Optional<DefinedOrUnknownSVal> reverseTest = 497 reverse.getAs<DefinedOrUnknownSVal>(); 498 if (!reverseTest) 499 return state; 500 501 std::tie(stateTrue, stateFalse) = state->assume(*reverseTest); 502 if (stateTrue) { 503 if (stateFalse) { 504 // If we don't know which one comes first, we can't perform this test. 505 return state; 506 } else { 507 // Switch the values so that firstVal is before secondVal. 508 std::swap(firstLoc, secondLoc); 509 510 // Switch the Exprs as well, so that they still correspond. 511 std::swap(First, Second); 512 } 513 } 514 515 // Get the length, and make sure it too is known. 516 SVal LengthVal = state->getSVal(Size, LCtx); 517 Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 518 if (!Length) 519 return state; 520 521 // Convert the first buffer's start address to char*. 522 // Bail out if the cast fails. 523 ASTContext &Ctx = svalBuilder.getContext(); 524 QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); 525 SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, 526 First->getType()); 527 Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>(); 528 if (!FirstStartLoc) 529 return state; 530 531 // Compute the end of the first buffer. Bail out if THAT fails. 532 SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, 533 *FirstStartLoc, *Length, CharPtrTy); 534 Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>(); 535 if (!FirstEndLoc) 536 return state; 537 538 // Is the end of the first buffer past the start of the second buffer? 539 SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT, 540 *FirstEndLoc, *secondLoc, cmpTy); 541 Optional<DefinedOrUnknownSVal> OverlapTest = 542 Overlap.getAs<DefinedOrUnknownSVal>(); 543 if (!OverlapTest) 544 return state; 545 546 std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); 547 548 if (stateTrue && !stateFalse) { 549 // Overlap! 550 emitOverlapBug(C, stateTrue, First, Second); 551 return nullptr; 552 } 553 554 // assume the two expressions don't overlap. 555 assert(stateFalse); 556 return stateFalse; 557 } 558 559 void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state, 560 const Stmt *First, const Stmt *Second) const { 561 ExplodedNode *N = C.generateErrorNode(state); 562 if (!N) 563 return; 564 565 if (!BT_Overlap) 566 BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap, 567 categories::UnixAPI, "Improper arguments")); 568 569 // Generate a report for this bug. 570 auto report = std::make_unique<PathSensitiveBugReport>( 571 *BT_Overlap, "Arguments must not be overlapping buffers", N); 572 report->addRange(First->getSourceRange()); 573 report->addRange(Second->getSourceRange()); 574 575 C.emitReport(std::move(report)); 576 } 577 578 void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State, 579 const Stmt *S, StringRef WarningMsg) const { 580 if (ExplodedNode *N = C.generateErrorNode(State)) { 581 if (!BT_Null) 582 BT_Null.reset(new BuiltinBug( 583 Filter.CheckNameCStringNullArg, categories::UnixAPI, 584 "Null pointer argument in call to byte string function")); 585 586 BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get()); 587 auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N); 588 Report->addRange(S->getSourceRange()); 589 if (const auto *Ex = dyn_cast<Expr>(S)) 590 bugreporter::trackExpressionValue(N, Ex, *Report); 591 C.emitReport(std::move(Report)); 592 } 593 } 594 595 void CStringChecker::emitOutOfBoundsBug(CheckerContext &C, 596 ProgramStateRef State, const Stmt *S, 597 StringRef WarningMsg) const { 598 if (ExplodedNode *N = C.generateErrorNode(State)) { 599 if (!BT_Bounds) 600 BT_Bounds.reset(new BuiltinBug( 601 Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds 602 : Filter.CheckNameCStringNullArg, 603 "Out-of-bound array access", 604 "Byte string function accesses out-of-bound array element")); 605 606 BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get()); 607 608 // FIXME: It would be nice to eventually make this diagnostic more clear, 609 // e.g., by referencing the original declaration or by saying *why* this 610 // reference is outside the range. 611 auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N); 612 Report->addRange(S->getSourceRange()); 613 C.emitReport(std::move(Report)); 614 } 615 } 616 617 void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State, 618 const Stmt *S, 619 StringRef WarningMsg) const { 620 if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) { 621 if (!BT_NotCString) 622 BT_NotCString.reset(new BuiltinBug( 623 Filter.CheckNameCStringNotNullTerm, categories::UnixAPI, 624 "Argument is not a null-terminated string.")); 625 626 auto Report = 627 std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N); 628 629 Report->addRange(S->getSourceRange()); 630 C.emitReport(std::move(Report)); 631 } 632 } 633 634 void CStringChecker::emitAdditionOverflowBug(CheckerContext &C, 635 ProgramStateRef State) const { 636 if (ExplodedNode *N = C.generateErrorNode(State)) { 637 if (!BT_NotCString) 638 BT_NotCString.reset( 639 new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API", 640 "Sum of expressions causes overflow.")); 641 642 // This isn't a great error message, but this should never occur in real 643 // code anyway -- you'd have to create a buffer longer than a size_t can 644 // represent, which is sort of a contradiction. 645 const char *WarningMsg = 646 "This expression will create a string whose length is too big to " 647 "be represented as a size_t"; 648 649 auto Report = 650 std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N); 651 C.emitReport(std::move(Report)); 652 } 653 } 654 655 ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C, 656 ProgramStateRef state, 657 NonLoc left, 658 NonLoc right) const { 659 // If out-of-bounds checking is turned off, skip the rest. 660 if (!Filter.CheckCStringOutOfBounds) 661 return state; 662 663 // If a previous check has failed, propagate the failure. 664 if (!state) 665 return nullptr; 666 667 SValBuilder &svalBuilder = C.getSValBuilder(); 668 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 669 670 QualType sizeTy = svalBuilder.getContext().getSizeType(); 671 const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); 672 NonLoc maxVal = svalBuilder.makeIntVal(maxValInt); 673 674 SVal maxMinusRight; 675 if (right.getAs<nonloc::ConcreteInt>()) { 676 maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right, 677 sizeTy); 678 } else { 679 // Try switching the operands. (The order of these two assignments is 680 // important!) 681 maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left, 682 sizeTy); 683 left = right; 684 } 685 686 if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) { 687 QualType cmpTy = svalBuilder.getConditionType(); 688 // If left > max - right, we have an overflow. 689 SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left, 690 *maxMinusRightNL, cmpTy); 691 692 ProgramStateRef stateOverflow, stateOkay; 693 std::tie(stateOverflow, stateOkay) = 694 state->assume(willOverflow.castAs<DefinedOrUnknownSVal>()); 695 696 if (stateOverflow && !stateOkay) { 697 // We have an overflow. Emit a bug report. 698 emitAdditionOverflowBug(C, stateOverflow); 699 return nullptr; 700 } 701 702 // From now on, assume an overflow didn't occur. 703 assert(stateOkay); 704 state = stateOkay; 705 } 706 707 return state; 708 } 709 710 ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state, 711 const MemRegion *MR, 712 SVal strLength) { 713 assert(!strLength.isUndef() && "Attempt to set an undefined string length"); 714 715 MR = MR->StripCasts(); 716 717 switch (MR->getKind()) { 718 case MemRegion::StringRegionKind: 719 // FIXME: This can happen if we strcpy() into a string region. This is 720 // undefined [C99 6.4.5p6], but we should still warn about it. 721 return state; 722 723 case MemRegion::SymbolicRegionKind: 724 case MemRegion::AllocaRegionKind: 725 case MemRegion::VarRegionKind: 726 case MemRegion::FieldRegionKind: 727 case MemRegion::ObjCIvarRegionKind: 728 // These are the types we can currently track string lengths for. 729 break; 730 731 case MemRegion::ElementRegionKind: 732 // FIXME: Handle element regions by upper-bounding the parent region's 733 // string length. 734 return state; 735 736 default: 737 // Other regions (mostly non-data) can't have a reliable C string length. 738 // For now, just ignore the change. 739 // FIXME: These are rare but not impossible. We should output some kind of 740 // warning for things like strcpy((char[]){'a', 0}, "b"); 741 return state; 742 } 743 744 if (strLength.isUnknown()) 745 return state->remove<CStringLength>(MR); 746 747 return state->set<CStringLength>(MR, strLength); 748 } 749 750 SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, 751 ProgramStateRef &state, 752 const Expr *Ex, 753 const MemRegion *MR, 754 bool hypothetical) { 755 if (!hypothetical) { 756 // If there's a recorded length, go ahead and return it. 757 const SVal *Recorded = state->get<CStringLength>(MR); 758 if (Recorded) 759 return *Recorded; 760 } 761 762 // Otherwise, get a new symbol and update the state. 763 SValBuilder &svalBuilder = C.getSValBuilder(); 764 QualType sizeTy = svalBuilder.getContext().getSizeType(); 765 SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), 766 MR, Ex, sizeTy, 767 C.getLocationContext(), 768 C.blockCount()); 769 770 if (!hypothetical) { 771 if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) { 772 // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4 773 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 774 const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); 775 llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4); 776 const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt, 777 fourInt); 778 NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt); 779 SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn, 780 maxLength, sizeTy); 781 state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true); 782 } 783 state = state->set<CStringLength>(MR, strLength); 784 } 785 786 return strLength; 787 } 788 789 SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state, 790 const Expr *Ex, SVal Buf, 791 bool hypothetical) const { 792 const MemRegion *MR = Buf.getAsRegion(); 793 if (!MR) { 794 // If we can't get a region, see if it's something we /know/ isn't a 795 // C string. In the context of locations, the only time we can issue such 796 // a warning is for labels. 797 if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) { 798 if (Filter.CheckCStringNotNullTerm) { 799 SmallString<120> buf; 800 llvm::raw_svector_ostream os(buf); 801 assert(CurrentFunctionDescription); 802 os << "Argument to " << CurrentFunctionDescription 803 << " is the address of the label '" << Label->getLabel()->getName() 804 << "', which is not a null-terminated string"; 805 806 emitNotCStringBug(C, state, Ex, os.str()); 807 } 808 return UndefinedVal(); 809 } 810 811 // If it's not a region and not a label, give up. 812 return UnknownVal(); 813 } 814 815 // If we have a region, strip casts from it and see if we can figure out 816 // its length. For anything we can't figure out, just return UnknownVal. 817 MR = MR->StripCasts(); 818 819 switch (MR->getKind()) { 820 case MemRegion::StringRegionKind: { 821 // Modifying the contents of string regions is undefined [C99 6.4.5p6], 822 // so we can assume that the byte length is the correct C string length. 823 SValBuilder &svalBuilder = C.getSValBuilder(); 824 QualType sizeTy = svalBuilder.getContext().getSizeType(); 825 const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); 826 return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy); 827 } 828 case MemRegion::SymbolicRegionKind: 829 case MemRegion::AllocaRegionKind: 830 case MemRegion::VarRegionKind: 831 case MemRegion::FieldRegionKind: 832 case MemRegion::ObjCIvarRegionKind: 833 return getCStringLengthForRegion(C, state, Ex, MR, hypothetical); 834 case MemRegion::CompoundLiteralRegionKind: 835 // FIXME: Can we track this? Is it necessary? 836 return UnknownVal(); 837 case MemRegion::ElementRegionKind: 838 // FIXME: How can we handle this? It's not good enough to subtract the 839 // offset from the base string length; consider "123\x00567" and &a[5]. 840 return UnknownVal(); 841 default: 842 // Other regions (mostly non-data) can't have a reliable C string length. 843 // In this case, an error is emitted and UndefinedVal is returned. 844 // The caller should always be prepared to handle this case. 845 if (Filter.CheckCStringNotNullTerm) { 846 SmallString<120> buf; 847 llvm::raw_svector_ostream os(buf); 848 849 assert(CurrentFunctionDescription); 850 os << "Argument to " << CurrentFunctionDescription << " is "; 851 852 if (SummarizeRegion(os, C.getASTContext(), MR)) 853 os << ", which is not a null-terminated string"; 854 else 855 os << "not a null-terminated string"; 856 857 emitNotCStringBug(C, state, Ex, os.str()); 858 } 859 return UndefinedVal(); 860 } 861 } 862 863 const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, 864 ProgramStateRef &state, const Expr *expr, SVal val) const { 865 866 // Get the memory region pointed to by the val. 867 const MemRegion *bufRegion = val.getAsRegion(); 868 if (!bufRegion) 869 return nullptr; 870 871 // Strip casts off the memory region. 872 bufRegion = bufRegion->StripCasts(); 873 874 // Cast the memory region to a string region. 875 const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); 876 if (!strRegion) 877 return nullptr; 878 879 // Return the actual string in the string region. 880 return strRegion->getStringLiteral(); 881 } 882 883 bool CStringChecker::IsFirstBufInBound(CheckerContext &C, 884 ProgramStateRef state, 885 const Expr *FirstBuf, 886 const Expr *Size) { 887 // If we do not know that the buffer is long enough we return 'true'. 888 // Otherwise the parent region of this field region would also get 889 // invalidated, which would lead to warnings based on an unknown state. 890 891 // Originally copied from CheckBufferAccess and CheckLocation. 892 SValBuilder &svalBuilder = C.getSValBuilder(); 893 ASTContext &Ctx = svalBuilder.getContext(); 894 const LocationContext *LCtx = C.getLocationContext(); 895 896 QualType sizeTy = Size->getType(); 897 QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); 898 SVal BufVal = state->getSVal(FirstBuf, LCtx); 899 900 SVal LengthVal = state->getSVal(Size, LCtx); 901 Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); 902 if (!Length) 903 return true; // cf top comment. 904 905 // Compute the offset of the last element to be accessed: size-1. 906 NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); 907 SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy); 908 if (Offset.isUnknown()) 909 return true; // cf top comment 910 NonLoc LastOffset = Offset.castAs<NonLoc>(); 911 912 // Check that the first buffer is sufficiently long. 913 SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType()); 914 Optional<Loc> BufLoc = BufStart.getAs<Loc>(); 915 if (!BufLoc) 916 return true; // cf top comment. 917 918 SVal BufEnd = 919 svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy); 920 921 // Check for out of bound array element access. 922 const MemRegion *R = BufEnd.getAsRegion(); 923 if (!R) 924 return true; // cf top comment. 925 926 const ElementRegion *ER = dyn_cast<ElementRegion>(R); 927 if (!ER) 928 return true; // cf top comment. 929 930 // FIXME: Does this crash when a non-standard definition 931 // of a library function is encountered? 932 assert(ER->getValueType() == C.getASTContext().CharTy && 933 "IsFirstBufInBound should only be called with char* ElementRegions"); 934 935 // Get the size of the array. 936 const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); 937 DefinedOrUnknownSVal SizeDV = getDynamicSize(state, superReg, svalBuilder); 938 939 // Get the index of the accessed element. 940 DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); 941 942 ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true); 943 944 return static_cast<bool>(StInBound); 945 } 946 947 ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C, 948 ProgramStateRef state, 949 const Expr *E, SVal V, 950 bool IsSourceBuffer, 951 const Expr *Size) { 952 Optional<Loc> L = V.getAs<Loc>(); 953 if (!L) 954 return state; 955 956 // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes 957 // some assumptions about the value that CFRefCount can't. Even so, it should 958 // probably be refactored. 959 if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) { 960 const MemRegion *R = MR->getRegion()->StripCasts(); 961 962 // Are we dealing with an ElementRegion? If so, we should be invalidating 963 // the super-region. 964 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 965 R = ER->getSuperRegion(); 966 // FIXME: What about layers of ElementRegions? 967 } 968 969 // Invalidate this region. 970 const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); 971 972 bool CausesPointerEscape = false; 973 RegionAndSymbolInvalidationTraits ITraits; 974 // Invalidate and escape only indirect regions accessible through the source 975 // buffer. 976 if (IsSourceBuffer) { 977 ITraits.setTrait(R->getBaseRegion(), 978 RegionAndSymbolInvalidationTraits::TK_PreserveContents); 979 ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape); 980 CausesPointerEscape = true; 981 } else { 982 const MemRegion::Kind& K = R->getKind(); 983 if (K == MemRegion::FieldRegionKind) 984 if (Size && IsFirstBufInBound(C, state, E, Size)) { 985 // If destination buffer is a field region and access is in bound, 986 // do not invalidate its super region. 987 ITraits.setTrait( 988 R, 989 RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 990 } 991 } 992 993 return state->invalidateRegions(R, E, C.blockCount(), LCtx, 994 CausesPointerEscape, nullptr, nullptr, 995 &ITraits); 996 } 997 998 // If we have a non-region value by chance, just remove the binding. 999 // FIXME: is this necessary or correct? This handles the non-Region 1000 // cases. Is it ever valid to store to these? 1001 return state->killBinding(*L); 1002 } 1003 1004 bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx, 1005 const MemRegion *MR) { 1006 switch (MR->getKind()) { 1007 case MemRegion::FunctionCodeRegionKind: { 1008 if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl()) 1009 os << "the address of the function '" << *FD << '\''; 1010 else 1011 os << "the address of a function"; 1012 return true; 1013 } 1014 case MemRegion::BlockCodeRegionKind: 1015 os << "block text"; 1016 return true; 1017 case MemRegion::BlockDataRegionKind: 1018 os << "a block"; 1019 return true; 1020 case MemRegion::CXXThisRegionKind: 1021 case MemRegion::CXXTempObjectRegionKind: 1022 os << "a C++ temp object of type " 1023 << cast<TypedValueRegion>(MR)->getValueType().getAsString(); 1024 return true; 1025 case MemRegion::VarRegionKind: 1026 os << "a variable of type" 1027 << cast<TypedValueRegion>(MR)->getValueType().getAsString(); 1028 return true; 1029 case MemRegion::FieldRegionKind: 1030 os << "a field of type " 1031 << cast<TypedValueRegion>(MR)->getValueType().getAsString(); 1032 return true; 1033 case MemRegion::ObjCIvarRegionKind: 1034 os << "an instance variable of type " 1035 << cast<TypedValueRegion>(MR)->getValueType().getAsString(); 1036 return true; 1037 default: 1038 return false; 1039 } 1040 } 1041 1042 bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal, 1043 const Expr *Size, CheckerContext &C, 1044 ProgramStateRef &State) { 1045 SVal MemVal = C.getSVal(DstBuffer); 1046 SVal SizeVal = C.getSVal(Size); 1047 const MemRegion *MR = MemVal.getAsRegion(); 1048 if (!MR) 1049 return false; 1050 1051 // We're about to model memset by producing a "default binding" in the Store. 1052 // Our current implementation - RegionStore - doesn't support default bindings 1053 // that don't cover the whole base region. So we should first get the offset 1054 // and the base region to figure out whether the offset of buffer is 0. 1055 RegionOffset Offset = MR->getAsOffset(); 1056 const MemRegion *BR = Offset.getRegion(); 1057 1058 Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>(); 1059 if (!SizeNL) 1060 return false; 1061 1062 SValBuilder &svalBuilder = C.getSValBuilder(); 1063 ASTContext &Ctx = C.getASTContext(); 1064 1065 // void *memset(void *dest, int ch, size_t count); 1066 // For now we can only handle the case of offset is 0 and concrete char value. 1067 if (Offset.isValid() && !Offset.hasSymbolicOffset() && 1068 Offset.getOffset() == 0) { 1069 // Get the base region's size. 1070 DefinedOrUnknownSVal SizeDV = getDynamicSize(State, BR, svalBuilder); 1071 1072 ProgramStateRef StateWholeReg, StateNotWholeReg; 1073 std::tie(StateWholeReg, StateNotWholeReg) = 1074 State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL)); 1075 1076 // With the semantic of 'memset()', we should convert the CharVal to 1077 // unsigned char. 1078 CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy); 1079 1080 ProgramStateRef StateNullChar, StateNonNullChar; 1081 std::tie(StateNullChar, StateNonNullChar) = 1082 assumeZero(C, State, CharVal, Ctx.UnsignedCharTy); 1083 1084 if (StateWholeReg && !StateNotWholeReg && StateNullChar && 1085 !StateNonNullChar) { 1086 // If the 'memset()' acts on the whole region of destination buffer and 1087 // the value of the second argument of 'memset()' is zero, bind the second 1088 // argument's value to the destination buffer with 'default binding'. 1089 // FIXME: Since there is no perfect way to bind the non-zero character, we 1090 // can only deal with zero value here. In the future, we need to deal with 1091 // the binding of non-zero value in the case of whole region. 1092 State = State->bindDefaultZero(svalBuilder.makeLoc(BR), 1093 C.getLocationContext()); 1094 } else { 1095 // If the destination buffer's extent is not equal to the value of 1096 // third argument, just invalidate buffer. 1097 State = InvalidateBuffer(C, State, DstBuffer, MemVal, 1098 /*IsSourceBuffer*/ false, Size); 1099 } 1100 1101 if (StateNullChar && !StateNonNullChar) { 1102 // If the value of the second argument of 'memset()' is zero, set the 1103 // string length of destination buffer to 0 directly. 1104 State = setCStringLength(State, MR, 1105 svalBuilder.makeZeroVal(Ctx.getSizeType())); 1106 } else if (!StateNullChar && StateNonNullChar) { 1107 SVal NewStrLen = svalBuilder.getMetadataSymbolVal( 1108 CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(), 1109 C.getLocationContext(), C.blockCount()); 1110 1111 // If the value of second argument is not zero, then the string length 1112 // is at least the size argument. 1113 SVal NewStrLenGESize = svalBuilder.evalBinOp( 1114 State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType()); 1115 1116 State = setCStringLength( 1117 State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true), 1118 MR, NewStrLen); 1119 } 1120 } else { 1121 // If the offset is not zero and char value is not concrete, we can do 1122 // nothing but invalidate the buffer. 1123 State = InvalidateBuffer(C, State, DstBuffer, MemVal, 1124 /*IsSourceBuffer*/ false, Size); 1125 } 1126 return true; 1127 } 1128 1129 //===----------------------------------------------------------------------===// 1130 // evaluation of individual function calls. 1131 //===----------------------------------------------------------------------===// 1132 1133 void CStringChecker::evalCopyCommon(CheckerContext &C, 1134 const CallExpr *CE, 1135 ProgramStateRef state, 1136 const Expr *Size, const Expr *Dest, 1137 const Expr *Source, bool Restricted, 1138 bool IsMempcpy) const { 1139 CurrentFunctionDescription = "memory copy function"; 1140 1141 // See if the size argument is zero. 1142 const LocationContext *LCtx = C.getLocationContext(); 1143 SVal sizeVal = state->getSVal(Size, LCtx); 1144 QualType sizeTy = Size->getType(); 1145 1146 ProgramStateRef stateZeroSize, stateNonZeroSize; 1147 std::tie(stateZeroSize, stateNonZeroSize) = 1148 assumeZero(C, state, sizeVal, sizeTy); 1149 1150 // Get the value of the Dest. 1151 SVal destVal = state->getSVal(Dest, LCtx); 1152 1153 // If the size is zero, there won't be any actual memory access, so 1154 // just bind the return value to the destination buffer and return. 1155 if (stateZeroSize && !stateNonZeroSize) { 1156 stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal); 1157 C.addTransition(stateZeroSize); 1158 return; 1159 } 1160 1161 // If the size can be nonzero, we have to check the other arguments. 1162 if (stateNonZeroSize) { 1163 state = stateNonZeroSize; 1164 1165 // Ensure the destination is not null. If it is NULL there will be a 1166 // NULL pointer dereference. 1167 state = checkNonNull(C, state, Dest, destVal, 1); 1168 if (!state) 1169 return; 1170 1171 // Get the value of the Src. 1172 SVal srcVal = state->getSVal(Source, LCtx); 1173 1174 // Ensure the source is not null. If it is NULL there will be a 1175 // NULL pointer dereference. 1176 state = checkNonNull(C, state, Source, srcVal, 2); 1177 if (!state) 1178 return; 1179 1180 // Ensure the accesses are valid and that the buffers do not overlap. 1181 const char * const writeWarning = 1182 "Memory copy function overflows destination buffer"; 1183 state = CheckBufferAccess(C, state, Size, Dest, Source, 1184 writeWarning, /* sourceWarning = */ nullptr); 1185 if (Restricted) 1186 state = CheckOverlap(C, state, Size, Dest, Source); 1187 1188 if (!state) 1189 return; 1190 1191 // If this is mempcpy, get the byte after the last byte copied and 1192 // bind the expr. 1193 if (IsMempcpy) { 1194 // Get the byte after the last byte copied. 1195 SValBuilder &SvalBuilder = C.getSValBuilder(); 1196 ASTContext &Ctx = SvalBuilder.getContext(); 1197 QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); 1198 SVal DestRegCharVal = 1199 SvalBuilder.evalCast(destVal, CharPtrTy, Dest->getType()); 1200 SVal lastElement = C.getSValBuilder().evalBinOp( 1201 state, BO_Add, DestRegCharVal, sizeVal, Dest->getType()); 1202 // If we don't know how much we copied, we can at least 1203 // conjure a return value for later. 1204 if (lastElement.isUnknown()) 1205 lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, 1206 C.blockCount()); 1207 1208 // The byte after the last byte copied is the return value. 1209 state = state->BindExpr(CE, LCtx, lastElement); 1210 } else { 1211 // All other copies return the destination buffer. 1212 // (Well, bcopy() has a void return type, but this won't hurt.) 1213 state = state->BindExpr(CE, LCtx, destVal); 1214 } 1215 1216 // Invalidate the destination (regular invalidation without pointer-escaping 1217 // the address of the top-level region). 1218 // FIXME: Even if we can't perfectly model the copy, we should see if we 1219 // can use LazyCompoundVals to copy the source values into the destination. 1220 // This would probably remove any existing bindings past the end of the 1221 // copied region, but that's still an improvement over blank invalidation. 1222 state = InvalidateBuffer(C, state, Dest, C.getSVal(Dest), 1223 /*IsSourceBuffer*/false, Size); 1224 1225 // Invalidate the source (const-invalidation without const-pointer-escaping 1226 // the address of the top-level region). 1227 state = InvalidateBuffer(C, state, Source, C.getSVal(Source), 1228 /*IsSourceBuffer*/true, nullptr); 1229 1230 C.addTransition(state); 1231 } 1232 } 1233 1234 1235 void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const { 1236 // void *memcpy(void *restrict dst, const void *restrict src, size_t n); 1237 // The return value is the address of the destination buffer. 1238 const Expr *Dest = CE->getArg(0); 1239 ProgramStateRef state = C.getState(); 1240 1241 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true); 1242 } 1243 1244 void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const { 1245 // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); 1246 // The return value is a pointer to the byte following the last written byte. 1247 const Expr *Dest = CE->getArg(0); 1248 ProgramStateRef state = C.getState(); 1249 1250 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true); 1251 } 1252 1253 void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const { 1254 // void *memmove(void *dst, const void *src, size_t n); 1255 // The return value is the address of the destination buffer. 1256 const Expr *Dest = CE->getArg(0); 1257 ProgramStateRef state = C.getState(); 1258 1259 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1)); 1260 } 1261 1262 void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const { 1263 // void bcopy(const void *src, void *dst, size_t n); 1264 evalCopyCommon(C, CE, C.getState(), 1265 CE->getArg(2), CE->getArg(1), CE->getArg(0)); 1266 } 1267 1268 void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const { 1269 // int memcmp(const void *s1, const void *s2, size_t n); 1270 CurrentFunctionDescription = "memory comparison function"; 1271 1272 const Expr *Left = CE->getArg(0); 1273 const Expr *Right = CE->getArg(1); 1274 const Expr *Size = CE->getArg(2); 1275 1276 ProgramStateRef state = C.getState(); 1277 SValBuilder &svalBuilder = C.getSValBuilder(); 1278 1279 // See if the size argument is zero. 1280 const LocationContext *LCtx = C.getLocationContext(); 1281 SVal sizeVal = state->getSVal(Size, LCtx); 1282 QualType sizeTy = Size->getType(); 1283 1284 ProgramStateRef stateZeroSize, stateNonZeroSize; 1285 std::tie(stateZeroSize, stateNonZeroSize) = 1286 assumeZero(C, state, sizeVal, sizeTy); 1287 1288 // If the size can be zero, the result will be 0 in that case, and we don't 1289 // have to check either of the buffers. 1290 if (stateZeroSize) { 1291 state = stateZeroSize; 1292 state = state->BindExpr(CE, LCtx, 1293 svalBuilder.makeZeroVal(CE->getType())); 1294 C.addTransition(state); 1295 } 1296 1297 // If the size can be nonzero, we have to check the other arguments. 1298 if (stateNonZeroSize) { 1299 state = stateNonZeroSize; 1300 // If we know the two buffers are the same, we know the result is 0. 1301 // First, get the two buffers' addresses. Another checker will have already 1302 // made sure they're not undefined. 1303 DefinedOrUnknownSVal LV = 1304 state->getSVal(Left, LCtx).castAs<DefinedOrUnknownSVal>(); 1305 DefinedOrUnknownSVal RV = 1306 state->getSVal(Right, LCtx).castAs<DefinedOrUnknownSVal>(); 1307 1308 // See if they are the same. 1309 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); 1310 ProgramStateRef StSameBuf, StNotSameBuf; 1311 std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); 1312 1313 // If the two arguments are the same buffer, we know the result is 0, 1314 // and we only need to check one size. 1315 if (StSameBuf && !StNotSameBuf) { 1316 state = StSameBuf; 1317 state = CheckBufferAccess(C, state, Size, Left); 1318 if (state) { 1319 state = StSameBuf->BindExpr(CE, LCtx, 1320 svalBuilder.makeZeroVal(CE->getType())); 1321 C.addTransition(state); 1322 } 1323 return; 1324 } 1325 1326 // If the two arguments might be different buffers, we have to check 1327 // the size of both of them. 1328 assert(StNotSameBuf); 1329 state = CheckBufferAccess(C, state, Size, Left, Right); 1330 if (state) { 1331 // The return value is the comparison result, which we don't know. 1332 SVal CmpV = 1333 svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 1334 state = state->BindExpr(CE, LCtx, CmpV); 1335 C.addTransition(state); 1336 } 1337 } 1338 } 1339 1340 void CStringChecker::evalstrLength(CheckerContext &C, 1341 const CallExpr *CE) const { 1342 // size_t strlen(const char *s); 1343 evalstrLengthCommon(C, CE, /* IsStrnlen = */ false); 1344 } 1345 1346 void CStringChecker::evalstrnLength(CheckerContext &C, 1347 const CallExpr *CE) const { 1348 // size_t strnlen(const char *s, size_t maxlen); 1349 evalstrLengthCommon(C, CE, /* IsStrnlen = */ true); 1350 } 1351 1352 void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, 1353 bool IsStrnlen) const { 1354 CurrentFunctionDescription = "string length function"; 1355 ProgramStateRef state = C.getState(); 1356 const LocationContext *LCtx = C.getLocationContext(); 1357 1358 if (IsStrnlen) { 1359 const Expr *maxlenExpr = CE->getArg(1); 1360 SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); 1361 1362 ProgramStateRef stateZeroSize, stateNonZeroSize; 1363 std::tie(stateZeroSize, stateNonZeroSize) = 1364 assumeZero(C, state, maxlenVal, maxlenExpr->getType()); 1365 1366 // If the size can be zero, the result will be 0 in that case, and we don't 1367 // have to check the string itself. 1368 if (stateZeroSize) { 1369 SVal zero = C.getSValBuilder().makeZeroVal(CE->getType()); 1370 stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero); 1371 C.addTransition(stateZeroSize); 1372 } 1373 1374 // If the size is GUARANTEED to be zero, we're done! 1375 if (!stateNonZeroSize) 1376 return; 1377 1378 // Otherwise, record the assumption that the size is nonzero. 1379 state = stateNonZeroSize; 1380 } 1381 1382 // Check that the string argument is non-null. 1383 const Expr *Arg = CE->getArg(0); 1384 SVal ArgVal = state->getSVal(Arg, LCtx); 1385 1386 state = checkNonNull(C, state, Arg, ArgVal, 1); 1387 1388 if (!state) 1389 return; 1390 1391 SVal strLength = getCStringLength(C, state, Arg, ArgVal); 1392 1393 // If the argument isn't a valid C string, there's no valid state to 1394 // transition to. 1395 if (strLength.isUndef()) 1396 return; 1397 1398 DefinedOrUnknownSVal result = UnknownVal(); 1399 1400 // If the check is for strnlen() then bind the return value to no more than 1401 // the maxlen value. 1402 if (IsStrnlen) { 1403 QualType cmpTy = C.getSValBuilder().getConditionType(); 1404 1405 // It's a little unfortunate to be getting this again, 1406 // but it's not that expensive... 1407 const Expr *maxlenExpr = CE->getArg(1); 1408 SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); 1409 1410 Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); 1411 Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>(); 1412 1413 if (strLengthNL && maxlenValNL) { 1414 ProgramStateRef stateStringTooLong, stateStringNotTooLong; 1415 1416 // Check if the strLength is greater than the maxlen. 1417 std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume( 1418 C.getSValBuilder() 1419 .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy) 1420 .castAs<DefinedOrUnknownSVal>()); 1421 1422 if (stateStringTooLong && !stateStringNotTooLong) { 1423 // If the string is longer than maxlen, return maxlen. 1424 result = *maxlenValNL; 1425 } else if (stateStringNotTooLong && !stateStringTooLong) { 1426 // If the string is shorter than maxlen, return its length. 1427 result = *strLengthNL; 1428 } 1429 } 1430 1431 if (result.isUnknown()) { 1432 // If we don't have enough information for a comparison, there's 1433 // no guarantee the full string length will actually be returned. 1434 // All we know is the return value is the min of the string length 1435 // and the limit. This is better than nothing. 1436 result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, 1437 C.blockCount()); 1438 NonLoc resultNL = result.castAs<NonLoc>(); 1439 1440 if (strLengthNL) { 1441 state = state->assume(C.getSValBuilder().evalBinOpNN( 1442 state, BO_LE, resultNL, *strLengthNL, cmpTy) 1443 .castAs<DefinedOrUnknownSVal>(), true); 1444 } 1445 1446 if (maxlenValNL) { 1447 state = state->assume(C.getSValBuilder().evalBinOpNN( 1448 state, BO_LE, resultNL, *maxlenValNL, cmpTy) 1449 .castAs<DefinedOrUnknownSVal>(), true); 1450 } 1451 } 1452 1453 } else { 1454 // This is a plain strlen(), not strnlen(). 1455 result = strLength.castAs<DefinedOrUnknownSVal>(); 1456 1457 // If we don't know the length of the string, conjure a return 1458 // value, so it can be used in constraints, at least. 1459 if (result.isUnknown()) { 1460 result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, 1461 C.blockCount()); 1462 } 1463 } 1464 1465 // Bind the return value. 1466 assert(!result.isUnknown() && "Should have conjured a value by now"); 1467 state = state->BindExpr(CE, LCtx, result); 1468 C.addTransition(state); 1469 } 1470 1471 void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const { 1472 // char *strcpy(char *restrict dst, const char *restrict src); 1473 evalStrcpyCommon(C, CE, 1474 /* ReturnEnd = */ false, 1475 /* IsBounded = */ false, 1476 /* appendK = */ ConcatFnKind::none); 1477 } 1478 1479 void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const { 1480 // char *strncpy(char *restrict dst, const char *restrict src, size_t n); 1481 evalStrcpyCommon(C, CE, 1482 /* ReturnEnd = */ false, 1483 /* IsBounded = */ true, 1484 /* appendK = */ ConcatFnKind::none); 1485 } 1486 1487 void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const { 1488 // char *stpcpy(char *restrict dst, const char *restrict src); 1489 evalStrcpyCommon(C, CE, 1490 /* ReturnEnd = */ true, 1491 /* IsBounded = */ false, 1492 /* appendK = */ ConcatFnKind::none); 1493 } 1494 1495 void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const { 1496 // size_t strlcpy(char *dest, const char *src, size_t size); 1497 evalStrcpyCommon(C, CE, 1498 /* ReturnEnd = */ true, 1499 /* IsBounded = */ true, 1500 /* appendK = */ ConcatFnKind::none, 1501 /* returnPtr = */ false); 1502 } 1503 1504 void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const { 1505 // char *strcat(char *restrict s1, const char *restrict s2); 1506 evalStrcpyCommon(C, CE, 1507 /* ReturnEnd = */ false, 1508 /* IsBounded = */ false, 1509 /* appendK = */ ConcatFnKind::strcat); 1510 } 1511 1512 void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const { 1513 //char *strncat(char *restrict s1, const char *restrict s2, size_t n); 1514 evalStrcpyCommon(C, CE, 1515 /* ReturnEnd = */ false, 1516 /* IsBounded = */ true, 1517 /* appendK = */ ConcatFnKind::strcat); 1518 } 1519 1520 void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const { 1521 // size_t strlcat(char *dst, const char *src, size_t size); 1522 // It will append at most size - strlen(dst) - 1 bytes, 1523 // NULL-terminating the result. 1524 evalStrcpyCommon(C, CE, 1525 /* ReturnEnd = */ false, 1526 /* IsBounded = */ true, 1527 /* appendK = */ ConcatFnKind::strlcat, 1528 /* returnPtr = */ false); 1529 } 1530 1531 void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, 1532 bool ReturnEnd, bool IsBounded, 1533 ConcatFnKind appendK, 1534 bool returnPtr) const { 1535 if (appendK == ConcatFnKind::none) 1536 CurrentFunctionDescription = "string copy function"; 1537 else 1538 CurrentFunctionDescription = "string concatenation function"; 1539 ProgramStateRef state = C.getState(); 1540 const LocationContext *LCtx = C.getLocationContext(); 1541 1542 // Check that the destination is non-null. 1543 const Expr *Dst = CE->getArg(0); 1544 SVal DstVal = state->getSVal(Dst, LCtx); 1545 1546 state = checkNonNull(C, state, Dst, DstVal, 1); 1547 if (!state) 1548 return; 1549 1550 // Check that the source is non-null. 1551 const Expr *srcExpr = CE->getArg(1); 1552 SVal srcVal = state->getSVal(srcExpr, LCtx); 1553 state = checkNonNull(C, state, srcExpr, srcVal, 2); 1554 if (!state) 1555 return; 1556 1557 // Get the string length of the source. 1558 SVal strLength = getCStringLength(C, state, srcExpr, srcVal); 1559 Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); 1560 1561 // Get the string length of the destination buffer. 1562 SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); 1563 Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>(); 1564 1565 // If the source isn't a valid C string, give up. 1566 if (strLength.isUndef()) 1567 return; 1568 1569 SValBuilder &svalBuilder = C.getSValBuilder(); 1570 QualType cmpTy = svalBuilder.getConditionType(); 1571 QualType sizeTy = svalBuilder.getContext().getSizeType(); 1572 1573 // These two values allow checking two kinds of errors: 1574 // - actual overflows caused by a source that doesn't fit in the destination 1575 // - potential overflows caused by a bound that could exceed the destination 1576 SVal amountCopied = UnknownVal(); 1577 SVal maxLastElementIndex = UnknownVal(); 1578 const char *boundWarning = nullptr; 1579 1580 state = CheckOverlap(C, state, IsBounded ? CE->getArg(2) : CE->getArg(1), Dst, 1581 srcExpr); 1582 1583 if (!state) 1584 return; 1585 1586 // If the function is strncpy, strncat, etc... it is bounded. 1587 if (IsBounded) { 1588 // Get the max number of characters to copy. 1589 const Expr *lenExpr = CE->getArg(2); 1590 SVal lenVal = state->getSVal(lenExpr, LCtx); 1591 1592 // Protect against misdeclared strncpy(). 1593 lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType()); 1594 1595 Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>(); 1596 1597 // If we know both values, we might be able to figure out how much 1598 // we're copying. 1599 if (strLengthNL && lenValNL) { 1600 switch (appendK) { 1601 case ConcatFnKind::none: 1602 case ConcatFnKind::strcat: { 1603 ProgramStateRef stateSourceTooLong, stateSourceNotTooLong; 1604 // Check if the max number to copy is less than the length of the src. 1605 // If the bound is equal to the source length, strncpy won't null- 1606 // terminate the result! 1607 std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume( 1608 svalBuilder 1609 .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy) 1610 .castAs<DefinedOrUnknownSVal>()); 1611 1612 if (stateSourceTooLong && !stateSourceNotTooLong) { 1613 // Max number to copy is less than the length of the src, so the 1614 // actual strLength copied is the max number arg. 1615 state = stateSourceTooLong; 1616 amountCopied = lenVal; 1617 1618 } else if (!stateSourceTooLong && stateSourceNotTooLong) { 1619 // The source buffer entirely fits in the bound. 1620 state = stateSourceNotTooLong; 1621 amountCopied = strLength; 1622 } 1623 break; 1624 } 1625 case ConcatFnKind::strlcat: 1626 if (!dstStrLengthNL) 1627 return; 1628 1629 // amountCopied = min (size - dstLen - 1 , srcLen) 1630 SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, 1631 *dstStrLengthNL, sizeTy); 1632 if (!freeSpace.getAs<NonLoc>()) 1633 return; 1634 freeSpace = 1635 svalBuilder.evalBinOp(state, BO_Sub, freeSpace, 1636 svalBuilder.makeIntVal(1, sizeTy), sizeTy); 1637 Optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>(); 1638 1639 // While unlikely, it is possible that the subtraction is 1640 // too complex to compute, let's check whether it succeeded. 1641 if (!freeSpaceNL) 1642 return; 1643 SVal hasEnoughSpace = svalBuilder.evalBinOpNN( 1644 state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy); 1645 1646 ProgramStateRef TrueState, FalseState; 1647 std::tie(TrueState, FalseState) = 1648 state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>()); 1649 1650 // srcStrLength <= size - dstStrLength -1 1651 if (TrueState && !FalseState) { 1652 amountCopied = strLength; 1653 } 1654 1655 // srcStrLength > size - dstStrLength -1 1656 if (!TrueState && FalseState) { 1657 amountCopied = freeSpace; 1658 } 1659 1660 if (TrueState && FalseState) 1661 amountCopied = UnknownVal(); 1662 break; 1663 } 1664 } 1665 // We still want to know if the bound is known to be too large. 1666 if (lenValNL) { 1667 switch (appendK) { 1668 case ConcatFnKind::strcat: 1669 // For strncat, the check is strlen(dst) + lenVal < sizeof(dst) 1670 1671 // Get the string length of the destination. If the destination is 1672 // memory that can't have a string length, we shouldn't be copying 1673 // into it anyway. 1674 if (dstStrLength.isUndef()) 1675 return; 1676 1677 if (dstStrLengthNL) { 1678 maxLastElementIndex = svalBuilder.evalBinOpNN( 1679 state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy); 1680 1681 boundWarning = "Size argument is greater than the free space in the " 1682 "destination buffer"; 1683 } 1684 break; 1685 case ConcatFnKind::none: 1686 case ConcatFnKind::strlcat: 1687 // For strncpy and strlcat, this is just checking 1688 // that lenVal <= sizeof(dst). 1689 // (Yes, strncpy and strncat differ in how they treat termination. 1690 // strncat ALWAYS terminates, but strncpy doesn't.) 1691 1692 // We need a special case for when the copy size is zero, in which 1693 // case strncpy will do no work at all. Our bounds check uses n-1 1694 // as the last element accessed, so n == 0 is problematic. 1695 ProgramStateRef StateZeroSize, StateNonZeroSize; 1696 std::tie(StateZeroSize, StateNonZeroSize) = 1697 assumeZero(C, state, *lenValNL, sizeTy); 1698 1699 // If the size is known to be zero, we're done. 1700 if (StateZeroSize && !StateNonZeroSize) { 1701 if (returnPtr) { 1702 StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal); 1703 } else { 1704 if (appendK == ConcatFnKind::none) { 1705 // strlcpy returns strlen(src) 1706 StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength); 1707 } else { 1708 // strlcat returns strlen(src) + strlen(dst) 1709 SVal retSize = svalBuilder.evalBinOp( 1710 state, BO_Add, strLength, dstStrLength, sizeTy); 1711 StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize); 1712 } 1713 } 1714 C.addTransition(StateZeroSize); 1715 return; 1716 } 1717 1718 // Otherwise, go ahead and figure out the last element we'll touch. 1719 // We don't record the non-zero assumption here because we can't 1720 // be sure. We won't warn on a possible zero. 1721 NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); 1722 maxLastElementIndex = 1723 svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy); 1724 boundWarning = "Size argument is greater than the length of the " 1725 "destination buffer"; 1726 break; 1727 } 1728 } 1729 } else { 1730 // The function isn't bounded. The amount copied should match the length 1731 // of the source buffer. 1732 amountCopied = strLength; 1733 } 1734 1735 assert(state); 1736 1737 // This represents the number of characters copied into the destination 1738 // buffer. (It may not actually be the strlen if the destination buffer 1739 // is not terminated.) 1740 SVal finalStrLength = UnknownVal(); 1741 SVal strlRetVal = UnknownVal(); 1742 1743 if (appendK == ConcatFnKind::none && !returnPtr) { 1744 // strlcpy returns the sizeof(src) 1745 strlRetVal = strLength; 1746 } 1747 1748 // If this is an appending function (strcat, strncat...) then set the 1749 // string length to strlen(src) + strlen(dst) since the buffer will 1750 // ultimately contain both. 1751 if (appendK != ConcatFnKind::none) { 1752 // Get the string length of the destination. If the destination is memory 1753 // that can't have a string length, we shouldn't be copying into it anyway. 1754 if (dstStrLength.isUndef()) 1755 return; 1756 1757 if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) { 1758 strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL, 1759 *dstStrLengthNL, sizeTy); 1760 } 1761 1762 Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>(); 1763 1764 // If we know both string lengths, we might know the final string length. 1765 if (amountCopiedNL && dstStrLengthNL) { 1766 // Make sure the two lengths together don't overflow a size_t. 1767 state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL); 1768 if (!state) 1769 return; 1770 1771 finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL, 1772 *dstStrLengthNL, sizeTy); 1773 } 1774 1775 // If we couldn't get a single value for the final string length, 1776 // we can at least bound it by the individual lengths. 1777 if (finalStrLength.isUnknown()) { 1778 // Try to get a "hypothetical" string length symbol, which we can later 1779 // set as a real value if that turns out to be the case. 1780 finalStrLength = getCStringLength(C, state, CE, DstVal, true); 1781 assert(!finalStrLength.isUndef()); 1782 1783 if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) { 1784 if (amountCopiedNL && appendK == ConcatFnKind::none) { 1785 // we overwrite dst string with the src 1786 // finalStrLength >= srcStrLength 1787 SVal sourceInResult = svalBuilder.evalBinOpNN( 1788 state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy); 1789 state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(), 1790 true); 1791 if (!state) 1792 return; 1793 } 1794 1795 if (dstStrLengthNL && appendK != ConcatFnKind::none) { 1796 // we extend the dst string with the src 1797 // finalStrLength >= dstStrLength 1798 SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE, 1799 *finalStrLengthNL, 1800 *dstStrLengthNL, 1801 cmpTy); 1802 state = 1803 state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true); 1804 if (!state) 1805 return; 1806 } 1807 } 1808 } 1809 1810 } else { 1811 // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and 1812 // the final string length will match the input string length. 1813 finalStrLength = amountCopied; 1814 } 1815 1816 SVal Result; 1817 1818 if (returnPtr) { 1819 // The final result of the function will either be a pointer past the last 1820 // copied element, or a pointer to the start of the destination buffer. 1821 Result = (ReturnEnd ? UnknownVal() : DstVal); 1822 } else { 1823 if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none) 1824 //strlcpy, strlcat 1825 Result = strlRetVal; 1826 else 1827 Result = finalStrLength; 1828 } 1829 1830 assert(state); 1831 1832 // If the destination is a MemRegion, try to check for a buffer overflow and 1833 // record the new string length. 1834 if (Optional<loc::MemRegionVal> dstRegVal = 1835 DstVal.getAs<loc::MemRegionVal>()) { 1836 QualType ptrTy = Dst->getType(); 1837 1838 // If we have an exact value on a bounded copy, use that to check for 1839 // overflows, rather than our estimate about how much is actually copied. 1840 if (boundWarning) { 1841 if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) { 1842 SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, 1843 *maxLastNL, ptrTy); 1844 state = CheckLocation(C, state, CE->getArg(2), maxLastElement, 1845 boundWarning); 1846 if (!state) 1847 return; 1848 } 1849 } 1850 1851 // Then, if the final length is known... 1852 if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) { 1853 SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, 1854 *knownStrLength, ptrTy); 1855 1856 // ...and we haven't checked the bound, we'll check the actual copy. 1857 if (!boundWarning) { 1858 const char * const warningMsg = 1859 "String copy function overflows destination buffer"; 1860 state = CheckLocation(C, state, Dst, lastElement, warningMsg); 1861 if (!state) 1862 return; 1863 } 1864 1865 // If this is a stpcpy-style copy, the last element is the return value. 1866 if (returnPtr && ReturnEnd) 1867 Result = lastElement; 1868 } 1869 1870 // Invalidate the destination (regular invalidation without pointer-escaping 1871 // the address of the top-level region). This must happen before we set the 1872 // C string length because invalidation will clear the length. 1873 // FIXME: Even if we can't perfectly model the copy, we should see if we 1874 // can use LazyCompoundVals to copy the source values into the destination. 1875 // This would probably remove any existing bindings past the end of the 1876 // string, but that's still an improvement over blank invalidation. 1877 state = InvalidateBuffer(C, state, Dst, *dstRegVal, 1878 /*IsSourceBuffer*/false, nullptr); 1879 1880 // Invalidate the source (const-invalidation without const-pointer-escaping 1881 // the address of the top-level region). 1882 state = InvalidateBuffer(C, state, srcExpr, srcVal, /*IsSourceBuffer*/true, 1883 nullptr); 1884 1885 // Set the C string length of the destination, if we know it. 1886 if (IsBounded && (appendK == ConcatFnKind::none)) { 1887 // strncpy is annoying in that it doesn't guarantee to null-terminate 1888 // the result string. If the original string didn't fit entirely inside 1889 // the bound (including the null-terminator), we don't know how long the 1890 // result is. 1891 if (amountCopied != strLength) 1892 finalStrLength = UnknownVal(); 1893 } 1894 state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength); 1895 } 1896 1897 assert(state); 1898 1899 if (returnPtr) { 1900 // If this is a stpcpy-style copy, but we were unable to check for a buffer 1901 // overflow, we still need a result. Conjure a return value. 1902 if (ReturnEnd && Result.isUnknown()) { 1903 Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 1904 } 1905 } 1906 // Set the return value. 1907 state = state->BindExpr(CE, LCtx, Result); 1908 C.addTransition(state); 1909 } 1910 1911 void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const { 1912 //int strcmp(const char *s1, const char *s2); 1913 evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false); 1914 } 1915 1916 void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const { 1917 //int strncmp(const char *s1, const char *s2, size_t n); 1918 evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false); 1919 } 1920 1921 void CStringChecker::evalStrcasecmp(CheckerContext &C, 1922 const CallExpr *CE) const { 1923 //int strcasecmp(const char *s1, const char *s2); 1924 evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true); 1925 } 1926 1927 void CStringChecker::evalStrncasecmp(CheckerContext &C, 1928 const CallExpr *CE) const { 1929 //int strncasecmp(const char *s1, const char *s2, size_t n); 1930 evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true); 1931 } 1932 1933 void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, 1934 bool IsBounded, bool IgnoreCase) const { 1935 CurrentFunctionDescription = "string comparison function"; 1936 ProgramStateRef state = C.getState(); 1937 const LocationContext *LCtx = C.getLocationContext(); 1938 1939 // Check that the first string is non-null 1940 const Expr *s1 = CE->getArg(0); 1941 SVal s1Val = state->getSVal(s1, LCtx); 1942 state = checkNonNull(C, state, s1, s1Val, 1); 1943 if (!state) 1944 return; 1945 1946 // Check that the second string is non-null. 1947 const Expr *s2 = CE->getArg(1); 1948 SVal s2Val = state->getSVal(s2, LCtx); 1949 state = checkNonNull(C, state, s2, s2Val, 2); 1950 if (!state) 1951 return; 1952 1953 // Get the string length of the first string or give up. 1954 SVal s1Length = getCStringLength(C, state, s1, s1Val); 1955 if (s1Length.isUndef()) 1956 return; 1957 1958 // Get the string length of the second string or give up. 1959 SVal s2Length = getCStringLength(C, state, s2, s2Val); 1960 if (s2Length.isUndef()) 1961 return; 1962 1963 // If we know the two buffers are the same, we know the result is 0. 1964 // First, get the two buffers' addresses. Another checker will have already 1965 // made sure they're not undefined. 1966 DefinedOrUnknownSVal LV = s1Val.castAs<DefinedOrUnknownSVal>(); 1967 DefinedOrUnknownSVal RV = s2Val.castAs<DefinedOrUnknownSVal>(); 1968 1969 // See if they are the same. 1970 SValBuilder &svalBuilder = C.getSValBuilder(); 1971 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); 1972 ProgramStateRef StSameBuf, StNotSameBuf; 1973 std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); 1974 1975 // If the two arguments might be the same buffer, we know the result is 0, 1976 // and we only need to check one size. 1977 if (StSameBuf) { 1978 StSameBuf = StSameBuf->BindExpr(CE, LCtx, 1979 svalBuilder.makeZeroVal(CE->getType())); 1980 C.addTransition(StSameBuf); 1981 1982 // If the two arguments are GUARANTEED to be the same, we're done! 1983 if (!StNotSameBuf) 1984 return; 1985 } 1986 1987 assert(StNotSameBuf); 1988 state = StNotSameBuf; 1989 1990 // At this point we can go about comparing the two buffers. 1991 // For now, we only do this if they're both known string literals. 1992 1993 // Attempt to extract string literals from both expressions. 1994 const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val); 1995 const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val); 1996 bool canComputeResult = false; 1997 SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, 1998 C.blockCount()); 1999 2000 if (s1StrLiteral && s2StrLiteral) { 2001 StringRef s1StrRef = s1StrLiteral->getString(); 2002 StringRef s2StrRef = s2StrLiteral->getString(); 2003 2004 if (IsBounded) { 2005 // Get the max number of characters to compare. 2006 const Expr *lenExpr = CE->getArg(2); 2007 SVal lenVal = state->getSVal(lenExpr, LCtx); 2008 2009 // If the length is known, we can get the right substrings. 2010 if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) { 2011 // Create substrings of each to compare the prefix. 2012 s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue()); 2013 s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue()); 2014 canComputeResult = true; 2015 } 2016 } else { 2017 // This is a normal, unbounded strcmp. 2018 canComputeResult = true; 2019 } 2020 2021 if (canComputeResult) { 2022 // Real strcmp stops at null characters. 2023 size_t s1Term = s1StrRef.find('\0'); 2024 if (s1Term != StringRef::npos) 2025 s1StrRef = s1StrRef.substr(0, s1Term); 2026 2027 size_t s2Term = s2StrRef.find('\0'); 2028 if (s2Term != StringRef::npos) 2029 s2StrRef = s2StrRef.substr(0, s2Term); 2030 2031 // Use StringRef's comparison methods to compute the actual result. 2032 int compareRes = IgnoreCase ? s1StrRef.compare_lower(s2StrRef) 2033 : s1StrRef.compare(s2StrRef); 2034 2035 // The strcmp function returns an integer greater than, equal to, or less 2036 // than zero, [c11, p7.24.4.2]. 2037 if (compareRes == 0) { 2038 resultVal = svalBuilder.makeIntVal(compareRes, CE->getType()); 2039 } 2040 else { 2041 DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType()); 2042 // Constrain strcmp's result range based on the result of StringRef's 2043 // comparison methods. 2044 BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT; 2045 SVal compareWithZero = 2046 svalBuilder.evalBinOp(state, op, resultVal, zeroVal, 2047 svalBuilder.getConditionType()); 2048 DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>(); 2049 state = state->assume(compareWithZeroVal, true); 2050 } 2051 } 2052 } 2053 2054 state = state->BindExpr(CE, LCtx, resultVal); 2055 2056 // Record this as a possible path. 2057 C.addTransition(state); 2058 } 2059 2060 void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const { 2061 //char *strsep(char **stringp, const char *delim); 2062 // Sanity: does the search string parameter match the return type? 2063 const Expr *SearchStrPtr = CE->getArg(0); 2064 QualType CharPtrTy = SearchStrPtr->getType()->getPointeeType(); 2065 if (CharPtrTy.isNull() || 2066 CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType()) 2067 return; 2068 2069 CurrentFunctionDescription = "strsep()"; 2070 ProgramStateRef State = C.getState(); 2071 const LocationContext *LCtx = C.getLocationContext(); 2072 2073 // Check that the search string pointer is non-null (though it may point to 2074 // a null string). 2075 SVal SearchStrVal = State->getSVal(SearchStrPtr, LCtx); 2076 State = checkNonNull(C, State, SearchStrPtr, SearchStrVal, 1); 2077 if (!State) 2078 return; 2079 2080 // Check that the delimiter string is non-null. 2081 const Expr *DelimStr = CE->getArg(1); 2082 SVal DelimStrVal = State->getSVal(DelimStr, LCtx); 2083 State = checkNonNull(C, State, DelimStr, DelimStrVal, 2); 2084 if (!State) 2085 return; 2086 2087 SValBuilder &SVB = C.getSValBuilder(); 2088 SVal Result; 2089 if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) { 2090 // Get the current value of the search string pointer, as a char*. 2091 Result = State->getSVal(*SearchStrLoc, CharPtrTy); 2092 2093 // Invalidate the search string, representing the change of one delimiter 2094 // character to NUL. 2095 State = InvalidateBuffer(C, State, SearchStrPtr, Result, 2096 /*IsSourceBuffer*/false, nullptr); 2097 2098 // Overwrite the search string pointer. The new value is either an address 2099 // further along in the same string, or NULL if there are no more tokens. 2100 State = State->bindLoc(*SearchStrLoc, 2101 SVB.conjureSymbolVal(getTag(), 2102 CE, 2103 LCtx, 2104 CharPtrTy, 2105 C.blockCount()), 2106 LCtx); 2107 } else { 2108 assert(SearchStrVal.isUnknown()); 2109 // Conjure a symbolic value. It's the best we can do. 2110 Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 2111 } 2112 2113 // Set the return value, and finish. 2114 State = State->BindExpr(CE, LCtx, Result); 2115 C.addTransition(State); 2116 } 2117 2118 // These should probably be moved into a C++ standard library checker. 2119 void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const { 2120 evalStdCopyCommon(C, CE); 2121 } 2122 2123 void CStringChecker::evalStdCopyBackward(CheckerContext &C, 2124 const CallExpr *CE) const { 2125 evalStdCopyCommon(C, CE); 2126 } 2127 2128 void CStringChecker::evalStdCopyCommon(CheckerContext &C, 2129 const CallExpr *CE) const { 2130 if (!CE->getArg(2)->getType()->isPointerType()) 2131 return; 2132 2133 ProgramStateRef State = C.getState(); 2134 2135 const LocationContext *LCtx = C.getLocationContext(); 2136 2137 // template <class _InputIterator, class _OutputIterator> 2138 // _OutputIterator 2139 // copy(_InputIterator __first, _InputIterator __last, 2140 // _OutputIterator __result) 2141 2142 // Invalidate the destination buffer 2143 const Expr *Dst = CE->getArg(2); 2144 SVal DstVal = State->getSVal(Dst, LCtx); 2145 State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false, 2146 /*Size=*/nullptr); 2147 2148 SValBuilder &SVB = C.getSValBuilder(); 2149 2150 SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); 2151 State = State->BindExpr(CE, LCtx, ResultVal); 2152 2153 C.addTransition(State); 2154 } 2155 2156 void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const { 2157 CurrentFunctionDescription = "memory set function"; 2158 2159 const Expr *Mem = CE->getArg(0); 2160 const Expr *CharE = CE->getArg(1); 2161 const Expr *Size = CE->getArg(2); 2162 ProgramStateRef State = C.getState(); 2163 2164 // See if the size argument is zero. 2165 const LocationContext *LCtx = C.getLocationContext(); 2166 SVal SizeVal = State->getSVal(Size, LCtx); 2167 QualType SizeTy = Size->getType(); 2168 2169 ProgramStateRef StateZeroSize, StateNonZeroSize; 2170 std::tie(StateZeroSize, StateNonZeroSize) = 2171 assumeZero(C, State, SizeVal, SizeTy); 2172 2173 // Get the value of the memory area. 2174 SVal MemVal = State->getSVal(Mem, LCtx); 2175 2176 // If the size is zero, there won't be any actual memory access, so 2177 // just bind the return value to the Mem buffer and return. 2178 if (StateZeroSize && !StateNonZeroSize) { 2179 StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, MemVal); 2180 C.addTransition(StateZeroSize); 2181 return; 2182 } 2183 2184 // Ensure the memory area is not null. 2185 // If it is NULL there will be a NULL pointer dereference. 2186 State = checkNonNull(C, StateNonZeroSize, Mem, MemVal, 1); 2187 if (!State) 2188 return; 2189 2190 State = CheckBufferAccess(C, State, Size, Mem); 2191 if (!State) 2192 return; 2193 2194 // According to the values of the arguments, bind the value of the second 2195 // argument to the destination buffer and set string length, or just 2196 // invalidate the destination buffer. 2197 if (!memsetAux(Mem, C.getSVal(CharE), Size, C, State)) 2198 return; 2199 2200 State = State->BindExpr(CE, LCtx, MemVal); 2201 C.addTransition(State); 2202 } 2203 2204 void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const { 2205 CurrentFunctionDescription = "memory clearance function"; 2206 2207 const Expr *Mem = CE->getArg(0); 2208 const Expr *Size = CE->getArg(1); 2209 SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy); 2210 2211 ProgramStateRef State = C.getState(); 2212 2213 // See if the size argument is zero. 2214 SVal SizeVal = C.getSVal(Size); 2215 QualType SizeTy = Size->getType(); 2216 2217 ProgramStateRef StateZeroSize, StateNonZeroSize; 2218 std::tie(StateZeroSize, StateNonZeroSize) = 2219 assumeZero(C, State, SizeVal, SizeTy); 2220 2221 // If the size is zero, there won't be any actual memory access, 2222 // In this case we just return. 2223 if (StateZeroSize && !StateNonZeroSize) { 2224 C.addTransition(StateZeroSize); 2225 return; 2226 } 2227 2228 // Get the value of the memory area. 2229 SVal MemVal = C.getSVal(Mem); 2230 2231 // Ensure the memory area is not null. 2232 // If it is NULL there will be a NULL pointer dereference. 2233 State = checkNonNull(C, StateNonZeroSize, Mem, MemVal, 1); 2234 if (!State) 2235 return; 2236 2237 State = CheckBufferAccess(C, State, Size, Mem); 2238 if (!State) 2239 return; 2240 2241 if (!memsetAux(Mem, Zero, Size, C, State)) 2242 return; 2243 2244 C.addTransition(State); 2245 } 2246 2247 //===----------------------------------------------------------------------===// 2248 // The driver method, and other Checker callbacks. 2249 //===----------------------------------------------------------------------===// 2250 2251 CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call, 2252 CheckerContext &C) const { 2253 const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr()); 2254 if (!CE) 2255 return nullptr; 2256 2257 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl()); 2258 if (!FD) 2259 return nullptr; 2260 2261 if (Call.isCalled(StdCopy)) { 2262 return &CStringChecker::evalStdCopy; 2263 } else if (Call.isCalled(StdCopyBackward)) { 2264 return &CStringChecker::evalStdCopyBackward; 2265 } 2266 2267 // Pro-actively check that argument types are safe to do arithmetic upon. 2268 // We do not want to crash if someone accidentally passes a structure 2269 // into, say, a C++ overload of any of these functions. We could not check 2270 // that for std::copy because they may have arguments of other types. 2271 for (auto I : CE->arguments()) { 2272 QualType T = I->getType(); 2273 if (!T->isIntegralOrEnumerationType() && !T->isPointerType()) 2274 return nullptr; 2275 } 2276 2277 const FnCheck *Callback = Callbacks.lookup(Call); 2278 if (Callback) 2279 return *Callback; 2280 2281 return nullptr; 2282 } 2283 2284 bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const { 2285 FnCheck Callback = identifyCall(Call, C); 2286 2287 // If the callee isn't a string function, let another checker handle it. 2288 if (!Callback) 2289 return false; 2290 2291 // Check and evaluate the call. 2292 const auto *CE = cast<CallExpr>(Call.getOriginExpr()); 2293 (this->*Callback)(C, CE); 2294 2295 // If the evaluate call resulted in no change, chain to the next eval call 2296 // handler. 2297 // Note, the custom CString evaluation calls assume that basic safety 2298 // properties are held. However, if the user chooses to turn off some of these 2299 // checks, we ignore the issues and leave the call evaluation to a generic 2300 // handler. 2301 return C.isDifferent(); 2302 } 2303 2304 void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { 2305 // Record string length for char a[] = "abc"; 2306 ProgramStateRef state = C.getState(); 2307 2308 for (const auto *I : DS->decls()) { 2309 const VarDecl *D = dyn_cast<VarDecl>(I); 2310 if (!D) 2311 continue; 2312 2313 // FIXME: Handle array fields of structs. 2314 if (!D->getType()->isArrayType()) 2315 continue; 2316 2317 const Expr *Init = D->getInit(); 2318 if (!Init) 2319 continue; 2320 if (!isa<StringLiteral>(Init)) 2321 continue; 2322 2323 Loc VarLoc = state->getLValue(D, C.getLocationContext()); 2324 const MemRegion *MR = VarLoc.getAsRegion(); 2325 if (!MR) 2326 continue; 2327 2328 SVal StrVal = C.getSVal(Init); 2329 assert(StrVal.isValid() && "Initializer string is unknown or undefined"); 2330 DefinedOrUnknownSVal strLength = 2331 getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>(); 2332 2333 state = state->set<CStringLength>(MR, strLength); 2334 } 2335 2336 C.addTransition(state); 2337 } 2338 2339 ProgramStateRef 2340 CStringChecker::checkRegionChanges(ProgramStateRef state, 2341 const InvalidatedSymbols *, 2342 ArrayRef<const MemRegion *> ExplicitRegions, 2343 ArrayRef<const MemRegion *> Regions, 2344 const LocationContext *LCtx, 2345 const CallEvent *Call) const { 2346 CStringLengthTy Entries = state->get<CStringLength>(); 2347 if (Entries.isEmpty()) 2348 return state; 2349 2350 llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; 2351 llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; 2352 2353 // First build sets for the changed regions and their super-regions. 2354 for (ArrayRef<const MemRegion *>::iterator 2355 I = Regions.begin(), E = Regions.end(); I != E; ++I) { 2356 const MemRegion *MR = *I; 2357 Invalidated.insert(MR); 2358 2359 SuperRegions.insert(MR); 2360 while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { 2361 MR = SR->getSuperRegion(); 2362 SuperRegions.insert(MR); 2363 } 2364 } 2365 2366 CStringLengthTy::Factory &F = state->get_context<CStringLength>(); 2367 2368 // Then loop over the entries in the current state. 2369 for (CStringLengthTy::iterator I = Entries.begin(), 2370 E = Entries.end(); I != E; ++I) { 2371 const MemRegion *MR = I.getKey(); 2372 2373 // Is this entry for a super-region of a changed region? 2374 if (SuperRegions.count(MR)) { 2375 Entries = F.remove(Entries, MR); 2376 continue; 2377 } 2378 2379 // Is this entry for a sub-region of a changed region? 2380 const MemRegion *Super = MR; 2381 while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { 2382 Super = SR->getSuperRegion(); 2383 if (Invalidated.count(Super)) { 2384 Entries = F.remove(Entries, MR); 2385 break; 2386 } 2387 } 2388 } 2389 2390 return state->set<CStringLength>(Entries); 2391 } 2392 2393 void CStringChecker::checkLiveSymbols(ProgramStateRef state, 2394 SymbolReaper &SR) const { 2395 // Mark all symbols in our string length map as valid. 2396 CStringLengthTy Entries = state->get<CStringLength>(); 2397 2398 for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end(); 2399 I != E; ++I) { 2400 SVal Len = I.getData(); 2401 2402 for (SymExpr::symbol_iterator si = Len.symbol_begin(), 2403 se = Len.symbol_end(); si != se; ++si) 2404 SR.markInUse(*si); 2405 } 2406 } 2407 2408 void CStringChecker::checkDeadSymbols(SymbolReaper &SR, 2409 CheckerContext &C) const { 2410 ProgramStateRef state = C.getState(); 2411 CStringLengthTy Entries = state->get<CStringLength>(); 2412 if (Entries.isEmpty()) 2413 return; 2414 2415 CStringLengthTy::Factory &F = state->get_context<CStringLength>(); 2416 for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end(); 2417 I != E; ++I) { 2418 SVal Len = I.getData(); 2419 if (SymbolRef Sym = Len.getAsSymbol()) { 2420 if (SR.isDead(Sym)) 2421 Entries = F.remove(Entries, I.getKey()); 2422 } 2423 } 2424 2425 state = state->set<CStringLength>(Entries); 2426 C.addTransition(state); 2427 } 2428 2429 void ento::registerCStringModeling(CheckerManager &Mgr) { 2430 Mgr.registerChecker<CStringChecker>(); 2431 } 2432 2433 bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) { 2434 return true; 2435 } 2436 2437 #define REGISTER_CHECKER(name) \ 2438 void ento::register##name(CheckerManager &mgr) { \ 2439 CStringChecker *checker = mgr.getChecker<CStringChecker>(); \ 2440 checker->Filter.Check##name = true; \ 2441 checker->Filter.CheckName##name = mgr.getCurrentCheckerName(); \ 2442 } \ 2443 \ 2444 bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; } 2445 2446 REGISTER_CHECKER(CStringNullArg) 2447 REGISTER_CHECKER(CStringOutOfBounds) 2448 REGISTER_CHECKER(CStringBufferOverlap) 2449 REGISTER_CHECKER(CStringNotNullTerm) 2450