25#include "llvm/ADT/STLExtras.h"
35 :
public Checker<check::Location, check::PreCall, check::RegionChanges> {
37 void checkLocation(SVal Loc,
bool IsLoad,
const Stmt *S,
38 CheckerContext &)
const;
39 void checkPreCall(
const CallEvent &
Call, CheckerContext &
C)
const;
43 ArrayRef<const MemRegion *> ExplicitRegions,
44 ArrayRef<const MemRegion *> Regions,
const StackFrame *SF,
45 const CallEvent *
Call)
const;
50 bool AllowErrnoReadOutsideConditions =
true;
54 const MemRegion *ErrnoRegion,
55 const CallEvent *CallMayChangeErrno)
const;
57 BugType BT_InvalidErrnoRead{
this,
"Value of 'errno' could be undefined",
59 BugType BT_ErrnoNotChecked{
this,
"Value of 'errno' was not checked",
73 bool CondFound =
false;
74 while (S && !CondFound) {
78 const auto *ParentS = Parents[0].get<
Stmt>();
81 switch (ParentS->getStmtClass()) {
82 case Expr::IfStmtClass:
85 case Expr::ForStmtClass:
88 case Expr::DoStmtClass:
91 case Expr::WhileStmtClass:
94 case Expr::SwitchStmtClass:
97 case Expr::ConditionalOperatorClass:
100 case Expr::BinaryConditionalOperatorClass:
111void ErrnoChecker::generateErrnoNotCheckedBug(
113 const CallEvent *CallMayChangeErrno)
const {
114 if (ExplodedNode *N =
C.generateNonFatalErrorNode(State)) {
115 SmallString<100> StrBuf;
116 llvm::raw_svector_ostream
OS(StrBuf);
117 if (CallMayChangeErrno) {
118 OS <<
"Value of 'errno' was not checked and may be overwritten by "
121 dyn_cast_or_null<FunctionDecl>(CallMayChangeErrno->
getDecl());
122 assert(CallD && CallD->getIdentifier());
123 OS << CallD->getIdentifier()->getName() <<
"'";
125 OS <<
"Value of 'errno' was not checked and is overwritten here";
127 auto BR = std::make_unique<PathSensitiveBugReport>(BT_ErrnoNotChecked,
129 BR->markInteresting(ErrnoRegion);
130 C.emitReport(std::move(BR));
134void ErrnoChecker::checkLocation(SVal Loc,
bool IsLoad,
const Stmt *S,
135 CheckerContext &
C)
const {
136 std::optional<ento::Loc> ErrnoLoc =
getErrnoLoc(
C.getState());
140 auto L = Loc.
getAs<ento::Loc>();
141 if (!L || *ErrnoLoc != *L)
152 if (ExplodedNode *N =
C.addSink()) {
153 auto BR = std::make_unique<PathSensitiveBugReport>(
155 "An undefined value may be read from 'errno'", N);
156 BR->markInteresting(ErrnoLoc->getAsRegion());
157 C.emitReport(std::move(BR));
166 C.addTransition(State);
177 ErrnoLoc->getAsRegion(),
nullptr);
183 C.addTransition(State);
191void ErrnoChecker::checkPreCall(
const CallEvent &
Call,
192 CheckerContext &
C)
const {
193 const auto *CallF = dyn_cast_or_null<FunctionDecl>(
Call.getDecl());
197 CallF = CallF->getCanonicalDecl();
206 if (CallF->isExternC() && CallF->isGlobal() &&
207 C.getSourceManager().isInSystemHeader(CallF->getLocation()) &&
210 std::optional<ento::Loc> ErrnoLoc =
getErrnoLoc(
C.getState());
211 assert(ErrnoLoc &&
"ErrnoLoc should exist if an errno state is set.");
213 ErrnoLoc->getAsRegion(), &
Call);
220 ArrayRef<const MemRegion *> ExplicitRegions,
221 ArrayRef<const MemRegion *> Regions,
const StackFrame *SF,
222 const CallEvent *
Call)
const {
223 std::optional<ento::Loc> ErrnoLoc =
getErrnoLoc(State);
226 const MemRegion *ErrnoRegion = ErrnoLoc->getAsRegion();
230 if (llvm::is_contained(Regions, ErrnoRegion))
235 if (llvm::is_contained(Regions, ErrnoRegion->
getMemorySpace(State)))
241void ento::registerErrnoChecker(CheckerManager &mgr) {
244 Checker->AllowErrnoReadOutsideConditions = Opts.getCheckerBooleanOption(
245 Checker,
"AllowErrnoReadOutsideConditionExpressions");
248bool ento::shouldRegisterErrnoChecker(
const CheckerManager &mgr) {
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)
Defines the SourceManager interface.
Container for either a single DynTypedNode or for an ArrayRef to DynTypedNode.
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)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
MemRegion - The root abstract class for all memory regions.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace(ProgramStateRef State) const
Returns the most specific memory space for this memory region in the given ProgramStateRef.
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
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
U cast(CodeGen::Address addr)