xref: /minix3/external/bsd/llvm/dist/clang/lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- SimpleStreamChecker.cpp -----------------------------------------*- C++ -*--//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // Defines a checker for proper use of fopen/fclose APIs.
11f4a2713aSLionel Sambuc //   - If a file has been closed with fclose, it should not be accessed again.
12f4a2713aSLionel Sambuc //   Accessing a closed file results in undefined behavior.
13f4a2713aSLionel Sambuc //   - If a file was opened with fopen, it must be closed with fclose before
14f4a2713aSLionel Sambuc //   the execution ends. Failing to do so results in a resource leak.
15f4a2713aSLionel Sambuc //
16f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
19f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
21f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc using namespace ento;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc namespace {
28f4a2713aSLionel Sambuc typedef SmallVector<SymbolRef, 2> SymbolVector;
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc struct StreamState {
31f4a2713aSLionel Sambuc private:
32f4a2713aSLionel Sambuc   enum Kind { Opened, Closed } K;
StreamState__anondeb7834c0111::StreamState33f4a2713aSLionel Sambuc   StreamState(Kind InK) : K(InK) { }
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc public:
isOpened__anondeb7834c0111::StreamState36f4a2713aSLionel Sambuc   bool isOpened() const { return K == Opened; }
isClosed__anondeb7834c0111::StreamState37f4a2713aSLionel Sambuc   bool isClosed() const { return K == Closed; }
38f4a2713aSLionel Sambuc 
getOpened__anondeb7834c0111::StreamState39f4a2713aSLionel Sambuc   static StreamState getOpened() { return StreamState(Opened); }
getClosed__anondeb7834c0111::StreamState40f4a2713aSLionel Sambuc   static StreamState getClosed() { return StreamState(Closed); }
41f4a2713aSLionel Sambuc 
operator ==__anondeb7834c0111::StreamState42f4a2713aSLionel Sambuc   bool operator==(const StreamState &X) const {
43f4a2713aSLionel Sambuc     return K == X.K;
44f4a2713aSLionel Sambuc   }
Profile__anondeb7834c0111::StreamState45f4a2713aSLionel Sambuc   void Profile(llvm::FoldingSetNodeID &ID) const {
46f4a2713aSLionel Sambuc     ID.AddInteger(K);
47f4a2713aSLionel Sambuc   }
48f4a2713aSLionel Sambuc };
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc class SimpleStreamChecker : public Checker<check::PostCall,
51f4a2713aSLionel Sambuc                                            check::PreCall,
52f4a2713aSLionel Sambuc                                            check::DeadSymbols,
53f4a2713aSLionel Sambuc                                            check::PointerEscape> {
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc   mutable IdentifierInfo *IIfopen, *IIfclose;
56f4a2713aSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc   std::unique_ptr<BugType> DoubleCloseBugType;
58*0a6a1f1dSLionel Sambuc   std::unique_ptr<BugType> LeakBugType;
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc   void initIdentifierInfo(ASTContext &Ctx) const;
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   void reportDoubleClose(SymbolRef FileDescSym,
63f4a2713aSLionel Sambuc                          const CallEvent &Call,
64f4a2713aSLionel Sambuc                          CheckerContext &C) const;
65f4a2713aSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc   void reportLeaks(ArrayRef<SymbolRef> LeakedStreams, CheckerContext &C,
67f4a2713aSLionel Sambuc                    ExplodedNode *ErrNode) const;
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc   bool guaranteedNotToCloseFile(const CallEvent &Call) const;
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc public:
72f4a2713aSLionel Sambuc   SimpleStreamChecker();
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   /// Process fopen.
75f4a2713aSLionel Sambuc   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
76f4a2713aSLionel Sambuc   /// Process fclose.
77f4a2713aSLionel Sambuc   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc   /// Stop tracking addresses which escape.
82f4a2713aSLionel Sambuc   ProgramStateRef checkPointerEscape(ProgramStateRef State,
83f4a2713aSLionel Sambuc                                     const InvalidatedSymbols &Escaped,
84f4a2713aSLionel Sambuc                                     const CallEvent *Call,
85f4a2713aSLionel Sambuc                                     PointerEscapeKind Kind) const;
86f4a2713aSLionel Sambuc };
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc } // end anonymous namespace
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc /// The state of the checker is a map from tracked stream symbols to their
91f4a2713aSLionel Sambuc /// state. Let's store it in the ProgramState.
92f4a2713aSLionel Sambuc REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc namespace {
95f4a2713aSLionel Sambuc class StopTrackingCallback : public SymbolVisitor {
96f4a2713aSLionel Sambuc   ProgramStateRef state;
97f4a2713aSLionel Sambuc public:
StopTrackingCallback(ProgramStateRef st)98f4a2713aSLionel Sambuc   StopTrackingCallback(ProgramStateRef st) : state(st) {}
getState() const99f4a2713aSLionel Sambuc   ProgramStateRef getState() const { return state; }
100f4a2713aSLionel Sambuc 
VisitSymbol(SymbolRef sym)101*0a6a1f1dSLionel Sambuc   bool VisitSymbol(SymbolRef sym) override {
102f4a2713aSLionel Sambuc     state = state->remove<StreamMap>(sym);
103f4a2713aSLionel Sambuc     return true;
104f4a2713aSLionel Sambuc   }
105f4a2713aSLionel Sambuc };
106f4a2713aSLionel Sambuc } // end anonymous namespace
107f4a2713aSLionel Sambuc 
SimpleStreamChecker()108*0a6a1f1dSLionel Sambuc SimpleStreamChecker::SimpleStreamChecker()
109*0a6a1f1dSLionel Sambuc     : IIfopen(nullptr), IIfclose(nullptr) {
110f4a2713aSLionel Sambuc   // Initialize the bug types.
111*0a6a1f1dSLionel Sambuc   DoubleCloseBugType.reset(
112*0a6a1f1dSLionel Sambuc       new BugType(this, "Double fclose", "Unix Stream API Error"));
113f4a2713aSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc   LeakBugType.reset(
115*0a6a1f1dSLionel Sambuc       new BugType(this, "Resource Leak", "Unix Stream API Error"));
116f4a2713aSLionel Sambuc   // Sinks are higher importance bugs as well as calls to assert() or exit(0).
117f4a2713aSLionel Sambuc   LeakBugType->setSuppressOnSink(true);
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc 
checkPostCall(const CallEvent & Call,CheckerContext & C) const120f4a2713aSLionel Sambuc void SimpleStreamChecker::checkPostCall(const CallEvent &Call,
121f4a2713aSLionel Sambuc                                         CheckerContext &C) const {
122f4a2713aSLionel Sambuc   initIdentifierInfo(C.getASTContext());
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc   if (!Call.isGlobalCFunction())
125f4a2713aSLionel Sambuc     return;
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc   if (Call.getCalleeIdentifier() != IIfopen)
128f4a2713aSLionel Sambuc     return;
129f4a2713aSLionel Sambuc 
130f4a2713aSLionel Sambuc   // Get the symbolic value corresponding to the file handle.
131f4a2713aSLionel Sambuc   SymbolRef FileDesc = Call.getReturnValue().getAsSymbol();
132f4a2713aSLionel Sambuc   if (!FileDesc)
133f4a2713aSLionel Sambuc     return;
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   // Generate the next transition (an edge in the exploded graph).
136f4a2713aSLionel Sambuc   ProgramStateRef State = C.getState();
137f4a2713aSLionel Sambuc   State = State->set<StreamMap>(FileDesc, StreamState::getOpened());
138f4a2713aSLionel Sambuc   C.addTransition(State);
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc 
checkPreCall(const CallEvent & Call,CheckerContext & C) const141f4a2713aSLionel Sambuc void SimpleStreamChecker::checkPreCall(const CallEvent &Call,
142f4a2713aSLionel Sambuc                                        CheckerContext &C) const {
143f4a2713aSLionel Sambuc   initIdentifierInfo(C.getASTContext());
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   if (!Call.isGlobalCFunction())
146f4a2713aSLionel Sambuc     return;
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc   if (Call.getCalleeIdentifier() != IIfclose)
149f4a2713aSLionel Sambuc     return;
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   if (Call.getNumArgs() != 1)
152f4a2713aSLionel Sambuc     return;
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc   // Get the symbolic value corresponding to the file handle.
155f4a2713aSLionel Sambuc   SymbolRef FileDesc = Call.getArgSVal(0).getAsSymbol();
156f4a2713aSLionel Sambuc   if (!FileDesc)
157f4a2713aSLionel Sambuc     return;
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   // Check if the stream has already been closed.
160f4a2713aSLionel Sambuc   ProgramStateRef State = C.getState();
161f4a2713aSLionel Sambuc   const StreamState *SS = State->get<StreamMap>(FileDesc);
162f4a2713aSLionel Sambuc   if (SS && SS->isClosed()) {
163f4a2713aSLionel Sambuc     reportDoubleClose(FileDesc, Call, C);
164f4a2713aSLionel Sambuc     return;
165f4a2713aSLionel Sambuc   }
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc   // Generate the next transition, in which the stream is closed.
168f4a2713aSLionel Sambuc   State = State->set<StreamMap>(FileDesc, StreamState::getClosed());
169f4a2713aSLionel Sambuc   C.addTransition(State);
170f4a2713aSLionel Sambuc }
171f4a2713aSLionel Sambuc 
isLeaked(SymbolRef Sym,const StreamState & SS,bool IsSymDead,ProgramStateRef State)172f4a2713aSLionel Sambuc static bool isLeaked(SymbolRef Sym, const StreamState &SS,
173f4a2713aSLionel Sambuc                      bool IsSymDead, ProgramStateRef State) {
174f4a2713aSLionel Sambuc   if (IsSymDead && SS.isOpened()) {
175f4a2713aSLionel Sambuc     // If a symbol is NULL, assume that fopen failed on this path.
176f4a2713aSLionel Sambuc     // A symbol should only be considered leaked if it is non-null.
177f4a2713aSLionel Sambuc     ConstraintManager &CMgr = State->getConstraintManager();
178f4a2713aSLionel Sambuc     ConditionTruthVal OpenFailed = CMgr.isNull(State, Sym);
179f4a2713aSLionel Sambuc     return !OpenFailed.isConstrainedTrue();
180f4a2713aSLionel Sambuc   }
181f4a2713aSLionel Sambuc   return false;
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc 
checkDeadSymbols(SymbolReaper & SymReaper,CheckerContext & C) const184f4a2713aSLionel Sambuc void SimpleStreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
185f4a2713aSLionel Sambuc                                            CheckerContext &C) const {
186f4a2713aSLionel Sambuc   ProgramStateRef State = C.getState();
187f4a2713aSLionel Sambuc   SymbolVector LeakedStreams;
188f4a2713aSLionel Sambuc   StreamMapTy TrackedStreams = State->get<StreamMap>();
189f4a2713aSLionel Sambuc   for (StreamMapTy::iterator I = TrackedStreams.begin(),
190f4a2713aSLionel Sambuc                              E = TrackedStreams.end(); I != E; ++I) {
191f4a2713aSLionel Sambuc     SymbolRef Sym = I->first;
192f4a2713aSLionel Sambuc     bool IsSymDead = SymReaper.isDead(Sym);
193f4a2713aSLionel Sambuc 
194f4a2713aSLionel Sambuc     // Collect leaked symbols.
195f4a2713aSLionel Sambuc     if (isLeaked(Sym, I->second, IsSymDead, State))
196f4a2713aSLionel Sambuc       LeakedStreams.push_back(Sym);
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc     // Remove the dead symbol from the streams map.
199f4a2713aSLionel Sambuc     if (IsSymDead)
200f4a2713aSLionel Sambuc       State = State->remove<StreamMap>(Sym);
201f4a2713aSLionel Sambuc   }
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc   ExplodedNode *N = C.addTransition(State);
204f4a2713aSLionel Sambuc   reportLeaks(LeakedStreams, C, N);
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc 
reportDoubleClose(SymbolRef FileDescSym,const CallEvent & Call,CheckerContext & C) const207f4a2713aSLionel Sambuc void SimpleStreamChecker::reportDoubleClose(SymbolRef FileDescSym,
208f4a2713aSLionel Sambuc                                             const CallEvent &Call,
209f4a2713aSLionel Sambuc                                             CheckerContext &C) const {
210f4a2713aSLionel Sambuc   // We reached a bug, stop exploring the path here by generating a sink.
211f4a2713aSLionel Sambuc   ExplodedNode *ErrNode = C.generateSink();
212f4a2713aSLionel Sambuc   // If we've already reached this node on another path, return.
213f4a2713aSLionel Sambuc   if (!ErrNode)
214f4a2713aSLionel Sambuc     return;
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc   // Generate the report.
217f4a2713aSLionel Sambuc   BugReport *R = new BugReport(*DoubleCloseBugType,
218f4a2713aSLionel Sambuc       "Closing a previously closed file stream", ErrNode);
219f4a2713aSLionel Sambuc   R->addRange(Call.getSourceRange());
220f4a2713aSLionel Sambuc   R->markInteresting(FileDescSym);
221f4a2713aSLionel Sambuc   C.emitReport(R);
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc 
reportLeaks(ArrayRef<SymbolRef> LeakedStreams,CheckerContext & C,ExplodedNode * ErrNode) const224*0a6a1f1dSLionel Sambuc void SimpleStreamChecker::reportLeaks(ArrayRef<SymbolRef> LeakedStreams,
225f4a2713aSLionel Sambuc                                       CheckerContext &C,
226f4a2713aSLionel Sambuc                                       ExplodedNode *ErrNode) const {
227f4a2713aSLionel Sambuc   // Attach bug reports to the leak node.
228f4a2713aSLionel Sambuc   // TODO: Identify the leaked file descriptor.
229*0a6a1f1dSLionel Sambuc   for (SymbolRef LeakedStream : LeakedStreams) {
230f4a2713aSLionel Sambuc     BugReport *R = new BugReport(*LeakBugType,
231f4a2713aSLionel Sambuc         "Opened file is never closed; potential resource leak", ErrNode);
232*0a6a1f1dSLionel Sambuc     R->markInteresting(LeakedStream);
233f4a2713aSLionel Sambuc     C.emitReport(R);
234f4a2713aSLionel Sambuc   }
235f4a2713aSLionel Sambuc }
236f4a2713aSLionel Sambuc 
guaranteedNotToCloseFile(const CallEvent & Call) const237f4a2713aSLionel Sambuc bool SimpleStreamChecker::guaranteedNotToCloseFile(const CallEvent &Call) const{
238f4a2713aSLionel Sambuc   // If it's not in a system header, assume it might close a file.
239f4a2713aSLionel Sambuc   if (!Call.isInSystemHeader())
240f4a2713aSLionel Sambuc     return false;
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc   // Handle cases where we know a buffer's /address/ can escape.
243f4a2713aSLionel Sambuc   if (Call.argumentsMayEscape())
244f4a2713aSLionel Sambuc     return false;
245f4a2713aSLionel Sambuc 
246f4a2713aSLionel Sambuc   // Note, even though fclose closes the file, we do not list it here
247f4a2713aSLionel Sambuc   // since the checker is modeling the call.
248f4a2713aSLionel Sambuc 
249f4a2713aSLionel Sambuc   return true;
250f4a2713aSLionel Sambuc }
251f4a2713aSLionel Sambuc 
252f4a2713aSLionel Sambuc // If the pointer we are tracking escaped, do not track the symbol as
253f4a2713aSLionel Sambuc // we cannot reason about it anymore.
254f4a2713aSLionel Sambuc ProgramStateRef
checkPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const255f4a2713aSLionel Sambuc SimpleStreamChecker::checkPointerEscape(ProgramStateRef State,
256f4a2713aSLionel Sambuc                                         const InvalidatedSymbols &Escaped,
257f4a2713aSLionel Sambuc                                         const CallEvent *Call,
258f4a2713aSLionel Sambuc                                         PointerEscapeKind Kind) const {
259f4a2713aSLionel Sambuc   // If we know that the call cannot close a file, there is nothing to do.
260f4a2713aSLionel Sambuc   if (Kind == PSK_DirectEscapeOnCall && guaranteedNotToCloseFile(*Call)) {
261f4a2713aSLionel Sambuc     return State;
262f4a2713aSLionel Sambuc   }
263f4a2713aSLionel Sambuc 
264f4a2713aSLionel Sambuc   for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
265f4a2713aSLionel Sambuc                                           E = Escaped.end();
266f4a2713aSLionel Sambuc                                           I != E; ++I) {
267f4a2713aSLionel Sambuc     SymbolRef Sym = *I;
268f4a2713aSLionel Sambuc 
269f4a2713aSLionel Sambuc     // The symbol escaped. Optimistically, assume that the corresponding file
270f4a2713aSLionel Sambuc     // handle will be closed somewhere else.
271f4a2713aSLionel Sambuc     State = State->remove<StreamMap>(Sym);
272f4a2713aSLionel Sambuc   }
273f4a2713aSLionel Sambuc   return State;
274f4a2713aSLionel Sambuc }
275f4a2713aSLionel Sambuc 
initIdentifierInfo(ASTContext & Ctx) const276f4a2713aSLionel Sambuc void SimpleStreamChecker::initIdentifierInfo(ASTContext &Ctx) const {
277f4a2713aSLionel Sambuc   if (IIfopen)
278f4a2713aSLionel Sambuc     return;
279f4a2713aSLionel Sambuc   IIfopen = &Ctx.Idents.get("fopen");
280f4a2713aSLionel Sambuc   IIfclose = &Ctx.Idents.get("fclose");
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc 
registerSimpleStreamChecker(CheckerManager & mgr)283f4a2713aSLionel Sambuc void ento::registerSimpleStreamChecker(CheckerManager &mgr) {
284f4a2713aSLionel Sambuc   mgr.registerChecker<SimpleStreamChecker>();
285f4a2713aSLionel Sambuc }
286