clang 19.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#include <optional>
20
21namespace clang {
22namespace ento {
23namespace errno_modeling {
24
25/// Describe how reads and writes of \c errno are handled by the checker.
26enum ErrnoCheckState : unsigned {
27 /// We do not know anything about 'errno'.
28 /// Read and write is always allowed.
30
31 /// Value of 'errno' should be checked to find out if a previous function call
32 /// has failed.
33 /// When this state is set \c errno must be read by the program before a next
34 /// standard function call or other overwrite of \c errno follows, otherwise
35 /// a bug report is emitted.
37
38 /// Value of 'errno' is not allowed to be read, it can contain an unspecified
39 /// value.
40 /// When this state is set \c errno is not allowed to be read by the program
41 /// until it is overwritten or invalidated.
43};
44
45/// Returns the value of 'errno', if 'errno' was found in the AST.
46std::optional<SVal> getErrnoValue(ProgramStateRef State);
47
48/// Returns the errno check state, \c Errno_Irrelevant if 'errno' was not found
49/// (this is not the only case for that value).
51
52/// Returns the location that points to the \c MemoryRegion where the 'errno'
53/// value is stored. Returns \c std::nullopt if 'errno' was not found. Otherwise
54/// it always returns a valid memory region in the system global memory space.
55std::optional<Loc> getErrnoLoc(ProgramStateRef State);
56
57/// Set value of 'errno' to any SVal, if possible.
58/// The errno check state is set always when the 'errno' value is set.
60 const LocationContext *LCtx, SVal Value,
61 ErrnoCheckState EState);
62
63/// Set value of 'errno' to a concrete (signed) integer, if possible.
64/// The errno check state is set always when the 'errno' value is set.
66 uint64_t Value, ErrnoCheckState EState);
67
68/// Set the errno check state, do not modify the errno value.
70
71/// Clear state of errno (make it irrelevant).
73
74/// Determine if a `Decl` node related to 'errno'.
75/// This is true if the declaration is the errno variable or a function
76/// that returns a pointer to the 'errno' value (usually the 'errno' macro is
77/// defined with this function). \p D is not required to be a canonical
78/// declaration.
79bool isErrno(const Decl *D);
80
81/// Create a NoteTag that displays the message if the 'errno' memory region is
82/// marked as interesting, and resets the interestingness.
83const NoteTag *getErrnoNoteTag(CheckerContext &C, const std::string &Message);
84
85/// Set errno state for the common case when a standard function is successful.
86/// Set \c ErrnoCheckState to \c MustNotBeChecked (the \c errno value is not
87/// affected).
89
90/// Set errno state for the common case when a standard function fails.
91/// Set \c errno value to be not equal to zero and \c ErrnoCheckState to
92/// \c Irrelevant . The irrelevant errno state ensures that no related bug
93/// report is emitted later and no note tag is needed.
94/// \arg \c ErrnoSym Value to be used for \c errno and constrained to be
95/// non-zero.
97 NonLoc ErrnoSym);
98
99/// Set errno state for the common case when a standard function indicates
100/// failure only by \c errno. Sets \c ErrnoCheckState to \c MustBeChecked, and
101/// invalidates the errno region (clear of previous value).
102/// \arg \c InvalE Expression that causes invalidation of \c errno.
104 CheckerContext &C, const Expr *InvalE);
105
106} // namespace errno_modeling
107} // namespace ento
108} // namespace clang
109
110#endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
This represents one expression.
Definition: Expr.h:110
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
The tag upon which the TagVisitor reacts.
Definition: BugReporter.h:767
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
std::optional< Loc > getErrnoLoc(ProgramStateRef State)
Returns the location that points to the MemoryRegion where the 'errno' value is stored.
ProgramStateRef setErrnoValue(ProgramStateRef State, const LocationContext *LCtx, SVal Value, ErrnoCheckState EState)
Set value of 'errno' to any SVal, if possible.
bool isErrno(const Decl *D)
Determine if a Decl node related to 'errno'.
ProgramStateRef clearErrnoState(ProgramStateRef State)
Clear state of errno (make it irrelevant).
ProgramStateRef setErrnoForStdSuccess(ProgramStateRef State, CheckerContext &C)
Set errno state for the common case when a standard function is successful.
ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState)
Set the errno check state, do not modify the errno value.
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,...
ProgramStateRef setErrnoStdMustBeChecked(ProgramStateRef State, CheckerContext &C, const Expr *InvalE)
Set errno state for the common case when a standard function indicates failure only by errno.
ProgramStateRef setErrnoForStdFailure(ProgramStateRef State, CheckerContext &C, NonLoc ErrnoSym)
Set errno state for the common case when a standard function fails.
std::optional< SVal > getErrnoValue(ProgramStateRef State)
Returns the value of 'errno', if 'errno' was found in the AST.
ErrnoCheckState
Describe how reads and writes of errno are handled by the checker.
Definition: ErrnoModeling.h:26
@ MustBeChecked
Value of 'errno' should be checked to find out if a previous function call has failed.
Definition: ErrnoModeling.h:36
@ Irrelevant
We do not know anything about 'errno'.
Definition: ErrnoModeling.h:29
@ MustNotBeChecked
Value of 'errno' is not allowed to be read, it can contain an unspecified value.
Definition: ErrnoModeling.h:42
ErrnoCheckState getErrnoState(ProgramStateRef State)
Returns the errno check state, Errno_Irrelevant if 'errno' was not found (this is not the only case f...
The JSON file list parser is used to communicate input to InstallAPI.