1*12c85518Srobert //=== ErrnoModeling.h - Tracking value of 'errno'. -----------------*- C++ -*-// 2*12c85518Srobert // 3*12c85518Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*12c85518Srobert // See https://llvm.org/LICENSE.txt for license information. 5*12c85518Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*12c85518Srobert // 7*12c85518Srobert //===----------------------------------------------------------------------===// 8*12c85518Srobert // 9*12c85518Srobert // Defines inter-checker API for using the system value 'errno'. 10*12c85518Srobert // 11*12c85518Srobert //===----------------------------------------------------------------------===// 12*12c85518Srobert 13*12c85518Srobert #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H 14*12c85518Srobert #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H 15*12c85518Srobert 16*12c85518Srobert #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 17*12c85518Srobert #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 18*12c85518Srobert #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 19*12c85518Srobert #include <optional> 20*12c85518Srobert 21*12c85518Srobert namespace clang { 22*12c85518Srobert namespace ento { 23*12c85518Srobert namespace errno_modeling { 24*12c85518Srobert 25*12c85518Srobert /// Describe how reads and writes of \c errno are handled by the checker. 26*12c85518Srobert enum ErrnoCheckState : unsigned { 27*12c85518Srobert /// We do not know anything about 'errno'. 28*12c85518Srobert /// Read and write is always allowed. 29*12c85518Srobert Irrelevant = 0, 30*12c85518Srobert 31*12c85518Srobert /// Value of 'errno' should be checked to find out if a previous function call 32*12c85518Srobert /// has failed. 33*12c85518Srobert /// When this state is set \c errno must be read by the program before a next 34*12c85518Srobert /// standard function call or other overwrite of \c errno follows, otherwise 35*12c85518Srobert /// a bug report is emitted. 36*12c85518Srobert MustBeChecked = 1, 37*12c85518Srobert 38*12c85518Srobert /// Value of 'errno' is not allowed to be read, it can contain an unspecified 39*12c85518Srobert /// value. 40*12c85518Srobert /// When this state is set \c errno is not allowed to be read by the program 41*12c85518Srobert /// until it is overwritten or invalidated. 42*12c85518Srobert MustNotBeChecked = 2 43*12c85518Srobert }; 44*12c85518Srobert 45*12c85518Srobert /// Returns the value of 'errno', if 'errno' was found in the AST. 46*12c85518Srobert std::optional<SVal> getErrnoValue(ProgramStateRef State); 47*12c85518Srobert 48*12c85518Srobert /// Returns the errno check state, \c Errno_Irrelevant if 'errno' was not found 49*12c85518Srobert /// (this is not the only case for that value). 50*12c85518Srobert ErrnoCheckState getErrnoState(ProgramStateRef State); 51*12c85518Srobert 52*12c85518Srobert /// Returns the location that points to the \c MemoryRegion where the 'errno' 53*12c85518Srobert /// value is stored. Returns \c std::nullopt if 'errno' was not found. Otherwise 54*12c85518Srobert /// it always returns a valid memory region in the system global memory space. 55*12c85518Srobert std::optional<Loc> getErrnoLoc(ProgramStateRef State); 56*12c85518Srobert 57*12c85518Srobert /// Set value of 'errno' to any SVal, if possible. 58*12c85518Srobert /// The errno check state is set always when the 'errno' value is set. 59*12c85518Srobert ProgramStateRef setErrnoValue(ProgramStateRef State, 60*12c85518Srobert const LocationContext *LCtx, SVal Value, 61*12c85518Srobert ErrnoCheckState EState); 62*12c85518Srobert 63*12c85518Srobert /// Set value of 'errno' to a concrete (signed) integer, if possible. 64*12c85518Srobert /// The errno check state is set always when the 'errno' value is set. 65*12c85518Srobert ProgramStateRef setErrnoValue(ProgramStateRef State, CheckerContext &C, 66*12c85518Srobert uint64_t Value, ErrnoCheckState EState); 67*12c85518Srobert 68*12c85518Srobert /// Set the errno check state, do not modify the errno value. 69*12c85518Srobert ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState); 70*12c85518Srobert 71*12c85518Srobert /// Clear state of errno (make it irrelevant). 72*12c85518Srobert ProgramStateRef clearErrnoState(ProgramStateRef State); 73*12c85518Srobert 74*12c85518Srobert /// Determine if a `Decl` node related to 'errno'. 75*12c85518Srobert /// This is true if the declaration is the errno variable or a function 76*12c85518Srobert /// that returns a pointer to the 'errno' value (usually the 'errno' macro is 77*12c85518Srobert /// defined with this function). \p D is not required to be a canonical 78*12c85518Srobert /// declaration. 79*12c85518Srobert bool isErrno(const Decl *D); 80*12c85518Srobert 81*12c85518Srobert /// Produce a textual description about how \c errno is allowed to be used 82*12c85518Srobert /// (in a \c ErrnoCheckState). 83*12c85518Srobert /// The returned string is insertable into a longer warning message in the form 84*12c85518Srobert /// "the value 'errno' <...>". 85*12c85518Srobert /// Currently only the \c errno_modeling::MustNotBeChecked state is supported, 86*12c85518Srobert /// others are not used by the clients. 87*12c85518Srobert const char *describeErrnoCheckState(ErrnoCheckState CS); 88*12c85518Srobert 89*12c85518Srobert /// Create a NoteTag that displays the message if the 'errno' memory region is 90*12c85518Srobert /// marked as interesting, and resets the interestingness. 91*12c85518Srobert const NoteTag *getErrnoNoteTag(CheckerContext &C, const std::string &Message); 92*12c85518Srobert 93*12c85518Srobert /// Set errno state for the common case when a standard function is successful. 94*12c85518Srobert /// Set \c ErrnoCheckState to \c MustNotBeChecked (the \c errno value is not 95*12c85518Srobert /// affected). At the state transition a note tag created by 96*12c85518Srobert /// \c getNoteTagForStdSuccess can be used. 97*12c85518Srobert ProgramStateRef setErrnoForStdSuccess(ProgramStateRef State, CheckerContext &C); 98*12c85518Srobert 99*12c85518Srobert /// Set errno state for the common case when a standard function fails. 100*12c85518Srobert /// Set \c errno value to be not equal to zero and \c ErrnoCheckState to 101*12c85518Srobert /// \c Irrelevant . The irrelevant errno state ensures that no related bug 102*12c85518Srobert /// report is emitted later and no note tag is needed. 103*12c85518Srobert /// \arg \c ErrnoSym Value to be used for \c errno and constrained to be 104*12c85518Srobert /// non-zero. 105*12c85518Srobert ProgramStateRef setErrnoForStdFailure(ProgramStateRef State, CheckerContext &C, 106*12c85518Srobert NonLoc ErrnoSym); 107*12c85518Srobert 108*12c85518Srobert /// Set errno state for the common case when a standard function indicates 109*12c85518Srobert /// failure only by \c errno. Sets \c ErrnoCheckState to \c MustBeChecked, and 110*12c85518Srobert /// invalidates the errno region (clear of previous value). 111*12c85518Srobert /// At the state transition a note tag created by 112*12c85518Srobert /// \c getNoteTagForStdMustBeChecked can be used. 113*12c85518Srobert /// \arg \c InvalE Expression that causes invalidation of \c errno. 114*12c85518Srobert ProgramStateRef setErrnoStdMustBeChecked(ProgramStateRef State, 115*12c85518Srobert CheckerContext &C, const Expr *InvalE); 116*12c85518Srobert 117*12c85518Srobert /// Generate the note tag that can be applied at the state generated by 118*12c85518Srobert /// \c setErrnoForStdSuccess . 119*12c85518Srobert /// \arg \c Fn Name of the (standard) function that is modeled. 120*12c85518Srobert const NoteTag *getNoteTagForStdSuccess(CheckerContext &C, llvm::StringRef Fn); 121*12c85518Srobert 122*12c85518Srobert /// Generate the note tag that can be applied at the state generated by 123*12c85518Srobert /// \c setErrnoStdMustBeChecked . 124*12c85518Srobert /// \arg \c Fn Name of the (standard) function that is modeled. 125*12c85518Srobert const NoteTag *getNoteTagForStdMustBeChecked(CheckerContext &C, 126*12c85518Srobert llvm::StringRef Fn); 127*12c85518Srobert 128*12c85518Srobert } // namespace errno_modeling 129*12c85518Srobert } // namespace ento 130*12c85518Srobert } // namespace clang 131*12c85518Srobert 132*12c85518Srobert #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H 133