30#include "llvm/ADT/STLExtras.h"
31#include "llvm/Support/FormatVariadic.h"
41const char *ErrnoVarName =
"errno";
46 {CDM::CLibrary, {
"__errno_location"}, 0, 0},
47 {CDM::CLibrary, {
"___errno"}, 0, 0},
48 {CDM::CLibrary, {
"__errno"}, 0, 0},
49 {CDM::CLibrary, {
"_errno"}, 0, 0},
50 {CDM::CLibrary, {
"__error"}, 0, 0}};
53 :
public Checker<check::ASTDecl<TranslationUnitDecl>, check::BeginFunction,
54 check::LiveSymbols, eval::Call> {
65 mutable const VarDecl *ErrnoDecl =
nullptr;
85 auto Found = llvm::find_if(LookupRes, [&ACtx](
const Decl *
D) {
86 if (
auto *VD = dyn_cast<VarDecl>(
D))
88 VD->hasExternalStorage() &&
89 VD->getType().getCanonicalType() == ACtx.
IntTy;
92 if (
Found != LookupRes.end())
93 ErrnoDecl = cast<VarDecl>(*
Found);
108 ErrnoR = State->getRegion(ErrnoDecl,
C.getLocationContext());
109 assert(ErrnoR &&
"Memory region should exist for the 'errno' variable.");
127 nullptr,
C.getLocationContext(),
137 State = State->set<ErrnoRegion>(ErrnoR);
140 C.addTransition(State);
149 const MemRegion *ErrnoR = State->get<ErrnoRegion>();
153 State = State->BindExpr(
Call.getOriginExpr(),
C.getLocationContext(),
155 C.addTransition(State);
165 if (
const auto *ErrnoR = State->get<ErrnoRegion>())
171namespace errno_modeling {
174 const MemRegion *ErrnoR = State->get<ErrnoRegion>();
177 QualType IntTy = State->getAnalysisManager().getASTContext().IntTy;
178 return State->getSVal(ErrnoR, IntTy);
184 const MemRegion *ErrnoR = State->get<ErrnoRegion>();
190 return State->set<ErrnoState>(EState);
195 const MemRegion *ErrnoR = State->get<ErrnoRegion>();
198 State = State->bindLoc(
200 C.getSValBuilder().makeIntVal(
Value,
C.getASTContext().IntTy),
201 C.getLocationContext());
202 return State->set<ErrnoState>(EState);
206 const MemRegion *ErrnoR = State->get<ErrnoRegion>();
213 return State->get<ErrnoState>();
217 return State->set<ErrnoState>(EState);
225 return ErrnoLocationCalls.contains(CE);
232 BR.markNotInteresting(ErrnoR);
250 .castAs<DefinedOrUnknownSVal>();
251 State = State->assume(Cond,
true);
259 const Expr *InvalE) {
260 const MemRegion *ErrnoR = State->get<ErrnoRegion>();
263 State = State->invalidateRegions(ErrnoR, InvalE,
C.blockCount(),
264 C.getLocationContext(),
false);
278bool ento::shouldRegisterErrnoModeling(
const CheckerManager &mgr) {
#define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type)
Declares a program state trait for type Type called Name, and introduce a type named NameTy.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Decl - This represents one declaration (or definition), e.g.
This represents one expression.
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
A (possibly-)qualified type.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
The top declaration context.
Represents a variable declaration or definition.
ASTContext & getASTContext() override
BugReporter is a utility class for generating PathDiagnostics for analysis.
An immutable set of CallDescriptions.
Represents an abstract call to a function or method along a particular path.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const ElementRegion * getElementRegion(QualType elementType, NonLoc Idx, const SubRegion *superRegion, const ASTContext &Ctx)
getElementRegion - Retrieve the memory region associated with the associated element type,...
const GlobalsSpaceRegion * getGlobalsRegion(MemRegion::Kind K=MemRegion::GlobalInternalSpaceRegionKind, const CodeTextRegion *R=nullptr)
getGlobalsRegion - Retrieve the memory region associated with global variables.
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym, const MemSpaceRegion *MemSpace=nullptr)
Retrieve or create a "symbolic" memory region.
MemRegion - The root abstract class for all memory regions.
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
The tag upon which the TagVisitor reacts.
const ExplodedNode * getErrorNode() const
bool isInteresting(SymbolRef sym) const
const SymbolConjured * conjureSymbol(const Stmt *stmt, const LocationContext *LCtx, QualType type, unsigned visitCount, const void *symbolTag=nullptr)
DefinedOrUnknownSVal makeZeroVal(QualType type)
Construct an SVal representing '0' for the specified type.
QualType getConditionType() const
NonLoc makeZeroArrayIndex()
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
A symbol representing the result of an expression in the case when we do not know anything about what...
A class responsible for cleaning up unused symbols.
void markLive(SymbolRef sym)
Unconditionally marks a symbol as live.
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.
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.
bool isErrnoLocationCall(const CallEvent &CE)
Determine if Call is a call to an internal function that returns the location of errno (in environmen...
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.
@ 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...
The JSON file list parser is used to communicate input to InstallAPI.