xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp (revision efa8b6e884aa4c3ad74ab44489352c0f80ac9741)
1 //===-- StreamChecker.cpp -----------------------------------------*- 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 file defines checkers that model and check stream handling functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
14 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
15 #include "clang/StaticAnalyzer/Core/Checker.h"
16 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
22 #include <functional>
23 
24 using namespace clang;
25 using namespace ento;
26 using namespace std::placeholders;
27 
28 namespace {
29 
30 struct FnDescription;
31 
32 /// State of the stream error flags.
33 /// Sometimes it is not known to the checker what error flags are set.
34 /// This is indicated by setting more than one flag to true.
35 /// This is an optimization to avoid state splits.
36 /// A stream can either be in FEOF or FERROR but not both at the same time.
37 /// Multiple flags are set to handle the corresponding states together.
38 struct StreamErrorState {
39   /// The stream can be in state where none of the error flags set.
40   bool NoError = true;
41   /// The stream can be in state where the EOF indicator is set.
42   bool FEof = false;
43   /// The stream can be in state where the error indicator is set.
44   bool FError = false;
45 
46   bool isNoError() const { return NoError && !FEof && !FError; }
47   bool isFEof() const { return !NoError && FEof && !FError; }
48   bool isFError() const { return !NoError && !FEof && FError; }
49 
50   bool operator==(const StreamErrorState &ES) const {
51     return NoError == ES.NoError && FEof == ES.FEof && FError == ES.FError;
52   }
53 
54   bool operator!=(const StreamErrorState &ES) const { return !(*this == ES); }
55 
56   StreamErrorState operator|(const StreamErrorState &E) const {
57     return {NoError || E.NoError, FEof || E.FEof, FError || E.FError};
58   }
59 
60   StreamErrorState operator&(const StreamErrorState &E) const {
61     return {NoError && E.NoError, FEof && E.FEof, FError && E.FError};
62   }
63 
64   StreamErrorState operator~() const { return {!NoError, !FEof, !FError}; }
65 
66   /// Returns if the StreamErrorState is a valid object.
67   operator bool() const { return NoError || FEof || FError; }
68 
69   void Profile(llvm::FoldingSetNodeID &ID) const {
70     ID.AddBoolean(NoError);
71     ID.AddBoolean(FEof);
72     ID.AddBoolean(FError);
73   }
74 };
75 
76 const StreamErrorState ErrorNone{true, false, false};
77 const StreamErrorState ErrorFEof{false, true, false};
78 const StreamErrorState ErrorFError{false, false, true};
79 
80 /// Full state information about a stream pointer.
81 struct StreamState {
82   /// The last file operation called in the stream.
83   const FnDescription *LastOperation;
84 
85   /// State of a stream symbol.
86   /// FIXME: We need maybe an "escaped" state later.
87   enum KindTy {
88     Opened, /// Stream is opened.
89     Closed, /// Closed stream (an invalid stream pointer after it was closed).
90     OpenFailed /// The last open operation has failed.
91   } State;
92 
93   /// State of the error flags.
94   /// Ignored in non-opened stream state but must be NoError.
95   StreamErrorState const ErrorState;
96 
97   /// Indicate if the file has an "indeterminate file position indicator".
98   /// This can be set at a failing read or write or seek operation.
99   /// If it is set no more read or write is allowed.
100   /// This value is not dependent on the stream error flags:
101   /// The error flag may be cleared with `clearerr` but the file position
102   /// remains still indeterminate.
103   /// This value applies to all error states in ErrorState except FEOF.
104   /// An EOF+indeterminate state is the same as EOF state.
105   bool const FilePositionIndeterminate = false;
106 
107   StreamState(const FnDescription *L, KindTy S, const StreamErrorState &ES,
108               bool IsFilePositionIndeterminate)
109       : LastOperation(L), State(S), ErrorState(ES),
110         FilePositionIndeterminate(IsFilePositionIndeterminate) {
111     assert((!ES.isFEof() || !IsFilePositionIndeterminate) &&
112            "FilePositionIndeterminate should be false in FEof case.");
113     assert((State == Opened || ErrorState.isNoError()) &&
114            "ErrorState should be None in non-opened stream state.");
115   }
116 
117   bool isOpened() const { return State == Opened; }
118   bool isClosed() const { return State == Closed; }
119   bool isOpenFailed() const { return State == OpenFailed; }
120 
121   bool operator==(const StreamState &X) const {
122     // In not opened state error state should always NoError, so comparison
123     // here is no problem.
124     return LastOperation == X.LastOperation && State == X.State &&
125            ErrorState == X.ErrorState &&
126            FilePositionIndeterminate == X.FilePositionIndeterminate;
127   }
128 
129   static StreamState getOpened(const FnDescription *L,
130                                const StreamErrorState &ES = ErrorNone,
131                                bool IsFilePositionIndeterminate = false) {
132     return StreamState{L, Opened, ES, IsFilePositionIndeterminate};
133   }
134   static StreamState getClosed(const FnDescription *L) {
135     return StreamState{L, Closed, {}, false};
136   }
137   static StreamState getOpenFailed(const FnDescription *L) {
138     return StreamState{L, OpenFailed, {}, false};
139   }
140 
141   void Profile(llvm::FoldingSetNodeID &ID) const {
142     ID.AddPointer(LastOperation);
143     ID.AddInteger(State);
144     ID.AddInteger(ErrorState);
145     ID.AddBoolean(FilePositionIndeterminate);
146   }
147 };
148 
149 class StreamChecker;
150 using FnCheck = std::function<void(const StreamChecker *, const FnDescription *,
151                                    const CallEvent &, CheckerContext &)>;
152 
153 using ArgNoTy = unsigned int;
154 static const ArgNoTy ArgNone = std::numeric_limits<ArgNoTy>::max();
155 
156 struct FnDescription {
157   FnCheck PreFn;
158   FnCheck EvalFn;
159   ArgNoTy StreamArgNo;
160 };
161 
162 /// Get the value of the stream argument out of the passed call event.
163 /// The call should contain a function that is described by Desc.
164 SVal getStreamArg(const FnDescription *Desc, const CallEvent &Call) {
165   assert(Desc && Desc->StreamArgNo != ArgNone &&
166          "Try to get a non-existing stream argument.");
167   return Call.getArgSVal(Desc->StreamArgNo);
168 }
169 
170 /// Create a conjured symbol return value for a call expression.
171 DefinedSVal makeRetVal(CheckerContext &C, const CallExpr *CE) {
172   assert(CE && "Expecting a call expression.");
173 
174   const LocationContext *LCtx = C.getLocationContext();
175   return C.getSValBuilder()
176       .conjureSymbolVal(nullptr, CE, LCtx, C.blockCount())
177       .castAs<DefinedSVal>();
178 }
179 
180 ProgramStateRef bindAndAssumeTrue(ProgramStateRef State, CheckerContext &C,
181                                   const CallExpr *CE) {
182   DefinedSVal RetVal = makeRetVal(C, CE);
183   State = State->BindExpr(CE, C.getLocationContext(), RetVal);
184   State = State->assume(RetVal, true);
185   assert(State && "Assumption on new value should not fail.");
186   return State;
187 }
188 
189 ProgramStateRef bindInt(uint64_t Value, ProgramStateRef State,
190                         CheckerContext &C, const CallExpr *CE) {
191   State = State->BindExpr(CE, C.getLocationContext(),
192                           C.getSValBuilder().makeIntVal(Value, false));
193   return State;
194 }
195 
196 class StreamChecker : public Checker<check::PreCall, eval::Call,
197                                      check::DeadSymbols, check::PointerEscape> {
198   BuiltinBug BT_FileNull{this, "NULL stream pointer",
199                          "Stream pointer might be NULL."};
200   BuiltinBug BT_UseAfterClose{
201       this, "Closed stream",
202       "Stream might be already closed. Causes undefined behaviour."};
203   BuiltinBug BT_UseAfterOpenFailed{this, "Invalid stream",
204                                    "Stream might be invalid after "
205                                    "(re-)opening it has failed. "
206                                    "Can cause undefined behaviour."};
207   BuiltinBug BT_IndeterminatePosition{
208       this, "Invalid stream state",
209       "File position of the stream might be 'indeterminate' "
210       "after a failed operation. "
211       "Can cause undefined behavior."};
212   BuiltinBug BT_IllegalWhence{this, "Illegal whence argument",
213                               "The whence argument to fseek() should be "
214                               "SEEK_SET, SEEK_END, or SEEK_CUR."};
215   BuiltinBug BT_StreamEof{this, "Stream already in EOF",
216                           "Read function called when stream is in EOF state. "
217                           "Function has no effect."};
218   BuiltinBug BT_ResourceLeak{
219       this, "Resource Leak",
220       "Opened File never closed. Potential Resource leak."};
221 
222 public:
223   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
224   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
225   void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
226   ProgramStateRef checkPointerEscape(ProgramStateRef State,
227                                      const InvalidatedSymbols &Escaped,
228                                      const CallEvent *Call,
229                                      PointerEscapeKind Kind) const;
230 
231   /// If true, evaluate special testing stream functions.
232   bool TestMode = false;
233 
234 private:
235   CallDescriptionMap<FnDescription> FnDescriptions = {
236       {{"fopen"}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
237       {{"freopen", 3},
238        {&StreamChecker::preFreopen, &StreamChecker::evalFreopen, 2}},
239       {{"tmpfile"}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
240       {{"fclose", 1},
241        {&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
242       {{"fread", 4},
243        {&StreamChecker::preFread,
244         std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, true), 3}},
245       {{"fwrite", 4},
246        {&StreamChecker::preFwrite,
247         std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, false), 3}},
248       {{"fseek", 3}, {&StreamChecker::preFseek, &StreamChecker::evalFseek, 0}},
249       {{"ftell", 1}, {&StreamChecker::preDefault, nullptr, 0}},
250       {{"rewind", 1}, {&StreamChecker::preDefault, nullptr, 0}},
251       {{"fgetpos", 2}, {&StreamChecker::preDefault, nullptr, 0}},
252       {{"fsetpos", 2}, {&StreamChecker::preDefault, nullptr, 0}},
253       {{"clearerr", 1},
254        {&StreamChecker::preDefault, &StreamChecker::evalClearerr, 0}},
255       {{"feof", 1},
256        {&StreamChecker::preDefault,
257         std::bind(&StreamChecker::evalFeofFerror, _1, _2, _3, _4, ErrorFEof),
258         0}},
259       {{"ferror", 1},
260        {&StreamChecker::preDefault,
261         std::bind(&StreamChecker::evalFeofFerror, _1, _2, _3, _4, ErrorFError),
262         0}},
263       {{"fileno", 1}, {&StreamChecker::preDefault, nullptr, 0}},
264   };
265 
266   CallDescriptionMap<FnDescription> FnTestDescriptions = {
267       {{"StreamTesterChecker_make_feof_stream", 1},
268        {nullptr,
269         std::bind(&StreamChecker::evalSetFeofFerror, _1, _2, _3, _4, ErrorFEof),
270         0}},
271       {{"StreamTesterChecker_make_ferror_stream", 1},
272        {nullptr,
273         std::bind(&StreamChecker::evalSetFeofFerror, _1, _2, _3, _4,
274                   ErrorFError),
275         0}},
276   };
277 
278   void evalFopen(const FnDescription *Desc, const CallEvent &Call,
279                  CheckerContext &C) const;
280 
281   void preFreopen(const FnDescription *Desc, const CallEvent &Call,
282                   CheckerContext &C) const;
283   void evalFreopen(const FnDescription *Desc, const CallEvent &Call,
284                    CheckerContext &C) const;
285 
286   void evalFclose(const FnDescription *Desc, const CallEvent &Call,
287                   CheckerContext &C) const;
288 
289   void preFread(const FnDescription *Desc, const CallEvent &Call,
290                 CheckerContext &C) const;
291 
292   void preFwrite(const FnDescription *Desc, const CallEvent &Call,
293                  CheckerContext &C) const;
294 
295   void evalFreadFwrite(const FnDescription *Desc, const CallEvent &Call,
296                        CheckerContext &C, bool IsFread) const;
297 
298   void preFseek(const FnDescription *Desc, const CallEvent &Call,
299                 CheckerContext &C) const;
300   void evalFseek(const FnDescription *Desc, const CallEvent &Call,
301                  CheckerContext &C) const;
302 
303   void preDefault(const FnDescription *Desc, const CallEvent &Call,
304                   CheckerContext &C) const;
305 
306   void evalClearerr(const FnDescription *Desc, const CallEvent &Call,
307                     CheckerContext &C) const;
308 
309   void evalFeofFerror(const FnDescription *Desc, const CallEvent &Call,
310                       CheckerContext &C,
311                       const StreamErrorState &ErrorKind) const;
312 
313   void evalSetFeofFerror(const FnDescription *Desc, const CallEvent &Call,
314                          CheckerContext &C,
315                          const StreamErrorState &ErrorKind) const;
316 
317   /// Check that the stream (in StreamVal) is not NULL.
318   /// If it can only be NULL a fatal error is emitted and nullptr returned.
319   /// Otherwise the return value is a new state where the stream is constrained
320   /// to be non-null.
321   ProgramStateRef ensureStreamNonNull(SVal StreamVal, CheckerContext &C,
322                                       ProgramStateRef State) const;
323 
324   /// Check that the stream is the opened state.
325   /// If the stream is known to be not opened an error is generated
326   /// and nullptr returned, otherwise the original state is returned.
327   ProgramStateRef ensureStreamOpened(SVal StreamVal, CheckerContext &C,
328                                      ProgramStateRef State) const;
329 
330   /// Check that the stream has not an invalid ("indeterminate") file position,
331   /// generate warning for it.
332   /// (EOF is not an invalid position.)
333   /// The returned state can be nullptr if a fatal error was generated.
334   /// It can return non-null state if the stream has not an invalid position or
335   /// there is execution path with non-invalid position.
336   ProgramStateRef
337   ensureNoFilePositionIndeterminate(SVal StreamVal, CheckerContext &C,
338                                     ProgramStateRef State) const;
339 
340   /// Check the legality of the 'whence' argument of 'fseek'.
341   /// Generate error and return nullptr if it is found to be illegal.
342   /// Otherwise returns the state.
343   /// (State is not changed here because the "whence" value is already known.)
344   ProgramStateRef ensureFseekWhenceCorrect(SVal WhenceVal, CheckerContext &C,
345                                            ProgramStateRef State) const;
346 
347   /// Generate warning about stream in EOF state.
348   /// There will be always a state transition into the passed State,
349   /// by the new non-fatal error node or (if failed) a normal transition,
350   /// to ensure uniform handling.
351   void reportFEofWarning(CheckerContext &C, ProgramStateRef State) const;
352 
353   /// Find the description data of the function called by a call event.
354   /// Returns nullptr if no function is recognized.
355   const FnDescription *lookupFn(const CallEvent &Call) const {
356     // Recognize "global C functions" with only integral or pointer arguments
357     // (and matching name) as stream functions.
358     if (!Call.isGlobalCFunction())
359       return nullptr;
360     for (auto P : Call.parameters()) {
361       QualType T = P->getType();
362       if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
363         return nullptr;
364     }
365 
366     return FnDescriptions.lookup(Call);
367   }
368 };
369 
370 } // end anonymous namespace
371 
372 REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
373 
374 inline void assertStreamStateOpened(const StreamState *SS) {
375   assert(SS->isOpened() &&
376          "Previous create of error node for non-opened stream failed?");
377 }
378 
379 void StreamChecker::checkPreCall(const CallEvent &Call,
380                                  CheckerContext &C) const {
381   const FnDescription *Desc = lookupFn(Call);
382   if (!Desc || !Desc->PreFn)
383     return;
384 
385   Desc->PreFn(this, Desc, Call, C);
386 }
387 
388 bool StreamChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
389   const FnDescription *Desc = lookupFn(Call);
390   if (!Desc && TestMode)
391     Desc = FnTestDescriptions.lookup(Call);
392   if (!Desc || !Desc->EvalFn)
393     return false;
394 
395   Desc->EvalFn(this, Desc, Call, C);
396 
397   return C.isDifferent();
398 }
399 
400 void StreamChecker::evalFopen(const FnDescription *Desc, const CallEvent &Call,
401                               CheckerContext &C) const {
402   ProgramStateRef State = C.getState();
403   const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
404   if (!CE)
405     return;
406 
407   DefinedSVal RetVal = makeRetVal(C, CE);
408   SymbolRef RetSym = RetVal.getAsSymbol();
409   assert(RetSym && "RetVal must be a symbol here.");
410 
411   State = State->BindExpr(CE, C.getLocationContext(), RetVal);
412 
413   // Bifurcate the state into two: one with a valid FILE* pointer, the other
414   // with a NULL.
415   ProgramStateRef StateNotNull, StateNull;
416   std::tie(StateNotNull, StateNull) =
417       C.getConstraintManager().assumeDual(State, RetVal);
418 
419   StateNotNull =
420       StateNotNull->set<StreamMap>(RetSym, StreamState::getOpened(Desc));
421   StateNull =
422       StateNull->set<StreamMap>(RetSym, StreamState::getOpenFailed(Desc));
423 
424   C.addTransition(StateNotNull);
425   C.addTransition(StateNull);
426 }
427 
428 void StreamChecker::preFreopen(const FnDescription *Desc, const CallEvent &Call,
429                                CheckerContext &C) const {
430   // Do not allow NULL as passed stream pointer but allow a closed stream.
431   ProgramStateRef State = C.getState();
432   State = ensureStreamNonNull(getStreamArg(Desc, Call), C, State);
433   if (!State)
434     return;
435 
436   C.addTransition(State);
437 }
438 
439 void StreamChecker::evalFreopen(const FnDescription *Desc,
440                                 const CallEvent &Call,
441                                 CheckerContext &C) const {
442   ProgramStateRef State = C.getState();
443 
444   auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
445   if (!CE)
446     return;
447 
448   Optional<DefinedSVal> StreamVal =
449       getStreamArg(Desc, Call).getAs<DefinedSVal>();
450   if (!StreamVal)
451     return;
452 
453   SymbolRef StreamSym = StreamVal->getAsSymbol();
454   // Do not care about concrete values for stream ("(FILE *)0x12345"?).
455   // FIXME: Can be stdin, stdout, stderr such values?
456   if (!StreamSym)
457     return;
458 
459   // Do not handle untracked stream. It is probably escaped.
460   if (!State->get<StreamMap>(StreamSym))
461     return;
462 
463   // Generate state for non-failed case.
464   // Return value is the passed stream pointer.
465   // According to the documentations, the stream is closed first
466   // but any close error is ignored. The state changes to (or remains) opened.
467   ProgramStateRef StateRetNotNull =
468       State->BindExpr(CE, C.getLocationContext(), *StreamVal);
469   // Generate state for NULL return value.
470   // Stream switches to OpenFailed state.
471   ProgramStateRef StateRetNull = State->BindExpr(CE, C.getLocationContext(),
472                                                  C.getSValBuilder().makeNull());
473 
474   StateRetNotNull =
475       StateRetNotNull->set<StreamMap>(StreamSym, StreamState::getOpened(Desc));
476   StateRetNull =
477       StateRetNull->set<StreamMap>(StreamSym, StreamState::getOpenFailed(Desc));
478 
479   C.addTransition(StateRetNotNull);
480   C.addTransition(StateRetNull);
481 }
482 
483 void StreamChecker::evalFclose(const FnDescription *Desc, const CallEvent &Call,
484                                CheckerContext &C) const {
485   ProgramStateRef State = C.getState();
486   SymbolRef Sym = getStreamArg(Desc, Call).getAsSymbol();
487   if (!Sym)
488     return;
489 
490   const StreamState *SS = State->get<StreamMap>(Sym);
491   if (!SS)
492     return;
493 
494   assertStreamStateOpened(SS);
495 
496   // Close the File Descriptor.
497   // Regardless if the close fails or not, stream becomes "closed"
498   // and can not be used any more.
499   State = State->set<StreamMap>(Sym, StreamState::getClosed(Desc));
500 
501   C.addTransition(State);
502 }
503 
504 void StreamChecker::preFread(const FnDescription *Desc, const CallEvent &Call,
505                              CheckerContext &C) const {
506   ProgramStateRef State = C.getState();
507   SVal StreamVal = getStreamArg(Desc, Call);
508   State = ensureStreamNonNull(StreamVal, C, State);
509   if (!State)
510     return;
511   State = ensureStreamOpened(StreamVal, C, State);
512   if (!State)
513     return;
514   State = ensureNoFilePositionIndeterminate(StreamVal, C, State);
515   if (!State)
516     return;
517 
518   SymbolRef Sym = StreamVal.getAsSymbol();
519   if (Sym && State->get<StreamMap>(Sym)) {
520     const StreamState *SS = State->get<StreamMap>(Sym);
521     if (SS->ErrorState & ErrorFEof)
522       reportFEofWarning(C, State);
523   } else {
524     C.addTransition(State);
525   }
526 }
527 
528 void StreamChecker::preFwrite(const FnDescription *Desc, const CallEvent &Call,
529                               CheckerContext &C) const {
530   ProgramStateRef State = C.getState();
531   SVal StreamVal = getStreamArg(Desc, Call);
532   State = ensureStreamNonNull(StreamVal, C, State);
533   if (!State)
534     return;
535   State = ensureStreamOpened(StreamVal, C, State);
536   if (!State)
537     return;
538   State = ensureNoFilePositionIndeterminate(StreamVal, C, State);
539   if (!State)
540     return;
541 
542   C.addTransition(State);
543 }
544 
545 void StreamChecker::evalFreadFwrite(const FnDescription *Desc,
546                                     const CallEvent &Call, CheckerContext &C,
547                                     bool IsFread) const {
548   ProgramStateRef State = C.getState();
549   SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
550   if (!StreamSym)
551     return;
552 
553   const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
554   if (!CE)
555     return;
556 
557   Optional<NonLoc> SizeVal = Call.getArgSVal(1).getAs<NonLoc>();
558   if (!SizeVal)
559     return;
560   Optional<NonLoc> NMembVal = Call.getArgSVal(2).getAs<NonLoc>();
561   if (!NMembVal)
562     return;
563 
564   const StreamState *SS = State->get<StreamMap>(StreamSym);
565   if (!SS)
566     return;
567 
568   assertStreamStateOpened(SS);
569 
570   // C'99 standard, §7.19.8.1.3, the return value of fread:
571   // The fread function returns the number of elements successfully read, which
572   // may be less than nmemb if a read error or end-of-file is encountered. If
573   // size or nmemb is zero, fread returns zero and the contents of the array and
574   // the state of the stream remain unchanged.
575 
576   if (State->isNull(*SizeVal).isConstrainedTrue() ||
577       State->isNull(*NMembVal).isConstrainedTrue()) {
578     // This is the "size or nmemb is zero" case.
579     // Just return 0, do nothing more (not clear the error flags).
580     State = bindInt(0, State, C, CE);
581     C.addTransition(State);
582     return;
583   }
584 
585   // Generate a transition for the success state.
586   // If we know the state to be FEOF at fread, do not add a success state.
587   if (!IsFread || (SS->ErrorState != ErrorFEof)) {
588     ProgramStateRef StateNotFailed =
589         State->BindExpr(CE, C.getLocationContext(), *NMembVal);
590     if (StateNotFailed) {
591       StateNotFailed = StateNotFailed->set<StreamMap>(
592           StreamSym, StreamState::getOpened(Desc));
593       C.addTransition(StateNotFailed);
594     }
595   }
596 
597   // Add transition for the failed state.
598   Optional<NonLoc> RetVal = makeRetVal(C, CE).castAs<NonLoc>();
599   assert(RetVal && "Value should be NonLoc.");
600   ProgramStateRef StateFailed =
601       State->BindExpr(CE, C.getLocationContext(), *RetVal);
602   if (!StateFailed)
603     return;
604   auto Cond = C.getSValBuilder()
605                   .evalBinOpNN(State, BO_LT, *RetVal, *NMembVal,
606                                C.getASTContext().IntTy)
607                   .getAs<DefinedOrUnknownSVal>();
608   if (!Cond)
609     return;
610   StateFailed = StateFailed->assume(*Cond, true);
611   if (!StateFailed)
612     return;
613 
614   StreamErrorState NewES;
615   if (IsFread)
616     NewES = (SS->ErrorState == ErrorFEof) ? ErrorFEof : ErrorFEof | ErrorFError;
617   else
618     NewES = ErrorFError;
619   // If a (non-EOF) error occurs, the resulting value of the file position
620   // indicator for the stream is indeterminate.
621   StreamState NewState = StreamState::getOpened(Desc, NewES, !NewES.isFEof());
622   StateFailed = StateFailed->set<StreamMap>(StreamSym, NewState);
623   C.addTransition(StateFailed);
624 }
625 
626 void StreamChecker::preFseek(const FnDescription *Desc, const CallEvent &Call,
627                              CheckerContext &C) const {
628   ProgramStateRef State = C.getState();
629   SVal StreamVal = getStreamArg(Desc, Call);
630   State = ensureStreamNonNull(StreamVal, C, State);
631   if (!State)
632     return;
633   State = ensureStreamOpened(StreamVal, C, State);
634   if (!State)
635     return;
636   State = ensureFseekWhenceCorrect(Call.getArgSVal(2), C, State);
637   if (!State)
638     return;
639 
640   C.addTransition(State);
641 }
642 
643 void StreamChecker::evalFseek(const FnDescription *Desc, const CallEvent &Call,
644                               CheckerContext &C) const {
645   ProgramStateRef State = C.getState();
646   SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
647   if (!StreamSym)
648     return;
649 
650   const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
651   if (!CE)
652     return;
653 
654   // Ignore the call if the stream is not tracked.
655   if (!State->get<StreamMap>(StreamSym))
656     return;
657 
658   DefinedSVal RetVal = makeRetVal(C, CE);
659 
660   // Make expression result.
661   State = State->BindExpr(CE, C.getLocationContext(), RetVal);
662 
663   // Bifurcate the state into failed and non-failed.
664   // Return zero on success, nonzero on error.
665   ProgramStateRef StateNotFailed, StateFailed;
666   std::tie(StateFailed, StateNotFailed) =
667       C.getConstraintManager().assumeDual(State, RetVal);
668 
669   // Reset the state to opened with no error.
670   StateNotFailed =
671       StateNotFailed->set<StreamMap>(StreamSym, StreamState::getOpened(Desc));
672   // We get error.
673   // It is possible that fseek fails but sets none of the error flags.
674   // If fseek failed, assume that the file position becomes indeterminate in any
675   // case.
676   StateFailed = StateFailed->set<StreamMap>(
677       StreamSym,
678       StreamState::getOpened(Desc, ErrorNone | ErrorFEof | ErrorFError, true));
679 
680   C.addTransition(StateNotFailed);
681   C.addTransition(StateFailed);
682 }
683 
684 void StreamChecker::evalClearerr(const FnDescription *Desc,
685                                  const CallEvent &Call,
686                                  CheckerContext &C) const {
687   ProgramStateRef State = C.getState();
688   SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
689   if (!StreamSym)
690     return;
691 
692   const StreamState *SS = State->get<StreamMap>(StreamSym);
693   if (!SS)
694     return;
695 
696   assertStreamStateOpened(SS);
697 
698   // FilePositionIndeterminate is not cleared.
699   State = State->set<StreamMap>(
700       StreamSym,
701       StreamState::getOpened(Desc, ErrorNone, SS->FilePositionIndeterminate));
702   C.addTransition(State);
703 }
704 
705 void StreamChecker::evalFeofFerror(const FnDescription *Desc,
706                                    const CallEvent &Call, CheckerContext &C,
707                                    const StreamErrorState &ErrorKind) const {
708   ProgramStateRef State = C.getState();
709   SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
710   if (!StreamSym)
711     return;
712 
713   const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
714   if (!CE)
715     return;
716 
717   const StreamState *SS = State->get<StreamMap>(StreamSym);
718   if (!SS)
719     return;
720 
721   assertStreamStateOpened(SS);
722 
723   if (SS->ErrorState & ErrorKind) {
724     // Execution path with error of ErrorKind.
725     // Function returns true.
726     // From now on it is the only one error state.
727     ProgramStateRef TrueState = bindAndAssumeTrue(State, C, CE);
728     C.addTransition(TrueState->set<StreamMap>(
729         StreamSym, StreamState::getOpened(Desc, ErrorKind,
730                                           SS->FilePositionIndeterminate &&
731                                               !ErrorKind.isFEof())));
732   }
733   if (StreamErrorState NewES = SS->ErrorState & (~ErrorKind)) {
734     // Execution path(s) with ErrorKind not set.
735     // Function returns false.
736     // New error state is everything before minus ErrorKind.
737     ProgramStateRef FalseState = bindInt(0, State, C, CE);
738     C.addTransition(FalseState->set<StreamMap>(
739         StreamSym,
740         StreamState::getOpened(
741             Desc, NewES, SS->FilePositionIndeterminate && !NewES.isFEof())));
742   }
743 }
744 
745 void StreamChecker::preDefault(const FnDescription *Desc, const CallEvent &Call,
746                                CheckerContext &C) const {
747   ProgramStateRef State = C.getState();
748   SVal StreamVal = getStreamArg(Desc, Call);
749   State = ensureStreamNonNull(StreamVal, C, State);
750   if (!State)
751     return;
752   State = ensureStreamOpened(StreamVal, C, State);
753   if (!State)
754     return;
755 
756   C.addTransition(State);
757 }
758 
759 void StreamChecker::evalSetFeofFerror(const FnDescription *Desc,
760                                       const CallEvent &Call, CheckerContext &C,
761                                       const StreamErrorState &ErrorKind) const {
762   ProgramStateRef State = C.getState();
763   SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
764   assert(StreamSym && "Operation not permitted on non-symbolic stream value.");
765   const StreamState *SS = State->get<StreamMap>(StreamSym);
766   assert(SS && "Stream should be tracked by the checker.");
767   State = State->set<StreamMap>(
768       StreamSym, StreamState::getOpened(SS->LastOperation, ErrorKind));
769   C.addTransition(State);
770 }
771 
772 ProgramStateRef
773 StreamChecker::ensureStreamNonNull(SVal StreamVal, CheckerContext &C,
774                                    ProgramStateRef State) const {
775   auto Stream = StreamVal.getAs<DefinedSVal>();
776   if (!Stream)
777     return State;
778 
779   ConstraintManager &CM = C.getConstraintManager();
780 
781   ProgramStateRef StateNotNull, StateNull;
782   std::tie(StateNotNull, StateNull) = CM.assumeDual(C.getState(), *Stream);
783 
784   if (!StateNotNull && StateNull) {
785     if (ExplodedNode *N = C.generateErrorNode(StateNull)) {
786       C.emitReport(std::make_unique<PathSensitiveBugReport>(
787           BT_FileNull, BT_FileNull.getDescription(), N));
788     }
789     return nullptr;
790   }
791 
792   return StateNotNull;
793 }
794 
795 ProgramStateRef StreamChecker::ensureStreamOpened(SVal StreamVal,
796                                                   CheckerContext &C,
797                                                   ProgramStateRef State) const {
798   SymbolRef Sym = StreamVal.getAsSymbol();
799   if (!Sym)
800     return State;
801 
802   const StreamState *SS = State->get<StreamMap>(Sym);
803   if (!SS)
804     return State;
805 
806   if (SS->isClosed()) {
807     // Using a stream pointer after 'fclose' causes undefined behavior
808     // according to cppreference.com .
809     ExplodedNode *N = C.generateErrorNode();
810     if (N) {
811       C.emitReport(std::make_unique<PathSensitiveBugReport>(
812           BT_UseAfterClose, BT_UseAfterClose.getDescription(), N));
813       return nullptr;
814     }
815 
816     return State;
817   }
818 
819   if (SS->isOpenFailed()) {
820     // Using a stream that has failed to open is likely to cause problems.
821     // This should usually not occur because stream pointer is NULL.
822     // But freopen can cause a state when stream pointer remains non-null but
823     // failed to open.
824     ExplodedNode *N = C.generateErrorNode();
825     if (N) {
826       C.emitReport(std::make_unique<PathSensitiveBugReport>(
827           BT_UseAfterOpenFailed, BT_UseAfterOpenFailed.getDescription(), N));
828       return nullptr;
829     }
830     return State;
831   }
832 
833   return State;
834 }
835 
836 ProgramStateRef StreamChecker::ensureNoFilePositionIndeterminate(
837     SVal StreamVal, CheckerContext &C, ProgramStateRef State) const {
838   SymbolRef Sym = StreamVal.getAsSymbol();
839   if (!Sym)
840     return State;
841 
842   const StreamState *SS = State->get<StreamMap>(Sym);
843   if (!SS)
844     return State;
845 
846   assert(SS->isOpened() && "First ensure that stream is opened.");
847 
848   if (SS->FilePositionIndeterminate) {
849     if (SS->ErrorState & ErrorFEof) {
850       // The error is unknown but may be FEOF.
851       // Continue analysis with the FEOF error state.
852       // Report warning because the other possible error states.
853       ExplodedNode *N = C.generateNonFatalErrorNode(State);
854       if (!N)
855         return nullptr;
856 
857       C.emitReport(std::make_unique<PathSensitiveBugReport>(
858           BT_IndeterminatePosition, BT_IndeterminatePosition.getDescription(),
859           N));
860       return State->set<StreamMap>(
861           Sym, StreamState::getOpened(SS->LastOperation, ErrorFEof, false));
862     }
863 
864     // Known or unknown error state without FEOF possible.
865     // Stop analysis, report error.
866     ExplodedNode *N = C.generateErrorNode(State);
867     if (N)
868       C.emitReport(std::make_unique<PathSensitiveBugReport>(
869           BT_IndeterminatePosition, BT_IndeterminatePosition.getDescription(),
870           N));
871 
872     return nullptr;
873   }
874 
875   return State;
876 }
877 
878 ProgramStateRef
879 StreamChecker::ensureFseekWhenceCorrect(SVal WhenceVal, CheckerContext &C,
880                                         ProgramStateRef State) const {
881   Optional<nonloc::ConcreteInt> CI = WhenceVal.getAs<nonloc::ConcreteInt>();
882   if (!CI)
883     return State;
884 
885   int64_t X = CI->getValue().getSExtValue();
886   if (X >= 0 && X <= 2)
887     return State;
888 
889   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
890     C.emitReport(std::make_unique<PathSensitiveBugReport>(
891         BT_IllegalWhence, BT_IllegalWhence.getDescription(), N));
892     return nullptr;
893   }
894 
895   return State;
896 }
897 
898 void StreamChecker::reportFEofWarning(CheckerContext &C,
899                                       ProgramStateRef State) const {
900   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
901     C.emitReport(std::make_unique<PathSensitiveBugReport>(
902         BT_StreamEof, BT_StreamEof.getDescription(), N));
903     return;
904   }
905   C.addTransition(State);
906 }
907 
908 void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
909                                      CheckerContext &C) const {
910   ProgramStateRef State = C.getState();
911 
912   // TODO: Clean up the state.
913   const StreamMapTy &Map = State->get<StreamMap>();
914   for (const auto &I : Map) {
915     SymbolRef Sym = I.first;
916     const StreamState &SS = I.second;
917     if (!SymReaper.isDead(Sym) || !SS.isOpened())
918       continue;
919 
920     ExplodedNode *N = C.generateErrorNode();
921     if (!N)
922       continue;
923 
924     C.emitReport(std::make_unique<PathSensitiveBugReport>(
925         BT_ResourceLeak, BT_ResourceLeak.getDescription(), N));
926   }
927 }
928 
929 ProgramStateRef StreamChecker::checkPointerEscape(
930     ProgramStateRef State, const InvalidatedSymbols &Escaped,
931     const CallEvent *Call, PointerEscapeKind Kind) const {
932   // Check for file-handling system call that is not handled by the checker.
933   // FIXME: The checker should be updated to handle all system calls that take
934   // 'FILE*' argument. These are now ignored.
935   if (Kind == PSK_DirectEscapeOnCall && Call->isInSystemHeader())
936     return State;
937 
938   for (SymbolRef Sym : Escaped) {
939     // The symbol escaped.
940     // From now the stream can be manipulated in unknown way to the checker,
941     // it is not possible to handle it any more.
942     // Optimistically, assume that the corresponding file handle will be closed
943     // somewhere else.
944     // Remove symbol from state so the following stream calls on this symbol are
945     // not handled by the checker.
946     State = State->remove<StreamMap>(Sym);
947   }
948   return State;
949 }
950 
951 void ento::registerStreamChecker(CheckerManager &Mgr) {
952   Mgr.registerChecker<StreamChecker>();
953 }
954 
955 bool ento::shouldRegisterStreamChecker(const CheckerManager &Mgr) {
956   return true;
957 }
958 
959 void ento::registerStreamTesterChecker(CheckerManager &Mgr) {
960   auto *Checker = Mgr.getChecker<StreamChecker>();
961   Checker->TestMode = true;
962 }
963 
964 bool ento::shouldRegisterStreamTesterChecker(const CheckerManager &Mgr) {
965   return true;
966 }
967