1 //===-- StreamChecker.cpp -----------------------------------------*- C++ -*--//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines checkers that model and check stream handling functions.
11 //===----------------------------------------------------------------------===//
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"
24 using namespace clang;
26 using namespace std::placeholders;
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.
41 /// The stream can be in state where the EOF indicator is set.
43 /// The stream can be in state where the error indicator is set.
46 bool isNoError() const { return NoError && !FEof && !FError; }
47 bool isFEof() const { return !NoError && FEof && !FError; }
48 bool isFError() const { return !NoError && !FEof && FError; }
50 bool operator==(const StreamErrorState &ES) const {
51 return NoError == ES.NoError && FEof == ES.FEof && FError == ES.FError;
54 bool operator!=(const StreamErrorState &ES) const { return !(*this == ES); }
56 StreamErrorState operator|(const StreamErrorState &E) const {
57 return {NoError || E.NoError, FEof || E.FEof, FError || E.FError};
60 StreamErrorState operator&(const StreamErrorState &E) const {
61 return {NoError && E.NoError, FEof && E.FEof, FError && E.FError};
64 StreamErrorState operator~() const { return {!NoError, !FEof, !FError}; }
66 /// Returns if the StreamErrorState is a valid object.
67 operator bool() const { return NoError || FEof || FError; }
69 void Profile(llvm::FoldingSetNodeID &ID) const {
70 ID.AddBoolean(NoError);
72 ID.AddBoolean(FError);
76 const StreamErrorState ErrorNone{true, false, false};
77 const StreamErrorState ErrorFEof{false, true, false};
78 const StreamErrorState ErrorFError{false, false, true};
80 /// Full state information about a stream pointer.
82 /// The last file operation called in the stream.
83 const FnDescription *LastOperation;
85 /// State of a stream symbol.
86 /// FIXME: We need maybe an "escaped" state later.
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.
93 /// State of the error flags.
94 /// Ignored in non-opened stream state but must be NoError.
95 StreamErrorState const ErrorState;
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;
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.");
117 bool isOpened() const { return State == Opened; }
118 bool isClosed() const { return State == Closed; }
119 bool isOpenFailed() const { return State == OpenFailed; }
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;
129 static StreamState getOpened(const FnDescription *L,
130 const StreamErrorState &ES = ErrorNone,
131 bool IsFilePositionIndeterminate = false) {
132 return StreamState{L, Opened, ES, IsFilePositionIndeterminate};
134 static StreamState getClosed(const FnDescription *L) {
135 return StreamState{L, Closed, {}, false};
137 static StreamState getOpenFailed(const FnDescription *L) {
138 return StreamState{L, OpenFailed, {}, false};
141 void Profile(llvm::FoldingSetNodeID &ID) const {
142 ID.AddPointer(LastOperation);
143 ID.AddInteger(State);
144 ID.AddInteger(ErrorState);
145 ID.AddBoolean(FilePositionIndeterminate);
150 using FnCheck = std::function<void(const StreamChecker *, const FnDescription *,
151 const CallEvent &, CheckerContext &)>;
153 using ArgNoTy = unsigned int;
154 static const ArgNoTy ArgNone = std::numeric_limits<ArgNoTy>::max();
156 struct FnDescription {
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);
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.");
174 const LocationContext *LCtx = C.getLocationContext();
175 return C.getSValBuilder()
176 .conjureSymbolVal(nullptr, CE, LCtx, C.blockCount())
177 .castAs<DefinedSVal>();
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.");
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));
196 class StreamChecker : public Checker<check::PreCall, eval::Call,
197 check::DeadSymbols, check::PointerEscape> {
198 BugType BT_FileNull{this, "NULL stream pointer", "Stream handling error"};
199 BugType BT_UseAfterClose{this, "Closed stream", "Stream handling error"};
200 BugType BT_UseAfterOpenFailed{this, "Invalid stream",
201 "Stream handling error"};
202 BugType BT_IndeterminatePosition{this, "Invalid stream state",
203 "Stream handling error"};
204 BugType BT_IllegalWhence{this, "Illegal whence argument",
205 "Stream handling error"};
206 BugType BT_StreamEof{this, "Stream already in EOF", "Stream handling error"};
207 BugType BT_ResourceLeak{this, "Resource leak", "Stream handling error"};
210 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
211 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
212 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
213 ProgramStateRef checkPointerEscape(ProgramStateRef State,
214 const InvalidatedSymbols &Escaped,
215 const CallEvent *Call,
216 PointerEscapeKind Kind) const;
218 /// If true, evaluate special testing stream functions.
219 bool TestMode = false;
222 CallDescriptionMap<FnDescription> FnDescriptions = {
223 {{"fopen"}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
225 {&StreamChecker::preFreopen, &StreamChecker::evalFreopen, 2}},
226 {{"tmpfile"}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
228 {&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
230 {&StreamChecker::preFread,
231 std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, true), 3}},
233 {&StreamChecker::preFwrite,
234 std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, false), 3}},
235 {{"fseek", 3}, {&StreamChecker::preFseek, &StreamChecker::evalFseek, 0}},
236 {{"ftell", 1}, {&StreamChecker::preDefault, nullptr, 0}},
237 {{"rewind", 1}, {&StreamChecker::preDefault, nullptr, 0}},
238 {{"fgetpos", 2}, {&StreamChecker::preDefault, nullptr, 0}},
239 {{"fsetpos", 2}, {&StreamChecker::preDefault, nullptr, 0}},
241 {&StreamChecker::preDefault, &StreamChecker::evalClearerr, 0}},
243 {&StreamChecker::preDefault,
244 std::bind(&StreamChecker::evalFeofFerror, _1, _2, _3, _4, ErrorFEof),
247 {&StreamChecker::preDefault,
248 std::bind(&StreamChecker::evalFeofFerror, _1, _2, _3, _4, ErrorFError),
250 {{"fileno", 1}, {&StreamChecker::preDefault, nullptr, 0}},
253 CallDescriptionMap<FnDescription> FnTestDescriptions = {
254 {{"StreamTesterChecker_make_feof_stream", 1},
256 std::bind(&StreamChecker::evalSetFeofFerror, _1, _2, _3, _4, ErrorFEof),
258 {{"StreamTesterChecker_make_ferror_stream", 1},
260 std::bind(&StreamChecker::evalSetFeofFerror, _1, _2, _3, _4,
265 void evalFopen(const FnDescription *Desc, const CallEvent &Call,
266 CheckerContext &C) const;
268 void preFreopen(const FnDescription *Desc, const CallEvent &Call,
269 CheckerContext &C) const;
270 void evalFreopen(const FnDescription *Desc, const CallEvent &Call,
271 CheckerContext &C) const;
273 void evalFclose(const FnDescription *Desc, const CallEvent &Call,
274 CheckerContext &C) const;
276 void preFread(const FnDescription *Desc, const CallEvent &Call,
277 CheckerContext &C) const;
279 void preFwrite(const FnDescription *Desc, const CallEvent &Call,
280 CheckerContext &C) const;
282 void evalFreadFwrite(const FnDescription *Desc, const CallEvent &Call,
283 CheckerContext &C, bool IsFread) const;
285 void preFseek(const FnDescription *Desc, const CallEvent &Call,
286 CheckerContext &C) const;
287 void evalFseek(const FnDescription *Desc, const CallEvent &Call,
288 CheckerContext &C) const;
290 void preDefault(const FnDescription *Desc, const CallEvent &Call,
291 CheckerContext &C) const;
293 void evalClearerr(const FnDescription *Desc, const CallEvent &Call,
294 CheckerContext &C) const;
296 void evalFeofFerror(const FnDescription *Desc, const CallEvent &Call,
298 const StreamErrorState &ErrorKind) const;
300 void evalSetFeofFerror(const FnDescription *Desc, const CallEvent &Call,
302 const StreamErrorState &ErrorKind) const;
304 /// Check that the stream (in StreamVal) is not NULL.
305 /// If it can only be NULL a fatal error is emitted and nullptr returned.
306 /// Otherwise the return value is a new state where the stream is constrained
308 ProgramStateRef ensureStreamNonNull(SVal StreamVal, CheckerContext &C,
309 ProgramStateRef State) const;
311 /// Check that the stream is the opened state.
312 /// If the stream is known to be not opened an error is generated
313 /// and nullptr returned, otherwise the original state is returned.
314 ProgramStateRef ensureStreamOpened(SVal StreamVal, CheckerContext &C,
315 ProgramStateRef State) const;
317 /// Check that the stream has not an invalid ("indeterminate") file position,
318 /// generate warning for it.
319 /// (EOF is not an invalid position.)
320 /// The returned state can be nullptr if a fatal error was generated.
321 /// It can return non-null state if the stream has not an invalid position or
322 /// there is execution path with non-invalid position.
324 ensureNoFilePositionIndeterminate(SVal StreamVal, CheckerContext &C,
325 ProgramStateRef State) const;
327 /// Check the legality of the 'whence' argument of 'fseek'.
328 /// Generate error and return nullptr if it is found to be illegal.
329 /// Otherwise returns the state.
330 /// (State is not changed here because the "whence" value is already known.)
331 ProgramStateRef ensureFseekWhenceCorrect(SVal WhenceVal, CheckerContext &C,
332 ProgramStateRef State) const;
334 /// Generate warning about stream in EOF state.
335 /// There will be always a state transition into the passed State,
336 /// by the new non-fatal error node or (if failed) a normal transition,
337 /// to ensure uniform handling.
338 void reportFEofWarning(CheckerContext &C, ProgramStateRef State) const;
340 /// Find the description data of the function called by a call event.
341 /// Returns nullptr if no function is recognized.
342 const FnDescription *lookupFn(const CallEvent &Call) const {
343 // Recognize "global C functions" with only integral or pointer arguments
344 // (and matching name) as stream functions.
345 if (!Call.isGlobalCFunction())
347 for (auto P : Call.parameters()) {
348 QualType T = P->getType();
349 if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
353 return FnDescriptions.lookup(Call);
356 /// Generate a message for BugReporterVisitor if the stored symbol is
357 /// marked as interesting by the actual bug report.
359 const CheckerNameRef CheckerName;
363 std::string operator()(PathSensitiveBugReport &BR) const {
364 if (BR.isInteresting(StreamSym) &&
365 CheckerName == BR.getBugType().getCheckerName())
372 const NoteTag *constructNoteTag(CheckerContext &C, SymbolRef StreamSym,
373 const std::string &Message) const {
374 return C.getNoteTag(NoteFn{getCheckerName(), StreamSym, Message});
377 /// Searches for the ExplodedNode where the file descriptor was acquired for
379 static const ExplodedNode *getAcquisitionSite(const ExplodedNode *N,
384 } // end anonymous namespace
386 REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
388 inline void assertStreamStateOpened(const StreamState *SS) {
389 assert(SS->isOpened() &&
390 "Previous create of error node for non-opened stream failed?");
393 const ExplodedNode *StreamChecker::getAcquisitionSite(const ExplodedNode *N,
396 ProgramStateRef State = N->getState();
397 // When bug type is resource leak, exploded node N may not have state info
398 // for leaked file descriptor, but predecessor should have it.
399 if (!State->get<StreamMap>(StreamSym))
400 N = N->getFirstPred();
402 const ExplodedNode *Pred = N;
404 State = N->getState();
405 if (!State->get<StreamMap>(StreamSym))
408 N = N->getFirstPred();
414 void StreamChecker::checkPreCall(const CallEvent &Call,
415 CheckerContext &C) const {
416 const FnDescription *Desc = lookupFn(Call);
417 if (!Desc || !Desc->PreFn)
420 Desc->PreFn(this, Desc, Call, C);
423 bool StreamChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
424 const FnDescription *Desc = lookupFn(Call);
425 if (!Desc && TestMode)
426 Desc = FnTestDescriptions.lookup(Call);
427 if (!Desc || !Desc->EvalFn)
430 Desc->EvalFn(this, Desc, Call, C);
432 return C.isDifferent();
435 void StreamChecker::evalFopen(const FnDescription *Desc, const CallEvent &Call,
436 CheckerContext &C) const {
437 ProgramStateRef State = C.getState();
438 const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
442 DefinedSVal RetVal = makeRetVal(C, CE);
443 SymbolRef RetSym = RetVal.getAsSymbol();
444 assert(RetSym && "RetVal must be a symbol here.");
446 State = State->BindExpr(CE, C.getLocationContext(), RetVal);
448 // Bifurcate the state into two: one with a valid FILE* pointer, the other
450 ProgramStateRef StateNotNull, StateNull;
451 std::tie(StateNotNull, StateNull) =
452 C.getConstraintManager().assumeDual(State, RetVal);
455 StateNotNull->set<StreamMap>(RetSym, StreamState::getOpened(Desc));
457 StateNull->set<StreamMap>(RetSym, StreamState::getOpenFailed(Desc));
459 C.addTransition(StateNotNull,
460 constructNoteTag(C, RetSym, "Stream opened here"));
461 C.addTransition(StateNull);
464 void StreamChecker::preFreopen(const FnDescription *Desc, const CallEvent &Call,
465 CheckerContext &C) const {
466 // Do not allow NULL as passed stream pointer but allow a closed stream.
467 ProgramStateRef State = C.getState();
468 State = ensureStreamNonNull(getStreamArg(Desc, Call), C, State);
472 C.addTransition(State);
475 void StreamChecker::evalFreopen(const FnDescription *Desc,
476 const CallEvent &Call,
477 CheckerContext &C) const {
478 ProgramStateRef State = C.getState();
480 auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
484 Optional<DefinedSVal> StreamVal =
485 getStreamArg(Desc, Call).getAs<DefinedSVal>();
489 SymbolRef StreamSym = StreamVal->getAsSymbol();
490 // Do not care about concrete values for stream ("(FILE *)0x12345"?).
491 // FIXME: Can be stdin, stdout, stderr such values?
495 // Do not handle untracked stream. It is probably escaped.
496 if (!State->get<StreamMap>(StreamSym))
499 // Generate state for non-failed case.
500 // Return value is the passed stream pointer.
501 // According to the documentations, the stream is closed first
502 // but any close error is ignored. The state changes to (or remains) opened.
503 ProgramStateRef StateRetNotNull =
504 State->BindExpr(CE, C.getLocationContext(), *StreamVal);
505 // Generate state for NULL return value.
506 // Stream switches to OpenFailed state.
507 ProgramStateRef StateRetNull = State->BindExpr(CE, C.getLocationContext(),
508 C.getSValBuilder().makeNull());
511 StateRetNotNull->set<StreamMap>(StreamSym, StreamState::getOpened(Desc));
513 StateRetNull->set<StreamMap>(StreamSym, StreamState::getOpenFailed(Desc));
515 C.addTransition(StateRetNotNull,
516 constructNoteTag(C, StreamSym, "Stream reopened here"));
517 C.addTransition(StateRetNull);
520 void StreamChecker::evalFclose(const FnDescription *Desc, const CallEvent &Call,
521 CheckerContext &C) const {
522 ProgramStateRef State = C.getState();
523 SymbolRef Sym = getStreamArg(Desc, Call).getAsSymbol();
527 const StreamState *SS = State->get<StreamMap>(Sym);
531 assertStreamStateOpened(SS);
533 // Close the File Descriptor.
534 // Regardless if the close fails or not, stream becomes "closed"
535 // and can not be used any more.
536 State = State->set<StreamMap>(Sym, StreamState::getClosed(Desc));
538 C.addTransition(State);
541 void StreamChecker::preFread(const FnDescription *Desc, const CallEvent &Call,
542 CheckerContext &C) const {
543 ProgramStateRef State = C.getState();
544 SVal StreamVal = getStreamArg(Desc, Call);
545 State = ensureStreamNonNull(StreamVal, C, State);
548 State = ensureStreamOpened(StreamVal, C, State);
551 State = ensureNoFilePositionIndeterminate(StreamVal, C, State);
555 SymbolRef Sym = StreamVal.getAsSymbol();
556 if (Sym && State->get<StreamMap>(Sym)) {
557 const StreamState *SS = State->get<StreamMap>(Sym);
558 if (SS->ErrorState & ErrorFEof)
559 reportFEofWarning(C, State);
561 C.addTransition(State);
565 void StreamChecker::preFwrite(const FnDescription *Desc, const CallEvent &Call,
566 CheckerContext &C) const {
567 ProgramStateRef State = C.getState();
568 SVal StreamVal = getStreamArg(Desc, Call);
569 State = ensureStreamNonNull(StreamVal, C, State);
572 State = ensureStreamOpened(StreamVal, C, State);
575 State = ensureNoFilePositionIndeterminate(StreamVal, C, State);
579 C.addTransition(State);
582 void StreamChecker::evalFreadFwrite(const FnDescription *Desc,
583 const CallEvent &Call, CheckerContext &C,
584 bool IsFread) const {
585 ProgramStateRef State = C.getState();
586 SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
590 const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
594 Optional<NonLoc> SizeVal = Call.getArgSVal(1).getAs<NonLoc>();
597 Optional<NonLoc> NMembVal = Call.getArgSVal(2).getAs<NonLoc>();
601 const StreamState *SS = State->get<StreamMap>(StreamSym);
605 assertStreamStateOpened(SS);
607 // C'99 standard, §7.19.8.1.3, the return value of fread:
608 // The fread function returns the number of elements successfully read, which
609 // may be less than nmemb if a read error or end-of-file is encountered. If
610 // size or nmemb is zero, fread returns zero and the contents of the array and
611 // the state of the stream remain unchanged.
613 if (State->isNull(*SizeVal).isConstrainedTrue() ||
614 State->isNull(*NMembVal).isConstrainedTrue()) {
615 // This is the "size or nmemb is zero" case.
616 // Just return 0, do nothing more (not clear the error flags).
617 State = bindInt(0, State, C, CE);
618 C.addTransition(State);
622 // Generate a transition for the success state.
623 // If we know the state to be FEOF at fread, do not add a success state.
624 if (!IsFread || (SS->ErrorState != ErrorFEof)) {
625 ProgramStateRef StateNotFailed =
626 State->BindExpr(CE, C.getLocationContext(), *NMembVal);
627 if (StateNotFailed) {
628 StateNotFailed = StateNotFailed->set<StreamMap>(
629 StreamSym, StreamState::getOpened(Desc));
630 C.addTransition(StateNotFailed);
634 // Add transition for the failed state.
635 Optional<NonLoc> RetVal = makeRetVal(C, CE).castAs<NonLoc>();
636 assert(RetVal && "Value should be NonLoc.");
637 ProgramStateRef StateFailed =
638 State->BindExpr(CE, C.getLocationContext(), *RetVal);
641 auto Cond = C.getSValBuilder()
642 .evalBinOpNN(State, BO_LT, *RetVal, *NMembVal,
643 C.getASTContext().IntTy)
644 .getAs<DefinedOrUnknownSVal>();
647 StateFailed = StateFailed->assume(*Cond, true);
651 StreamErrorState NewES;
653 NewES = (SS->ErrorState == ErrorFEof) ? ErrorFEof : ErrorFEof | ErrorFError;
656 // If a (non-EOF) error occurs, the resulting value of the file position
657 // indicator for the stream is indeterminate.
658 StreamState NewState = StreamState::getOpened(Desc, NewES, !NewES.isFEof());
659 StateFailed = StateFailed->set<StreamMap>(StreamSym, NewState);
660 C.addTransition(StateFailed);
663 void StreamChecker::preFseek(const FnDescription *Desc, const CallEvent &Call,
664 CheckerContext &C) const {
665 ProgramStateRef State = C.getState();
666 SVal StreamVal = getStreamArg(Desc, Call);
667 State = ensureStreamNonNull(StreamVal, C, State);
670 State = ensureStreamOpened(StreamVal, C, State);
673 State = ensureFseekWhenceCorrect(Call.getArgSVal(2), C, State);
677 C.addTransition(State);
680 void StreamChecker::evalFseek(const FnDescription *Desc, const CallEvent &Call,
681 CheckerContext &C) const {
682 ProgramStateRef State = C.getState();
683 SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
687 const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
691 // Ignore the call if the stream is not tracked.
692 if (!State->get<StreamMap>(StreamSym))
695 DefinedSVal RetVal = makeRetVal(C, CE);
697 // Make expression result.
698 State = State->BindExpr(CE, C.getLocationContext(), RetVal);
700 // Bifurcate the state into failed and non-failed.
701 // Return zero on success, nonzero on error.
702 ProgramStateRef StateNotFailed, StateFailed;
703 std::tie(StateFailed, StateNotFailed) =
704 C.getConstraintManager().assumeDual(State, RetVal);
706 // Reset the state to opened with no error.
708 StateNotFailed->set<StreamMap>(StreamSym, StreamState::getOpened(Desc));
710 // It is possible that fseek fails but sets none of the error flags.
711 // If fseek failed, assume that the file position becomes indeterminate in any
713 StateFailed = StateFailed->set<StreamMap>(
715 StreamState::getOpened(Desc, ErrorNone | ErrorFEof | ErrorFError, true));
717 C.addTransition(StateNotFailed);
718 C.addTransition(StateFailed);
721 void StreamChecker::evalClearerr(const FnDescription *Desc,
722 const CallEvent &Call,
723 CheckerContext &C) const {
724 ProgramStateRef State = C.getState();
725 SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
729 const StreamState *SS = State->get<StreamMap>(StreamSym);
733 assertStreamStateOpened(SS);
735 // FilePositionIndeterminate is not cleared.
736 State = State->set<StreamMap>(
738 StreamState::getOpened(Desc, ErrorNone, SS->FilePositionIndeterminate));
739 C.addTransition(State);
742 void StreamChecker::evalFeofFerror(const FnDescription *Desc,
743 const CallEvent &Call, CheckerContext &C,
744 const StreamErrorState &ErrorKind) const {
745 ProgramStateRef State = C.getState();
746 SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
750 const CallExpr *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
754 const StreamState *SS = State->get<StreamMap>(StreamSym);
758 assertStreamStateOpened(SS);
760 if (SS->ErrorState & ErrorKind) {
761 // Execution path with error of ErrorKind.
762 // Function returns true.
763 // From now on it is the only one error state.
764 ProgramStateRef TrueState = bindAndAssumeTrue(State, C, CE);
765 C.addTransition(TrueState->set<StreamMap>(
766 StreamSym, StreamState::getOpened(Desc, ErrorKind,
767 SS->FilePositionIndeterminate &&
768 !ErrorKind.isFEof())));
770 if (StreamErrorState NewES = SS->ErrorState & (~ErrorKind)) {
771 // Execution path(s) with ErrorKind not set.
772 // Function returns false.
773 // New error state is everything before minus ErrorKind.
774 ProgramStateRef FalseState = bindInt(0, State, C, CE);
775 C.addTransition(FalseState->set<StreamMap>(
777 StreamState::getOpened(
778 Desc, NewES, SS->FilePositionIndeterminate && !NewES.isFEof())));
782 void StreamChecker::preDefault(const FnDescription *Desc, const CallEvent &Call,
783 CheckerContext &C) const {
784 ProgramStateRef State = C.getState();
785 SVal StreamVal = getStreamArg(Desc, Call);
786 State = ensureStreamNonNull(StreamVal, C, State);
789 State = ensureStreamOpened(StreamVal, C, State);
793 C.addTransition(State);
796 void StreamChecker::evalSetFeofFerror(const FnDescription *Desc,
797 const CallEvent &Call, CheckerContext &C,
798 const StreamErrorState &ErrorKind) const {
799 ProgramStateRef State = C.getState();
800 SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol();
801 assert(StreamSym && "Operation not permitted on non-symbolic stream value.");
802 const StreamState *SS = State->get<StreamMap>(StreamSym);
803 assert(SS && "Stream should be tracked by the checker.");
804 State = State->set<StreamMap>(
805 StreamSym, StreamState::getOpened(SS->LastOperation, ErrorKind));
806 C.addTransition(State);
810 StreamChecker::ensureStreamNonNull(SVal StreamVal, CheckerContext &C,
811 ProgramStateRef State) const {
812 auto Stream = StreamVal.getAs<DefinedSVal>();
816 ConstraintManager &CM = C.getConstraintManager();
818 ProgramStateRef StateNotNull, StateNull;
819 std::tie(StateNotNull, StateNull) = CM.assumeDual(C.getState(), *Stream);
821 if (!StateNotNull && StateNull) {
822 if (ExplodedNode *N = C.generateErrorNode(StateNull)) {
823 C.emitReport(std::make_unique<PathSensitiveBugReport>(
824 BT_FileNull, "Stream pointer might be NULL.", N));
832 ProgramStateRef StreamChecker::ensureStreamOpened(SVal StreamVal,
834 ProgramStateRef State) const {
835 SymbolRef Sym = StreamVal.getAsSymbol();
839 const StreamState *SS = State->get<StreamMap>(Sym);
843 if (SS->isClosed()) {
844 // Using a stream pointer after 'fclose' causes undefined behavior
845 // according to cppreference.com .
846 ExplodedNode *N = C.generateErrorNode();
848 C.emitReport(std::make_unique<PathSensitiveBugReport>(
850 "Stream might be already closed. Causes undefined behaviour.", N));
857 if (SS->isOpenFailed()) {
858 // Using a stream that has failed to open is likely to cause problems.
859 // This should usually not occur because stream pointer is NULL.
860 // But freopen can cause a state when stream pointer remains non-null but
862 ExplodedNode *N = C.generateErrorNode();
864 C.emitReport(std::make_unique<PathSensitiveBugReport>(
865 BT_UseAfterOpenFailed,
866 "Stream might be invalid after "
867 "(re-)opening it has failed. "
868 "Can cause undefined behaviour.",
878 ProgramStateRef StreamChecker::ensureNoFilePositionIndeterminate(
879 SVal StreamVal, CheckerContext &C, ProgramStateRef State) const {
880 static const char *BugMessage =
881 "File position of the stream might be 'indeterminate' "
882 "after a failed operation. "
883 "Can cause undefined behavior.";
885 SymbolRef Sym = StreamVal.getAsSymbol();
889 const StreamState *SS = State->get<StreamMap>(Sym);
893 assert(SS->isOpened() && "First ensure that stream is opened.");
895 if (SS->FilePositionIndeterminate) {
896 if (SS->ErrorState & ErrorFEof) {
897 // The error is unknown but may be FEOF.
898 // Continue analysis with the FEOF error state.
899 // Report warning because the other possible error states.
900 ExplodedNode *N = C.generateNonFatalErrorNode(State);
904 C.emitReport(std::make_unique<PathSensitiveBugReport>(
905 BT_IndeterminatePosition, BugMessage, N));
906 return State->set<StreamMap>(
907 Sym, StreamState::getOpened(SS->LastOperation, ErrorFEof, false));
910 // Known or unknown error state without FEOF possible.
911 // Stop analysis, report error.
912 ExplodedNode *N = C.generateErrorNode(State);
914 C.emitReport(std::make_unique<PathSensitiveBugReport>(
915 BT_IndeterminatePosition, BugMessage, N));
924 StreamChecker::ensureFseekWhenceCorrect(SVal WhenceVal, CheckerContext &C,
925 ProgramStateRef State) const {
926 Optional<nonloc::ConcreteInt> CI = WhenceVal.getAs<nonloc::ConcreteInt>();
930 int64_t X = CI->getValue().getSExtValue();
931 if (X >= 0 && X <= 2)
934 if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
935 C.emitReport(std::make_unique<PathSensitiveBugReport>(
937 "The whence argument to fseek() should be "
938 "SEEK_SET, SEEK_END, or SEEK_CUR.",
946 void StreamChecker::reportFEofWarning(CheckerContext &C,
947 ProgramStateRef State) const {
948 if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
949 C.emitReport(std::make_unique<PathSensitiveBugReport>(
951 "Read function called when stream is in EOF state. "
952 "Function has no effect.",
956 C.addTransition(State);
959 void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
960 CheckerContext &C) const {
961 ProgramStateRef State = C.getState();
963 // TODO: Clean up the state.
964 const StreamMapTy &Map = State->get<StreamMap>();
965 for (const auto &I : Map) {
966 SymbolRef Sym = I.first;
967 const StreamState &SS = I.second;
968 if (!SymReaper.isDead(Sym) || !SS.isOpened())
971 ExplodedNode *N = C.generateErrorNode();
975 // Do not warn for non-closed stream at program exit.
976 ExplodedNode *Pred = C.getPredecessor();
977 if (Pred && Pred->getCFGBlock() &&
978 Pred->getCFGBlock()->hasNoReturnElement())
981 // Resource leaks can result in multiple warning that describe the same kind
982 // of programming error:
984 // FILE *F = fopen("a.txt");
985 // if (rand()) // state split
986 // return; // warning
988 // While this isn't necessarily true (leaking the same stream could result
989 // from a different kinds of errors), the reduction in redundant reports
990 // makes this a worthwhile heuristic.
991 // FIXME: Add a checker option to turn this uniqueing feature off.
993 const ExplodedNode *StreamOpenNode = getAcquisitionSite(N, Sym, C);
994 assert(StreamOpenNode && "Could not find place of stream opening.");
995 PathDiagnosticLocation LocUsedForUniqueing =
996 PathDiagnosticLocation::createBegin(
997 StreamOpenNode->getStmtForDiagnostics(), C.getSourceManager(),
998 StreamOpenNode->getLocationContext());
1000 std::unique_ptr<PathSensitiveBugReport> R =
1001 std::make_unique<PathSensitiveBugReport>(
1003 "Opened stream never closed. Potential resource leak.", N,
1004 LocUsedForUniqueing,
1005 StreamOpenNode->getLocationContext()->getDecl());
1006 R->markInteresting(Sym);
1007 C.emitReport(std::move(R));
1011 ProgramStateRef StreamChecker::checkPointerEscape(
1012 ProgramStateRef State, const InvalidatedSymbols &Escaped,
1013 const CallEvent *Call, PointerEscapeKind Kind) const {
1014 // Check for file-handling system call that is not handled by the checker.
1015 // FIXME: The checker should be updated to handle all system calls that take
1016 // 'FILE*' argument. These are now ignored.
1017 if (Kind == PSK_DirectEscapeOnCall && Call->isInSystemHeader())
1020 for (SymbolRef Sym : Escaped) {
1021 // The symbol escaped.
1022 // From now the stream can be manipulated in unknown way to the checker,
1023 // it is not possible to handle it any more.
1024 // Optimistically, assume that the corresponding file handle will be closed
1026 // Remove symbol from state so the following stream calls on this symbol are
1027 // not handled by the checker.
1028 State = State->remove<StreamMap>(Sym);
1033 void ento::registerStreamChecker(CheckerManager &Mgr) {
1034 Mgr.registerChecker<StreamChecker>();
1037 bool ento::shouldRegisterStreamChecker(const CheckerManager &Mgr) {
1041 void ento::registerStreamTesterChecker(CheckerManager &Mgr) {
1042 auto *Checker = Mgr.getChecker<StreamChecker>();
1043 Checker->TestMode = true;
1046 bool ento::shouldRegisterStreamTesterChecker(const CheckerManager &Mgr) {