xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp (revision 3c134670993bf525fcd6c4dfef84a3dfc3d4ed1b)
1 //=== FuchsiaHandleChecker.cpp - Find handle leaks/double closes -*- 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 // This checker checks if the handle of Fuchsia is properly used according to
10 // following rules.
11 //   - If a handle is acquired, it should be released before execution
12 //        ends.
13 //   - If a handle is released, it should not be released again.
14 //   - If a handle is released, it should not be used for other purposes
15 //        such as I/O.
16 //
17 // In this checker, each tracked handle is associated with a state. When the
18 // handle variable is passed to different function calls or syscalls, its state
19 // changes. The state changes can be generally represented by following ASCII
20 // Art:
21 //
22 //
23 //                              +-+---------v-+         +------------+
24 //       acquire_func succeeded |             | Escape  |            |
25 //            +----------------->  Allocated  +--------->  Escaped   <--+
26 //            |                 |             |         |            |  |
27 //            |                 +-----+------++         +------------+  |
28 //            |                       |      |                          |
29 //            |         release_func  |      +--+                       |
30 //            |                       |         | handle  +--------+    |
31 //            |                       |         | dies    |        |    |
32 //            |                  +----v-----+   +---------> Leaked |    |
33 //            |                  |          |             |(REPORT)|    |
34 // +----------+--+               | Released | Escape      +--------+    |
35 // |             |               |          +---------------------------+
36 // | Not tracked <--+            +----+---+-+
37 // |             |  |                 |   |        As argument by value
38 // +------+------+  |    release_func |   +------+ in function call
39 //        |         |                 |          | or by reference in
40 //        |         |                 |          | use_func call
41 //        +---------+            +----v-----+    |     +-----------+
42 //        acquire_func failed    | Double   |    +-----> Use after |
43 //                               | released |          | released  |
44 //                               | (REPORT) |          | (REPORT)  |
45 //                               +----------+          +-----------+
46 //
47 // acquire_func represents the functions or syscalls that may acquire a handle.
48 // release_func represents the functions or syscalls that may release a handle.
49 // use_func represents the functions or syscall that requires an open handle.
50 //
51 // If a tracked handle dies in "Released" or "Not Tracked" state, we assume it
52 // is properly used. Otherwise a bug and will be reported.
53 //
54 // Note that, the analyzer does not always know for sure if a function failed
55 // or succeeded. In those cases we use the state MaybeAllocated.
56 // Thus, the diagramm above captures the intent, not implementation details.
57 //
58 // Due to the fact that the number of handle related syscalls in Fuchsia
59 // is large, we adopt the annotation attributes to descript syscalls'
60 // operations(acquire/release/use) on handles instead of hardcoding
61 // everything in the checker.
62 //
63 // We use following annotation attributes for handle related syscalls or
64 // functions:
65 //  1. __attribute__((acquire_handle("Fuchsia"))) |handle will be acquired
66 //  2. __attribute__((release_handle("Fuchsia"))) |handle will be released
67 //  3. __attribute__((use_handle("Fuchsia"))) |handle will not transit to
68 //     escaped state, it also needs to be open.
69 //
70 // For example, an annotated syscall:
71 //   zx_status_t zx_channel_create(
72 //   uint32_t options,
73 //   zx_handle_t* out0 __attribute__((acquire_handle("Fuchsia"))) ,
74 //   zx_handle_t* out1 __attribute__((acquire_handle("Fuchsia"))));
75 // denotes a syscall which will acquire two handles and save them to 'out0' and
76 // 'out1' when succeeded.
77 //
78 //===----------------------------------------------------------------------===//
79 
80 #include "clang/AST/Attr.h"
81 #include "clang/AST/Decl.h"
82 #include "clang/AST/Type.h"
83 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
84 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
85 #include "clang/StaticAnalyzer/Core/Checker.h"
86 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
87 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
88 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
89 #include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
90 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
91 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
92 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
93 #include "llvm/ADT/StringExtras.h"
94 
95 using namespace clang;
96 using namespace ento;
97 
98 namespace {
99 
100 static const StringRef HandleTypeName = "zx_handle_t";
101 static const StringRef ErrorTypeName = "zx_status_t";
102 
103 class HandleState {
104 private:
105   enum class Kind { MaybeAllocated, Allocated, Released, Escaped } K;
106   SymbolRef ErrorSym;
107   HandleState(Kind K, SymbolRef ErrorSym) : K(K), ErrorSym(ErrorSym) {}
108 
109 public:
110   bool operator==(const HandleState &Other) const {
111     return K == Other.K && ErrorSym == Other.ErrorSym;
112   }
113   bool isAllocated() const { return K == Kind::Allocated; }
114   bool maybeAllocated() const { return K == Kind::MaybeAllocated; }
115   bool isReleased() const { return K == Kind::Released; }
116   bool isEscaped() const { return K == Kind::Escaped; }
117 
118   static HandleState getMaybeAllocated(SymbolRef ErrorSym) {
119     return HandleState(Kind::MaybeAllocated, ErrorSym);
120   }
121   static HandleState getAllocated(ProgramStateRef State, HandleState S) {
122     assert(S.maybeAllocated());
123     assert(State->getConstraintManager()
124                .isNull(State, S.getErrorSym())
125                .isConstrained());
126     return HandleState(Kind::Allocated, nullptr);
127   }
128   static HandleState getReleased() {
129     return HandleState(Kind::Released, nullptr);
130   }
131   static HandleState getEscaped() {
132     return HandleState(Kind::Escaped, nullptr);
133   }
134 
135   SymbolRef getErrorSym() const { return ErrorSym; }
136 
137   void Profile(llvm::FoldingSetNodeID &ID) const {
138     ID.AddInteger(static_cast<int>(K));
139     ID.AddPointer(ErrorSym);
140   }
141 
142   LLVM_DUMP_METHOD void dump(raw_ostream &OS) const {
143     switch (K) {
144 #define CASE(ID)                                                               \
145   case ID:                                                                     \
146     OS << #ID;                                                                 \
147     break;
148       CASE(Kind::MaybeAllocated)
149       CASE(Kind::Allocated)
150       CASE(Kind::Released)
151       CASE(Kind::Escaped)
152     }
153     if (ErrorSym) {
154       OS << " ErrorSym: ";
155       ErrorSym->dumpToStream(OS);
156     }
157   }
158 
159   LLVM_DUMP_METHOD void dump() const { dump(llvm::errs()); }
160 };
161 
162 template <typename Attr> static bool hasFuchsiaAttr(const Decl *D) {
163   return D->hasAttr<Attr>() && D->getAttr<Attr>()->getHandleType() == "Fuchsia";
164 }
165 
166 class FuchsiaHandleChecker
167     : public Checker<check::PostCall, check::PreCall, check::DeadSymbols,
168                      check::PointerEscape, eval::Assume> {
169   BugType LeakBugType{this, "Fuchsia handle leak", "Fuchsia Handle Error",
170                       /*SuppressOnSink=*/true};
171   BugType DoubleReleaseBugType{this, "Fuchsia handle double release",
172                                "Fuchsia Handle Error"};
173   BugType UseAfterReleaseBugType{this, "Fuchsia handle use after release",
174                                  "Fuchsia Handle Error"};
175 
176 public:
177   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
178   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
179   void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
180   ProgramStateRef evalAssume(ProgramStateRef State, SVal Cond,
181                              bool Assumption) const;
182   ProgramStateRef checkPointerEscape(ProgramStateRef State,
183                                      const InvalidatedSymbols &Escaped,
184                                      const CallEvent *Call,
185                                      PointerEscapeKind Kind) const;
186 
187   ExplodedNode *reportLeaks(ArrayRef<SymbolRef> LeakedHandles,
188                             CheckerContext &C, ExplodedNode *Pred) const;
189 
190   void reportDoubleRelease(SymbolRef HandleSym, const SourceRange &Range,
191                            CheckerContext &C) const;
192 
193   void reportUseAfterFree(SymbolRef HandleSym, const SourceRange &Range,
194                           CheckerContext &C) const;
195 
196   void reportBug(SymbolRef Sym, ExplodedNode *ErrorNode, CheckerContext &C,
197                  const SourceRange *Range, const BugType &Type,
198                  StringRef Msg) const;
199 
200   void printState(raw_ostream &Out, ProgramStateRef State, const char *NL,
201                   const char *Sep) const override;
202 };
203 } // end anonymous namespace
204 
205 REGISTER_MAP_WITH_PROGRAMSTATE(HStateMap, SymbolRef, HandleState)
206 
207 static const ExplodedNode *getAcquireSite(const ExplodedNode *N, SymbolRef Sym,
208                                           CheckerContext &Ctx) {
209   ProgramStateRef State = N->getState();
210   // When bug type is handle leak, exploded node N does not have state info for
211   // leaking handle. Get the predecessor of N instead.
212   if (!State->get<HStateMap>(Sym))
213     N = N->getFirstPred();
214 
215   const ExplodedNode *Pred = N;
216   while (N) {
217     State = N->getState();
218     if (!State->get<HStateMap>(Sym)) {
219       const HandleState *HState = Pred->getState()->get<HStateMap>(Sym);
220       if (HState && (HState->isAllocated() || HState->maybeAllocated()))
221         return N;
222     }
223     Pred = N;
224     N = N->getFirstPred();
225   }
226   return nullptr;
227 }
228 
229 /// Returns the symbols extracted from the argument or null if it cannot be
230 /// found.
231 static SymbolRef getFuchsiaHandleSymbol(QualType QT, SVal Arg,
232                                         ProgramStateRef State) {
233   int PtrToHandleLevel = 0;
234   while (QT->isAnyPointerType() || QT->isReferenceType()) {
235     ++PtrToHandleLevel;
236     QT = QT->getPointeeType();
237   }
238   if (const auto *HandleType = QT->getAs<TypedefType>()) {
239     if (HandleType->getDecl()->getName() != HandleTypeName)
240       return nullptr;
241     if (PtrToHandleLevel > 1) {
242       // Not supported yet.
243       return nullptr;
244     }
245 
246     if (PtrToHandleLevel == 0) {
247       return Arg.getAsSymbol();
248     } else {
249       assert(PtrToHandleLevel == 1);
250       if (Optional<Loc> ArgLoc = Arg.getAs<Loc>())
251         return State->getSVal(*ArgLoc).getAsSymbol();
252     }
253   }
254   return nullptr;
255 }
256 
257 void FuchsiaHandleChecker::checkPreCall(const CallEvent &Call,
258                                         CheckerContext &C) const {
259   ProgramStateRef State = C.getState();
260   const FunctionDecl *FuncDecl = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
261   if (!FuncDecl) {
262     // Unknown call, escape by value handles. They are not covered by
263     // PointerEscape callback.
264     for (unsigned Arg = 0; Arg < Call.getNumArgs(); ++Arg) {
265       if (SymbolRef Handle = Call.getArgSVal(Arg).getAsSymbol())
266         State = State->set<HStateMap>(Handle, HandleState::getEscaped());
267     }
268     C.addTransition(State);
269     return;
270   }
271 
272   for (unsigned Arg = 0; Arg < Call.getNumArgs(); ++Arg) {
273     if (Arg >= FuncDecl->getNumParams())
274       break;
275     const ParmVarDecl *PVD = FuncDecl->getParamDecl(Arg);
276     SymbolRef Handle =
277         getFuchsiaHandleSymbol(PVD->getType(), Call.getArgSVal(Arg), State);
278     if (!Handle)
279       continue;
280 
281     // Handled in checkPostCall.
282     if (hasFuchsiaAttr<ReleaseHandleAttr>(PVD) ||
283         hasFuchsiaAttr<AcquireHandleAttr>(PVD))
284       continue;
285 
286     const HandleState *HState = State->get<HStateMap>(Handle);
287     if (!HState || HState->isEscaped())
288       continue;
289 
290     if (hasFuchsiaAttr<UseHandleAttr>(PVD) || PVD->getType()->isIntegerType()) {
291       if (HState->isReleased()) {
292         reportUseAfterFree(Handle, Call.getArgSourceRange(Arg), C);
293         return;
294       }
295     }
296     if (!hasFuchsiaAttr<UseHandleAttr>(PVD) &&
297         PVD->getType()->isIntegerType()) {
298       // Working around integer by-value escapes.
299       State = State->set<HStateMap>(Handle, HandleState::getEscaped());
300     }
301   }
302   C.addTransition(State);
303 }
304 
305 void FuchsiaHandleChecker::checkPostCall(const CallEvent &Call,
306                                          CheckerContext &C) const {
307   const FunctionDecl *FuncDecl = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
308   if (!FuncDecl)
309     return;
310 
311   ProgramStateRef State = C.getState();
312 
313   std::vector<std::function<std::string(BugReport & BR)>> Notes;
314   SymbolRef ResultSymbol = nullptr;
315   if (const auto *TypeDefTy = FuncDecl->getReturnType()->getAs<TypedefType>())
316     if (TypeDefTy->getDecl()->getName() == ErrorTypeName)
317       ResultSymbol = Call.getReturnValue().getAsSymbol();
318 
319   // Function returns an open handle.
320   if (hasFuchsiaAttr<AcquireHandleAttr>(FuncDecl)) {
321     SymbolRef RetSym = Call.getReturnValue().getAsSymbol();
322     Notes.push_back([RetSym, FuncDecl](BugReport &BR) -> std::string {
323       auto *PathBR = static_cast<PathSensitiveBugReport *>(&BR);
324       if (auto IsInteresting = PathBR->getInterestingnessKind(RetSym)) {
325         std::string SBuf;
326         llvm::raw_string_ostream OS(SBuf);
327         OS << "Function '" << FuncDecl->getNameAsString()
328            << "' returns an open handle";
329         return OS.str();
330       } else
331         return "";
332     });
333     State =
334         State->set<HStateMap>(RetSym, HandleState::getMaybeAllocated(nullptr));
335   }
336 
337   for (unsigned Arg = 0; Arg < Call.getNumArgs(); ++Arg) {
338     if (Arg >= FuncDecl->getNumParams())
339       break;
340     const ParmVarDecl *PVD = FuncDecl->getParamDecl(Arg);
341     unsigned ParamDiagIdx = PVD->getFunctionScopeIndex() + 1;
342     SymbolRef Handle =
343         getFuchsiaHandleSymbol(PVD->getType(), Call.getArgSVal(Arg), State);
344     if (!Handle)
345       continue;
346 
347     const HandleState *HState = State->get<HStateMap>(Handle);
348     if (HState && HState->isEscaped())
349       continue;
350     if (hasFuchsiaAttr<ReleaseHandleAttr>(PVD)) {
351       if (HState && HState->isReleased()) {
352         reportDoubleRelease(Handle, Call.getArgSourceRange(Arg), C);
353         return;
354       } else {
355         Notes.push_back([Handle, ParamDiagIdx](BugReport &BR) -> std::string {
356           auto *PathBR = static_cast<PathSensitiveBugReport *>(&BR);
357           if (auto IsInteresting = PathBR->getInterestingnessKind(Handle)) {
358             std::string SBuf;
359             llvm::raw_string_ostream OS(SBuf);
360             OS << "Handle released through " << ParamDiagIdx
361                << llvm::getOrdinalSuffix(ParamDiagIdx) << " parameter";
362             return OS.str();
363           } else
364             return "";
365         });
366         State = State->set<HStateMap>(Handle, HandleState::getReleased());
367       }
368     } else if (hasFuchsiaAttr<AcquireHandleAttr>(PVD)) {
369       Notes.push_back([Handle, ParamDiagIdx](BugReport &BR) -> std::string {
370         auto *PathBR = static_cast<PathSensitiveBugReport *>(&BR);
371         if (auto IsInteresting = PathBR->getInterestingnessKind(Handle)) {
372           std::string SBuf;
373           llvm::raw_string_ostream OS(SBuf);
374           OS << "Handle allocated through " << ParamDiagIdx
375              << llvm::getOrdinalSuffix(ParamDiagIdx) << " parameter";
376           return OS.str();
377         } else
378           return "";
379       });
380       State = State->set<HStateMap>(
381           Handle, HandleState::getMaybeAllocated(ResultSymbol));
382     }
383   }
384   const NoteTag *T = nullptr;
385   if (!Notes.empty()) {
386     T = C.getNoteTag([this, Notes{std::move(Notes)}](
387                          PathSensitiveBugReport &BR) -> std::string {
388           if (&BR.getBugType() != &UseAfterReleaseBugType &&
389               &BR.getBugType() != &LeakBugType &&
390               &BR.getBugType() != &DoubleReleaseBugType)
391             return "";
392           for (auto &Note : Notes) {
393             std::string Text = Note(BR);
394             if (!Text.empty())
395               return Text;
396           }
397           return "";
398         });
399   }
400   C.addTransition(State, T);
401 }
402 
403 void FuchsiaHandleChecker::checkDeadSymbols(SymbolReaper &SymReaper,
404                                             CheckerContext &C) const {
405   ProgramStateRef State = C.getState();
406   SmallVector<SymbolRef, 2> LeakedSyms;
407   HStateMapTy TrackedHandles = State->get<HStateMap>();
408   for (auto &CurItem : TrackedHandles) {
409     SymbolRef ErrorSym = CurItem.second.getErrorSym();
410     // Keeping zombie handle symbols. In case the error symbol is dying later
411     // than the handle symbol we might produce spurious leak warnings (in case
412     // we find out later from the status code that the handle allocation failed
413     // in the first place).
414     if (!SymReaper.isDead(CurItem.first) ||
415         (ErrorSym && !SymReaper.isDead(ErrorSym)))
416       continue;
417     if (CurItem.second.isAllocated() || CurItem.second.maybeAllocated())
418       LeakedSyms.push_back(CurItem.first);
419     State = State->remove<HStateMap>(CurItem.first);
420   }
421 
422   ExplodedNode *N = C.getPredecessor();
423   if (!LeakedSyms.empty())
424     N = reportLeaks(LeakedSyms, C, N);
425 
426   C.addTransition(State, N);
427 }
428 
429 // Acquiring a handle is not always successful. In Fuchsia most functions
430 // return a status code that determines the status of the handle.
431 // When we split the path based on this status code we know that on one
432 // path we do have the handle and on the other path the acquire failed.
433 // This method helps avoiding false positive leak warnings on paths where
434 // the function failed.
435 // Moreover, when a handle is known to be zero (the invalid handle),
436 // we no longer can follow the symbol on the path, becaue the constant
437 // zero will be used instead of the symbol. We also do not need to release
438 // an invalid handle, so we remove the corresponding symbol from the state.
439 ProgramStateRef FuchsiaHandleChecker::evalAssume(ProgramStateRef State,
440                                                  SVal Cond,
441                                                  bool Assumption) const {
442   // TODO: add notes about successes/fails for APIs.
443   ConstraintManager &Cmr = State->getConstraintManager();
444   HStateMapTy TrackedHandles = State->get<HStateMap>();
445   for (auto &CurItem : TrackedHandles) {
446     ConditionTruthVal HandleVal = Cmr.isNull(State, CurItem.first);
447     if (HandleVal.isConstrainedTrue()) {
448       // The handle is invalid. We can no longer follow the symbol on this path.
449       State = State->remove<HStateMap>(CurItem.first);
450     }
451     SymbolRef ErrorSym = CurItem.second.getErrorSym();
452     if (!ErrorSym)
453       continue;
454     ConditionTruthVal ErrorVal = Cmr.isNull(State, ErrorSym);
455     if (ErrorVal.isConstrainedTrue()) {
456       // Allocation succeeded.
457       if (CurItem.second.maybeAllocated())
458         State = State->set<HStateMap>(
459             CurItem.first, HandleState::getAllocated(State, CurItem.second));
460     } else if (ErrorVal.isConstrainedFalse()) {
461       // Allocation failed.
462       if (CurItem.second.maybeAllocated())
463         State = State->remove<HStateMap>(CurItem.first);
464     }
465   }
466   return State;
467 }
468 
469 ProgramStateRef FuchsiaHandleChecker::checkPointerEscape(
470     ProgramStateRef State, const InvalidatedSymbols &Escaped,
471     const CallEvent *Call, PointerEscapeKind Kind) const {
472   const FunctionDecl *FuncDecl =
473       Call ? dyn_cast_or_null<FunctionDecl>(Call->getDecl()) : nullptr;
474 
475   llvm::DenseSet<SymbolRef> UnEscaped;
476   // Not all calls should escape our symbols.
477   if (FuncDecl &&
478       (Kind == PSK_DirectEscapeOnCall || Kind == PSK_IndirectEscapeOnCall ||
479        Kind == PSK_EscapeOutParameters)) {
480     for (unsigned Arg = 0; Arg < Call->getNumArgs(); ++Arg) {
481       if (Arg >= FuncDecl->getNumParams())
482         break;
483       const ParmVarDecl *PVD = FuncDecl->getParamDecl(Arg);
484       SymbolRef Handle =
485           getFuchsiaHandleSymbol(PVD->getType(), Call->getArgSVal(Arg), State);
486       if (!Handle)
487         continue;
488       if (hasFuchsiaAttr<UseHandleAttr>(PVD) ||
489           hasFuchsiaAttr<ReleaseHandleAttr>(PVD))
490         UnEscaped.insert(Handle);
491     }
492   }
493 
494   // For out params, we have to deal with derived symbols. See
495   // MacOSKeychainAPIChecker for details.
496   for (auto I : State->get<HStateMap>()) {
497     if (Escaped.count(I.first) && !UnEscaped.count(I.first))
498       State = State->set<HStateMap>(I.first, HandleState::getEscaped());
499     if (const auto *SD = dyn_cast<SymbolDerived>(I.first)) {
500       auto ParentSym = SD->getParentSymbol();
501       if (Escaped.count(ParentSym))
502         State = State->set<HStateMap>(I.first, HandleState::getEscaped());
503     }
504   }
505 
506   return State;
507 }
508 
509 ExplodedNode *
510 FuchsiaHandleChecker::reportLeaks(ArrayRef<SymbolRef> LeakedHandles,
511                                   CheckerContext &C, ExplodedNode *Pred) const {
512   ExplodedNode *ErrNode = C.generateNonFatalErrorNode(C.getState(), Pred);
513   for (SymbolRef LeakedHandle : LeakedHandles) {
514     reportBug(LeakedHandle, ErrNode, C, nullptr, LeakBugType,
515               "Potential leak of handle");
516   }
517   return ErrNode;
518 }
519 
520 void FuchsiaHandleChecker::reportDoubleRelease(SymbolRef HandleSym,
521                                                const SourceRange &Range,
522                                                CheckerContext &C) const {
523   ExplodedNode *ErrNode = C.generateErrorNode(C.getState());
524   reportBug(HandleSym, ErrNode, C, &Range, DoubleReleaseBugType,
525             "Releasing a previously released handle");
526 }
527 
528 void FuchsiaHandleChecker::reportUseAfterFree(SymbolRef HandleSym,
529                                               const SourceRange &Range,
530                                               CheckerContext &C) const {
531   ExplodedNode *ErrNode = C.generateErrorNode(C.getState());
532   reportBug(HandleSym, ErrNode, C, &Range, UseAfterReleaseBugType,
533             "Using a previously released handle");
534 }
535 
536 void FuchsiaHandleChecker::reportBug(SymbolRef Sym, ExplodedNode *ErrorNode,
537                                      CheckerContext &C,
538                                      const SourceRange *Range,
539                                      const BugType &Type, StringRef Msg) const {
540   if (!ErrorNode)
541     return;
542 
543   std::unique_ptr<PathSensitiveBugReport> R;
544   if (Type.isSuppressOnSink()) {
545     const ExplodedNode *AcquireNode = getAcquireSite(ErrorNode, Sym, C);
546     if (AcquireNode) {
547       PathDiagnosticLocation LocUsedForUniqueing =
548           PathDiagnosticLocation::createBegin(
549               AcquireNode->getStmtForDiagnostics(), C.getSourceManager(),
550               AcquireNode->getLocationContext());
551 
552       R = std::make_unique<PathSensitiveBugReport>(
553           Type, Msg, ErrorNode, LocUsedForUniqueing,
554           AcquireNode->getLocationContext()->getDecl());
555     }
556   }
557   if (!R)
558     R = std::make_unique<PathSensitiveBugReport>(Type, Msg, ErrorNode);
559   if (Range)
560     R->addRange(*Range);
561   R->markInteresting(Sym);
562   C.emitReport(std::move(R));
563 }
564 
565 void ento::registerFuchsiaHandleChecker(CheckerManager &mgr) {
566   mgr.registerChecker<FuchsiaHandleChecker>();
567 }
568 
569 bool ento::shouldRegisterFuchsiaHandleChecker(const CheckerManager &mgr) {
570   return true;
571 }
572 
573 void FuchsiaHandleChecker::printState(raw_ostream &Out, ProgramStateRef State,
574                                       const char *NL, const char *Sep) const {
575 
576   HStateMapTy StateMap = State->get<HStateMap>();
577 
578   if (!StateMap.isEmpty()) {
579     Out << Sep << "FuchsiaHandleChecker :" << NL;
580     for (HStateMapTy::iterator I = StateMap.begin(), E = StateMap.end(); I != E;
581          ++I) {
582       I.getKey()->dumpToStream(Out);
583       Out << " : ";
584       I.getData().dump(Out);
585       Out << NL;
586     }
587   }
588 }
589