MPIChecker.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- MPIChecker.h - Verify MPI API usage- --------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file defines the main class of MPI-Checker which serves as an entry
  11. /// point. It is created once for each translation unit analysed.
  12. /// The checker defines path-sensitive checks, to verify correct usage of the
  13. /// MPI API.
  14. ///
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_MPICHECKER_MPICHECKER_H
  17. #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_MPICHECKER_MPICHECKER_H
  18. #include "MPIBugReporter.h"
  19. #include "MPITypes.h"
  20. #include "clang/StaticAnalyzer/Checkers/MPIFunctionClassifier.h"
  21. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  22. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  23. namespace clang {
  24. namespace ento {
  25. namespace mpi {
  26. class MPIChecker : public Checker<check::PreCall, check::DeadSymbols> {
  27. public:
  28. MPIChecker() : BReporter(*this) {}
  29. // path-sensitive callbacks
  30. void checkPreCall(const CallEvent &CE, CheckerContext &Ctx) const {
  31. dynamicInit(Ctx);
  32. checkUnmatchedWaits(CE, Ctx);
  33. checkDoubleNonblocking(CE, Ctx);
  34. }
  35. void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &Ctx) const {
  36. dynamicInit(Ctx);
  37. checkMissingWaits(SymReaper, Ctx);
  38. }
  39. void dynamicInit(CheckerContext &Ctx) const {
  40. if (FuncClassifier)
  41. return;
  42. const_cast<std::unique_ptr<MPIFunctionClassifier> &>(FuncClassifier)
  43. .reset(new MPIFunctionClassifier{Ctx.getASTContext()});
  44. }
  45. /// Checks if a request is used by nonblocking calls multiple times
  46. /// in sequence without intermediate wait. The check contains a guard,
  47. /// in order to only inspect nonblocking functions.
  48. ///
  49. /// \param PreCallEvent MPI call to verify
  50. void checkDoubleNonblocking(const clang::ento::CallEvent &PreCallEvent,
  51. clang::ento::CheckerContext &Ctx) const;
  52. /// Checks if the request used by the wait function was not used at all
  53. /// before. The check contains a guard, in order to only inspect wait
  54. /// functions.
  55. ///
  56. /// \param PreCallEvent MPI call to verify
  57. void checkUnmatchedWaits(const clang::ento::CallEvent &PreCallEvent,
  58. clang::ento::CheckerContext &Ctx) const;
  59. /// Check if a nonblocking call is not matched by a wait.
  60. /// If a memory region is not alive and the last function using the
  61. /// request was a nonblocking call, this is rated as a missing wait.
  62. void checkMissingWaits(clang::ento::SymbolReaper &SymReaper,
  63. clang::ento::CheckerContext &Ctx) const;
  64. private:
  65. /// Collects all memory regions of a request(array) used by a wait
  66. /// function. If the wait function uses a single request, this is a single
  67. /// region. For wait functions using multiple requests, multiple regions
  68. /// representing elements in the array are collected.
  69. ///
  70. /// \param ReqRegions vector the regions get pushed into
  71. /// \param MR top most region to iterate
  72. /// \param CE MPI wait call using the request(s)
  73. void allRegionsUsedByWait(
  74. llvm::SmallVector<const clang::ento::MemRegion *, 2> &ReqRegions,
  75. const clang::ento::MemRegion *const MR, const clang::ento::CallEvent &CE,
  76. clang::ento::CheckerContext &Ctx) const;
  77. /// Returns the memory region used by a wait function.
  78. /// Distinguishes between MPI_Wait and MPI_Waitall.
  79. ///
  80. /// \param CE MPI wait call
  81. const clang::ento::MemRegion *
  82. topRegionUsedByWait(const clang::ento::CallEvent &CE) const;
  83. const std::unique_ptr<MPIFunctionClassifier> FuncClassifier;
  84. MPIBugReporter BReporter;
  85. };
  86. } // end of namespace: mpi
  87. } // end of namespace: ento
  88. } // end of namespace: clang
  89. #endif