MPIBugReporter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===-- MPIBugReporter.cpp - bug reporter -----------------------*- 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 prefabricated reports which are emitted in
  11. /// case of MPI related bugs, detected by path-sensitive analysis.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "MPIBugReporter.h"
  15. #include "MPIChecker.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  17. namespace clang {
  18. namespace ento {
  19. namespace mpi {
  20. void MPIBugReporter::reportDoubleNonblocking(
  21. const CallEvent &MPICallEvent, const ento::mpi::Request &Req,
  22. const MemRegion *const RequestRegion,
  23. const ExplodedNode *const ExplNode,
  24. BugReporter &BReporter) const {
  25. std::string ErrorText;
  26. ErrorText = "Double nonblocking on request " +
  27. RequestRegion->getDescriptiveName() + ". ";
  28. auto Report = std::make_unique<PathSensitiveBugReport>(
  29. *DoubleNonblockingBugType, ErrorText, ExplNode);
  30. Report->addRange(MPICallEvent.getSourceRange());
  31. SourceRange Range = RequestRegion->sourceRange();
  32. if (Range.isValid())
  33. Report->addRange(Range);
  34. Report->addVisitor(std::make_unique<RequestNodeVisitor>(
  35. RequestRegion, "Request is previously used by nonblocking call here. "));
  36. Report->markInteresting(RequestRegion);
  37. BReporter.emitReport(std::move(Report));
  38. }
  39. void MPIBugReporter::reportMissingWait(
  40. const ento::mpi::Request &Req, const MemRegion *const RequestRegion,
  41. const ExplodedNode *const ExplNode,
  42. BugReporter &BReporter) const {
  43. std::string ErrorText{"Request " + RequestRegion->getDescriptiveName() +
  44. " has no matching wait. "};
  45. auto Report = std::make_unique<PathSensitiveBugReport>(*MissingWaitBugType,
  46. ErrorText, ExplNode);
  47. SourceRange Range = RequestRegion->sourceRange();
  48. if (Range.isValid())
  49. Report->addRange(Range);
  50. Report->addVisitor(std::make_unique<RequestNodeVisitor>(
  51. RequestRegion, "Request is previously used by nonblocking call here. "));
  52. Report->markInteresting(RequestRegion);
  53. BReporter.emitReport(std::move(Report));
  54. }
  55. void MPIBugReporter::reportUnmatchedWait(
  56. const CallEvent &CE, const clang::ento::MemRegion *const RequestRegion,
  57. const ExplodedNode *const ExplNode,
  58. BugReporter &BReporter) const {
  59. std::string ErrorText{"Request " + RequestRegion->getDescriptiveName() +
  60. " has no matching nonblocking call. "};
  61. auto Report = std::make_unique<PathSensitiveBugReport>(*UnmatchedWaitBugType,
  62. ErrorText, ExplNode);
  63. Report->addRange(CE.getSourceRange());
  64. SourceRange Range = RequestRegion->sourceRange();
  65. if (Range.isValid())
  66. Report->addRange(Range);
  67. BReporter.emitReport(std::move(Report));
  68. }
  69. PathDiagnosticPieceRef
  70. MPIBugReporter::RequestNodeVisitor::VisitNode(const ExplodedNode *N,
  71. BugReporterContext &BRC,
  72. PathSensitiveBugReport &BR) {
  73. if (IsNodeFound)
  74. return nullptr;
  75. const Request *const Req = N->getState()->get<RequestMap>(RequestRegion);
  76. assert(Req && "The region must be tracked and alive, given that we've "
  77. "just emitted a report against it");
  78. const Request *const PrevReq =
  79. N->getFirstPred()->getState()->get<RequestMap>(RequestRegion);
  80. // Check if request was previously unused or in a different state.
  81. if (!PrevReq || (Req->CurrentState != PrevReq->CurrentState)) {
  82. IsNodeFound = true;
  83. ProgramPoint P = N->getFirstPred()->getLocation();
  84. PathDiagnosticLocation L =
  85. PathDiagnosticLocation::create(P, BRC.getSourceManager());
  86. return std::make_shared<PathDiagnosticEventPiece>(L, ErrorText);
  87. }
  88. return nullptr;
  89. }
  90. } // end of namespace: mpi
  91. } // end of namespace: ento
  92. } // end of namespace: clang