clang  19.0.0git
MPIChecker.cpp
Go to the documentation of this file.
1 //===-- MPIChecker.cpp - Checker Entry Point Class --------------*- 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 
17 #include "MPIChecker.h"
20 
21 namespace clang {
22 namespace ento {
23 namespace mpi {
24 
26  CheckerContext &Ctx) const {
27  if (!FuncClassifier->isNonBlockingType(PreCallEvent.getCalleeIdentifier())) {
28  return;
29  }
30  const MemRegion *const MR =
31  PreCallEvent.getArgSVal(PreCallEvent.getNumArgs() - 1).getAsRegion();
32  if (!MR)
33  return;
34  const ElementRegion *const ER = dyn_cast<ElementRegion>(MR);
35 
36  // The region must be typed, in order to reason about it.
37  if (!isa<TypedRegion>(MR) || (ER && !isa<TypedRegion>(ER->getSuperRegion())))
38  return;
39 
41  const Request *const Req = State->get<RequestMap>(MR);
42 
43  // double nonblocking detected
44  if (Req && Req->CurrentState == Request::State::Nonblocking) {
45  ExplodedNode *ErrorNode = Ctx.generateNonFatalErrorNode();
46  BReporter.reportDoubleNonblocking(PreCallEvent, *Req, MR, ErrorNode,
47  Ctx.getBugReporter());
48  Ctx.addTransition(ErrorNode->getState(), ErrorNode);
49  }
50  // no error
51  else {
52  State = State->set<RequestMap>(MR, Request::State::Nonblocking);
53  Ctx.addTransition(State);
54  }
55 }
56 
57 void MPIChecker::checkUnmatchedWaits(const CallEvent &PreCallEvent,
58  CheckerContext &Ctx) const {
59  if (!FuncClassifier->isWaitType(PreCallEvent.getCalleeIdentifier()))
60  return;
61  const MemRegion *const MR = topRegionUsedByWait(PreCallEvent);
62  if (!MR)
63  return;
64  const ElementRegion *const ER = dyn_cast<ElementRegion>(MR);
65 
66  // The region must be typed, in order to reason about it.
67  if (!isa<TypedRegion>(MR) || (ER && !isa<TypedRegion>(ER->getSuperRegion())))
68  return;
69 
71  allRegionsUsedByWait(ReqRegions, MR, PreCallEvent, Ctx);
72  if (ReqRegions.empty())
73  return;
74 
76  static CheckerProgramPointTag Tag("MPI-Checker", "UnmatchedWait");
77  ExplodedNode *ErrorNode{nullptr};
78 
79  // Check all request regions used by the wait function.
80  for (const auto &ReqRegion : ReqRegions) {
81  const Request *const Req = State->get<RequestMap>(ReqRegion);
82  State = State->set<RequestMap>(ReqRegion, Request::State::Wait);
83  if (!Req) {
84  if (!ErrorNode) {
85  ErrorNode = Ctx.generateNonFatalErrorNode(State, &Tag);
86  State = ErrorNode->getState();
87  }
88  // A wait has no matching nonblocking call.
89  BReporter.reportUnmatchedWait(PreCallEvent, ReqRegion, ErrorNode,
90  Ctx.getBugReporter());
91  }
92  }
93 
94  if (!ErrorNode) {
95  Ctx.addTransition(State);
96  } else {
97  Ctx.addTransition(State, ErrorNode);
98  }
99 }
100 
102  CheckerContext &Ctx) const {
104  const auto &Requests = State->get<RequestMap>();
105  if (Requests.isEmpty())
106  return;
107 
108  static CheckerProgramPointTag Tag("MPI-Checker", "MissingWait");
109  ExplodedNode *ErrorNode{nullptr};
110 
111  auto ReqMap = State->get<RequestMap>();
112  for (const auto &Req : ReqMap) {
113  if (!SymReaper.isLiveRegion(Req.first)) {
114  if (Req.second.CurrentState == Request::State::Nonblocking) {
115 
116  if (!ErrorNode) {
117  ErrorNode = Ctx.generateNonFatalErrorNode(State, &Tag);
118  State = ErrorNode->getState();
119  }
120  BReporter.reportMissingWait(Req.second, Req.first, ErrorNode,
121  Ctx.getBugReporter());
122  }
123  State = State->remove<RequestMap>(Req.first);
124  }
125  }
126 
127  // Transition to update the state regarding removed requests.
128  if (!ErrorNode) {
129  Ctx.addTransition(State);
130  } else {
131  Ctx.addTransition(State, ErrorNode);
132  }
133 }
134 
135 const MemRegion *MPIChecker::topRegionUsedByWait(const CallEvent &CE) const {
136 
137  if (FuncClassifier->isMPI_Wait(CE.getCalleeIdentifier())) {
138  return CE.getArgSVal(0).getAsRegion();
139  } else if (FuncClassifier->isMPI_Waitall(CE.getCalleeIdentifier())) {
140  return CE.getArgSVal(1).getAsRegion();
141  } else {
142  return (const MemRegion *)nullptr;
143  }
144 }
145 
146 void MPIChecker::allRegionsUsedByWait(
148  const MemRegion *const MR, const CallEvent &CE, CheckerContext &Ctx) const {
149 
150  MemRegionManager &RegionManager = MR->getMemRegionManager();
151 
152  if (FuncClassifier->isMPI_Waitall(CE.getCalleeIdentifier())) {
153  const SubRegion *SuperRegion{nullptr};
154  if (const ElementRegion *const ER = MR->getAs<ElementRegion>()) {
155  SuperRegion = cast<SubRegion>(ER->getSuperRegion());
156  }
157 
158  // A single request is passed to MPI_Waitall.
159  if (!SuperRegion) {
160  ReqRegions.push_back(MR);
161  return;
162  }
163 
164  DefinedOrUnknownSVal ElementCount = getDynamicElementCount(
165  Ctx.getState(), SuperRegion, Ctx.getSValBuilder(),
166  CE.getArgExpr(1)->getType()->getPointeeType());
167  const llvm::APSInt &ArrSize =
168  ElementCount.castAs<nonloc::ConcreteInt>().getValue();
169 
170  for (size_t i = 0; i < ArrSize; ++i) {
171  const NonLoc Idx = Ctx.getSValBuilder().makeArrayIndex(i);
172 
173  const ElementRegion *const ER = RegionManager.getElementRegion(
174  CE.getArgExpr(1)->getType()->getPointeeType(), Idx, SuperRegion,
175  Ctx.getASTContext());
176 
177  ReqRegions.push_back(ER->getAs<MemRegion>());
178  }
179  } else if (FuncClassifier->isMPI_Wait(CE.getCalleeIdentifier())) {
180  ReqRegions.push_back(MR);
181  }
182 }
183 
184 } // end of namespace: mpi
185 } // end of namespace: ento
186 } // end of namespace: clang
187 
188 // Registers the checker for static analysis.
189 void clang::ento::registerMPIChecker(CheckerManager &MGR) {
190  MGR.registerChecker<clang::ento::mpi::MPIChecker>();
191 }
192 
193 bool clang::ento::shouldRegisterMPIChecker(const CheckerManager &mgr) {
194  return true;
195 }
llvm::APSInt APSInt
This file defines the main class of MPI-Checker which serves as an entry point.
LineState State
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:153
const IdentifierInfo * getCalleeIdentifier() const
Returns the name of the callee, if its name is a simple identifier.
Definition: CallEvent.h:351
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
Definition: CallEvent.cpp:308
virtual unsigned getNumArgs() const =0
Returns the number of arguments (explicit and implicit).
BugReporter & getBugReporter()
const ProgramStateRef & getState() const
ExplodedNode * generateNonFatalErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
Tag that can use a checker name as a message provider (see SimpleProgramPointTag).
Definition: Checker.h:505
ElementRegion is used to represent both array elements and casts.
Definition: MemRegion.h:1194
const ProgramStateRef & getState() const
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:96
virtual MemRegionManager & getMemRegionManager() const =0
const MemRegion * getAsRegion() const
Definition: SVals.cpp:120
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getSuperRegion() const
Definition: MemRegion.h:454
A class responsible for cleaning up unused symbols.
bool isLiveRegion(const MemRegion *region)
void reportMissingWait(const Request &Req, const MemRegion *const RequestRegion, const ExplodedNode *const ExplNode, BugReporter &BReporter) const
Report a missing wait for a nonblocking call.
void reportDoubleNonblocking(const CallEvent &MPICallEvent, const Request &Req, const MemRegion *const RequestRegion, const ExplodedNode *const ExplNode, BugReporter &BReporter) const
Report duplicate request use by nonblocking calls without intermediate wait.
void reportUnmatchedWait(const CallEvent &CE, const MemRegion *const RequestRegion, const ExplodedNode *const ExplNode, BugReporter &BReporter) const
Report a wait on a request that has not been used at all before.
void checkMissingWaits(clang::ento::SymbolReaper &SymReaper, clang::ento::CheckerContext &Ctx) const
Check if a nonblocking call is not matched by a wait.
Definition: MPIChecker.cpp:101
void checkDoubleNonblocking(const clang::ento::CallEvent &PreCallEvent, clang::ento::CheckerContext &Ctx) const
Checks if a request is used by nonblocking calls multiple times in sequence without intermediate wait...
Definition: MPIChecker.cpp:25
void checkUnmatchedWaits(const clang::ento::CallEvent &PreCallEvent, clang::ento::CheckerContext &Ctx) const
Checks if the request used by the wait function was not used at all before.
Definition: MPIChecker.cpp:57
const State CurrentState
Definition: MPITypes.h:41
DefinedOrUnknownSVal getDynamicElementCount(ProgramStateRef State, const MemRegion *MR, SValBuilder &SVB, QualType Ty)
The JSON file list parser is used to communicate input to InstallAPI.