clang  16.0.0git
ErrnoModeling.h
Go to the documentation of this file.
1 //=== ErrnoModeling.h - Tracking value of 'errno'. -----------------*- 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 // Defines inter-checker API for using the system value 'errno'.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
14 #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
15 
19 
20 namespace clang {
21 namespace ento {
22 namespace errno_modeling {
23 
24 /// Describe how reads and writes of \c errno are handled by the checker.
25 enum ErrnoCheckState : unsigned {
26  /// We do not know anything about 'errno'.
27  /// Read and write is always allowed.
29 
30  /// Value of 'errno' should be checked to find out if a previous function call
31  /// has failed.
32  /// When this state is set \c errno must be read by the program before a next
33  /// standard function call or other overwrite of \c errno follows, otherwise
34  /// a bug report is emitted.
36 
37  /// Value of 'errno' is not allowed to be read, it can contain an unspecified
38  /// value.
39  /// When this state is set \c errno is not allowed to be read by the program
40  /// until it is overwritten or invalidated.
42 };
43 
44 /// Returns the value of 'errno', if 'errno' was found in the AST.
46 
47 /// Returns the errno check state, \c Errno_Irrelevant if 'errno' was not found
48 /// (this is not the only case for that value).
50 
51 /// Returns the location that points to the \c MemoryRegion where the 'errno'
52 /// value is stored. Returns \c None if 'errno' was not found. Otherwise it
53 /// always returns a valid memory region in the system global memory space.
55 
56 /// Set value of 'errno' to any SVal, if possible.
57 /// The errno check state is set always when the 'errno' value is set.
59  const LocationContext *LCtx, SVal Value,
60  ErrnoCheckState EState);
61 
62 /// Set value of 'errno' to a concrete (signed) integer, if possible.
63 /// The errno check state is set always when the 'errno' value is set.
66 
67 /// Set the errno check state, do not modify the errno value.
69 
70 /// Determine if a `Decl` node related to 'errno'.
71 /// This is true if the declaration is the errno variable or a function
72 /// that returns a pointer to the 'errno' value (usually the 'errno' macro is
73 /// defined with this function). \p D is not required to be a canonical
74 /// declaration.
75 bool isErrno(const Decl *D);
76 
77 /// Produce a textual description about how \c errno is allowed to be used
78 /// (in a \c ErrnoCheckState).
79 /// The returned string is insertable into a longer warning message in the form
80 /// "the value 'errno' <...>".
81 /// Currently only the \c errno_modeling::MustNotBeChecked state is supported,
82 /// others are not used by the clients.
84 
85 /// Create a NoteTag that displays the message if the 'errno' memory region is
86 /// marked as interesting, and resets the interestingness.
87 const NoteTag *getErrnoNoteTag(CheckerContext &C, const std::string &Message);
88 
89 /// Set errno state for the common case when a standard function is successful.
90 /// Set \c ErrnoCheckState to \c MustNotBeChecked (the \c errno value is not
91 /// affected). At the state transition a note tag created by
92 /// \c getNoteTagForStdSuccess can be used.
94 
95 /// Set errno state for the common case when a standard function fails.
96 /// Set \c errno value to be not equal to zero and \c ErrnoCheckState to
97 /// \c Irrelevant . The irrelevant errno state ensures that no related bug
98 /// report is emitted later and no note tag is needed.
99 /// \arg \c ErrnoSym Value to be used for \c errno and constrained to be
100 /// non-zero.
102  NonLoc ErrnoSym);
103 
104 /// Generate the note tag that can be applied at the state generated by
105 /// \c setErrnoForStdSuccess .
106 /// \arg \c Fn Name of the (standard) function that is modeled.
107 const NoteTag *getNoteTagForStdSuccess(CheckerContext &C, llvm::StringRef Fn);
108 
109 } // namespace errno_modeling
110 } // namespace ento
111 } // namespace clang
112 
113 #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
clang::LocationContext
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Definition: AnalysisDeclContext.h:215
SVals.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ento::NonLoc
Definition: SVals.h:266
clang::ento::errno_modeling::Irrelevant
@ Irrelevant
We do not know anything about 'errno'.
Definition: ErrnoModeling.h:28
clang::ento::errno_modeling::MustBeChecked
@ MustBeChecked
Value of 'errno' should be checked to find out if a previous function call has failed.
Definition: ErrnoModeling.h:35
llvm::Optional
Definition: LLVM.h:40
clang::ento::errno_modeling::getErrnoNoteTag
const NoteTag * getErrnoNoteTag(CheckerContext &C, const std::string &Message)
Create a NoteTag that displays the message if the 'errno' memory region is marked as interesting,...
Definition: ErrnoModeling.cpp:273
hlsl::uint64_t
unsigned long uint64_t
Definition: hlsl_basic_types.h:25
clang::ento::errno_modeling::getNoteTagForStdSuccess
const NoteTag * getNoteTagForStdSuccess(CheckerContext &C, llvm::StringRef Fn)
Generate the note tag that can be applied at the state generated by setErrnoForStdSuccess .
Definition: ErrnoModeling.cpp:302
clang::ento::errno_modeling::setErrnoState
ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState)
Set the errno check state, do not modify the errno value.
Definition: ErrnoModeling.cpp:249
clang::ento::errno_modeling::setErrnoForStdSuccess
ProgramStateRef setErrnoForStdSuccess(ProgramStateRef State, CheckerContext &C)
Set errno state for the common case when a standard function is successful.
Definition: ErrnoModeling.cpp:284
clang::ento::errno_modeling::describeErrnoCheckState
const char * describeErrnoCheckState(ErrnoCheckState CS)
Produce a textual description about how errno is allowed to be used (in a ErrnoCheckState).
Definition: ErrnoModeling.cpp:267
clang::ento::errno_modeling::MustNotBeChecked
@ MustNotBeChecked
Value of 'errno' is not allowed to be read, it can contain an unspecified value.
Definition: ErrnoModeling.h:41
clang::ento::errno_modeling::setErrnoValue
ProgramStateRef setErrnoValue(ProgramStateRef State, const LocationContext *LCtx, SVal Value, ErrnoCheckState EState)
Set value of 'errno' to any SVal, if possible.
Definition: ErrnoModeling.cpp:218
clang::ento::errno_modeling::ErrnoCheckState
ErrnoCheckState
Describe how reads and writes of errno are handled by the checker.
Definition: ErrnoModeling.h:25
clang::ento::errno_modeling::getErrnoValue
Optional< SVal > getErrnoValue(ProgramStateRef State)
Returns the value of 'errno', if 'errno' was found in the AST.
Definition: ErrnoModeling.cpp:210
clang::ento::errno_modeling::setErrnoForStdFailure
ProgramStateRef setErrnoForStdFailure(ProgramStateRef State, CheckerContext &C, NonLoc ErrnoSym)
Set errno state for the common case when a standard function fails.
Definition: ErrnoModeling.cpp:289
clang::ento::errno_modeling::isErrno
bool isErrno(const Decl *D)
Determine if a Decl node related to 'errno'.
Definition: ErrnoModeling.cpp:257
Value
Value
Definition: UninitializedValues.cpp:103
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
State
LineState State
Definition: UnwrappedLineFormatter.cpp:1147
CheckerContext.h
ProgramState.h
clang
Definition: CalledOnceCheck.h:17
clang::ento::errno_modeling::getErrnoLoc
Optional< Loc > getErrnoLoc(ProgramStateRef State)
Returns the location that points to the MemoryRegion where the 'errno' value is stored.
Definition: ErrnoModeling.cpp:242
clang::ento::CheckerContext
Definition: CheckerContext.h:23
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:73
clang::ento::errno_modeling::getErrnoState
ErrnoCheckState getErrnoState(ProgramStateRef State)
Returns the errno check state, Errno_Irrelevant if 'errno' was not found (this is not the only case f...
Definition: ErrnoModeling.cpp:253
clang::ento::NoteTag
The tag upon which the TagVisitor reacts.
Definition: BugReporter.h:763
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47