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 
16 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19 #include <optional>
20 
21 namespace clang {
22 namespace ento {
23 namespace errno_modeling {
24 
25 /// Describe how reads and writes of \c errno are handled by the checker.
26 enum ErrnoCheckState : unsigned {
27   /// We do not know anything about 'errno'.
28   /// Read and write is always allowed.
29   Irrelevant = 0,
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.
36   MustBeChecked = 1,
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.
42   MustNotBeChecked = 2
43 };
44 
45 /// Returns the value of 'errno', if 'errno' was found in the AST.
46 std::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).
50 ErrnoCheckState getErrnoState(ProgramStateRef State);
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.
55 std::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.
59 ProgramStateRef setErrnoValue(ProgramStateRef State,
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.
65 ProgramStateRef setErrnoValue(ProgramStateRef State, CheckerContext &C,
66                               uint64_t Value, ErrnoCheckState EState);
67 
68 /// Set the errno check state, do not modify the errno value.
69 ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState);
70 
71 /// Clear state of errno (make it irrelevant).
72 ProgramStateRef clearErrnoState(ProgramStateRef State);
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.
79 bool isErrno(const Decl *D);
80 
81 /// Produce a textual description about how \c errno is allowed to be used
82 /// (in a \c ErrnoCheckState).
83 /// The returned string is insertable into a longer warning message in the form
84 /// "the value 'errno' <...>".
85 /// Currently only the \c errno_modeling::MustNotBeChecked state is supported,
86 /// others are not used by the clients.
87 const char *describeErrnoCheckState(ErrnoCheckState CS);
88 
89 /// Create a NoteTag that displays the message if the 'errno' memory region is
90 /// marked as interesting, and resets the interestingness.
91 const NoteTag *getErrnoNoteTag(CheckerContext &C, const std::string &Message);
92 
93 /// Set errno state for the common case when a standard function is successful.
94 /// Set \c ErrnoCheckState to \c MustNotBeChecked (the \c errno value is not
95 /// affected). At the state transition a note tag created by
96 /// \c getNoteTagForStdSuccess can be used.
97 ProgramStateRef setErrnoForStdSuccess(ProgramStateRef State, CheckerContext &C);
98 
99 /// Set errno state for the common case when a standard function fails.
100 /// Set \c errno value to be not equal to zero and \c ErrnoCheckState to
101 /// \c Irrelevant . The irrelevant errno state ensures that no related bug
102 /// report is emitted later and no note tag is needed.
103 /// \arg \c ErrnoSym Value to be used for \c errno and constrained to be
104 /// non-zero.
105 ProgramStateRef setErrnoForStdFailure(ProgramStateRef State, CheckerContext &C,
106                                       NonLoc ErrnoSym);
107 
108 /// Set errno state for the common case when a standard function indicates
109 /// failure only by \c errno. Sets \c ErrnoCheckState to \c MustBeChecked, and
110 /// invalidates the errno region (clear of previous value).
111 /// At the state transition a note tag created by
112 /// \c getNoteTagForStdMustBeChecked can be used.
113 /// \arg \c InvalE Expression that causes invalidation of \c errno.
114 ProgramStateRef setErrnoStdMustBeChecked(ProgramStateRef State,
115                                          CheckerContext &C, const Expr *InvalE);
116 
117 /// Generate the note tag that can be applied at the state generated by
118 /// \c setErrnoForStdSuccess .
119 /// \arg \c Fn Name of the (standard) function that is modeled.
120 const NoteTag *getNoteTagForStdSuccess(CheckerContext &C, llvm::StringRef Fn);
121 
122 /// Generate the note tag that can be applied at the state generated by
123 /// \c setErrnoStdMustBeChecked .
124 /// \arg \c Fn Name of the (standard) function that is modeled.
125 const NoteTag *getNoteTagForStdMustBeChecked(CheckerContext &C,
126                                              llvm::StringRef Fn);
127 
128 } // namespace errno_modeling
129 } // namespace ento
130 } // namespace clang
131 
132 #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ERRNOMODELING_H
133