22 using namespace clang;
30 class ValistChecker :
public Checker<check::PreCall, check::PreStmt<VAArgExpr>,
32 mutable std::unique_ptr<BugType> BT_leakedvalist, BT_uninitaccess;
34 struct VAListAccepter {
59 StringRef getVariableNameFromRegion(
const MemRegion *Reg)
const;
63 void reportUninitializedAccess(
const MemRegion *VAList, StringRef Msg,
65 void reportLeakedVALists(
const RegionVector &LeakedVALists, StringRef Msg1,
67 bool ForceReport =
false)
const;
75 ValistBugVisitor(
const MemRegion *Reg,
bool IsLeak =
false)
76 : Reg(Reg), IsLeak(IsLeak) {}
77 void Profile(llvm::FoldingSetNodeID &
ID)
const override {
82 std::unique_ptr<PathDiagnosticPiece>
91 return llvm::make_unique<PathDiagnosticEventPiece>(L, BR.
getDescription(),
94 std::shared_ptr<PathDiagnosticPiece> VisitNode(
const ExplodedNode *N,
106 ValistChecker::VAListAccepters = {
107 {{
"vfprintf", 3}, 2},
111 {{
"vsnprintf", 4}, 3},
112 {{
"vsprintf", 3}, 2},
114 {{
"vfwprintf", 3}, 2},
115 {{
"vfwscanf", 3}, 2},
116 {{
"vwprintf", 2}, 1},
118 {{
"vswprintf", 4}, 3},
121 {{
"vswscanf", 3}, 2}};
123 ValistChecker::VaCopy(
"__builtin_va_copy", 2),
124 ValistChecker::VaEnd(
"__builtin_va_end", 1);
127 void ValistChecker::checkPreCall(
const CallEvent &Call,
132 checkVAListStartCall(Call, C,
false);
134 checkVAListStartCall(Call, C,
true);
136 checkVAListEndCall(Call, C);
138 for (
auto FuncInfo : VAListAccepters) {
143 getVAListAsRegion(Call.
getArgSVal(FuncInfo.VAListPos),
144 Call.
getArgExpr(FuncInfo.VAListPos), Symbolic, C);
148 if (C.
getState()->contains<InitializedVALists>(VAList))
157 Errmsg += FuncInfo.Func.getFunctionName();
158 Errmsg +=
"' is called with an uninitialized va_list argument";
159 reportUninitializedAccess(VAList, Errmsg.c_str(), C);
172 bool VaListModelledAsArray =
false;
173 if (
const auto *Cast = dyn_cast<CastExpr>(E)) {
175 VaListModelledAsArray =
179 if (isa<ParmVarDecl>(DeclReg->getDecl()))
184 const auto *EReg = dyn_cast_or_null<ElementRegion>(Reg);
185 return (EReg && VaListModelledAsArray) ? EReg->
getSuperRegion() : Reg;
188 void ValistChecker::checkPreStmt(
const VAArgExpr *VAA,
195 getVAListAsRegion(VAListSVal, VASubExpr, Symbolic, C);
200 if (!State->contains<InitializedVALists>(VAList))
201 reportUninitializedAccess(
202 VAList,
"va_arg() is called on an uninitialized va_list", C);
208 InitializedVAListsTy TrackedVALists = State->get<InitializedVALists>();
209 RegionVector LeakedVALists;
210 for (
auto Reg : TrackedVALists) {
213 LeakedVALists.push_back(Reg);
214 State = State->remove<InitializedVALists>(Reg);
217 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
" is leaked", C,
231 bool FoundInitializedState =
false;
235 if (!State->contains<InitializedVALists>(Reg)) {
236 if (FoundInitializedState)
239 FoundInitializedState =
true;
242 if (NContext == LeakContext || NContext->
isParentOf(LeakContext))
247 return StartCallNode;
250 void ValistChecker::reportUninitializedAccess(
const MemRegion *VAList,
253 if (!ChecksEnabled[CK_Uninitialized])
256 if (!BT_uninitaccess)
257 BT_uninitaccess.reset(
new BugType(CheckNames[CK_Uninitialized],
258 "Uninitialized va_list",
260 auto R = llvm::make_unique<BugReport>(*BT_uninitaccess, Msg, N);
261 R->markInteresting(VAList);
262 R->addVisitor(llvm::make_unique<ValistBugVisitor>(VAList));
267 void ValistChecker::reportLeakedVALists(
const RegionVector &LeakedVALists,
268 StringRef Msg1, StringRef Msg2,
270 bool ForceReport)
const {
271 if (!(ChecksEnabled[CK_Unterminated] ||
272 (ChecksEnabled[CK_Uninitialized] && ForceReport)))
274 for (
auto Reg : LeakedVALists) {
275 if (!BT_leakedvalist) {
276 BT_leakedvalist.reset(
new BugType(CheckNames[CK_Unterminated],
279 BT_leakedvalist->setSuppressOnSink(
true);
282 const ExplodedNode *StartNode = getStartCallSite(N, Reg);
290 llvm::raw_svector_ostream OS(Buf);
293 if (!VariableName.empty())
294 OS <<
" " << VariableName;
297 auto R = llvm::make_unique<BugReport>(
298 *BT_leakedvalist, OS.str(), N, LocUsedForUniqueing,
300 R->markInteresting(Reg);
301 R->addVisitor(llvm::make_unique<ValistBugVisitor>(Reg,
true));
306 void ValistChecker::checkVAListStartCall(
const CallEvent &Call,
320 if (ChecksEnabled[CK_CopyToSelf] && VAList == Arg2) {
321 RegionVector LeakedVALists{VAList};
323 reportLeakedVALists(LeakedVALists,
"va_list",
324 " is copied onto itself", C, N,
true);
326 }
else if (!State->contains<InitializedVALists>(Arg2) && !Symbolic) {
327 if (State->contains<InitializedVALists>(VAList)) {
328 State = State->remove<InitializedVALists>(VAList);
329 RegionVector LeakedVALists{VAList};
331 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
332 " is overwritten by an uninitialized one", C, N,
335 reportUninitializedAccess(Arg2,
"Uninitialized va_list is copied", C);
341 if (State->contains<InitializedVALists>(VAList)) {
342 RegionVector LeakedVALists{VAList};
344 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
345 " is initialized again", C, N);
349 State = State->add<InitializedVALists>(VAList);
353 void ValistChecker::checkVAListEndCall(
const CallEvent &Call,
366 if (!C.
getState()->contains<InitializedVALists>(VAList)) {
367 reportUninitializedAccess(
368 VAList,
"va_end() is called on an uninitialized va_list", C);
372 State = State->remove<InitializedVALists>(VAList);
376 std::shared_ptr<PathDiagnosticPiece> ValistChecker::ValistBugVisitor::VisitNode(
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);
402 #define REGISTER_CHECKER(name) \
403 void ento::register##name##Checker(CheckerManager &mgr) { \
404 ValistChecker *checker = mgr.registerChecker<ValistChecker>(); \
405 checker->ChecksEnabled[ValistChecker::CK_##name] = true; \
406 checker->CheckNames[ValistChecker::CK_##name] = mgr.getCurrentCheckName(); \
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
A (possibly-)qualified type.
MemRegion - The root abstract class for all memory regions.
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
const char *const MemoryError
Stmt - This represents one statement.
A helper class which wraps a boolean value set to false by default.
bool isRecordType() const
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
const RegionTy * getAs() const
std::string getDescriptiveName(bool UseQuotes=true) const
Get descriptive name for memory region.
bool isCalled(const CallDescription &CD) const
Returns true if the CallEvent is a call to a function that matches the CallDescription.
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
This class provides a convenience implementation for clone() using the Curiously-Recurring Template P...
bool isLiveRegion(const MemRegion *region)
const Expr * getSubExpr() const
bool isParentOf(const LocationContext *LC) const
StringRef getDescription() const
const MemRegion * getSuperRegion() const
const LocationContext * getLocationContext() const
bool isGlobalCFunction(StringRef SpecificName=StringRef()) const
Returns true if the callee is an externally-visible function in the top-level namespace, such as malloc.
Represents a call to the builtin function __builtin_va_arg.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
SymbolicRegion - A special, "non-concrete" region.
virtual const Expr * getArgExpr(unsigned Index) const
Returns the expression associated with a given argument.
Expr - This represents one expression.
const ProgramStateRef & getState() const
const ProgramStateRef & getState() const
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
static const Stmt * getStmt(const ExplodedNode *N)
Given an exploded node, retrieve the statement that should be used for the diagnostic location...
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
This class represents a description of a function call using the number of arguments and the name of ...
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
const Decl * getDecl() const
A class responsible for cleaning up unused symbols.
detail::InMemoryDirectory::const_iterator E
const MemRegion * getAsRegion() const
Represents an abstract call to a function or method along a particular path.
#define REGISTER_CHECKER(name)
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
pred_iterator pred_begin()
SourceManager & getSourceManager()
static PathDiagnosticLocation createEndOfPath(const ExplodedNode *N, const SourceManager &SM)
Create a location corresponding to the next valid ExplodedNode as end of path location.
This class provides an interface through which checkers can create individual bug reports...
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
SourceManager & getSourceManager()
const LocationContext * getLocationContext() const
bool isPointerType() const