21#include "llvm/Support/FormatVariadic.h"
28enum class VAListState {
35constexpr llvm::StringLiteral StateNames[] = {
36 "uninitialized",
"unknown",
"initialized",
"already released"};
40 return StateNames[
static_cast<int>(S)];
46 if (
const VAListState *Res = State->get<VAListStateMap>(Reg))
48 return Reg->getSymbolicBase() ? VAListState::Unknown
49 : VAListState::Uninitialized;
55class VAListChecker :
public Checker<check::PreCall, check::PreStmt<VAArgExpr>,
59 const BugType UninitAccessBug{
this,
"Uninitialized va_list",
62 struct VAListAccepter {
66 static const SmallVector<VAListAccepter, 15> VAListAccepters;
67 static const CallDescriptionSet VaStart;
68 static const CallDescription VaEnd, VaCopy;
71 void checkPreStmt(
const VAArgExpr *VAA, CheckerContext &
C)
const;
72 void checkPreCall(
const CallEvent &
Call, CheckerContext &
C)
const;
73 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &
C)
const;
76 const MemRegion *getVAListAsRegion(SVal SV,
const Expr *VAExpr,
77 CheckerContext &
C)
const;
78 const ExplodedNode *getStartCallSite(
const ExplodedNode *N,
79 const MemRegion *Reg)
const;
81 void reportUninitializedAccess(
const MemRegion *VAList, StringRef Msg,
82 CheckerContext &
C)
const;
83 void reportLeaked(
const RegionVector &Leaked, StringRef Msg1, StringRef Msg2,
84 CheckerContext &
C, ExplodedNode *N)
const;
86 void checkVAListStartCall(
const CallEvent &
Call, CheckerContext &
C)
const;
87 void checkVAListCopyCall(
const CallEvent &
Call, CheckerContext &
C)
const;
88 void checkVAListEndCall(
const CallEvent &
Call, CheckerContext &
C)
const;
90 class VAListBugVisitor :
public BugReporterVisitor {
92 VAListBugVisitor(
const MemRegion *Reg,
bool IsLeak =
false)
93 : Reg(Reg), IsLeak(IsLeak) {}
94 void Profile(llvm::FoldingSetNodeID &ID)
const override {
100 const ExplodedNode *EndPathNode,
101 PathSensitiveBugReport &BR)
override {
107 return std::make_shared<PathDiagnosticEventPiece>(L, BR.
getDescription(),
111 BugReporterContext &BRC,
112 PathSensitiveBugReport &BR)
override;
115 const MemRegion *Reg;
121 VAListChecker::VAListAccepters = {{{CDM::CLibrary, {
"vfprintf"}, 3}, 2},
122 {{CDM::CLibrary, {
"vfscanf"}, 3}, 2},
123 {{CDM::CLibrary, {
"vprintf"}, 2}, 1},
124 {{CDM::CLibrary, {
"vscanf"}, 2}, 1},
125 {{CDM::CLibrary, {
"vsnprintf"}, 4}, 3},
126 {{CDM::CLibrary, {
"vsprintf"}, 3}, 2},
127 {{CDM::CLibrary, {
"vsscanf"}, 3}, 2},
128 {{CDM::CLibrary, {
"vfwprintf"}, 3}, 2},
129 {{CDM::CLibrary, {
"vfwscanf"}, 3}, 2},
130 {{CDM::CLibrary, {
"vwprintf"}, 2}, 1},
131 {{CDM::CLibrary, {
"vwscanf"}, 2}, 1},
132 {{CDM::CLibrary, {
"vswprintf"}, 4}, 3},
135 {{CDM::CLibrary, {
"vswscanf"}, 3}, 2}};
138 {CDM::CLibrary, {
"__builtin_va_start"}},
139 {CDM::CLibrary, {
"__builtin_c23_va_start"}}};
142 {
"__builtin_va_copy"}, 2),
143 VAListChecker::VaEnd(CDM::CLibrary, {
"__builtin_va_end"}, 1);
149 checkVAListStartCall(
Call,
C);
151 checkVAListCopyCall(
Call,
C);
153 checkVAListEndCall(
Call,
C);
155 for (
const auto &FuncInfo : VAListAccepters) {
156 if (!FuncInfo.Func.matches(
Call))
158 const MemRegion *VAList =
159 getVAListAsRegion(
Call.getArgSVal(FuncInfo.ParamIndex),
160 Call.getArgExpr(FuncInfo.ParamIndex),
C);
165 if (S == VAListState::Initialized || S == VAListState::Unknown)
169 formatv(
"Function '{0}' is called with an {1} va_list argument",
171 reportUninitializedAccess(VAList, ErrMsg,
C);
177const MemRegion *VAListChecker::getVAListAsRegion(SVal SV,
const Expr *E,
178 CheckerContext &
C)
const {
183 bool VAListModelledAsArray =
false;
184 if (
const auto *Cast = dyn_cast<CastExpr>(E)) {
185 QualType Ty =
Cast->getType();
186 VAListModelledAsArray =
189 if (
const auto *DeclReg = Reg->
getAs<DeclRegion>()) {
191 Reg =
C.getState()->getSVal(SV.
castAs<Loc>()).getAsRegion();
194 const auto *EReg = dyn_cast_or_null<ElementRegion>(Reg);
195 return (EReg && VAListModelledAsArray) ? EReg->getSuperRegion() : Reg;
198void VAListChecker::checkPreStmt(
const VAArgExpr *VAA,
199 CheckerContext &
C)
const {
202 const MemRegion *VAList = getVAListAsRegion(
C.getSVal(ArgExpr), ArgExpr,
C);
206 if (S == VAListState::Initialized || S == VAListState::Unknown)
210 formatv(
"va_arg() is called on an {0} va_list",
describeState(S));
211 reportUninitializedAccess(VAList, ErrMsg,
C);
214void VAListChecker::checkDeadSymbols(SymbolReaper &SR,
215 CheckerContext &
C)
const {
217 VAListStateMapTy Tracked = State->get<VAListStateMap>();
219 for (
const auto &[Reg, S] : Tracked) {
222 if (S == VAListState::Initialized)
223 Leaked.push_back(Reg);
224 State = State->remove<VAListStateMap>(Reg);
226 if (ExplodedNode *N =
C.addTransition(State)) {
227 reportLeaked(Leaked,
"Initialized va_list",
" is leaked",
C, N);
236VAListChecker::getStartCallSite(
const ExplodedNode *N,
237 const MemRegion *Reg)
const {
239 const ExplodedNode *StartCallNode = N;
241 bool SeenInitializedState =
false;
245 if (S == VAListState::Initialized) {
246 SeenInitializedState =
true;
247 }
else if (SeenInitializedState) {
251 if (NContext == LeakContext || NContext->
isParentOf(LeakContext))
256 return StartCallNode;
259void VAListChecker::reportUninitializedAccess(
const MemRegion *VAList,
261 CheckerContext &
C)
const {
262 if (ExplodedNode *N =
C.generateErrorNode()) {
263 auto R = std::make_unique<PathSensitiveBugReport>(UninitAccessBug, Msg, N);
264 R->markInteresting(VAList);
265 R->addVisitor(std::make_unique<VAListBugVisitor>(VAList));
266 C.emitReport(std::move(R));
270void VAListChecker::reportLeaked(
const RegionVector &Leaked, StringRef Msg1,
271 StringRef Msg2, CheckerContext &
C,
272 ExplodedNode *N)
const {
273 for (
const MemRegion *Reg : Leaked) {
274 const ExplodedNode *StartNode = getStartCallSite(N, Reg);
275 PathDiagnosticLocation LocUsedForUniqueing;
281 SmallString<100> Buf;
282 llvm::raw_svector_ostream
OS(Buf);
285 if (!VariableName.empty())
286 OS <<
" " << VariableName;
289 auto R = std::make_unique<PathSensitiveBugReport>(
290 LeakBug,
OS.str(), N, LocUsedForUniqueing,
292 R->markInteresting(Reg);
293 R->addVisitor(std::make_unique<VAListBugVisitor>(Reg,
true));
294 C.emitReport(std::move(R));
298void VAListChecker::checkVAListStartCall(
const CallEvent &
Call,
299 CheckerContext &
C)
const {
300 if (
Call.getNumArgs() == 0)
303 const MemRegion *Arg =
304 getVAListAsRegion(
Call.getArgSVal(0),
Call.getArgExpr(0),
C);
311 if (ArgState == VAListState::Initialized) {
312 RegionVector Leaked{Arg};
313 if (ExplodedNode *N =
C.addTransition(State))
314 reportLeaked(Leaked,
"Initialized va_list",
" is initialized again",
C,
319 State = State->set<VAListStateMap>(Arg, VAListState::Initialized);
320 C.addTransition(State);
323void VAListChecker::checkVAListCopyCall(
const CallEvent &
Call,
324 CheckerContext &
C)
const {
325 const MemRegion *Arg1 =
326 getVAListAsRegion(
Call.getArgSVal(0),
Call.getArgExpr(0),
C);
327 const MemRegion *Arg2 =
328 getVAListAsRegion(
Call.getArgSVal(1),
Call.getArgExpr(1),
C);
334 RegionVector Leaked{Arg1};
335 if (ExplodedNode *N =
C.addTransition(State))
336 reportLeaked(Leaked,
"va_list",
" is copied onto itself",
C, N);
342 State = State->set<VAListStateMap>(Arg1, State2);
343 if (State1 == VAListState::Initialized) {
344 RegionVector Leaked{Arg1};
346 formatv(
" is overwritten by {0} {1} one",
347 (State2 == VAListState::Initialized) ?
"another" :
"an",
349 if (ExplodedNode *N =
C.addTransition(State))
350 reportLeaked(Leaked,
"Initialized va_list", Msg2,
C, N);
353 if (State2 != VAListState::Initialized && State2 != VAListState::Unknown) {
354 std::string Msg = formatv(
"{0} va_list is copied",
describeState(State2));
355 Msg[0] = toupper(Msg[0]);
356 reportUninitializedAccess(Arg2, Msg,
C);
359 C.addTransition(State);
362void VAListChecker::checkVAListEndCall(
const CallEvent &
Call,
363 CheckerContext &
C)
const {
364 const MemRegion *Arg =
365 getVAListAsRegion(
Call.getArgSVal(0),
Call.getArgExpr(0),
C);
372 if (ArgState != VAListState::Unknown &&
373 ArgState != VAListState::Initialized) {
374 std::string Msg = formatv(
"va_end() is called on an {0} va_list",
376 reportUninitializedAccess(Arg, Msg,
C);
379 State = State->set<VAListStateMap>(Arg, VAListState::Released);
380 C.addTransition(State);
384 const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &) {
399 case VAListState::Uninitialized:
400 Msg =
"Copied uninitialized contents into the va_list";
402 case VAListState::Unknown:
403 Msg =
"Copied unknown contents into the va_list";
405 case VAListState::Initialized:
406 Msg =
"Initialized va_list";
408 case VAListState::Released:
409 Msg =
"Ended va_list";
418 return std::make_shared<PathDiagnosticEventPiece>(Pos, Msg,
true);
421void ento::registerVAListChecker(CheckerManager &Mgr) {
425bool ento::shouldRegisterVAListChecker(
const CheckerManager &) {
return true; }
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
static VAListState getVAListState(ProgramStateRef State, const MemRegion *Reg)
static StringRef describeState(const VAListState S)
bool isParentOf(const LocationContext *LC) const
const Decl * getDecl() const
bool isPointerType() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isRecordType() const
const Expr * getSubExpr() const
StringRef getDescription() const
A verbose warning message that is appropriate for displaying next to the source code that introduces ...
const SourceManager & getSourceManager() const
An immutable set of CallDescriptions.
bool contains(const CallEvent &Call) const
A CallDescription is a pattern that can be used to match calls based on the qualified name and the ar...
bool matches(const CallEvent &Call) const
Returns true if the CallEvent is a call to a function that matches the CallDescription.
Represents an abstract call to a function or method along a particular path.
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.
const ProgramStateRef & getState() const
pred_iterator pred_begin()
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
const LocationContext * getLocationContext() const
ExplodedNode * getFirstPred()
MemRegion - The root abstract class for all memory regions.
std::string getDescriptiveName(bool UseQuotes=true) const
Get descriptive name for memory region.
const RegionTy * getAs() const
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
PathDiagnosticLocation getLocation() const override
The primary location of the bug report that points at the undesirable behavior in the code.
const MemRegion * getAsRegion() const
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
bool isLiveRegion(const MemRegion *region)
const char *const MemoryError
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
@ After
Like System, but searched after the system directories.
bool Cast(InterpState &S, CodePtr OpPC)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...