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 {
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 {
81 std::shared_ptr<PathDiagnosticPiece>
90 return std::make_shared<PathDiagnosticEventPiece>(L, BR.
getDescription(),
false);
92 std::shared_ptr<PathDiagnosticPiece> VisitNode(
const ExplodedNode *N,
104 ValistChecker::VAListAccepters = {
105 {{
"vfprintf", 3}, 2},
109 {{
"vsnprintf", 4}, 3},
110 {{
"vsprintf", 3}, 2},
112 {{
"vfwprintf", 3}, 2},
113 {{
"vfwscanf", 3}, 2},
114 {{
"vwprintf", 2}, 1},
116 {{
"vswprintf", 4}, 3},
119 {{
"vswscanf", 3}, 2}};
121 ValistChecker::VaCopy(
"__builtin_va_copy", 2),
122 ValistChecker::VaEnd(
"__builtin_va_end", 1);
125 void ValistChecker::checkPreCall(
const CallEvent &Call,
130 checkVAListStartCall(Call, C,
false);
132 checkVAListStartCall(Call, C,
true);
134 checkVAListEndCall(Call, C);
136 for (
auto FuncInfo : VAListAccepters) {
141 getVAListAsRegion(Call.
getArgSVal(FuncInfo.VAListPos),
142 Call.
getArgExpr(FuncInfo.VAListPos), Symbolic, C);
146 if (C.
getState()->contains<InitializedVALists>(VAList))
155 Errmsg += FuncInfo.Func.getFunctionName();
156 Errmsg +=
"' is called with an uninitialized va_list argument";
157 reportUninitializedAccess(VAList, Errmsg.c_str(), C);
170 bool VaListModelledAsArray =
false;
171 if (
const auto *Cast = dyn_cast<CastExpr>(E)) {
173 VaListModelledAsArray =
177 if (isa<ParmVarDecl>(DeclReg->getDecl()))
182 const auto *EReg = dyn_cast_or_null<ElementRegion>(Reg);
183 return (EReg && VaListModelledAsArray) ? EReg->
getSuperRegion() : Reg;
186 void ValistChecker::checkPreStmt(
const VAArgExpr *VAA,
193 getVAListAsRegion(VAListSVal, VASubExpr, Symbolic, C);
198 if (!State->contains<InitializedVALists>(VAList))
199 reportUninitializedAccess(
200 VAList,
"va_arg() is called on an uninitialized va_list", C);
206 InitializedVAListsTy TrackedVALists = State->get<InitializedVALists>();
207 RegionVector LeakedVALists;
208 for (
auto Reg : TrackedVALists) {
211 LeakedVALists.push_back(Reg);
212 State = State->remove<InitializedVALists>(Reg);
215 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
" is leaked", C,
229 bool FoundInitializedState =
false;
233 if (!State->contains<InitializedVALists>(Reg)) {
234 if (FoundInitializedState)
237 FoundInitializedState =
true;
240 if (NContext == LeakContext || NContext->
isParentOf(LeakContext))
245 return StartCallNode;
248 void ValistChecker::reportUninitializedAccess(
const MemRegion *VAList,
251 if (!ChecksEnabled[CK_Uninitialized])
254 if (!BT_uninitaccess)
255 BT_uninitaccess.reset(
new BugType(CheckNames[CK_Uninitialized],
256 "Uninitialized va_list",
258 auto R = llvm::make_unique<BugReport>(*BT_uninitaccess, Msg, N);
259 R->markInteresting(VAList);
260 R->addVisitor(llvm::make_unique<ValistBugVisitor>(VAList));
265 void ValistChecker::reportLeakedVALists(
const RegionVector &LeakedVALists,
266 StringRef Msg1, StringRef Msg2,
268 bool ReportUninit)
const {
269 if (!(ChecksEnabled[CK_Unterminated] ||
270 (ChecksEnabled[CK_Uninitialized] && ReportUninit)))
272 for (
auto Reg : LeakedVALists) {
273 if (!BT_leakedvalist) {
276 BT_leakedvalist.reset(
278 ? CheckNames[CK_Uninitialized]
279 : CheckNames[CK_Unterminated],
281 BT_leakedvalist->setSuppressOnSink(
true);
284 const ExplodedNode *StartNode = getStartCallSite(N, Reg);
292 llvm::raw_svector_ostream OS(Buf);
295 if (!VariableName.empty())
296 OS <<
" " << VariableName;
299 auto R = llvm::make_unique<BugReport>(
300 *BT_leakedvalist, OS.str(), N, LocUsedForUniqueing,
302 R->markInteresting(Reg);
303 R->addVisitor(llvm::make_unique<ValistBugVisitor>(Reg,
true));
308 void ValistChecker::checkVAListStartCall(
const CallEvent &Call,
322 if (ChecksEnabled[CK_CopyToSelf] && VAList == Arg2) {
323 RegionVector LeakedVALists{VAList};
325 reportLeakedVALists(LeakedVALists,
"va_list",
326 " is copied onto itself", C, N,
true);
328 }
else if (!State->contains<InitializedVALists>(Arg2) && !Symbolic) {
329 if (State->contains<InitializedVALists>(VAList)) {
330 State = State->remove<InitializedVALists>(VAList);
331 RegionVector LeakedVALists{VAList};
333 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
334 " is overwritten by an uninitialized one", C, N,
337 reportUninitializedAccess(Arg2,
"Uninitialized va_list is copied", C);
343 if (State->contains<InitializedVALists>(VAList)) {
344 RegionVector LeakedVALists{VAList};
346 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
347 " is initialized again", C, N);
351 State = State->add<InitializedVALists>(VAList);
355 void ValistChecker::checkVAListEndCall(
const CallEvent &Call,
368 if (!C.
getState()->contains<InitializedVALists>(VAList)) {
369 reportUninitializedAccess(
370 VAList,
"va_end() is called on an uninitialized va_list", C);
374 State = State->remove<InitializedVALists>(VAList);
378 std::shared_ptr<PathDiagnosticPiece> ValistChecker::ValistBugVisitor::VisitNode(
389 if (State->contains<InitializedVALists>(Reg) &&
390 !StatePrev->contains<InitializedVALists>(Reg))
391 Msg =
"Initialized va_list";
392 else if (!State->contains<InitializedVALists>(Reg) &&
393 StatePrev->contains<InitializedVALists>(Reg))
394 Msg =
"Ended va_list";
401 return std::make_shared<PathDiagnosticEventPiece>(Pos, Msg,
true);
404 #define REGISTER_CHECKER(name) \ 405 void ento::register##name##Checker(CheckerManager &mgr) { \ 406 ValistChecker *checker = mgr.registerChecker<ValistChecker>(); \ 407 checker->ChecksEnabled[ValistChecker::CK_##name] = true; \ 408 checker->CheckNames[ValistChecker::CK_##name] = mgr.getCurrentCheckName(); \
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.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
bool isRecordType() const
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
StringRef getDescription() const
const Expr * getSubExpr() const
const ProgramStateRef & getState() const
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
std::string getName(ArrayRef< StringRef > Parts) const
Get the platform-specific name separator.
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
bool isParentOf(const LocationContext *LC) const
const MemRegion * getSuperRegion() const
virtual const Expr * getArgExpr(unsigned Index) const
Returns the expression associated with a given argument.
bool isLiveRegion(const MemRegion *region)
const LocationContext * getLocationContext() const
Represents a call to the builtin function __builtin_va_arg.
const RegionTy * getAs() const
SymbolicRegion - A special, "non-concrete" region.
Expr - This represents one expression.
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 ...
const MemRegion * getAsRegion() const
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
A class responsible for cleaning up unused symbols.
std::string getDescriptiveName(bool UseQuotes=true) const
Get descriptive name for memory region.
Dataflow Directional Tag Classes.
Represents an abstract call to a function or method along a particular path.
const Decl * getDecl() const
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
const ProgramStateRef & getState() 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.
#define REGISTER_CHECKER(name)
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
bool isCalled(const CallDescription &CD) const
Returns true if the CallEvent is a call to a function that matches the CallDescription.
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.
bool isPointerType() const
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
This class provides an interface through which checkers can create individual bug reports...
SourceManager & getSourceManager()