17330f729Sjoerg //===- ObjCSuperDeallocChecker.cpp - Check correct use of [super dealloc] -===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This defines ObjCSuperDeallocChecker, a builtin check that warns when
107330f729Sjoerg // self is used after a call to [super dealloc] in MRR mode.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
157330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
167330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
177330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
187330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
197330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
207330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
217330f729Sjoerg
227330f729Sjoerg using namespace clang;
237330f729Sjoerg using namespace ento;
247330f729Sjoerg
257330f729Sjoerg namespace {
267330f729Sjoerg class ObjCSuperDeallocChecker
277330f729Sjoerg : public Checker<check::PostObjCMessage, check::PreObjCMessage,
287330f729Sjoerg check::PreCall, check::Location> {
297330f729Sjoerg
307330f729Sjoerg mutable IdentifierInfo *IIdealloc, *IINSObject;
317330f729Sjoerg mutable Selector SELdealloc;
327330f729Sjoerg
337330f729Sjoerg std::unique_ptr<BugType> DoubleSuperDeallocBugType;
347330f729Sjoerg
357330f729Sjoerg void initIdentifierInfoAndSelectors(ASTContext &Ctx) const;
367330f729Sjoerg
377330f729Sjoerg bool isSuperDeallocMessage(const ObjCMethodCall &M) const;
387330f729Sjoerg
397330f729Sjoerg public:
407330f729Sjoerg ObjCSuperDeallocChecker();
417330f729Sjoerg void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
427330f729Sjoerg void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
437330f729Sjoerg
447330f729Sjoerg void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
457330f729Sjoerg
467330f729Sjoerg void checkLocation(SVal l, bool isLoad, const Stmt *S,
477330f729Sjoerg CheckerContext &C) const;
487330f729Sjoerg
497330f729Sjoerg private:
507330f729Sjoerg
517330f729Sjoerg void diagnoseCallArguments(const CallEvent &CE, CheckerContext &C) const;
527330f729Sjoerg
537330f729Sjoerg void reportUseAfterDealloc(SymbolRef Sym, StringRef Desc, const Stmt *S,
547330f729Sjoerg CheckerContext &C) const;
557330f729Sjoerg };
567330f729Sjoerg
577330f729Sjoerg } // End anonymous namespace.
587330f729Sjoerg
597330f729Sjoerg // Remember whether [super dealloc] has previously been called on the
607330f729Sjoerg // SymbolRef for the receiver.
617330f729Sjoerg REGISTER_SET_WITH_PROGRAMSTATE(CalledSuperDealloc, SymbolRef)
627330f729Sjoerg
637330f729Sjoerg namespace {
647330f729Sjoerg class SuperDeallocBRVisitor final : public BugReporterVisitor {
657330f729Sjoerg SymbolRef ReceiverSymbol;
667330f729Sjoerg bool Satisfied;
677330f729Sjoerg
687330f729Sjoerg public:
SuperDeallocBRVisitor(SymbolRef ReceiverSymbol)697330f729Sjoerg SuperDeallocBRVisitor(SymbolRef ReceiverSymbol)
707330f729Sjoerg : ReceiverSymbol(ReceiverSymbol), Satisfied(false) {}
717330f729Sjoerg
727330f729Sjoerg PathDiagnosticPieceRef VisitNode(const ExplodedNode *Succ,
737330f729Sjoerg BugReporterContext &BRC,
747330f729Sjoerg PathSensitiveBugReport &BR) override;
757330f729Sjoerg
Profile(llvm::FoldingSetNodeID & ID) const767330f729Sjoerg void Profile(llvm::FoldingSetNodeID &ID) const override {
777330f729Sjoerg ID.Add(ReceiverSymbol);
787330f729Sjoerg }
797330f729Sjoerg };
807330f729Sjoerg } // End anonymous namespace.
817330f729Sjoerg
checkPreObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const827330f729Sjoerg void ObjCSuperDeallocChecker::checkPreObjCMessage(const ObjCMethodCall &M,
837330f729Sjoerg CheckerContext &C) const {
847330f729Sjoerg
857330f729Sjoerg ProgramStateRef State = C.getState();
867330f729Sjoerg SymbolRef ReceiverSymbol = M.getReceiverSVal().getAsSymbol();
877330f729Sjoerg if (!ReceiverSymbol) {
887330f729Sjoerg diagnoseCallArguments(M, C);
897330f729Sjoerg return;
907330f729Sjoerg }
917330f729Sjoerg
927330f729Sjoerg bool AlreadyCalled = State->contains<CalledSuperDealloc>(ReceiverSymbol);
937330f729Sjoerg if (!AlreadyCalled)
947330f729Sjoerg return;
957330f729Sjoerg
967330f729Sjoerg StringRef Desc;
977330f729Sjoerg
987330f729Sjoerg if (isSuperDeallocMessage(M)) {
997330f729Sjoerg Desc = "[super dealloc] should not be called multiple times";
1007330f729Sjoerg } else {
1017330f729Sjoerg Desc = StringRef();
1027330f729Sjoerg }
1037330f729Sjoerg
1047330f729Sjoerg reportUseAfterDealloc(ReceiverSymbol, Desc, M.getOriginExpr(), C);
1057330f729Sjoerg }
1067330f729Sjoerg
checkPreCall(const CallEvent & Call,CheckerContext & C) const1077330f729Sjoerg void ObjCSuperDeallocChecker::checkPreCall(const CallEvent &Call,
1087330f729Sjoerg CheckerContext &C) const {
1097330f729Sjoerg diagnoseCallArguments(Call, C);
1107330f729Sjoerg }
1117330f729Sjoerg
checkPostObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const1127330f729Sjoerg void ObjCSuperDeallocChecker::checkPostObjCMessage(const ObjCMethodCall &M,
1137330f729Sjoerg CheckerContext &C) const {
1147330f729Sjoerg // Check for [super dealloc] method call.
1157330f729Sjoerg if (!isSuperDeallocMessage(M))
1167330f729Sjoerg return;
1177330f729Sjoerg
1187330f729Sjoerg ProgramStateRef State = C.getState();
119*e038c9c4Sjoerg const LocationContext *LC = C.getLocationContext();
120*e038c9c4Sjoerg SymbolRef SelfSymbol = State->getSelfSVal(LC).getAsSymbol();
121*e038c9c4Sjoerg assert(SelfSymbol && "No receiver symbol at call to [super dealloc]?");
1227330f729Sjoerg
1237330f729Sjoerg // We add this transition in checkPostObjCMessage to avoid warning when
1247330f729Sjoerg // we inline a call to [super dealloc] where the inlined call itself
1257330f729Sjoerg // calls [super dealloc].
126*e038c9c4Sjoerg State = State->add<CalledSuperDealloc>(SelfSymbol);
1277330f729Sjoerg C.addTransition(State);
1287330f729Sjoerg }
1297330f729Sjoerg
checkLocation(SVal L,bool IsLoad,const Stmt * S,CheckerContext & C) const1307330f729Sjoerg void ObjCSuperDeallocChecker::checkLocation(SVal L, bool IsLoad, const Stmt *S,
1317330f729Sjoerg CheckerContext &C) const {
1327330f729Sjoerg SymbolRef BaseSym = L.getLocSymbolInBase();
1337330f729Sjoerg if (!BaseSym)
1347330f729Sjoerg return;
1357330f729Sjoerg
1367330f729Sjoerg ProgramStateRef State = C.getState();
1377330f729Sjoerg
1387330f729Sjoerg if (!State->contains<CalledSuperDealloc>(BaseSym))
1397330f729Sjoerg return;
1407330f729Sjoerg
1417330f729Sjoerg const MemRegion *R = L.getAsRegion();
1427330f729Sjoerg if (!R)
1437330f729Sjoerg return;
1447330f729Sjoerg
1457330f729Sjoerg // Climb the super regions to find the base symbol while recording
1467330f729Sjoerg // the second-to-last region for error reporting.
1477330f729Sjoerg const MemRegion *PriorSubRegion = nullptr;
1487330f729Sjoerg while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1497330f729Sjoerg if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR)) {
1507330f729Sjoerg BaseSym = SymR->getSymbol();
1517330f729Sjoerg break;
1527330f729Sjoerg } else {
1537330f729Sjoerg R = SR->getSuperRegion();
1547330f729Sjoerg PriorSubRegion = SR;
1557330f729Sjoerg }
1567330f729Sjoerg }
1577330f729Sjoerg
1587330f729Sjoerg StringRef Desc = StringRef();
1597330f729Sjoerg auto *IvarRegion = dyn_cast_or_null<ObjCIvarRegion>(PriorSubRegion);
1607330f729Sjoerg
1617330f729Sjoerg std::string Buf;
1627330f729Sjoerg llvm::raw_string_ostream OS(Buf);
1637330f729Sjoerg if (IvarRegion) {
1647330f729Sjoerg OS << "Use of instance variable '" << *IvarRegion->getDecl() <<
1657330f729Sjoerg "' after 'self' has been deallocated";
1667330f729Sjoerg Desc = OS.str();
1677330f729Sjoerg }
1687330f729Sjoerg
1697330f729Sjoerg reportUseAfterDealloc(BaseSym, Desc, S, C);
1707330f729Sjoerg }
1717330f729Sjoerg
1727330f729Sjoerg /// Report a use-after-dealloc on Sym. If not empty,
1737330f729Sjoerg /// Desc will be used to describe the error; otherwise,
1747330f729Sjoerg /// a default warning will be used.
reportUseAfterDealloc(SymbolRef Sym,StringRef Desc,const Stmt * S,CheckerContext & C) const1757330f729Sjoerg void ObjCSuperDeallocChecker::reportUseAfterDealloc(SymbolRef Sym,
1767330f729Sjoerg StringRef Desc,
1777330f729Sjoerg const Stmt *S,
1787330f729Sjoerg CheckerContext &C) const {
1797330f729Sjoerg // We have a use of self after free.
1807330f729Sjoerg // This likely causes a crash, so stop exploring the
1817330f729Sjoerg // path by generating a sink.
1827330f729Sjoerg ExplodedNode *ErrNode = C.generateErrorNode();
1837330f729Sjoerg // If we've already reached this node on another path, return.
1847330f729Sjoerg if (!ErrNode)
1857330f729Sjoerg return;
1867330f729Sjoerg
1877330f729Sjoerg if (Desc.empty())
1887330f729Sjoerg Desc = "Use of 'self' after it has been deallocated";
1897330f729Sjoerg
1907330f729Sjoerg // Generate the report.
1917330f729Sjoerg auto BR = std::make_unique<PathSensitiveBugReport>(*DoubleSuperDeallocBugType,
1927330f729Sjoerg Desc, ErrNode);
1937330f729Sjoerg BR->addRange(S->getSourceRange());
1947330f729Sjoerg BR->addVisitor(std::make_unique<SuperDeallocBRVisitor>(Sym));
1957330f729Sjoerg C.emitReport(std::move(BR));
1967330f729Sjoerg }
1977330f729Sjoerg
1987330f729Sjoerg /// Diagnose if any of the arguments to CE have already been
1997330f729Sjoerg /// dealloc'd.
diagnoseCallArguments(const CallEvent & CE,CheckerContext & C) const2007330f729Sjoerg void ObjCSuperDeallocChecker::diagnoseCallArguments(const CallEvent &CE,
2017330f729Sjoerg CheckerContext &C) const {
2027330f729Sjoerg ProgramStateRef State = C.getState();
2037330f729Sjoerg unsigned ArgCount = CE.getNumArgs();
2047330f729Sjoerg for (unsigned I = 0; I < ArgCount; I++) {
2057330f729Sjoerg SymbolRef Sym = CE.getArgSVal(I).getAsSymbol();
2067330f729Sjoerg if (!Sym)
2077330f729Sjoerg continue;
2087330f729Sjoerg
2097330f729Sjoerg if (State->contains<CalledSuperDealloc>(Sym)) {
2107330f729Sjoerg reportUseAfterDealloc(Sym, StringRef(), CE.getArgExpr(I), C);
2117330f729Sjoerg return;
2127330f729Sjoerg }
2137330f729Sjoerg }
2147330f729Sjoerg }
2157330f729Sjoerg
ObjCSuperDeallocChecker()2167330f729Sjoerg ObjCSuperDeallocChecker::ObjCSuperDeallocChecker()
2177330f729Sjoerg : IIdealloc(nullptr), IINSObject(nullptr) {
2187330f729Sjoerg
2197330f729Sjoerg DoubleSuperDeallocBugType.reset(
2207330f729Sjoerg new BugType(this, "[super dealloc] should not be called more than once",
2217330f729Sjoerg categories::CoreFoundationObjectiveC));
2227330f729Sjoerg }
2237330f729Sjoerg
2247330f729Sjoerg void
initIdentifierInfoAndSelectors(ASTContext & Ctx) const2257330f729Sjoerg ObjCSuperDeallocChecker::initIdentifierInfoAndSelectors(ASTContext &Ctx) const {
2267330f729Sjoerg if (IIdealloc)
2277330f729Sjoerg return;
2287330f729Sjoerg
2297330f729Sjoerg IIdealloc = &Ctx.Idents.get("dealloc");
2307330f729Sjoerg IINSObject = &Ctx.Idents.get("NSObject");
2317330f729Sjoerg
2327330f729Sjoerg SELdealloc = Ctx.Selectors.getSelector(0, &IIdealloc);
2337330f729Sjoerg }
2347330f729Sjoerg
2357330f729Sjoerg bool
isSuperDeallocMessage(const ObjCMethodCall & M) const2367330f729Sjoerg ObjCSuperDeallocChecker::isSuperDeallocMessage(const ObjCMethodCall &M) const {
2377330f729Sjoerg if (M.getOriginExpr()->getReceiverKind() != ObjCMessageExpr::SuperInstance)
2387330f729Sjoerg return false;
2397330f729Sjoerg
2407330f729Sjoerg ASTContext &Ctx = M.getState()->getStateManager().getContext();
2417330f729Sjoerg initIdentifierInfoAndSelectors(Ctx);
2427330f729Sjoerg
2437330f729Sjoerg return M.getSelector() == SELdealloc;
2447330f729Sjoerg }
2457330f729Sjoerg
2467330f729Sjoerg PathDiagnosticPieceRef
VisitNode(const ExplodedNode * Succ,BugReporterContext & BRC,PathSensitiveBugReport &)2477330f729Sjoerg SuperDeallocBRVisitor::VisitNode(const ExplodedNode *Succ,
2487330f729Sjoerg BugReporterContext &BRC,
2497330f729Sjoerg PathSensitiveBugReport &) {
2507330f729Sjoerg if (Satisfied)
2517330f729Sjoerg return nullptr;
2527330f729Sjoerg
2537330f729Sjoerg ProgramStateRef State = Succ->getState();
2547330f729Sjoerg
2557330f729Sjoerg bool CalledNow =
2567330f729Sjoerg Succ->getState()->contains<CalledSuperDealloc>(ReceiverSymbol);
2577330f729Sjoerg bool CalledBefore =
2587330f729Sjoerg Succ->getFirstPred()->getState()->contains<CalledSuperDealloc>(
2597330f729Sjoerg ReceiverSymbol);
2607330f729Sjoerg
2617330f729Sjoerg // Is Succ the node on which the analyzer noted that [super dealloc] was
2627330f729Sjoerg // called on ReceiverSymbol?
2637330f729Sjoerg if (CalledNow && !CalledBefore) {
2647330f729Sjoerg Satisfied = true;
2657330f729Sjoerg
2667330f729Sjoerg ProgramPoint P = Succ->getLocation();
2677330f729Sjoerg PathDiagnosticLocation L =
2687330f729Sjoerg PathDiagnosticLocation::create(P, BRC.getSourceManager());
2697330f729Sjoerg
2707330f729Sjoerg if (!L.isValid() || !L.asLocation().isValid())
2717330f729Sjoerg return nullptr;
2727330f729Sjoerg
2737330f729Sjoerg return std::make_shared<PathDiagnosticEventPiece>(
2747330f729Sjoerg L, "[super dealloc] called here");
2757330f729Sjoerg }
2767330f729Sjoerg
2777330f729Sjoerg return nullptr;
2787330f729Sjoerg }
2797330f729Sjoerg
2807330f729Sjoerg //===----------------------------------------------------------------------===//
2817330f729Sjoerg // Checker Registration.
2827330f729Sjoerg //===----------------------------------------------------------------------===//
2837330f729Sjoerg
registerObjCSuperDeallocChecker(CheckerManager & Mgr)2847330f729Sjoerg void ento::registerObjCSuperDeallocChecker(CheckerManager &Mgr) {
2857330f729Sjoerg Mgr.registerChecker<ObjCSuperDeallocChecker>();
2867330f729Sjoerg }
2877330f729Sjoerg
shouldRegisterObjCSuperDeallocChecker(const CheckerManager & mgr)288*e038c9c4Sjoerg bool ento::shouldRegisterObjCSuperDeallocChecker(const CheckerManager &mgr) {
2897330f729Sjoerg return true;
2907330f729Sjoerg }
291