24#include "llvm/ADT/STLExtras.h"
29using namespace errno_modeling;
34 :
public Checker<check::Location, check::PreCall, check::RegionChanges> {
36 void checkLocation(
SVal Loc,
bool IsLoad,
const Stmt *S,
49 bool AllowErrnoReadOutsideConditions =
true;
54 const CallEvent *CallMayChangeErrno)
const;
56 BugType BT_InvalidErrnoRead{
this,
"Value of 'errno' could be undefined",
58 BugType BT_ErrnoNotChecked{
this,
"Value of 'errno' was not checked",
72 bool CondFound =
false;
73 while (S && !CondFound) {
77 const auto *ParentS = Parents[0].get<
Stmt>();
78 if (!ParentS || isa<CallExpr>(ParentS))
80 switch (ParentS->getStmtClass()) {
81 case Expr::IfStmtClass:
82 CondFound = (S == cast<IfStmt>(ParentS)->getCond());
84 case Expr::ForStmtClass:
85 CondFound = (S == cast<ForStmt>(ParentS)->getCond());
87 case Expr::DoStmtClass:
88 CondFound = (S == cast<DoStmt>(ParentS)->getCond());
90 case Expr::WhileStmtClass:
91 CondFound = (S == cast<WhileStmt>(ParentS)->getCond());
93 case Expr::SwitchStmtClass:
94 CondFound = (S == cast<SwitchStmt>(ParentS)->getCond());
96 case Expr::ConditionalOperatorClass:
97 CondFound = (S == cast<ConditionalOperator>(ParentS)->getCond());
99 case Expr::BinaryConditionalOperatorClass:
100 CondFound = (S == cast<BinaryConditionalOperator>(ParentS)->getCommon());
110void ErrnoChecker::generateErrnoNotCheckedBug(
112 const CallEvent *CallMayChangeErrno)
const {
115 llvm::raw_svector_ostream OS(StrBuf);
116 if (CallMayChangeErrno) {
117 OS <<
"Value of 'errno' was not checked and may be overwritten by "
120 dyn_cast_or_null<FunctionDecl>(CallMayChangeErrno->
getDecl());
121 assert(CallD && CallD->getIdentifier());
122 OS << CallD->getIdentifier()->getName() <<
"'";
124 OS <<
"Value of 'errno' was not checked and is overwritten here";
126 auto BR = std::make_unique<PathSensitiveBugReport>(BT_ErrnoNotChecked,
128 BR->markInteresting(ErrnoRegion);
129 C.emitReport(std::move(BR));
133void ErrnoChecker::checkLocation(
SVal Loc,
bool IsLoad,
const Stmt *S,
135 std::optional<ento::Loc> ErrnoLoc =
getErrnoLoc(
C.getState());
140 if (!L || *ErrnoLoc != *L)
152 auto BR = std::make_unique<PathSensitiveBugReport>(
154 "An undefined value may be read from 'errno'", N);
155 BR->markInteresting(ErrnoLoc->getAsRegion());
156 C.emitReport(std::move(BR));
165 C.addTransition(State);
176 ErrnoLoc->getAsRegion(),
nullptr);
182 C.addTransition(State);
192 const auto *CallF = dyn_cast_or_null<FunctionDecl>(
Call.getDecl());
196 CallF = CallF->getCanonicalDecl();
205 if (CallF->isExternC() && CallF->isGlobal() &&
206 C.getSourceManager().isInSystemHeader(CallF->getLocation()) &&
209 std::optional<ento::Loc> ErrnoLoc =
getErrnoLoc(
C.getState());
210 assert(ErrnoLoc &&
"ErrnoLoc should exist if an errno state is set.");
212 ErrnoLoc->getAsRegion(), &
Call);
222 std::optional<ento::Loc> ErrnoLoc =
getErrnoLoc(State);
225 const MemRegion *ErrnoRegion = ErrnoLoc->getAsRegion();
229 if (llvm::is_contained(Regions, ErrnoRegion))
244 Checker,
"AllowErrnoReadOutsideConditionExpressions");
static bool isInCondition(const Stmt *S, CheckerContext &C)
Check if a statement (expression) or an ancestor of it is in a condition part of a (conditional,...
static ProgramStateRef setErrnoStateIrrelevant(ProgramStateRef State)
Stores options for the analyzer from the command line.
bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as a boolean.
Container for either a single DynTypedNode or for an ArrayRef to DynTypedNode.
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
DynTypedNodeList getParents(const NodeT &Node)
Returns the parents of the given node (within the traversal scope).
Stmt - This represents one statement.
Represents an abstract call to a function or method along a particular path.
virtual const Decl * getDecl() const
Returns the declaration of the function or method that will be called.
const AnalyzerOptions & getAnalyzerOptions() const
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
MemRegion - The root abstract class for all memory regions.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace() const
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
std::optional< Loc > getErrnoLoc(ProgramStateRef State)
Returns the location that points to the MemoryRegion where the 'errno' value is stored.
ProgramStateRef clearErrnoState(ProgramStateRef State)
Clear state of errno (make it irrelevant).
ProgramStateRef setErrnoState(ProgramStateRef State, ErrnoCheckState EState)
Set the errno check state, do not modify the errno value.
bool isErrnoLocationCall(const CallEvent &CE)
Determine if Call is a call to an internal function that returns the location of errno (in environmen...
ErrnoCheckState
Describe how reads and writes of errno are handled by the checker.
@ 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.
ErrnoCheckState getErrnoState(ProgramStateRef State)
Returns the errno check state, Errno_Irrelevant if 'errno' was not found (this is not the only case f...
llvm::DenseSet< SymbolRef > InvalidatedSymbols
The JSON file list parser is used to communicate input to InstallAPI.