30class ValistChecker :
public Checker<check::PreCall, check::PreStmt<VAArgExpr>,
32 mutable std::unique_ptr<BugType> BT_leakedvalist, BT_uninitaccess;
34 struct VAListAccepter {
49 bool ChecksEnabled[CK_NumCheckKinds] = {
false};
62 void reportUninitializedAccess(
const MemRegion *VAList, StringRef Msg,
64 void reportLeakedVALists(
const RegionVector &LeakedVALists, StringRef Msg1,
66 bool ReportUninit =
false)
const;
74 ValistBugVisitor(
const MemRegion *Reg,
bool IsLeak =
false)
75 : Reg(Reg), IsLeak(IsLeak) {}
76 void Profile(llvm::FoldingSetNodeID &ID)
const override {
89 return std::make_shared<PathDiagnosticEventPiece>(L, BR.
getDescription(),
103 ValistChecker::VAListAccepters = {{{CDM::CLibrary, {
"vfprintf"}, 3}, 2},
104 {{CDM::CLibrary, {
"vfscanf"}, 3}, 2},
105 {{CDM::CLibrary, {
"vprintf"}, 2}, 1},
106 {{CDM::CLibrary, {
"vscanf"}, 2}, 1},
107 {{CDM::CLibrary, {
"vsnprintf"}, 4}, 3},
108 {{CDM::CLibrary, {
"vsprintf"}, 3}, 2},
109 {{CDM::CLibrary, {
"vsscanf"}, 3}, 2},
110 {{CDM::CLibrary, {
"vfwprintf"}, 3}, 2},
111 {{CDM::CLibrary, {
"vfwscanf"}, 3}, 2},
112 {{CDM::CLibrary, {
"vwprintf"}, 2}, 1},
113 {{CDM::CLibrary, {
"vwscanf"}, 2}, 1},
114 {{CDM::CLibrary, {
"vswprintf"}, 4}, 3},
117 {{CDM::CLibrary, {
"vswscanf"}, 3}, 2}};
120 {
"__builtin_va_start"}, 2,
122 ValistChecker::VaCopy(CDM::CLibrary, {
"__builtin_va_copy"}, 2),
123 ValistChecker::VaEnd(CDM::CLibrary, {
"__builtin_va_end"}, 1);
129 checkVAListStartCall(
Call,
C,
false);
131 checkVAListStartCall(
Call,
C,
true);
133 checkVAListEndCall(
Call,
C);
135 for (
auto FuncInfo : VAListAccepters) {
136 if (!FuncInfo.Func.matches(
Call))
140 getVAListAsRegion(
Call.getArgSVal(FuncInfo.VAListPos),
141 Call.getArgExpr(FuncInfo.VAListPos), Symbolic,
C);
145 if (
C.getState()->contains<InitializedVALists>(VAList))
154 Errmsg += FuncInfo.Func.getFunctionName();
155 Errmsg +=
"' is called with an uninitialized va_list argument";
156 reportUninitializedAccess(VAList, Errmsg.c_str(),
C);
169 bool VaListModelledAsArray =
false;
170 if (
const auto *Cast = dyn_cast<CastExpr>(
E)) {
172 VaListModelledAsArray =
176 if (isa<ParmVarDecl>(DeclReg->getDecl()))
177 Reg =
C.getState()->getSVal(SV.
castAs<
Loc>()).getAsRegion();
181 const auto *EReg = dyn_cast_or_null<ElementRegion>(Reg);
182 return (EReg && VaListModelledAsArray) ? EReg->
getSuperRegion() : Reg;
185void ValistChecker::checkPreStmt(
const VAArgExpr *VAA,
189 SVal VAListSVal =
C.getSVal(VASubExpr);
192 getVAListAsRegion(VAListSVal, VASubExpr, Symbolic,
C);
197 if (!State->contains<InitializedVALists>(VAList))
198 reportUninitializedAccess(
199 VAList,
"va_arg() is called on an uninitialized va_list",
C);
205 InitializedVAListsTy TrackedVALists = State->get<InitializedVALists>();
206 RegionVector LeakedVALists;
207 for (
auto Reg : TrackedVALists) {
210 LeakedVALists.push_back(Reg);
211 State = State->remove<InitializedVALists>(Reg);
214 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
" is leaked",
C,
228 bool FoundInitializedState =
false;
232 if (!State->contains<InitializedVALists>(Reg)) {
233 if (FoundInitializedState)
236 FoundInitializedState =
true;
239 if (NContext == LeakContext || NContext->
isParentOf(LeakContext))
244 return StartCallNode;
247void ValistChecker::reportUninitializedAccess(
const MemRegion *VAList,
250 if (!ChecksEnabled[CK_Uninitialized])
253 if (!BT_uninitaccess)
254 BT_uninitaccess.reset(
new BugType(CheckNames[CK_Uninitialized],
255 "Uninitialized va_list",
257 auto R = std::make_unique<PathSensitiveBugReport>(*BT_uninitaccess, Msg, N);
258 R->markInteresting(VAList);
259 R->addVisitor(std::make_unique<ValistBugVisitor>(VAList));
260 C.emitReport(std::move(R));
264void ValistChecker::reportLeakedVALists(
const RegionVector &LeakedVALists,
265 StringRef Msg1, StringRef Msg2,
267 bool ReportUninit)
const {
268 if (!(ChecksEnabled[CK_Unterminated] ||
269 (ChecksEnabled[CK_Uninitialized] && ReportUninit)))
271 for (
auto Reg : LeakedVALists) {
272 if (!BT_leakedvalist) {
275 BT_leakedvalist.reset(
277 ? CheckNames[CK_Uninitialized]
278 : CheckNames[CK_Unterminated],
283 const ExplodedNode *StartNode = getStartCallSite(N, Reg);
291 llvm::raw_svector_ostream OS(Buf);
294 if (!VariableName.empty())
295 OS <<
" " << VariableName;
298 auto R = std::make_unique<PathSensitiveBugReport>(
299 *BT_leakedvalist, OS.str(), N, LocUsedForUniqueing,
301 R->markInteresting(Reg);
302 R->addVisitor(std::make_unique<ValistBugVisitor>(Reg,
true));
303 C.emitReport(std::move(R));
307void ValistChecker::checkVAListStartCall(
const CallEvent &
Call,
311 getVAListAsRegion(
Call.getArgSVal(0),
Call.getArgExpr(0), Symbolic,
C);
319 getVAListAsRegion(
Call.getArgSVal(1),
Call.getArgExpr(1), Symbolic,
C);
321 if (ChecksEnabled[CK_CopyToSelf] && VAList == Arg2) {
322 RegionVector LeakedVALists{VAList};
324 reportLeakedVALists(LeakedVALists,
"va_list",
325 " is copied onto itself",
C, N,
true);
327 }
else if (!State->contains<InitializedVALists>(Arg2) && !Symbolic) {
328 if (State->contains<InitializedVALists>(VAList)) {
329 State = State->remove<InitializedVALists>(VAList);
330 RegionVector LeakedVALists{VAList};
332 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
333 " is overwritten by an uninitialized one",
C, N,
336 reportUninitializedAccess(Arg2,
"Uninitialized va_list is copied",
C);
342 if (State->contains<InitializedVALists>(VAList)) {
343 RegionVector LeakedVALists{VAList};
345 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
346 " is initialized again",
C, N);
350 State = State->add<InitializedVALists>(VAList);
351 C.addTransition(State);
358 getVAListAsRegion(
Call.getArgSVal(0),
Call.getArgExpr(0), Symbolic,
C);
367 if (!
C.getState()->contains<InitializedVALists>(VAList)) {
368 reportUninitializedAccess(
369 VAList,
"va_end() is called on an uninitialized va_list",
C);
373 State = State->remove<InitializedVALists>(VAList);
374 C.addTransition(State);
387 if (State->contains<InitializedVALists>(Reg) &&
388 !StatePrev->contains<InitializedVALists>(Reg))
389 Msg =
"Initialized va_list";
390 else if (!State->contains<InitializedVALists>(Reg) &&
391 StatePrev->contains<InitializedVALists>(Reg))
392 Msg =
"Ended va_list";
399 return std::make_shared<PathDiagnosticEventPiece>(Pos, Msg,
true);
410#define REGISTER_CHECKER(name) \
411 void ento::register##name##Checker(CheckerManager &mgr) { \
412 ValistChecker *checker = mgr.getChecker<ValistChecker>(); \
413 checker->ChecksEnabled[ValistChecker::CK_##name] = true; \
414 checker->CheckNames[ValistChecker::CK_##name] = \
415 mgr.getCurrentCheckerName(); \
418 bool ento::shouldRegister##name##Checker(const CheckerManager &mgr) { \
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
static std::string getName(const CallEvent &Call)
#define REGISTER_CHECKER(name)
This represents one expression.
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
bool isParentOf(const LocationContext *LC) const
const Decl * getDecl() const
A (possibly-)qualified type.
Stmt - This represents one statement.
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
Represents a call to the builtin function __builtin_va_arg.
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
BugReporterVisitors are used to add custom diagnostics along a path.
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)
Used to register checkers.
This wrapper is used to ensure that only StringRefs originating from the CheckerRegistry are used as ...
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.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
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.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
const MemRegion * getAsRegion() const
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getSuperRegion() const
A class responsible for cleaning up unused symbols.
bool isLiveRegion(const MemRegion *region)
SymbolicRegion - A special, "non-concrete" region.
const char *const MemoryError
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
bool Cast(InterpState &S, CodePtr OpPC)
The JSON file list parser is used to communicate input to InstallAPI.