1eb6673cfSDevin Coughlin //===- ObjCSuperDeallocChecker.cpp - Check correct use of [super dealloc] -===//
2eb6673cfSDevin Coughlin //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb6673cfSDevin Coughlin //
7eb6673cfSDevin Coughlin //===----------------------------------------------------------------------===//
8eb6673cfSDevin Coughlin //
9eb6673cfSDevin Coughlin // This defines ObjCSuperDeallocChecker, a builtin check that warns when
10591991c8SDevin Coughlin // self is used after a call to [super dealloc] in MRR mode.
11eb6673cfSDevin Coughlin //
12eb6673cfSDevin Coughlin //===----------------------------------------------------------------------===//
13eb6673cfSDevin Coughlin
1476a21502SKristof Umann #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15eb6673cfSDevin Coughlin #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16eb6673cfSDevin Coughlin #include "clang/StaticAnalyzer/Core/Checker.h"
17eb6673cfSDevin Coughlin #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18eb6673cfSDevin Coughlin #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19eb6673cfSDevin Coughlin #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20eb6673cfSDevin Coughlin #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
21eb6673cfSDevin Coughlin
22eb6673cfSDevin Coughlin using namespace clang;
23eb6673cfSDevin Coughlin using namespace ento;
24eb6673cfSDevin Coughlin
25eb6673cfSDevin Coughlin namespace {
26eb6673cfSDevin Coughlin class ObjCSuperDeallocChecker
27591991c8SDevin Coughlin : public Checker<check::PostObjCMessage, check::PreObjCMessage,
28591991c8SDevin Coughlin check::PreCall, check::Location> {
29eb6673cfSDevin Coughlin
30eb6673cfSDevin Coughlin mutable IdentifierInfo *IIdealloc, *IINSObject;
31eb6673cfSDevin Coughlin mutable Selector SELdealloc;
32eb6673cfSDevin Coughlin
33eb6673cfSDevin Coughlin std::unique_ptr<BugType> DoubleSuperDeallocBugType;
34eb6673cfSDevin Coughlin
35eb6673cfSDevin Coughlin void initIdentifierInfoAndSelectors(ASTContext &Ctx) const;
36eb6673cfSDevin Coughlin
37eb6673cfSDevin Coughlin bool isSuperDeallocMessage(const ObjCMethodCall &M) const;
38eb6673cfSDevin Coughlin
39eb6673cfSDevin Coughlin public:
40eb6673cfSDevin Coughlin ObjCSuperDeallocChecker();
41eb6673cfSDevin Coughlin void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
42eb6673cfSDevin Coughlin void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
43591991c8SDevin Coughlin
44591991c8SDevin Coughlin void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
45591991c8SDevin Coughlin
46591991c8SDevin Coughlin void checkLocation(SVal l, bool isLoad, const Stmt *S,
47591991c8SDevin Coughlin CheckerContext &C) const;
48591991c8SDevin Coughlin
49591991c8SDevin Coughlin private:
50591991c8SDevin Coughlin
51591991c8SDevin Coughlin void diagnoseCallArguments(const CallEvent &CE, CheckerContext &C) const;
52591991c8SDevin Coughlin
53591991c8SDevin Coughlin void reportUseAfterDealloc(SymbolRef Sym, StringRef Desc, const Stmt *S,
54591991c8SDevin Coughlin CheckerContext &C) const;
55eb6673cfSDevin Coughlin };
56eb6673cfSDevin Coughlin
57eb6673cfSDevin Coughlin } // End anonymous namespace.
58eb6673cfSDevin Coughlin
59eb6673cfSDevin Coughlin // Remember whether [super dealloc] has previously been called on the
60591991c8SDevin Coughlin // SymbolRef for the receiver.
61eb6673cfSDevin Coughlin REGISTER_SET_WITH_PROGRAMSTATE(CalledSuperDealloc, SymbolRef)
62eb6673cfSDevin Coughlin
636c3856d8SBenjamin Kramer namespace {
6470ec1dd1SGeorge Karpenkov class SuperDeallocBRVisitor final : public BugReporterVisitor {
65eb6673cfSDevin Coughlin SymbolRef ReceiverSymbol;
66eb6673cfSDevin Coughlin bool Satisfied;
67eb6673cfSDevin Coughlin
68eb6673cfSDevin Coughlin public:
SuperDeallocBRVisitor(SymbolRef ReceiverSymbol)69eb6673cfSDevin Coughlin SuperDeallocBRVisitor(SymbolRef ReceiverSymbol)
706d716ef1SKristof Umann : ReceiverSymbol(ReceiverSymbol), Satisfied(false) {}
71eb6673cfSDevin Coughlin
726d716ef1SKristof Umann PathDiagnosticPieceRef VisitNode(const ExplodedNode *Succ,
73eb6673cfSDevin Coughlin BugReporterContext &BRC,
742f169e7cSArtem Dergachev PathSensitiveBugReport &BR) override;
75eb6673cfSDevin Coughlin
Profile(llvm::FoldingSetNodeID & ID) const76eb6673cfSDevin Coughlin void Profile(llvm::FoldingSetNodeID &ID) const override {
77eb6673cfSDevin Coughlin ID.Add(ReceiverSymbol);
78eb6673cfSDevin Coughlin }
79eb6673cfSDevin Coughlin };
806c3856d8SBenjamin Kramer } // End anonymous namespace.
81eb6673cfSDevin Coughlin
checkPreObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const82eb6673cfSDevin Coughlin void ObjCSuperDeallocChecker::checkPreObjCMessage(const ObjCMethodCall &M,
83eb6673cfSDevin Coughlin CheckerContext &C) const {
84eb6673cfSDevin Coughlin
85eb6673cfSDevin Coughlin ProgramStateRef State = C.getState();
86eb6673cfSDevin Coughlin SymbolRef ReceiverSymbol = M.getReceiverSVal().getAsSymbol();
87591991c8SDevin Coughlin if (!ReceiverSymbol) {
88591991c8SDevin Coughlin diagnoseCallArguments(M, C);
89591991c8SDevin Coughlin return;
90591991c8SDevin Coughlin }
91eb6673cfSDevin Coughlin
92eb6673cfSDevin Coughlin bool AlreadyCalled = State->contains<CalledSuperDealloc>(ReceiverSymbol);
93eb6673cfSDevin Coughlin if (!AlreadyCalled)
94eb6673cfSDevin Coughlin return;
95eb6673cfSDevin Coughlin
96591991c8SDevin Coughlin StringRef Desc;
97eb6673cfSDevin Coughlin
98591991c8SDevin Coughlin if (isSuperDeallocMessage(M)) {
99591991c8SDevin Coughlin Desc = "[super dealloc] should not be called multiple times";
100591991c8SDevin Coughlin } else {
101591991c8SDevin Coughlin Desc = StringRef();
102591991c8SDevin Coughlin }
103591991c8SDevin Coughlin
104591991c8SDevin Coughlin reportUseAfterDealloc(ReceiverSymbol, Desc, M.getOriginExpr(), C);
105eb6673cfSDevin Coughlin }
106eb6673cfSDevin Coughlin
checkPreCall(const CallEvent & Call,CheckerContext & C) const107591991c8SDevin Coughlin void ObjCSuperDeallocChecker::checkPreCall(const CallEvent &Call,
108591991c8SDevin Coughlin CheckerContext &C) const {
109591991c8SDevin Coughlin diagnoseCallArguments(Call, C);
110591991c8SDevin Coughlin }
111591991c8SDevin Coughlin
checkPostObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const112eb6673cfSDevin Coughlin void ObjCSuperDeallocChecker::checkPostObjCMessage(const ObjCMethodCall &M,
113eb6673cfSDevin Coughlin CheckerContext &C) const {
114eb6673cfSDevin Coughlin // Check for [super dealloc] method call.
115eb6673cfSDevin Coughlin if (!isSuperDeallocMessage(M))
116eb6673cfSDevin Coughlin return;
117eb6673cfSDevin Coughlin
118eb6673cfSDevin Coughlin ProgramStateRef State = C.getState();
119*239c53b7SValeriy Savchenko const LocationContext *LC = C.getLocationContext();
120*239c53b7SValeriy Savchenko SymbolRef SelfSymbol = State->getSelfSVal(LC).getAsSymbol();
121*239c53b7SValeriy Savchenko assert(SelfSymbol && "No receiver symbol at call to [super dealloc]?");
122eb6673cfSDevin Coughlin
123eb6673cfSDevin Coughlin // We add this transition in checkPostObjCMessage to avoid warning when
124eb6673cfSDevin Coughlin // we inline a call to [super dealloc] where the inlined call itself
125eb6673cfSDevin Coughlin // calls [super dealloc].
126*239c53b7SValeriy Savchenko State = State->add<CalledSuperDealloc>(SelfSymbol);
127eb6673cfSDevin Coughlin C.addTransition(State);
128eb6673cfSDevin Coughlin }
129eb6673cfSDevin Coughlin
checkLocation(SVal L,bool IsLoad,const Stmt * S,CheckerContext & C) const130591991c8SDevin Coughlin void ObjCSuperDeallocChecker::checkLocation(SVal L, bool IsLoad, const Stmt *S,
131591991c8SDevin Coughlin CheckerContext &C) const {
132591991c8SDevin Coughlin SymbolRef BaseSym = L.getLocSymbolInBase();
133591991c8SDevin Coughlin if (!BaseSym)
134591991c8SDevin Coughlin return;
135591991c8SDevin Coughlin
136591991c8SDevin Coughlin ProgramStateRef State = C.getState();
137591991c8SDevin Coughlin
138591991c8SDevin Coughlin if (!State->contains<CalledSuperDealloc>(BaseSym))
139591991c8SDevin Coughlin return;
140591991c8SDevin Coughlin
141591991c8SDevin Coughlin const MemRegion *R = L.getAsRegion();
142591991c8SDevin Coughlin if (!R)
143591991c8SDevin Coughlin return;
144591991c8SDevin Coughlin
145591991c8SDevin Coughlin // Climb the super regions to find the base symbol while recording
146591991c8SDevin Coughlin // the second-to-last region for error reporting.
147591991c8SDevin Coughlin const MemRegion *PriorSubRegion = nullptr;
148591991c8SDevin Coughlin while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
149591991c8SDevin Coughlin if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR)) {
150591991c8SDevin Coughlin BaseSym = SymR->getSymbol();
151591991c8SDevin Coughlin break;
152591991c8SDevin Coughlin } else {
153591991c8SDevin Coughlin R = SR->getSuperRegion();
154591991c8SDevin Coughlin PriorSubRegion = SR;
155591991c8SDevin Coughlin }
156591991c8SDevin Coughlin }
157591991c8SDevin Coughlin
158591991c8SDevin Coughlin StringRef Desc = StringRef();
159591991c8SDevin Coughlin auto *IvarRegion = dyn_cast_or_null<ObjCIvarRegion>(PriorSubRegion);
160591991c8SDevin Coughlin
161591991c8SDevin Coughlin std::string Buf;
162591991c8SDevin Coughlin llvm::raw_string_ostream OS(Buf);
163ea5415faSDevin Coughlin if (IvarRegion) {
164dce8d8b3SDevin Coughlin OS << "Use of instance variable '" << *IvarRegion->getDecl() <<
165896dffe7SDevin Coughlin "' after 'self' has been deallocated";
166591991c8SDevin Coughlin Desc = OS.str();
167591991c8SDevin Coughlin }
168591991c8SDevin Coughlin
169591991c8SDevin Coughlin reportUseAfterDealloc(BaseSym, Desc, S, C);
170591991c8SDevin Coughlin }
171591991c8SDevin Coughlin
172ec6f61ccSDevin Coughlin /// Report a use-after-dealloc on Sym. If not empty,
173ec6f61ccSDevin Coughlin /// Desc will be used to describe the error; otherwise,
174591991c8SDevin Coughlin /// a default warning will be used.
reportUseAfterDealloc(SymbolRef Sym,StringRef Desc,const Stmt * S,CheckerContext & C) const175591991c8SDevin Coughlin void ObjCSuperDeallocChecker::reportUseAfterDealloc(SymbolRef Sym,
176591991c8SDevin Coughlin StringRef Desc,
177591991c8SDevin Coughlin const Stmt *S,
178591991c8SDevin Coughlin CheckerContext &C) const {
179591991c8SDevin Coughlin // We have a use of self after free.
180591991c8SDevin Coughlin // This likely causes a crash, so stop exploring the
181591991c8SDevin Coughlin // path by generating a sink.
182591991c8SDevin Coughlin ExplodedNode *ErrNode = C.generateErrorNode();
183591991c8SDevin Coughlin // If we've already reached this node on another path, return.
184591991c8SDevin Coughlin if (!ErrNode)
185591991c8SDevin Coughlin return;
186591991c8SDevin Coughlin
187591991c8SDevin Coughlin if (Desc.empty())
18805c03845SDevin Coughlin Desc = "Use of 'self' after it has been deallocated";
189591991c8SDevin Coughlin
190591991c8SDevin Coughlin // Generate the report.
1912f169e7cSArtem Dergachev auto BR = std::make_unique<PathSensitiveBugReport>(*DoubleSuperDeallocBugType,
1922f169e7cSArtem Dergachev Desc, ErrNode);
193591991c8SDevin Coughlin BR->addRange(S->getSourceRange());
1942b3d49b6SJonas Devlieghere BR->addVisitor(std::make_unique<SuperDeallocBRVisitor>(Sym));
195591991c8SDevin Coughlin C.emitReport(std::move(BR));
196591991c8SDevin Coughlin }
197591991c8SDevin Coughlin
198ec6f61ccSDevin Coughlin /// Diagnose if any of the arguments to CE have already been
199591991c8SDevin Coughlin /// dealloc'd.
diagnoseCallArguments(const CallEvent & CE,CheckerContext & C) const200591991c8SDevin Coughlin void ObjCSuperDeallocChecker::diagnoseCallArguments(const CallEvent &CE,
201591991c8SDevin Coughlin CheckerContext &C) const {
202591991c8SDevin Coughlin ProgramStateRef State = C.getState();
203591991c8SDevin Coughlin unsigned ArgCount = CE.getNumArgs();
204591991c8SDevin Coughlin for (unsigned I = 0; I < ArgCount; I++) {
205591991c8SDevin Coughlin SymbolRef Sym = CE.getArgSVal(I).getAsSymbol();
206591991c8SDevin Coughlin if (!Sym)
207591991c8SDevin Coughlin continue;
208591991c8SDevin Coughlin
209591991c8SDevin Coughlin if (State->contains<CalledSuperDealloc>(Sym)) {
210591991c8SDevin Coughlin reportUseAfterDealloc(Sym, StringRef(), CE.getArgExpr(I), C);
211591991c8SDevin Coughlin return;
212591991c8SDevin Coughlin }
213591991c8SDevin Coughlin }
214591991c8SDevin Coughlin }
215591991c8SDevin Coughlin
ObjCSuperDeallocChecker()216eb6673cfSDevin Coughlin ObjCSuperDeallocChecker::ObjCSuperDeallocChecker()
217eb6673cfSDevin Coughlin : IIdealloc(nullptr), IINSObject(nullptr) {
218eb6673cfSDevin Coughlin
219eb6673cfSDevin Coughlin DoubleSuperDeallocBugType.reset(
220eb6673cfSDevin Coughlin new BugType(this, "[super dealloc] should not be called more than once",
221eb6673cfSDevin Coughlin categories::CoreFoundationObjectiveC));
222eb6673cfSDevin Coughlin }
223eb6673cfSDevin Coughlin
224eb6673cfSDevin Coughlin void
initIdentifierInfoAndSelectors(ASTContext & Ctx) const225eb6673cfSDevin Coughlin ObjCSuperDeallocChecker::initIdentifierInfoAndSelectors(ASTContext &Ctx) const {
226eb6673cfSDevin Coughlin if (IIdealloc)
227eb6673cfSDevin Coughlin return;
228eb6673cfSDevin Coughlin
229eb6673cfSDevin Coughlin IIdealloc = &Ctx.Idents.get("dealloc");
230eb6673cfSDevin Coughlin IINSObject = &Ctx.Idents.get("NSObject");
231eb6673cfSDevin Coughlin
232eb6673cfSDevin Coughlin SELdealloc = Ctx.Selectors.getSelector(0, &IIdealloc);
233eb6673cfSDevin Coughlin }
234eb6673cfSDevin Coughlin
235eb6673cfSDevin Coughlin bool
isSuperDeallocMessage(const ObjCMethodCall & M) const236eb6673cfSDevin Coughlin ObjCSuperDeallocChecker::isSuperDeallocMessage(const ObjCMethodCall &M) const {
237eb6673cfSDevin Coughlin if (M.getOriginExpr()->getReceiverKind() != ObjCMessageExpr::SuperInstance)
238eb6673cfSDevin Coughlin return false;
239eb6673cfSDevin Coughlin
240eb6673cfSDevin Coughlin ASTContext &Ctx = M.getState()->getStateManager().getContext();
241eb6673cfSDevin Coughlin initIdentifierInfoAndSelectors(Ctx);
242eb6673cfSDevin Coughlin
243eb6673cfSDevin Coughlin return M.getSelector() == SELdealloc;
244eb6673cfSDevin Coughlin }
245eb6673cfSDevin Coughlin
2466d716ef1SKristof Umann PathDiagnosticPieceRef
VisitNode(const ExplodedNode * Succ,BugReporterContext & BRC,PathSensitiveBugReport &)2470a0c275fSDavid Blaikie SuperDeallocBRVisitor::VisitNode(const ExplodedNode *Succ,
2482f169e7cSArtem Dergachev BugReporterContext &BRC,
2492f169e7cSArtem Dergachev PathSensitiveBugReport &) {
250eb6673cfSDevin Coughlin if (Satisfied)
251eb6673cfSDevin Coughlin return nullptr;
252eb6673cfSDevin Coughlin
253eb6673cfSDevin Coughlin ProgramStateRef State = Succ->getState();
254eb6673cfSDevin Coughlin
255eb6673cfSDevin Coughlin bool CalledNow =
256eb6673cfSDevin Coughlin Succ->getState()->contains<CalledSuperDealloc>(ReceiverSymbol);
257eb6673cfSDevin Coughlin bool CalledBefore =
258c82d457dSGeorge Karpenkov Succ->getFirstPred()->getState()->contains<CalledSuperDealloc>(
259c82d457dSGeorge Karpenkov ReceiverSymbol);
260eb6673cfSDevin Coughlin
261eb6673cfSDevin Coughlin // Is Succ the node on which the analyzer noted that [super dealloc] was
262eb6673cfSDevin Coughlin // called on ReceiverSymbol?
263eb6673cfSDevin Coughlin if (CalledNow && !CalledBefore) {
264eb6673cfSDevin Coughlin Satisfied = true;
265eb6673cfSDevin Coughlin
266eb6673cfSDevin Coughlin ProgramPoint P = Succ->getLocation();
267eb6673cfSDevin Coughlin PathDiagnosticLocation L =
268eb6673cfSDevin Coughlin PathDiagnosticLocation::create(P, BRC.getSourceManager());
269eb6673cfSDevin Coughlin
270eb6673cfSDevin Coughlin if (!L.isValid() || !L.asLocation().isValid())
271eb6673cfSDevin Coughlin return nullptr;
272eb6673cfSDevin Coughlin
2730a0c275fSDavid Blaikie return std::make_shared<PathDiagnosticEventPiece>(
274eb6673cfSDevin Coughlin L, "[super dealloc] called here");
275eb6673cfSDevin Coughlin }
276eb6673cfSDevin Coughlin
277eb6673cfSDevin Coughlin return nullptr;
278eb6673cfSDevin Coughlin }
279eb6673cfSDevin Coughlin
280eb6673cfSDevin Coughlin //===----------------------------------------------------------------------===//
281eb6673cfSDevin Coughlin // Checker Registration.
282eb6673cfSDevin Coughlin //===----------------------------------------------------------------------===//
283eb6673cfSDevin Coughlin
registerObjCSuperDeallocChecker(CheckerManager & Mgr)284eb6673cfSDevin Coughlin void ento::registerObjCSuperDeallocChecker(CheckerManager &Mgr) {
285eb6673cfSDevin Coughlin Mgr.registerChecker<ObjCSuperDeallocChecker>();
286eb6673cfSDevin Coughlin }
287058a7a45SKristof Umann
shouldRegisterObjCSuperDeallocChecker(const CheckerManager & mgr)288bda3dd0dSKirstóf Umann bool ento::shouldRegisterObjCSuperDeallocChecker(const CheckerManager &mgr) {
289058a7a45SKristof Umann return true;
290058a7a45SKristof Umann }
291