| 1 | //===- CheckerManager.cpp - Static Analyzer Checker Manager ---------------===// |
| 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 | // Defines the Static Analyzer Checker Manager. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 14 | #include "clang/AST/DeclBase.h" |
| 15 | #include "clang/AST/Stmt.h" |
| 16 | #include "clang/Analysis/ProgramPoint.h" |
| 17 | #include "clang/Basic/JsonSupport.h" |
| 18 | #include "clang/Basic/LLVM.h" |
| 19 | #include "clang/Driver/DriverDiagnostic.h" |
| 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h" |
| 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
| 25 | #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/FormatVariadic.h" |
| 30 | #include "llvm/Support/TimeProfiler.h" |
| 31 | #include <cassert> |
| 32 | #include <optional> |
| 33 | #include <vector> |
| 34 | |
| 35 | using namespace clang; |
| 36 | using namespace ento; |
| 37 | |
| 38 | bool CheckerManager::hasPathSensitiveCheckers() const { |
| 39 | const auto IfAnyAreNonEmpty = [](const auto &...Callbacks) -> bool { |
| 40 | return (!Callbacks.empty() || ...); |
| 41 | }; |
| 42 | return IfAnyAreNonEmpty( |
| 43 | StmtCheckers, PreObjCMessageCheckers, ObjCMessageNilCheckers, |
| 44 | PostObjCMessageCheckers, PreCallCheckers, PostCallCheckers, |
| 45 | LifetimeEndCheckers, LocationCheckers, BindCheckers, |
| 46 | BlockEntranceCheckers, EndAnalysisCheckers, BeginFunctionCheckers, |
| 47 | EndFunctionCheckers, BranchConditionCheckers, NewAllocatorCheckers, |
| 48 | LiveSymbolsCheckers, DeadSymbolsCheckers, RegionChangesCheckers, |
| 49 | PointerEscapeCheckers, EvalAssumeCheckers, EvalCallCheckers, |
| 50 | EndOfTranslationUnitCheckers); |
| 51 | } |
| 52 | |
| 53 | void CheckerManager::reportInvalidCheckerOptionValue( |
| 54 | const CheckerFrontend *Checker, StringRef OptionName, |
| 55 | StringRef ExpectedValueDesc) const { |
| 56 | |
| 57 | getDiagnostics().Report(DiagID: diag::err_analyzer_checker_option_invalid_input) |
| 58 | << (llvm::Twine(Checker->getName()) + ":" + OptionName).str() |
| 59 | << ExpectedValueDesc; |
| 60 | } |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // Functions for running checkers for AST traversing.. |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
| 66 | void CheckerManager::runCheckersOnASTDecl(const Decl *D, AnalysisManager& mgr, |
| 67 | BugReporter &BR) { |
| 68 | assert(D); |
| 69 | |
| 70 | unsigned DeclKind = D->getKind(); |
| 71 | auto [CCI, Inserted] = CachedDeclCheckersMap.try_emplace(Key: DeclKind); |
| 72 | CachedDeclCheckers *checkers = &(CCI->second); |
| 73 | if (Inserted) { |
| 74 | // Find the checkers that should run for this Decl and cache them. |
| 75 | for (const auto &info : DeclCheckers) |
| 76 | if (info.IsForDeclFn(D)) |
| 77 | checkers->push_back(Elt: info.CheckFn); |
| 78 | } |
| 79 | |
| 80 | assert(checkers); |
| 81 | for (const auto &checker : *checkers) |
| 82 | checker(D, mgr, BR); |
| 83 | } |
| 84 | |
| 85 | void CheckerManager::runCheckersOnASTBody(const Decl *D, AnalysisManager& mgr, |
| 86 | BugReporter &BR) { |
| 87 | assert(D && D->hasBody()); |
| 88 | |
| 89 | for (const auto &BodyChecker : BodyCheckers) |
| 90 | BodyChecker(D, mgr, BR); |
| 91 | } |
| 92 | |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | // Functions for running checkers for path-sensitive checking. |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | |
| 97 | template <typename CHECK_CTX> |
| 98 | static void expandGraphWithCheckers(CHECK_CTX checkCtx, ExplodedNodeSet &Dst, |
| 99 | const ExplodedNodeSet &Src) { |
| 100 | if (Src.empty()) |
| 101 | return; |
| 102 | |
| 103 | typename CHECK_CTX::CheckersTy::const_iterator |
| 104 | I = checkCtx.checkers_begin(), E = checkCtx.checkers_end(); |
| 105 | if (I == E) { |
| 106 | Dst.insert(S: Src); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | ExplodedNodeSet Tmp1, Tmp2; |
| 111 | const ExplodedNodeSet *PrevSet = &Src; |
| 112 | |
| 113 | for (; I != E; ++I) { |
| 114 | ExplodedNodeSet *CurrSet = nullptr; |
| 115 | if (I+1 == E) |
| 116 | CurrSet = &Dst; |
| 117 | else { |
| 118 | CurrSet = (PrevSet == &Tmp1) ? &Tmp2 : &Tmp1; |
| 119 | CurrSet->clear(); |
| 120 | } |
| 121 | |
| 122 | CurrSet->insert(S: *PrevSet); |
| 123 | for (const auto &NI : *PrevSet) |
| 124 | checkCtx.runChecker(*I, NI, *CurrSet); |
| 125 | |
| 126 | // If all the produced transitions are sinks, stop. |
| 127 | if (CurrSet->empty()) |
| 128 | return; |
| 129 | |
| 130 | // Update which NodeSet is the current one. |
| 131 | PrevSet = CurrSet; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | namespace { |
| 136 | |
| 137 | std::string checkerScopeName(StringRef Name, const CheckerBackend *Checker) { |
| 138 | if (!llvm::timeTraceProfilerEnabled()) |
| 139 | return "" ; |
| 140 | StringRef CheckerTag = Checker ? Checker->getDebugTag() : "<unknown>" ; |
| 141 | return (Name + ":" + CheckerTag).str(); |
| 142 | } |
| 143 | |
| 144 | struct CheckStmtContext { |
| 145 | using CheckersTy = SmallVectorImpl<CheckerManager::CheckStmtFunc>; |
| 146 | |
| 147 | bool IsPreVisit; |
| 148 | const CheckersTy &Checkers; |
| 149 | const Stmt *S; |
| 150 | ExprEngine &Eng; |
| 151 | bool WasInlined; |
| 152 | |
| 153 | CheckStmtContext(bool isPreVisit, const CheckersTy &checkers, |
| 154 | const Stmt *s, ExprEngine &eng, bool wasInlined = false) |
| 155 | : IsPreVisit(isPreVisit), Checkers(checkers), S(s), Eng(eng), |
| 156 | WasInlined(wasInlined) {} |
| 157 | |
| 158 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 159 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 160 | |
| 161 | void runChecker(CheckerManager::CheckStmtFunc checkFn, ExplodedNode *Pred, |
| 162 | ExplodedNodeSet &Dst) { |
| 163 | llvm::TimeTraceScope TimeScope(checkerScopeName(Name: "Stmt" , Checker: checkFn.Checker)); |
| 164 | // FIXME: Remove respondsToCallback from CheckerContext; |
| 165 | ProgramPoint::Kind K = IsPreVisit ? ProgramPoint::PreStmtKind : |
| 166 | ProgramPoint::PostStmtKind; |
| 167 | const ProgramPoint &L = ProgramPoint::getProgramPoint( |
| 168 | S, K, SF: Pred->getStackFrame(), tag: checkFn.Checker); |
| 169 | CheckerContext C(Eng, Pred, Dst, L, WasInlined); |
| 170 | checkFn(S, C); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | } // namespace |
| 175 | |
| 176 | /// Run checkers for visiting Stmts. |
| 177 | void CheckerManager::runCheckersForStmt(bool isPreVisit, |
| 178 | ExplodedNodeSet &Dst, |
| 179 | const ExplodedNodeSet &Src, |
| 180 | const Stmt *S, |
| 181 | ExprEngine &Eng, |
| 182 | bool WasInlined) { |
| 183 | CheckStmtContext C(isPreVisit, getCachedStmtCheckersFor(S, isPreVisit), |
| 184 | S, Eng, WasInlined); |
| 185 | llvm::TimeTraceScope TimeScope( |
| 186 | isPreVisit ? "CheckerManager::runCheckersForStmt (Pre)" |
| 187 | : "CheckerManager::runCheckersForStmt (Post)" ); |
| 188 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 189 | } |
| 190 | |
| 191 | namespace { |
| 192 | |
| 193 | struct CheckObjCMessageContext { |
| 194 | using CheckersTy = std::vector<CheckerManager::CheckObjCMessageFunc>; |
| 195 | |
| 196 | ObjCMessageVisitKind Kind; |
| 197 | bool WasInlined; |
| 198 | const CheckersTy &Checkers; |
| 199 | const ObjCMethodCall &Msg; |
| 200 | ExprEngine &Eng; |
| 201 | |
| 202 | CheckObjCMessageContext(ObjCMessageVisitKind visitKind, |
| 203 | const CheckersTy &checkers, |
| 204 | const ObjCMethodCall &msg, ExprEngine &eng, |
| 205 | bool wasInlined) |
| 206 | : Kind(visitKind), WasInlined(wasInlined), Checkers(checkers), Msg(msg), |
| 207 | Eng(eng) {} |
| 208 | |
| 209 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 210 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 211 | |
| 212 | void runChecker(CheckerManager::CheckObjCMessageFunc checkFn, |
| 213 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 214 | llvm::TimeTraceScope TimeScope( |
| 215 | checkerScopeName(Name: "ObjCMsg" , Checker: checkFn.Checker)); |
| 216 | bool IsPreVisit; |
| 217 | |
| 218 | switch (Kind) { |
| 219 | case ObjCMessageVisitKind::Pre: |
| 220 | IsPreVisit = true; |
| 221 | break; |
| 222 | case ObjCMessageVisitKind::MessageNil: |
| 223 | case ObjCMessageVisitKind::Post: |
| 224 | IsPreVisit = false; |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | const ProgramPoint &L = Msg.getProgramPoint(IsPreVisit,Tag: checkFn.Checker); |
| 229 | CheckerContext C(Eng, Pred, Dst, L, WasInlined); |
| 230 | |
| 231 | checkFn(*Msg.cloneWithState<ObjCMethodCall>(NewState: Pred->getState()), C); |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | } // namespace |
| 236 | |
| 237 | /// Run checkers for visiting obj-c messages. |
| 238 | void CheckerManager::runCheckersForObjCMessage(ObjCMessageVisitKind visitKind, |
| 239 | ExplodedNodeSet &Dst, |
| 240 | const ExplodedNodeSet &Src, |
| 241 | const ObjCMethodCall &msg, |
| 242 | ExprEngine &Eng, |
| 243 | bool WasInlined) { |
| 244 | const auto &checkers = getObjCMessageCheckers(Kind: visitKind); |
| 245 | CheckObjCMessageContext C(visitKind, checkers, msg, Eng, WasInlined); |
| 246 | llvm::TimeTraceScope TimeScope("CheckerManager::runCheckersForObjCMessage" ); |
| 247 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 248 | } |
| 249 | |
| 250 | const std::vector<CheckerManager::CheckObjCMessageFunc> & |
| 251 | CheckerManager::getObjCMessageCheckers(ObjCMessageVisitKind Kind) const { |
| 252 | switch (Kind) { |
| 253 | case ObjCMessageVisitKind::Pre: |
| 254 | return PreObjCMessageCheckers; |
| 255 | break; |
| 256 | case ObjCMessageVisitKind::Post: |
| 257 | return PostObjCMessageCheckers; |
| 258 | case ObjCMessageVisitKind::MessageNil: |
| 259 | return ObjCMessageNilCheckers; |
| 260 | } |
| 261 | llvm_unreachable("Unknown Kind" ); |
| 262 | } |
| 263 | |
| 264 | namespace { |
| 265 | |
| 266 | // FIXME: This has all the same signatures as CheckObjCMessageContext. |
| 267 | // Is there a way we can merge the two? |
| 268 | struct CheckCallContext { |
| 269 | using CheckersTy = std::vector<CheckerManager::CheckCallFunc>; |
| 270 | |
| 271 | bool IsPreVisit, WasInlined; |
| 272 | const CheckersTy &Checkers; |
| 273 | const CallEvent &Call; |
| 274 | ExprEngine &Eng; |
| 275 | |
| 276 | CheckCallContext(bool isPreVisit, const CheckersTy &checkers, |
| 277 | const CallEvent &call, ExprEngine &eng, |
| 278 | bool wasInlined) |
| 279 | : IsPreVisit(isPreVisit), WasInlined(wasInlined), Checkers(checkers), |
| 280 | Call(call), Eng(eng) {} |
| 281 | |
| 282 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 283 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 284 | |
| 285 | void runChecker(CheckerManager::CheckCallFunc checkFn, ExplodedNode *Pred, |
| 286 | ExplodedNodeSet &Dst) { |
| 287 | llvm::TimeTraceScope TimeScope(checkerScopeName(Name: "Call" , Checker: checkFn.Checker)); |
| 288 | const ProgramPoint &L = Call.getProgramPoint(IsPreVisit,Tag: checkFn.Checker); |
| 289 | CheckerContext C(Eng, Pred, Dst, L, WasInlined); |
| 290 | |
| 291 | checkFn(*Call.cloneWithState(NewState: Pred->getState()), C); |
| 292 | } |
| 293 | }; |
| 294 | |
| 295 | } // namespace |
| 296 | |
| 297 | /// Run checkers for visiting an abstract call event. |
| 298 | void CheckerManager::runCheckersForCallEvent(bool isPreVisit, |
| 299 | ExplodedNodeSet &Dst, |
| 300 | const ExplodedNodeSet &Src, |
| 301 | const CallEvent &Call, |
| 302 | ExprEngine &Eng, |
| 303 | bool WasInlined) { |
| 304 | CheckCallContext C(isPreVisit, |
| 305 | isPreVisit ? PreCallCheckers |
| 306 | : PostCallCheckers, |
| 307 | Call, Eng, WasInlined); |
| 308 | llvm::TimeTraceScope TimeScope( |
| 309 | isPreVisit ? "CheckerManager::runCheckersForCallEvent (Pre)" |
| 310 | : "CheckerManager::runCheckersForCallEvent (Post)" ); |
| 311 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 312 | } |
| 313 | |
| 314 | namespace { |
| 315 | |
| 316 | struct CheckLifetimeEndContext { |
| 317 | using CheckersTy = std::vector<CheckerManager::CheckLifetimeEndFunc>; |
| 318 | |
| 319 | const CheckersTy &Checkers; |
| 320 | const VarDecl *Decl; |
| 321 | ExprEngine &Eng; |
| 322 | |
| 323 | CheckLifetimeEndContext(const CheckersTy &checkers, const VarDecl *decl, |
| 324 | ExprEngine &eng) |
| 325 | : Checkers(checkers), Decl(decl), Eng(eng) {} |
| 326 | |
| 327 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 328 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 329 | |
| 330 | void runChecker(CheckerManager::CheckLifetimeEndFunc checkFn, |
| 331 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 332 | assert(Pred->getLocation().getAs<LifetimeEnd>().has_value()); |
| 333 | const ProgramPoint L = Pred->getLocation().withTag(NewTag: checkFn.Checker); |
| 334 | CheckerContext C(Eng, Pred, Dst, L); |
| 335 | checkFn(Decl, C); |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | } // namespace |
| 340 | |
| 341 | /// Run checkers for end of variable lifetime |
| 342 | void CheckerManager::runCheckersForLifetimeEnd(ExplodedNodeSet &Dst, |
| 343 | const ExplodedNodeSet &Src, |
| 344 | const VarDecl *Decl, |
| 345 | ExprEngine &Eng) { |
| 346 | llvm::TimeTraceScope TimeScope("CheckerManager::runCheckersForLifetimeEnd" ); |
| 347 | CheckLifetimeEndContext C(LifetimeEndCheckers, Decl, Eng); |
| 348 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 349 | } |
| 350 | |
| 351 | namespace { |
| 352 | |
| 353 | struct CheckLocationContext { |
| 354 | using CheckersTy = std::vector<CheckerManager::CheckLocationFunc>; |
| 355 | |
| 356 | const CheckersTy &Checkers; |
| 357 | SVal Loc; |
| 358 | bool IsLoad; |
| 359 | const Stmt *NodeEx; /* Will become a CFGStmt */ |
| 360 | const Stmt *BoundEx; |
| 361 | ExprEngine &Eng; |
| 362 | |
| 363 | CheckLocationContext(const CheckersTy &checkers, |
| 364 | SVal loc, bool isLoad, const Stmt *NodeEx, |
| 365 | const Stmt *BoundEx, |
| 366 | ExprEngine &eng) |
| 367 | : Checkers(checkers), Loc(loc), IsLoad(isLoad), NodeEx(NodeEx), |
| 368 | BoundEx(BoundEx), Eng(eng) {} |
| 369 | |
| 370 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 371 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 372 | |
| 373 | void runChecker(CheckerManager::CheckLocationFunc checkFn, |
| 374 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 375 | llvm::TimeTraceScope TimeScope(checkerScopeName(Name: "Loc" , Checker: checkFn.Checker)); |
| 376 | ProgramPoint::Kind K = IsLoad ? ProgramPoint::PreLoadKind : |
| 377 | ProgramPoint::PreStoreKind; |
| 378 | const ProgramPoint &L = ProgramPoint::getProgramPoint( |
| 379 | S: NodeEx, K, SF: Pred->getStackFrame(), tag: checkFn.Checker); |
| 380 | CheckerContext C(Eng, Pred, Dst, L); |
| 381 | checkFn(Loc, IsLoad, BoundEx, C); |
| 382 | } |
| 383 | }; |
| 384 | |
| 385 | } // namespace |
| 386 | |
| 387 | /// Run checkers for load/store of a location. |
| 388 | |
| 389 | void CheckerManager::runCheckersForLocation(ExplodedNodeSet &Dst, |
| 390 | const ExplodedNodeSet &Src, |
| 391 | SVal location, bool isLoad, |
| 392 | const Stmt *NodeEx, |
| 393 | const Stmt *BoundEx, |
| 394 | ExprEngine &Eng) { |
| 395 | CheckLocationContext C(LocationCheckers, location, isLoad, NodeEx, |
| 396 | BoundEx, Eng); |
| 397 | llvm::TimeTraceScope TimeScope( |
| 398 | isLoad ? "CheckerManager::runCheckersForLocation (Load)" |
| 399 | : "CheckerManager::runCheckersForLocation (Store)" ); |
| 400 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 401 | } |
| 402 | |
| 403 | namespace { |
| 404 | |
| 405 | struct CheckBindContext { |
| 406 | using CheckersTy = std::vector<CheckerManager::CheckBindFunc>; |
| 407 | |
| 408 | const CheckersTy &Checkers; |
| 409 | SVal Loc; |
| 410 | SVal Val; |
| 411 | const Stmt *S; |
| 412 | ExprEngine &Eng; |
| 413 | const ProgramPoint &PP; |
| 414 | bool AtDeclInit; |
| 415 | |
| 416 | CheckBindContext(const CheckersTy &checkers, SVal loc, SVal val, |
| 417 | const Stmt *s, bool AtDeclInit, ExprEngine &eng, |
| 418 | const ProgramPoint &pp) |
| 419 | : Checkers(checkers), Loc(loc), Val(val), S(s), Eng(eng), PP(pp), |
| 420 | AtDeclInit(AtDeclInit) {} |
| 421 | |
| 422 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 423 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 424 | |
| 425 | void runChecker(CheckerManager::CheckBindFunc checkFn, ExplodedNode *Pred, |
| 426 | ExplodedNodeSet &Dst) { |
| 427 | llvm::TimeTraceScope TimeScope(checkerScopeName(Name: "Bind" , Checker: checkFn.Checker)); |
| 428 | const ProgramPoint &L = PP.withTag(NewTag: checkFn.Checker); |
| 429 | CheckerContext C(Eng, Pred, Dst, L); |
| 430 | |
| 431 | checkFn(Loc, Val, S, AtDeclInit, C); |
| 432 | } |
| 433 | }; |
| 434 | |
| 435 | llvm::TimeTraceMetadata getTimeTraceBindMetadata(SVal Val) { |
| 436 | assert(llvm::timeTraceProfilerEnabled()); |
| 437 | std::string Name; |
| 438 | llvm::raw_string_ostream OS(Name); |
| 439 | Val.dumpToStream(OS); |
| 440 | return llvm::TimeTraceMetadata{.Detail: OS.str(), .File: "" }; |
| 441 | } |
| 442 | |
| 443 | } // namespace |
| 444 | |
| 445 | /// Run checkers for binding of a value to a location. |
| 446 | void CheckerManager::runCheckersForBind(ExplodedNodeSet &Dst, |
| 447 | const ExplodedNodeSet &Src, |
| 448 | SVal location, SVal val, const Stmt *S, |
| 449 | bool AtDeclInit, ExprEngine &Eng, |
| 450 | const ProgramPoint &PP) { |
| 451 | CheckBindContext C(BindCheckers, location, val, S, AtDeclInit, Eng, PP); |
| 452 | llvm::TimeTraceScope TimeScope{ |
| 453 | "CheckerManager::runCheckersForBind" , |
| 454 | [&val]() { return getTimeTraceBindMetadata(Val: val); }}; |
| 455 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 456 | } |
| 457 | |
| 458 | namespace { |
| 459 | struct CheckBlockEntranceContext { |
| 460 | using CheckBlockEntranceFunc = CheckerManager::CheckBlockEntranceFunc; |
| 461 | using CheckersTy = std::vector<CheckBlockEntranceFunc>; |
| 462 | |
| 463 | const CheckersTy &Checkers; |
| 464 | const BlockEntrance &Entrance; |
| 465 | ExprEngine &Eng; |
| 466 | |
| 467 | CheckBlockEntranceContext(const CheckersTy &Checkers, |
| 468 | const BlockEntrance &Entrance, ExprEngine &Eng) |
| 469 | : Checkers(Checkers), Entrance(Entrance), Eng(Eng) {} |
| 470 | |
| 471 | auto checkers_begin() const { return Checkers.begin(); } |
| 472 | auto checkers_end() const { return Checkers.end(); } |
| 473 | |
| 474 | void runChecker(CheckBlockEntranceFunc CheckFn, ExplodedNode *Pred, |
| 475 | ExplodedNodeSet &Dst) { |
| 476 | llvm::TimeTraceScope TimeScope( |
| 477 | checkerScopeName(Name: "BlockEntrance" , Checker: CheckFn.Checker)); |
| 478 | CheckerContext C(Eng, Pred, Dst, Entrance.withTag(NewTag: CheckFn.Checker)); |
| 479 | CheckFn(Entrance, C); |
| 480 | } |
| 481 | }; |
| 482 | |
| 483 | } // namespace |
| 484 | |
| 485 | void CheckerManager::runCheckersForBlockEntrance(ExplodedNodeSet &Dst, |
| 486 | const ExplodedNodeSet &Src, |
| 487 | const BlockEntrance &Entrance, |
| 488 | ExprEngine &Eng) const { |
| 489 | CheckBlockEntranceContext C(BlockEntranceCheckers, Entrance, Eng); |
| 490 | llvm::TimeTraceScope TimeScope{"CheckerManager::runCheckersForBlockEntrance" }; |
| 491 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 492 | } |
| 493 | |
| 494 | void CheckerManager::runCheckersForEndAnalysis(ExplodedGraph &G, |
| 495 | BugReporter &BR, |
| 496 | ExprEngine &Eng) { |
| 497 | for (const auto &EndAnalysisChecker : EndAnalysisCheckers) |
| 498 | EndAnalysisChecker(G, BR, Eng); |
| 499 | } |
| 500 | |
| 501 | namespace { |
| 502 | |
| 503 | struct CheckBeginFunctionContext { |
| 504 | using CheckersTy = std::vector<CheckerManager::CheckBeginFunctionFunc>; |
| 505 | |
| 506 | const CheckersTy &Checkers; |
| 507 | ExprEngine &Eng; |
| 508 | const ProgramPoint &PP; |
| 509 | |
| 510 | CheckBeginFunctionContext(const CheckersTy &Checkers, ExprEngine &Eng, |
| 511 | const ProgramPoint &PP) |
| 512 | : Checkers(Checkers), Eng(Eng), PP(PP) {} |
| 513 | |
| 514 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 515 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 516 | |
| 517 | void runChecker(CheckerManager::CheckBeginFunctionFunc checkFn, |
| 518 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 519 | llvm::TimeTraceScope TimeScope(checkerScopeName(Name: "Begin" , Checker: checkFn.Checker)); |
| 520 | const ProgramPoint &L = PP.withTag(NewTag: checkFn.Checker); |
| 521 | CheckerContext C(Eng, Pred, Dst, L); |
| 522 | |
| 523 | checkFn(C); |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | } // namespace |
| 528 | |
| 529 | void CheckerManager::runCheckersForBeginFunction(ExplodedNodeSet &Dst, |
| 530 | const BlockEdge &L, |
| 531 | ExplodedNode *Pred, |
| 532 | ExprEngine &Eng) { |
| 533 | ExplodedNodeSet Src; |
| 534 | Src.insert(N: Pred); |
| 535 | CheckBeginFunctionContext C(BeginFunctionCheckers, Eng, L); |
| 536 | llvm::TimeTraceScope TimeScope("CheckerManager::runCheckersForBeginFunction" ); |
| 537 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 538 | } |
| 539 | |
| 540 | namespace { |
| 541 | |
| 542 | struct CheckEndFunctionContext { |
| 543 | using CheckersTy = std::vector<CheckerManager::CheckEndFunctionFunc>; |
| 544 | |
| 545 | const CheckersTy &Checkers; |
| 546 | const ReturnStmt *RS; |
| 547 | ExprEngine &Eng; |
| 548 | |
| 549 | CheckEndFunctionContext(const CheckersTy &Checkers, const ReturnStmt *RS, |
| 550 | ExprEngine &Eng) |
| 551 | : Checkers(Checkers), RS(RS), Eng(Eng) {} |
| 552 | |
| 553 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 554 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 555 | |
| 556 | void runChecker(CheckerManager::CheckEndFunctionFunc checkFn, |
| 557 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 558 | llvm::TimeTraceScope TimeScope(checkerScopeName(Name: "End" , Checker: checkFn.Checker)); |
| 559 | const ProgramPoint &L = |
| 560 | FunctionExitPoint(RS, Pred->getStackFrame(), checkFn.Checker); |
| 561 | CheckerContext C(Eng, Pred, Dst, L); |
| 562 | |
| 563 | checkFn(RS, C); |
| 564 | } |
| 565 | }; |
| 566 | |
| 567 | } // namespace |
| 568 | |
| 569 | /// Run checkers for end of a function (either the entrypoint or another |
| 570 | /// function that was inlined). |
| 571 | void CheckerManager::runCheckersForEndFunction(ExplodedNodeSet &Dst, |
| 572 | ExplodedNode *Pred, |
| 573 | ExprEngine &Eng, |
| 574 | const ReturnStmt *RS) { |
| 575 | ExplodedNodeSet Src; |
| 576 | Src.insert(N: Pred); |
| 577 | CheckEndFunctionContext C(EndFunctionCheckers, RS, Eng); |
| 578 | llvm::TimeTraceScope TimeScope("CheckerManager::runCheckersForEndFunction" ); |
| 579 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 580 | } |
| 581 | |
| 582 | namespace { |
| 583 | |
| 584 | struct CheckBranchConditionContext { |
| 585 | using CheckersTy = std::vector<CheckerManager::CheckBranchConditionFunc>; |
| 586 | |
| 587 | const CheckersTy &Checkers; |
| 588 | const Stmt *Condition; |
| 589 | ExprEngine &Eng; |
| 590 | |
| 591 | CheckBranchConditionContext(const CheckersTy &checkers, |
| 592 | const Stmt *Cond, ExprEngine &eng) |
| 593 | : Checkers(checkers), Condition(Cond), Eng(eng) {} |
| 594 | |
| 595 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 596 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 597 | |
| 598 | void runChecker(CheckerManager::CheckBranchConditionFunc checkFn, |
| 599 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 600 | llvm::TimeTraceScope TimeScope( |
| 601 | checkerScopeName(Name: "BranchCond" , Checker: checkFn.Checker)); |
| 602 | ProgramPoint L = |
| 603 | PostCondition(Condition, Pred->getStackFrame(), checkFn.Checker); |
| 604 | CheckerContext C(Eng, Pred, Dst, L); |
| 605 | checkFn(Condition, C); |
| 606 | } |
| 607 | }; |
| 608 | |
| 609 | } // namespace |
| 610 | |
| 611 | /// Run checkers for branch condition. |
| 612 | void CheckerManager::runCheckersForBranchCondition(const Stmt *Condition, |
| 613 | ExplodedNodeSet &Dst, |
| 614 | ExplodedNode *Pred, |
| 615 | ExprEngine &Eng) { |
| 616 | ExplodedNodeSet Src; |
| 617 | Src.insert(N: Pred); |
| 618 | CheckBranchConditionContext C(BranchConditionCheckers, Condition, Eng); |
| 619 | llvm::TimeTraceScope TimeScope( |
| 620 | "CheckerManager::runCheckersForBranchCondition" ); |
| 621 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 622 | } |
| 623 | |
| 624 | namespace { |
| 625 | |
| 626 | struct CheckNewAllocatorContext { |
| 627 | using CheckersTy = std::vector<CheckerManager::CheckNewAllocatorFunc>; |
| 628 | |
| 629 | const CheckersTy &Checkers; |
| 630 | const CXXAllocatorCall &Call; |
| 631 | bool WasInlined; |
| 632 | ExprEngine &Eng; |
| 633 | |
| 634 | CheckNewAllocatorContext(const CheckersTy &Checkers, |
| 635 | const CXXAllocatorCall &Call, bool WasInlined, |
| 636 | ExprEngine &Eng) |
| 637 | : Checkers(Checkers), Call(Call), WasInlined(WasInlined), Eng(Eng) {} |
| 638 | |
| 639 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 640 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 641 | |
| 642 | void runChecker(CheckerManager::CheckNewAllocatorFunc checkFn, |
| 643 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 644 | llvm::TimeTraceScope TimeScope( |
| 645 | checkerScopeName(Name: "Allocator" , Checker: checkFn.Checker)); |
| 646 | ProgramPoint L = PostAllocatorCall( |
| 647 | Call.getOriginExpr(), Pred->getStackFrame(), checkFn.Checker); |
| 648 | CheckerContext C(Eng, Pred, Dst, L, WasInlined); |
| 649 | checkFn(cast<CXXAllocatorCall>(Val: *Call.cloneWithState(NewState: Pred->getState())), |
| 650 | C); |
| 651 | } |
| 652 | }; |
| 653 | |
| 654 | } // namespace |
| 655 | |
| 656 | void CheckerManager::runCheckersForNewAllocator(const CXXAllocatorCall &Call, |
| 657 | ExplodedNodeSet &Dst, |
| 658 | ExplodedNode *Pred, |
| 659 | ExprEngine &Eng, |
| 660 | bool WasInlined) { |
| 661 | ExplodedNodeSet Src; |
| 662 | Src.insert(N: Pred); |
| 663 | CheckNewAllocatorContext C(NewAllocatorCheckers, Call, WasInlined, Eng); |
| 664 | llvm::TimeTraceScope TimeScope("CheckerManager::runCheckersForNewAllocator" ); |
| 665 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 666 | } |
| 667 | |
| 668 | /// Run checkers for live symbols. |
| 669 | void CheckerManager::runCheckersForLiveSymbols(ProgramStateRef state, |
| 670 | SymbolReaper &SymReaper) { |
| 671 | for (const auto &LiveSymbolsChecker : LiveSymbolsCheckers) |
| 672 | LiveSymbolsChecker(state, SymReaper); |
| 673 | } |
| 674 | |
| 675 | namespace { |
| 676 | |
| 677 | struct CheckDeadSymbolsContext { |
| 678 | using CheckersTy = std::vector<CheckerManager::CheckDeadSymbolsFunc>; |
| 679 | |
| 680 | const CheckersTy &Checkers; |
| 681 | SymbolReaper &SR; |
| 682 | const Stmt *S; |
| 683 | ExprEngine &Eng; |
| 684 | ProgramPoint::Kind ProgramPointKind; |
| 685 | |
| 686 | CheckDeadSymbolsContext(const CheckersTy &checkers, SymbolReaper &sr, |
| 687 | const Stmt *s, ExprEngine &eng, |
| 688 | ProgramPoint::Kind K) |
| 689 | : Checkers(checkers), SR(sr), S(s), Eng(eng), ProgramPointKind(K) {} |
| 690 | |
| 691 | CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); } |
| 692 | CheckersTy::const_iterator checkers_end() { return Checkers.end(); } |
| 693 | |
| 694 | void runChecker(CheckerManager::CheckDeadSymbolsFunc checkFn, |
| 695 | ExplodedNode *Pred, ExplodedNodeSet &Dst) { |
| 696 | llvm::TimeTraceScope TimeScope( |
| 697 | checkerScopeName(Name: "DeadSymbols" , Checker: checkFn.Checker)); |
| 698 | const ProgramPoint &L = ProgramPoint::getProgramPoint( |
| 699 | S, K: ProgramPointKind, SF: Pred->getStackFrame(), tag: checkFn.Checker); |
| 700 | CheckerContext C(Eng, Pred, Dst, L); |
| 701 | |
| 702 | // Note, do not pass the statement to the checkers without letting them |
| 703 | // differentiate if we ran remove dead bindings before or after the |
| 704 | // statement. |
| 705 | checkFn(SR, C); |
| 706 | } |
| 707 | }; |
| 708 | |
| 709 | } // namespace |
| 710 | |
| 711 | /// Run checkers for dead symbols. |
| 712 | void CheckerManager::runCheckersForDeadSymbols(ExplodedNodeSet &Dst, |
| 713 | const ExplodedNodeSet &Src, |
| 714 | SymbolReaper &SymReaper, |
| 715 | const Stmt *S, |
| 716 | ExprEngine &Eng, |
| 717 | ProgramPoint::Kind K) { |
| 718 | CheckDeadSymbolsContext C(DeadSymbolsCheckers, SymReaper, S, Eng, K); |
| 719 | llvm::TimeTraceScope TimeScope("CheckerManager::runCheckersForDeadSymbols" ); |
| 720 | expandGraphWithCheckers(checkCtx: C, Dst, Src); |
| 721 | } |
| 722 | |
| 723 | /// Run checkers for region changes. |
| 724 | ProgramStateRef CheckerManager::runCheckersForRegionChanges( |
| 725 | ProgramStateRef state, const InvalidatedSymbols *invalidated, |
| 726 | ArrayRef<const MemRegion *> ExplicitRegions, |
| 727 | ArrayRef<const MemRegion *> Regions, const StackFrame *SF, |
| 728 | const CallEvent *Call) { |
| 729 | for (const auto &RegionChangesChecker : RegionChangesCheckers) { |
| 730 | // If any checker declares the state infeasible (or if it starts that way), |
| 731 | // bail out. |
| 732 | if (!state) |
| 733 | return nullptr; |
| 734 | state = RegionChangesChecker(state, invalidated, ExplicitRegions, Regions, |
| 735 | SF, Call); |
| 736 | } |
| 737 | return state; |
| 738 | } |
| 739 | |
| 740 | /// Run checkers to process symbol escape event. |
| 741 | ProgramStateRef |
| 742 | CheckerManager::runCheckersForPointerEscape(ProgramStateRef State, |
| 743 | const InvalidatedSymbols &Escaped, |
| 744 | const CallEvent *Call, |
| 745 | PointerEscapeKind Kind, |
| 746 | RegionAndSymbolInvalidationTraits *ETraits) { |
| 747 | assert((Call != nullptr || |
| 748 | (Kind != PSK_DirectEscapeOnCall && |
| 749 | Kind != PSK_IndirectEscapeOnCall)) && |
| 750 | "Call must not be NULL when escaping on call" ); |
| 751 | for (const auto &PointerEscapeChecker : PointerEscapeCheckers) { |
| 752 | // If any checker declares the state infeasible (or if it starts that |
| 753 | // way), bail out. |
| 754 | if (!State) |
| 755 | return nullptr; |
| 756 | State = PointerEscapeChecker(State, Escaped, Call, Kind, ETraits); |
| 757 | } |
| 758 | return State; |
| 759 | } |
| 760 | |
| 761 | /// Run checkers for handling assumptions on symbolic values. |
| 762 | ProgramStateRef |
| 763 | CheckerManager::runCheckersForEvalAssume(ProgramStateRef state, |
| 764 | SVal Cond, bool Assumption) { |
| 765 | for (const auto &EvalAssumeChecker : EvalAssumeCheckers) { |
| 766 | // If any checker declares the state infeasible (or if it starts that way), |
| 767 | // bail out. |
| 768 | if (!state) |
| 769 | return nullptr; |
| 770 | state = EvalAssumeChecker(state, Cond, Assumption); |
| 771 | } |
| 772 | return state; |
| 773 | } |
| 774 | |
| 775 | /// Run checkers for evaluating a call. |
| 776 | /// Only one checker will evaluate the call. |
| 777 | void CheckerManager::runCheckersForEvalCall(ExplodedNodeSet &Dst, |
| 778 | const ExplodedNodeSet &Src, |
| 779 | const CallEvent &Call, |
| 780 | ExprEngine &Eng, |
| 781 | const EvalCallOptions &CallOpts) { |
| 782 | for (auto *const Pred : Src) { |
| 783 | std::optional<StringRef> evaluatorChecker; |
| 784 | |
| 785 | ExplodedNodeSet checkDst{Pred}; |
| 786 | |
| 787 | ProgramStateRef State = Pred->getState(); |
| 788 | CallEventRef<> UpdatedCall = Call.cloneWithState(NewState: State); |
| 789 | |
| 790 | // Check if any of the EvalCall callbacks can evaluate the call. |
| 791 | for (const auto &EvalCallChecker : EvalCallCheckers) { |
| 792 | // TODO: Support the situation when the call doesn't correspond |
| 793 | // to any Expr. |
| 794 | ProgramPoint L = ProgramPoint::getProgramPoint( |
| 795 | S: UpdatedCall->getOriginExpr(), K: ProgramPoint::PostStmtKind, |
| 796 | SF: Pred->getStackFrame(), tag: EvalCallChecker.Checker); |
| 797 | |
| 798 | CheckerContext C(Eng, Pred, checkDst, L); |
| 799 | bool evaluated = EvalCallChecker(*UpdatedCall, C); |
| 800 | #ifndef NDEBUG |
| 801 | if (evaluated && evaluatorChecker) { |
| 802 | const auto toString = [](const CallEvent &Call) -> std::string { |
| 803 | std::string Buf; |
| 804 | llvm::raw_string_ostream OS(Buf); |
| 805 | Call.dump(OS); |
| 806 | return Buf; |
| 807 | }; |
| 808 | std::string AssertionMessage = llvm::formatv( |
| 809 | "The '{0}' call has been already evaluated by the {1} checker, " |
| 810 | "while the {2} checker also tried to evaluate the same call. At " |
| 811 | "most one checker supposed to evaluate a call." , |
| 812 | toString(Call), evaluatorChecker, |
| 813 | EvalCallChecker.Checker->getDebugTag()); |
| 814 | llvm_unreachable(AssertionMessage.c_str()); |
| 815 | } |
| 816 | #endif |
| 817 | if (evaluated) { |
| 818 | evaluatorChecker = EvalCallChecker.Checker->getDebugTag(); |
| 819 | Dst.insert(S: checkDst); |
| 820 | #ifdef NDEBUG |
| 821 | break; // on release don't check that no other checker also evals. |
| 822 | #endif |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // If none of the checkers evaluated the call, ask ExprEngine to handle it. |
| 827 | if (!evaluatorChecker) |
| 828 | Eng.defaultEvalCall(Dst, Pred, Call: *UpdatedCall, CallOpts); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /// Run checkers for the entire Translation Unit. |
| 833 | void CheckerManager::runCheckersOnEndOfTranslationUnit( |
| 834 | const TranslationUnitDecl *TU, |
| 835 | AnalysisManager &mgr, |
| 836 | BugReporter &BR) { |
| 837 | for (const auto &EndOfTranslationUnitChecker : EndOfTranslationUnitCheckers) |
| 838 | EndOfTranslationUnitChecker(TU, mgr, BR); |
| 839 | } |
| 840 | |
| 841 | void CheckerManager::runCheckersForPrintStateJson(raw_ostream &Out, |
| 842 | ProgramStateRef State, |
| 843 | const char *NL, |
| 844 | unsigned int Space, |
| 845 | bool IsDot) const { |
| 846 | Indent(Out, Space, IsDot) << "\"checker_messages\": " ; |
| 847 | |
| 848 | // Create a temporary stream to see whether we have any message. |
| 849 | SmallString<1024> TempBuf; |
| 850 | llvm::raw_svector_ostream TempOut(TempBuf); |
| 851 | unsigned int InnerSpace = Space + 2; |
| 852 | |
| 853 | // Create the new-line in JSON with enough space. |
| 854 | SmallString<128> NewLine; |
| 855 | llvm::raw_svector_ostream NLOut(NewLine); |
| 856 | NLOut << "\", " << NL; // Inject the ending and a new line |
| 857 | Indent(Out&: NLOut, Space: InnerSpace, IsDot) << "\"" ; // then begin the next message. |
| 858 | |
| 859 | ++Space; |
| 860 | |
| 861 | std::vector<std::pair<std::string, std::string>> CheckerMsgs; |
| 862 | for (const auto &CT : CheckerTags) { |
| 863 | CT.second->printState(Out&: TempOut, State, /*NL=*/NewLine.c_str(), /*Sep=*/"" ); |
| 864 | const std::string Msg = TempBuf.str().trim().str(); |
| 865 | TempBuf.clear(); |
| 866 | |
| 867 | if (!Msg.empty()) |
| 868 | CheckerMsgs.push_back(x: {CT.second->getDebugTag().str(), Msg}); |
| 869 | } |
| 870 | llvm::sort(C&: CheckerMsgs); |
| 871 | |
| 872 | if (!CheckerMsgs.empty()) |
| 873 | Out << '[' << NL; |
| 874 | |
| 875 | llvm::interleave( |
| 876 | c: CheckerMsgs, |
| 877 | each_fn: [&](const std::pair<std::string, std::string> &CM) { |
| 878 | Indent(Out, Space, IsDot) |
| 879 | << "{ \"checker\": \"" << CM.first << "\", \"messages\": [" << NL; |
| 880 | Indent(Out, Space: InnerSpace, IsDot) << '\"' << CM.second << '\"' << NL; |
| 881 | Indent(Out, Space, IsDot) << "]}" ; |
| 882 | }, |
| 883 | between_fn: [&] { Out << ',' << NL; }); |
| 884 | |
| 885 | // It is the last element of the 'program_state' so do not add a comma. |
| 886 | if (!CheckerMsgs.empty()) { |
| 887 | Out << NL; |
| 888 | Indent(Out, Space: --Space, IsDot) << "]" ; |
| 889 | } else { |
| 890 | Out << "null" ; |
| 891 | } |
| 892 | |
| 893 | Out << NL; |
| 894 | } |
| 895 | |
| 896 | //===----------------------------------------------------------------------===// |
| 897 | // Internal registration functions for AST traversing. |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | |
| 900 | void CheckerManager::_registerForDecl(CheckDeclFunc checkfn, |
| 901 | HandlesDeclFunc isForDeclFn) { |
| 902 | DeclCheckerInfo info = { .CheckFn: checkfn, .IsForDeclFn: isForDeclFn }; |
| 903 | DeclCheckers.push_back(x: info); |
| 904 | } |
| 905 | |
| 906 | void CheckerManager::_registerForBody(CheckDeclFunc checkfn) { |
| 907 | BodyCheckers.push_back(x: checkfn); |
| 908 | } |
| 909 | |
| 910 | //===----------------------------------------------------------------------===// |
| 911 | // Internal registration functions for path-sensitive checking. |
| 912 | //===----------------------------------------------------------------------===// |
| 913 | |
| 914 | void CheckerManager::_registerForPreStmt(CheckStmtFunc checkfn, |
| 915 | HandlesStmtFunc isForStmtFn) { |
| 916 | StmtCheckerInfo info = { .CheckFn: checkfn, .IsForStmtFn: isForStmtFn, /*IsPreVisit*/true }; |
| 917 | StmtCheckers.push_back(x: info); |
| 918 | } |
| 919 | |
| 920 | void CheckerManager::_registerForPostStmt(CheckStmtFunc checkfn, |
| 921 | HandlesStmtFunc isForStmtFn) { |
| 922 | StmtCheckerInfo info = { .CheckFn: checkfn, .IsForStmtFn: isForStmtFn, /*IsPreVisit*/false }; |
| 923 | StmtCheckers.push_back(x: info); |
| 924 | } |
| 925 | |
| 926 | void CheckerManager::_registerForPreObjCMessage(CheckObjCMessageFunc checkfn) { |
| 927 | PreObjCMessageCheckers.push_back(x: checkfn); |
| 928 | } |
| 929 | |
| 930 | void CheckerManager::_registerForObjCMessageNil(CheckObjCMessageFunc checkfn) { |
| 931 | ObjCMessageNilCheckers.push_back(x: checkfn); |
| 932 | } |
| 933 | |
| 934 | void CheckerManager::_registerForPostObjCMessage(CheckObjCMessageFunc checkfn) { |
| 935 | PostObjCMessageCheckers.push_back(x: checkfn); |
| 936 | } |
| 937 | |
| 938 | void CheckerManager::_registerForPreCall(CheckCallFunc checkfn) { |
| 939 | PreCallCheckers.push_back(x: checkfn); |
| 940 | } |
| 941 | void CheckerManager::_registerForPostCall(CheckCallFunc checkfn) { |
| 942 | PostCallCheckers.push_back(x: checkfn); |
| 943 | } |
| 944 | |
| 945 | void CheckerManager::_registerForLifetimeEnd(CheckLifetimeEndFunc checkfn) { |
| 946 | LifetimeEndCheckers.push_back(x: checkfn); |
| 947 | } |
| 948 | |
| 949 | void CheckerManager::_registerForLocation(CheckLocationFunc checkfn) { |
| 950 | LocationCheckers.push_back(x: checkfn); |
| 951 | } |
| 952 | |
| 953 | void CheckerManager::_registerForBind(CheckBindFunc checkfn) { |
| 954 | BindCheckers.push_back(x: checkfn); |
| 955 | } |
| 956 | |
| 957 | void CheckerManager::_registerForBlockEntrance(CheckBlockEntranceFunc checkfn) { |
| 958 | BlockEntranceCheckers.push_back(x: checkfn); |
| 959 | } |
| 960 | |
| 961 | void CheckerManager::_registerForEndAnalysis(CheckEndAnalysisFunc checkfn) { |
| 962 | EndAnalysisCheckers.push_back(x: checkfn); |
| 963 | } |
| 964 | |
| 965 | void CheckerManager::_registerForBeginFunction(CheckBeginFunctionFunc checkfn) { |
| 966 | BeginFunctionCheckers.push_back(x: checkfn); |
| 967 | } |
| 968 | |
| 969 | void CheckerManager::_registerForEndFunction(CheckEndFunctionFunc checkfn) { |
| 970 | EndFunctionCheckers.push_back(x: checkfn); |
| 971 | } |
| 972 | |
| 973 | void CheckerManager::_registerForBranchCondition( |
| 974 | CheckBranchConditionFunc checkfn) { |
| 975 | BranchConditionCheckers.push_back(x: checkfn); |
| 976 | } |
| 977 | |
| 978 | void CheckerManager::_registerForNewAllocator(CheckNewAllocatorFunc checkfn) { |
| 979 | NewAllocatorCheckers.push_back(x: checkfn); |
| 980 | } |
| 981 | |
| 982 | void CheckerManager::_registerForLiveSymbols(CheckLiveSymbolsFunc checkfn) { |
| 983 | LiveSymbolsCheckers.push_back(x: checkfn); |
| 984 | } |
| 985 | |
| 986 | void CheckerManager::_registerForDeadSymbols(CheckDeadSymbolsFunc checkfn) { |
| 987 | DeadSymbolsCheckers.push_back(x: checkfn); |
| 988 | } |
| 989 | |
| 990 | void CheckerManager::_registerForRegionChanges(CheckRegionChangesFunc checkfn) { |
| 991 | RegionChangesCheckers.push_back(x: checkfn); |
| 992 | } |
| 993 | |
| 994 | void CheckerManager::_registerForPointerEscape(CheckPointerEscapeFunc checkfn){ |
| 995 | PointerEscapeCheckers.push_back(x: checkfn); |
| 996 | } |
| 997 | |
| 998 | void CheckerManager::_registerForConstPointerEscape( |
| 999 | CheckPointerEscapeFunc checkfn) { |
| 1000 | PointerEscapeCheckers.push_back(x: checkfn); |
| 1001 | } |
| 1002 | |
| 1003 | void CheckerManager::_registerForEvalAssume(EvalAssumeFunc checkfn) { |
| 1004 | EvalAssumeCheckers.push_back(x: checkfn); |
| 1005 | } |
| 1006 | |
| 1007 | void CheckerManager::_registerForEvalCall(EvalCallFunc checkfn) { |
| 1008 | EvalCallCheckers.push_back(x: checkfn); |
| 1009 | } |
| 1010 | |
| 1011 | void CheckerManager::_registerForEndOfTranslationUnit( |
| 1012 | CheckEndOfTranslationUnit checkfn) { |
| 1013 | EndOfTranslationUnitCheckers.push_back(x: checkfn); |
| 1014 | } |
| 1015 | |
| 1016 | //===----------------------------------------------------------------------===// |
| 1017 | // Implementation details. |
| 1018 | //===----------------------------------------------------------------------===// |
| 1019 | |
| 1020 | const CheckerManager::CachedStmtCheckers & |
| 1021 | CheckerManager::getCachedStmtCheckersFor(const Stmt *S, bool isPreVisit) { |
| 1022 | assert(S); |
| 1023 | |
| 1024 | unsigned Key = (S->getStmtClass() << 1) | unsigned(isPreVisit); |
| 1025 | auto [CCI, Inserted] = CachedStmtCheckersMap.try_emplace(Key); |
| 1026 | CachedStmtCheckers &Checkers = CCI->second; |
| 1027 | if (Inserted) { |
| 1028 | // Find the checkers that should run for this Stmt and cache them. |
| 1029 | for (const auto &Info : StmtCheckers) |
| 1030 | if (Info.IsPreVisit == isPreVisit && Info.IsForStmtFn(S)) |
| 1031 | Checkers.push_back(Elt: Info.CheckFn); |
| 1032 | } |
| 1033 | return Checkers; |
| 1034 | } |
| 1035 | |