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