21 using namespace clang;
29 class ValistChecker :
public Checker<check::PreCall, check::PreStmt<VAArgExpr>,
31 mutable std::unique_ptr<BugType> BT_leakedvalist, BT_uninitaccess;
33 struct VAListAccepter {
48 DefaultBool ChecksEnabled[CK_NumCheckKinds];
49 CheckName CheckNames[CK_NumCheckKinds];
51 void checkPreStmt(
const VAArgExpr *VAA, CheckerContext &C)
const;
52 void checkPreCall(
const CallEvent &Call, CheckerContext &C)
const;
53 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C)
const;
56 const MemRegion *getVAListAsRegion(SVal SV,
const Expr *VAExpr,
57 bool &IsSymbolic, CheckerContext &C)
const;
58 const ExplodedNode *getStartCallSite(
const ExplodedNode *N,
59 const MemRegion *Reg)
const;
61 void reportUninitializedAccess(
const MemRegion *VAList, StringRef Msg,
62 CheckerContext &C)
const;
63 void reportLeakedVALists(
const RegionVector &LeakedVALists, StringRef Msg1,
64 StringRef Msg2, CheckerContext &C, ExplodedNode *N,
65 bool ReportUninit =
false)
const;
67 void checkVAListStartCall(
const CallEvent &Call, CheckerContext &C,
69 void checkVAListEndCall(
const CallEvent &Call, CheckerContext &C)
const;
73 ValistBugVisitor(
const MemRegion *Reg,
bool IsLeak =
false)
74 : Reg(Reg), IsLeak(IsLeak) {}
75 void Profile(llvm::FoldingSetNodeID &
ID)
const override {
80 std::shared_ptr<PathDiagnosticPiece>
81 getEndPath(BugReporterContext &BRC,
const ExplodedNode *EndPathNode,
82 BugReport &BR)
override {
87 EndPathNode, BRC.getSourceManager());
89 return std::make_shared<PathDiagnosticEventPiece>(L, BR.getDescription(),
false);
91 std::shared_ptr<PathDiagnosticPiece> VisitNode(
const ExplodedNode *N,
92 BugReporterContext &BRC,
93 BugReport &BR)
override;
102 ValistChecker::VAListAccepters = {
103 {{
"vfprintf", 3}, 2},
107 {{
"vsnprintf", 4}, 3},
108 {{
"vsprintf", 3}, 2},
110 {{
"vfwprintf", 3}, 2},
111 {{
"vfwscanf", 3}, 2},
112 {{
"vwprintf", 2}, 1},
114 {{
"vswprintf", 4}, 3},
117 {{
"vswscanf", 3}, 2}};
119 ValistChecker::VaCopy(
"__builtin_va_copy", 2),
120 ValistChecker::VaEnd(
"__builtin_va_end", 1);
123 void ValistChecker::checkPreCall(
const CallEvent &Call,
124 CheckerContext &C)
const {
125 if (!Call.isGlobalCFunction())
127 if (Call.isCalled(VaStart))
128 checkVAListStartCall(Call, C,
false);
129 else if (Call.isCalled(VaCopy))
130 checkVAListStartCall(Call, C,
true);
131 else if (Call.isCalled(VaEnd))
132 checkVAListEndCall(Call, C);
134 for (
auto FuncInfo : VAListAccepters) {
135 if (!Call.isCalled(FuncInfo.Func))
138 const MemRegion *VAList =
139 getVAListAsRegion(Call.getArgSVal(FuncInfo.VAListPos),
140 Call.getArgExpr(FuncInfo.VAListPos), Symbolic, C);
144 if (C.getState()->contains<InitializedVALists>(VAList))
153 Errmsg += FuncInfo.Func.getFunctionName();
154 Errmsg +=
"' is called with an uninitialized va_list argument";
155 reportUninitializedAccess(VAList, Errmsg.c_str(), C);
161 const MemRegion *ValistChecker::getVAListAsRegion(SVal SV,
const Expr *E,
163 CheckerContext &C)
const {
164 const MemRegion *Reg = SV.getAsRegion();
168 bool VaListModelledAsArray =
false;
169 if (
const auto *Cast = dyn_cast<CastExpr>(E)) {
171 VaListModelledAsArray =
174 if (
const auto *DeclReg = Reg->getAs<DeclRegion>()) {
175 if (isa<ParmVarDecl>(DeclReg->getDecl()))
176 Reg = C.getState()->getSVal(SV.castAs<Loc>()).getAsRegion();
178 IsSymbolic = Reg && Reg->getAs<SymbolicRegion>();
180 const auto *EReg = dyn_cast_or_null<ElementRegion>(Reg);
181 return (EReg && VaListModelledAsArray) ? EReg->getSuperRegion() : Reg;
184 void ValistChecker::checkPreStmt(
const VAArgExpr *VAA,
185 CheckerContext &C)
const {
188 SVal VAListSVal = C.getSVal(VASubExpr);
190 const MemRegion *VAList =
191 getVAListAsRegion(VAListSVal, VASubExpr, Symbolic, C);
196 if (!State->contains<InitializedVALists>(VAList))
197 reportUninitializedAccess(
198 VAList,
"va_arg() is called on an uninitialized va_list", C);
201 void ValistChecker::checkDeadSymbols(SymbolReaper &SR,
202 CheckerContext &C)
const {
204 InitializedVAListsTy TrackedVALists = State->get<InitializedVALists>();
205 RegionVector LeakedVALists;
206 for (
auto Reg : TrackedVALists) {
207 if (SR.isLiveRegion(Reg))
209 LeakedVALists.push_back(Reg);
210 State = State->remove<InitializedVALists>(Reg);
212 if (ExplodedNode *N = C.addTransition(State))
213 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
" is leaked", C,
222 ValistChecker::getStartCallSite(
const ExplodedNode *N,
223 const MemRegion *Reg)
const {
225 const ExplodedNode *StartCallNode = N;
227 bool FoundInitializedState =
false;
231 if (!State->contains<InitializedVALists>(Reg)) {
232 if (FoundInitializedState)
235 FoundInitializedState =
true;
238 if (NContext == LeakContext || NContext->
isParentOf(LeakContext))
240 N = N->pred_empty() ? nullptr : *(N->pred_begin());
243 return StartCallNode;
246 void ValistChecker::reportUninitializedAccess(
const MemRegion *VAList,
248 CheckerContext &C)
const {
249 if (!ChecksEnabled[CK_Uninitialized])
251 if (ExplodedNode *N = C.generateErrorNode()) {
252 if (!BT_uninitaccess)
253 BT_uninitaccess.reset(
new BugType(CheckNames[CK_Uninitialized],
254 "Uninitialized va_list",
256 auto R = llvm::make_unique<BugReport>(*BT_uninitaccess, Msg, N);
257 R->markInteresting(VAList);
258 R->addVisitor(llvm::make_unique<ValistBugVisitor>(VAList));
259 C.emitReport(std::move(R));
263 void ValistChecker::reportLeakedVALists(
const RegionVector &LeakedVALists,
264 StringRef Msg1, StringRef Msg2,
265 CheckerContext &C, ExplodedNode *N,
266 bool ReportUninit)
const {
267 if (!(ChecksEnabled[CK_Unterminated] ||
268 (ChecksEnabled[CK_Uninitialized] && ReportUninit)))
270 for (
auto Reg : LeakedVALists) {
271 if (!BT_leakedvalist) {
274 BT_leakedvalist.reset(
275 new BugType(CheckNames[CK_Unterminated].
getName().empty()
276 ? CheckNames[CK_Uninitialized]
277 : CheckNames[CK_Unterminated],
282 const ExplodedNode *StartNode = getStartCallSite(N, Reg);
283 PathDiagnosticLocation LocUsedForUniqueing;
287 StartCallStmt, C.getSourceManager(), StartNode->getLocationContext());
290 llvm::raw_svector_ostream
OS(Buf);
292 std::string VariableName = Reg->getDescriptiveName();
293 if (!VariableName.empty())
294 OS <<
" " << VariableName;
297 auto R = llvm::make_unique<BugReport>(
298 *BT_leakedvalist,
OS.str(), N, LocUsedForUniqueing,
299 StartNode->getLocationContext()->getDecl());
300 R->markInteresting(Reg);
301 R->addVisitor(llvm::make_unique<ValistBugVisitor>(Reg,
true));
302 C.emitReport(std::move(R));
306 void ValistChecker::checkVAListStartCall(
const CallEvent &Call,
307 CheckerContext &C,
bool IsCopy)
const {
309 const MemRegion *VAList =
310 getVAListAsRegion(Call.getArgSVal(0), Call.getArgExpr(0), Symbolic, C);
317 const MemRegion *Arg2 =
318 getVAListAsRegion(Call.getArgSVal(1), Call.getArgExpr(1), Symbolic, C);
320 if (ChecksEnabled[CK_CopyToSelf] && VAList == Arg2) {
321 RegionVector LeakedVALists{VAList};
322 if (ExplodedNode *N = C.addTransition(State))
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};
330 if (ExplodedNode *N = C.addTransition(State))
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};
343 if (ExplodedNode *N = C.addTransition(State))
344 reportLeakedVALists(LeakedVALists,
"Initialized va_list",
345 " is initialized again", C, N);
349 State = State->add<InitializedVALists>(VAList);
350 C.addTransition(State);
353 void ValistChecker::checkVAListEndCall(
const CallEvent &Call,
354 CheckerContext &C)
const {
356 const MemRegion *VAList =
357 getVAListAsRegion(Call.getArgSVal(0), Call.getArgExpr(0), Symbolic, C);
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);
373 C.addTransition(State);
376 std::shared_ptr<PathDiagnosticPiece> ValistChecker::ValistBugVisitor::VisitNode(
377 const ExplodedNode *N, BugReporterContext &BRC,
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";
397 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
398 N->getLocationContext());
399 return std::make_shared<PathDiagnosticEventPiece>(Pos, Msg,
true);
402 void ento::registerValistBase(CheckerManager &mgr) {
403 mgr.registerChecker<ValistChecker>();
406 bool ento::shouldRegisterValistBase(
const LangOptions &LO) {
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] = mgr.getCurrentCheckName(); \ 417 bool ento::shouldRegister##name##Checker(const LangOptions &LO) { \
A (possibly-)qualified type.
const char *const MemoryError
Stmt - This represents one statement.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
bool isRecordType() const
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const Expr * getSubExpr() const
bool isParentOf(const LocationContext *LC) const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
This class represents a description of a function call using the number of arguments and the name of ...
Represents a call to the builtin function __builtin_va_arg.
This represents one expression.
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.
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
Dataflow Directional Tag Classes.
static std::string getName(const CallEvent &Call)
Indicates that the tracking object is a descendant of a referenced-counted OSObject, used in the Darwin kernel.
#define REGISTER_CHECKER(name)
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
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