clang 23.0.0git
ErrnoTesterChecker.cpp
Go to the documentation of this file.
1//=== ErrnoTesterChecker.cpp ------------------------------------*- 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// This defines ErrnoTesterChecker, which is used to test functionality of the
10// errno_check API.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ErrnoModeling.h"
20#include <optional>
21
22using namespace clang;
23using namespace ento;
24using namespace errno_modeling;
25
26namespace {
27
28class ErrnoTesterChecker : public Checker<eval::Call> {
29public:
30 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
31
32private:
33 /// Evaluate function \code void ErrnoTesterChecker_setErrno(int) \endcode.
34 /// Set value of \c errno to the argument.
35 static void evalSetErrno(CheckerContext &C, const CallEvent &Call);
36 /// Evaluate function \code int ErrnoTesterChecker_getErrno() \endcode.
37 /// Return the value of \c errno.
38 static void evalGetErrno(CheckerContext &C, const CallEvent &Call);
39 /// Evaluate function \code int ErrnoTesterChecker_setErrnoIfError() \endcode.
40 /// Simulate a standard library function tha returns 0 on success and 1 on
41 /// failure. On the success case \c errno is not allowed to be used (may be
42 /// undefined). On the failure case \c errno is set to a fixed value 11 and
43 /// is not needed to be checked.
44 static void evalSetErrnoIfError(CheckerContext &C, const CallEvent &Call);
45 /// Evaluate function \code int ErrnoTesterChecker_setErrnoIfErrorRange()
46 /// \endcode. Same as \c ErrnoTesterChecker_setErrnoIfError but \c errno is
47 /// set to a range (to be nonzero) at the failure case.
48 static void evalSetErrnoIfErrorRange(CheckerContext &C,
49 const CallEvent &Call);
50 /// Evaluate function \code int ErrnoTesterChecker_setErrnoCheckState()
51 /// \endcode. This function simulates the following:
52 /// - Return 0 and leave \c errno with undefined value.
53 /// This is the case of a successful standard function call.
54 /// For example if \c ftell returns not -1.
55 /// - Return 1 and sets \c errno to a specific error code (1).
56 /// This is the case of a failed standard function call.
57 /// The function indicates the failure by a special return value
58 /// that is returned only at failure.
59 /// \c errno can be checked but it is not required.
60 /// For example if \c ftell returns -1.
61 /// - Return 2 and may set errno to a value (actually it does not set it).
62 /// This is the case of a standard function call where the failure can only
63 /// be checked by reading from \c errno. The value of \c errno is changed by
64 /// the function only at failure, the user should set \c errno to 0 before
65 /// the call (\c ErrnoChecker does not check for this rule).
66 /// \c strtol is an example of this case, if it returns \c LONG_MIN (or
67 /// \c LONG_MAX). This case applies only if \c LONG_MIN or \c LONG_MAX is
68 /// returned, otherwise the first case in this list applies.
69 static void evalSetErrnoCheckState(CheckerContext &C, const CallEvent &Call);
70
71 using EvalFn = std::function<void(CheckerContext &, const CallEvent &)>;
72 const CallDescriptionMap<EvalFn> TestCalls{
73 {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrno"}, 1},
74 &ErrnoTesterChecker::evalSetErrno},
75 {{CDM::SimpleFunc, {"ErrnoTesterChecker_getErrno"}, 0},
76 &ErrnoTesterChecker::evalGetErrno},
77 {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoIfError"}, 0},
78 &ErrnoTesterChecker::evalSetErrnoIfError},
79 {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoIfErrorRange"}, 0},
80 &ErrnoTesterChecker::evalSetErrnoIfErrorRange},
81 {{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoCheckState"}, 0},
82 &ErrnoTesterChecker::evalSetErrnoCheckState}};
83};
84
85} // namespace
86
87void ErrnoTesterChecker::evalSetErrno(CheckerContext &C,
88 const CallEvent &Call) {
89 C.addTransition(setErrnoValue(C.getState(), C.getStackFrame(),
90 Call.getArgSVal(0), Irrelevant));
91}
92
93void ErrnoTesterChecker::evalGetErrno(CheckerContext &C,
94 const CallEvent &Call) {
95 ProgramStateRef State = C.getState();
96
97 std::optional<SVal> ErrnoVal = getErrnoValue(State);
98 assert(ErrnoVal && "Errno value should be available.");
99 State = State->BindExpr(Call.getOriginExpr(), C.getStackFrame(), *ErrnoVal);
100
101 C.addTransition(State);
102}
103
104void ErrnoTesterChecker::evalSetErrnoIfError(CheckerContext &C,
105 const CallEvent &Call) {
106 ProgramStateRef State = C.getState();
107 SValBuilder &SVB = C.getSValBuilder();
108
109 ProgramStateRef StateSuccess = State->BindExpr(
110 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(0, true));
111 StateSuccess = setErrnoState(StateSuccess, MustNotBeChecked);
112
113 ProgramStateRef StateFailure = State->BindExpr(
114 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(1, true));
115 StateFailure = setErrnoValue(StateFailure, C, 11, Irrelevant);
116
117 C.addTransition(StateSuccess);
118 C.addTransition(StateFailure);
119}
120
121void ErrnoTesterChecker::evalSetErrnoIfErrorRange(CheckerContext &C,
122 const CallEvent &Call) {
123 ProgramStateRef State = C.getState();
124 SValBuilder &SVB = C.getSValBuilder();
125
126 ProgramStateRef StateSuccess = State->BindExpr(
127 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(0, true));
128 StateSuccess = setErrnoState(StateSuccess, MustNotBeChecked);
129
130 ProgramStateRef StateFailure = State->BindExpr(
131 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(1, true));
132 DefinedOrUnknownSVal ErrnoVal = SVB.conjureSymbolVal(Call, C.blockCount());
133 StateFailure = StateFailure->assume(ErrnoVal, true);
134 assert(StateFailure && "Failed to assume on an initial value.");
135 StateFailure =
136 setErrnoValue(StateFailure, C.getStackFrame(), ErrnoVal, Irrelevant);
137
138 C.addTransition(StateSuccess);
139 C.addTransition(StateFailure);
140}
141
142void ErrnoTesterChecker::evalSetErrnoCheckState(CheckerContext &C,
143 const CallEvent &Call) {
144 ProgramStateRef State = C.getState();
145 SValBuilder &SVB = C.getSValBuilder();
146
147 ProgramStateRef StateSuccess = State->BindExpr(
148 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(0, true));
149 StateSuccess = setErrnoState(StateSuccess, MustNotBeChecked);
150
151 ProgramStateRef StateFailure1 = State->BindExpr(
152 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(1, true));
153 StateFailure1 = setErrnoValue(StateFailure1, C, 1, Irrelevant);
154
155 ProgramStateRef StateFailure2 = State->BindExpr(
156 Call.getOriginExpr(), C.getStackFrame(), SVB.makeIntVal(2, true));
157 StateFailure2 = setErrnoValue(StateFailure2, C, 2, MustBeChecked);
158
159 C.addTransition(StateSuccess,
160 getErrnoNoteTag(C, "Assuming that this function succeeds but "
161 "sets 'errno' to an unspecified value."));
162 C.addTransition(StateFailure1);
163 C.addTransition(
164 StateFailure2,
165 getErrnoNoteTag(C, "Assuming that this function returns 2. 'errno' "
166 "should be checked to test for failure."));
167}
168
169bool ErrnoTesterChecker::evalCall(const CallEvent &Call,
170 CheckerContext &C) const {
171 const EvalFn *Fn = TestCalls.lookup(Call);
172 if (Fn) {
173 (*Fn)(C, Call);
174 return C.isDifferent();
175 }
176 return false;
177}
178
179void ento::registerErrnoTesterChecker(CheckerManager &Mgr) {
180 Mgr.registerChecker<ErrnoTesterChecker>();
181}
182
183bool ento::shouldRegisterErrnoTesterChecker(const CheckerManager &Mgr) {
184 return true;
185}
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:550
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, ConstCFGElementRef elem, const StackFrame *SF, unsigned count)
Create a new symbol with a unique 'name'.
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,...
std::optional< SVal > getErrnoValue(ProgramStateRef State)
Returns the value of 'errno', if 'errno' was found in the AST.
@ MustBeChecked
Value of 'errno' should be checked to find out if a previous function call has failed.
@ Irrelevant
We do not know anything about 'errno'.
@ MustNotBeChecked
Value of 'errno' is not allowed to be read, it can contain an unspecified value.
ProgramStateRef setErrnoValue(ProgramStateRef State, const StackFrame *SF, SVal Value, ErrnoCheckState EState)
Set value of 'errno' to any SVal, if possible.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The JSON file list parser is used to communicate input to InstallAPI.
int const char * function
Definition c++config.h:31