2018-08-21 11:09:02 +08:00
|
|
|
//== RetainSummaryManager.cpp - Summaries for reference counting --*- C++ -*--//
|
2018-08-18 05:41:07 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-08-18 05:41:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-08-21 11:09:02 +08:00
|
|
|
// This file defines summaries implementation for retain counting, which
|
2018-10-24 07:11:50 +08:00
|
|
|
// implements a reference count checker for Core Foundation, Cocoa
|
|
|
|
// and OSObject (on Mac OS X).
|
2018-08-18 05:41:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
|
2019-01-25 09:24:04 +08:00
|
|
|
#include "clang/Analysis/RetainSummaryManager.h"
|
2018-08-18 05:41:07 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/ParentMap.h"
|
2018-08-30 04:28:33 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2018-12-08 04:21:51 +08:00
|
|
|
template <class T>
|
|
|
|
constexpr static bool isOneOf() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to check whether the class is one of the
|
|
|
|
/// rest of varargs.
|
|
|
|
template <class T, class P, class... ToCompare>
|
|
|
|
constexpr static bool isOneOf() {
|
|
|
|
return std::is_same<T, P>::value || isOneOf<T, ToCompare...>();
|
|
|
|
}
|
|
|
|
|
2019-01-11 02:14:51 +08:00
|
|
|
namespace {
|
|
|
|
|
2019-01-11 02:15:04 +08:00
|
|
|
/// Fake attribute class for RC* attributes.
|
2019-01-11 02:14:51 +08:00
|
|
|
struct GeneralizedReturnsRetainedAttr {
|
|
|
|
static bool classof(const Attr *A) {
|
|
|
|
if (auto AA = dyn_cast<AnnotateAttr>(A))
|
|
|
|
return AA->getAnnotation() == "rc_ownership_returns_retained";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GeneralizedReturnsNotRetainedAttr {
|
|
|
|
static bool classof(const Attr *A) {
|
|
|
|
if (auto AA = dyn_cast<AnnotateAttr>(A))
|
|
|
|
return AA->getAnnotation() == "rc_ownership_returns_not_retained";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GeneralizedConsumedAttr {
|
|
|
|
static bool classof(const Attr *A) {
|
|
|
|
if (auto AA = dyn_cast<AnnotateAttr>(A))
|
|
|
|
return AA->getAnnotation() == "rc_ownership_consumed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Optional<ObjKind> RetainSummaryManager::hasAnyEnabledAttrOf(const Decl *D,
|
|
|
|
QualType QT) {
|
|
|
|
ObjKind K;
|
2018-12-08 04:21:51 +08:00
|
|
|
if (isOneOf<T, CFConsumedAttr, CFReturnsRetainedAttr,
|
2019-01-11 02:14:51 +08:00
|
|
|
CFReturnsNotRetainedAttr>()) {
|
|
|
|
if (!TrackObjCAndCFObjects)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
K = ObjKind::CF;
|
|
|
|
} else if (isOneOf<T, NSConsumedAttr, NSConsumesSelfAttr,
|
|
|
|
NSReturnsAutoreleasedAttr, NSReturnsRetainedAttr,
|
|
|
|
NSReturnsNotRetainedAttr, NSConsumesSelfAttr>()) {
|
|
|
|
|
|
|
|
if (!TrackObjCAndCFObjects)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
if (isOneOf<T, NSReturnsRetainedAttr, NSReturnsAutoreleasedAttr,
|
|
|
|
NSReturnsNotRetainedAttr>() &&
|
|
|
|
!cocoa::isCocoaObjectRef(QT))
|
|
|
|
return None;
|
|
|
|
K = ObjKind::ObjC;
|
2018-12-08 04:21:51 +08:00
|
|
|
} else if (isOneOf<T, OSConsumedAttr, OSConsumesThisAttr,
|
2019-01-12 07:35:17 +08:00
|
|
|
OSReturnsNotRetainedAttr, OSReturnsRetainedAttr,
|
|
|
|
OSReturnsRetainedOnZeroAttr,
|
|
|
|
OSReturnsRetainedOnNonZeroAttr>()) {
|
2019-01-11 02:14:51 +08:00
|
|
|
if (!TrackOSObjects)
|
|
|
|
return None;
|
|
|
|
K = ObjKind::OS;
|
|
|
|
} else if (isOneOf<T, GeneralizedReturnsNotRetainedAttr,
|
|
|
|
GeneralizedReturnsRetainedAttr,
|
|
|
|
GeneralizedConsumedAttr>()) {
|
|
|
|
K = ObjKind::Generalized;
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected attribute");
|
2018-12-08 04:21:51 +08:00
|
|
|
}
|
2019-01-11 02:14:51 +08:00
|
|
|
if (D->hasAttr<T>())
|
|
|
|
return K;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T1, class T2, class... Others>
|
|
|
|
Optional<ObjKind> RetainSummaryManager::hasAnyEnabledAttrOf(const Decl *D,
|
|
|
|
QualType QT) {
|
|
|
|
if (auto Out = hasAnyEnabledAttrOf<T1>(D, QT))
|
|
|
|
return Out;
|
|
|
|
return hasAnyEnabledAttrOf<T2, Others...>(D, QT);
|
2018-12-08 04:21:51 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getPersistentSummary(const RetainSummary &OldSumm) {
|
|
|
|
// Unique "simple" summaries -- those without ArgEffects.
|
|
|
|
if (OldSumm.isSimple()) {
|
|
|
|
::llvm::FoldingSetNodeID ID;
|
|
|
|
OldSumm.Profile(ID);
|
|
|
|
|
|
|
|
void *Pos;
|
|
|
|
CachedSummaryNode *N = SimpleSummaries.FindNodeOrInsertPos(ID, Pos);
|
|
|
|
|
|
|
|
if (!N) {
|
|
|
|
N = (CachedSummaryNode *) BPAlloc.Allocate<CachedSummaryNode>();
|
|
|
|
new (N) CachedSummaryNode(OldSumm);
|
|
|
|
SimpleSummaries.InsertNode(N, Pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &N->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainSummary *Summ = (RetainSummary *) BPAlloc.Allocate<RetainSummary>();
|
|
|
|
new (Summ) RetainSummary(OldSumm);
|
|
|
|
return Summ;
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:28:33 +08:00
|
|
|
static bool isSubclass(const Decl *D,
|
|
|
|
StringRef ClassName) {
|
|
|
|
using namespace ast_matchers;
|
|
|
|
DeclarationMatcher SubclassM = cxxRecordDecl(isSameOrDerivedFrom(ClassName));
|
|
|
|
return !(match(SubclassM, *D, D->getASTContext()).empty());
|
|
|
|
}
|
2018-08-23 08:26:59 +08:00
|
|
|
|
2018-08-30 04:28:33 +08:00
|
|
|
static bool isOSObjectSubclass(const Decl *D) {
|
2019-01-19 03:24:55 +08:00
|
|
|
return isSubclass(D, "OSMetaClassBase");
|
2018-08-23 08:26:59 +08:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:38:07 +08:00
|
|
|
static bool isOSObjectDynamicCast(StringRef S) {
|
|
|
|
return S == "safeMetaCast";
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:50:47 +08:00
|
|
|
static bool isOSObjectThisCast(StringRef S) {
|
|
|
|
return S == "metaCast";
|
|
|
|
}
|
|
|
|
|
2018-11-01 01:38:46 +08:00
|
|
|
static bool isOSIteratorSubclass(const Decl *D) {
|
|
|
|
return isSubclass(D, "OSIterator");
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:42:05 +08:00
|
|
|
static bool hasRCAnnotation(const Decl *D, StringRef rcAnnotation) {
|
|
|
|
for (const auto *Ann : D->specific_attrs<AnnotateAttr>()) {
|
|
|
|
if (Ann->getAnnotation() == rcAnnotation)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isRetain(const FunctionDecl *FD, StringRef FName) {
|
|
|
|
return FName.startswith_lower("retain") || FName.endswith_lower("retain");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isRelease(const FunctionDecl *FD, StringRef FName) {
|
|
|
|
return FName.startswith_lower("release") || FName.endswith_lower("release");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAutorelease(const FunctionDecl *FD, StringRef FName) {
|
|
|
|
return FName.startswith_lower("autorelease") ||
|
|
|
|
FName.endswith_lower("autorelease");
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:42:59 +08:00
|
|
|
static bool isMakeCollectable(StringRef FName) {
|
|
|
|
return FName.contains_lower("MakeCollectable");
|
|
|
|
}
|
|
|
|
|
2018-10-24 07:11:50 +08:00
|
|
|
/// A function is OSObject related if it is declared on a subclass
|
|
|
|
/// of OSObject, or any of the parameters is a subclass of an OSObject.
|
|
|
|
static bool isOSObjectRelated(const CXXMethodDecl *MD) {
|
|
|
|
if (isOSObjectSubclass(MD->getParent()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (ParmVarDecl *Param : MD->parameters()) {
|
2018-11-01 01:38:29 +08:00
|
|
|
QualType PT = Param->getType()->getPointeeType();
|
|
|
|
if (!PT.isNull())
|
|
|
|
if (CXXRecordDecl *RD = PT->getAsCXXRecordDecl())
|
|
|
|
if (isOSObjectSubclass(RD))
|
|
|
|
return true;
|
2018-10-24 07:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-19 03:24:55 +08:00
|
|
|
bool
|
|
|
|
RetainSummaryManager::isKnownSmartPointer(QualType QT) {
|
|
|
|
QT = QT.getCanonicalType();
|
|
|
|
const auto *RD = QT->getAsCXXRecordDecl();
|
|
|
|
if (!RD)
|
|
|
|
return false;
|
|
|
|
const IdentifierInfo *II = RD->getIdentifier();
|
|
|
|
if (II && II->getName() == "smart_ptr")
|
|
|
|
if (const auto *ND = dyn_cast<NamespaceDecl>(RD->getDeclContext()))
|
|
|
|
if (ND->getNameAsString() == "os")
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
const RetainSummary *
|
2018-12-08 04:21:51 +08:00
|
|
|
RetainSummaryManager::getSummaryForOSObject(const FunctionDecl *FD,
|
|
|
|
StringRef FName, QualType RetTy) {
|
|
|
|
if (RetTy->isPointerType()) {
|
|
|
|
const CXXRecordDecl *PD = RetTy->getPointeeType()->getAsCXXRecordDecl();
|
|
|
|
if (PD && isOSObjectSubclass(PD)) {
|
|
|
|
if (const IdentifierInfo *II = FD->getIdentifier()) {
|
2019-01-23 03:50:47 +08:00
|
|
|
StringRef FuncName = II->getName();
|
|
|
|
if (isOSObjectDynamicCast(FuncName) || isOSObjectThisCast(FuncName))
|
2018-12-08 04:21:51 +08:00
|
|
|
return getDefaultSummary();
|
|
|
|
|
|
|
|
// All objects returned with functions *not* starting with
|
|
|
|
// get, or iterators, are returned at +1.
|
2019-01-23 03:50:47 +08:00
|
|
|
if ((!FuncName.startswith("get") && !FuncName.startswith("Get")) ||
|
2018-12-08 04:21:51 +08:00
|
|
|
isOSIteratorSubclass(PD)) {
|
|
|
|
return getOSSummaryCreateRule(FD);
|
|
|
|
} else {
|
|
|
|
return getOSSummaryGetRule(FD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 05:41:37 +08:00
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-12-08 04:21:51 +08:00
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
const CXXRecordDecl *Parent = MD->getParent();
|
|
|
|
if (TrackOSObjects && Parent && isOSObjectSubclass(Parent)) {
|
2019-01-30 03:29:07 +08:00
|
|
|
if (FName == "release" || FName == "taggedRelease")
|
2018-12-08 04:21:51 +08:00
|
|
|
return getOSSummaryReleaseRule(FD);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2019-01-30 03:29:07 +08:00
|
|
|
if (FName == "retain" || FName == "taggedRetain")
|
2018-12-08 04:21:51 +08:00
|
|
|
return getOSSummaryRetainRule(FD);
|
2018-08-18 05:41:37 +08:00
|
|
|
|
2018-12-08 04:21:51 +08:00
|
|
|
if (FName == "free")
|
|
|
|
return getOSSummaryFreeRule(FD);
|
|
|
|
|
|
|
|
if (MD->getOverloadedOperator() == OO_New)
|
|
|
|
return getOSSummaryCreateRule(MD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *RetainSummaryManager::getSummaryForObjCOrCFObject(
|
|
|
|
const FunctionDecl *FD,
|
|
|
|
StringRef FName,
|
|
|
|
QualType RetTy,
|
|
|
|
const FunctionType *FT,
|
|
|
|
bool &AllowAnnotations) {
|
2018-08-18 05:41:37 +08:00
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
ArgEffects ScratchArgs(AF.getEmptyMap());
|
2018-08-18 05:41:37 +08:00
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
std::string RetTyName = RetTy.getAsString();
|
2018-08-18 05:41:37 +08:00
|
|
|
if (FName == "pthread_create" || FName == "pthread_setspecific") {
|
|
|
|
// Part of: <rdar://problem/7299394> and <rdar://problem/11282706>.
|
|
|
|
// This will be addressed better with IPA.
|
|
|
|
return getPersistentStopSummary();
|
2018-08-18 05:42:59 +08:00
|
|
|
} else if(FName == "NSMakeCollectable") {
|
|
|
|
// Handle: id NSMakeCollectable(CFTypeRef)
|
|
|
|
AllowAnnotations = false;
|
2019-01-11 02:14:38 +08:00
|
|
|
return RetTy->isObjCIdType() ? getUnarySummary(FT, DoNothing)
|
2018-08-18 05:42:59 +08:00
|
|
|
: getPersistentStopSummary();
|
2018-12-21 08:18:58 +08:00
|
|
|
} else if (FName == "CMBufferQueueDequeueAndRetain" ||
|
|
|
|
FName == "CMBufferQueueDequeueIfDataReadyAndRetain") {
|
|
|
|
// Part of: <rdar://problem/39390714>.
|
2019-01-11 02:13:46 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeOwned(ObjKind::CF),
|
2018-12-25 02:45:18 +08:00
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing),
|
|
|
|
ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "CFPlugInInstanceCreate") {
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(), ScratchArgs);
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "IORegistryEntrySearchCFProperty" ||
|
|
|
|
(RetTyName == "CFMutableDictionaryRef" &&
|
|
|
|
(FName == "IOBSDNameMatching" || FName == "IOServiceMatching" ||
|
2018-08-18 05:41:07 +08:00
|
|
|
FName == "IOServiceNameMatching" ||
|
|
|
|
FName == "IORegistryEntryIDMatching" ||
|
2018-08-18 05:41:37 +08:00
|
|
|
FName == "IOOpenFirmwarePathMatching"))) {
|
|
|
|
// Part of <rdar://problem/6961230>. (IOKit)
|
|
|
|
// This should be addressed using a API table.
|
2019-01-11 02:13:59 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeOwned(ObjKind::CF), ScratchArgs,
|
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "IOServiceGetMatchingService" ||
|
|
|
|
FName == "IOServiceGetMatchingServices") {
|
|
|
|
// FIXES: <rdar://problem/6326900>
|
|
|
|
// This should be addressed using a API table. This strcmp is also
|
|
|
|
// a little gross, but there is no need to super optimize here.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 1, ArgEffect(DecRef, ObjKind::CF));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "IOServiceAddNotification" ||
|
|
|
|
FName == "IOServiceAddMatchingNotification") {
|
|
|
|
// Part of <rdar://problem/6961230>. (IOKit)
|
|
|
|
// This should be addressed using a API table.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 2, ArgEffect(DecRef, ObjKind::CF));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "CVPixelBufferCreateWithBytes") {
|
|
|
|
// FIXES: <rdar://problem/7283567>
|
|
|
|
// Eventually this can be improved by recognizing that the pixel
|
|
|
|
// buffer passed to CVPixelBufferCreateWithBytes is released via
|
|
|
|
// a callback and doing full IPA to make sure this is done correctly.
|
|
|
|
// FIXME: This function has an out parameter that returns an
|
|
|
|
// allocated object.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 7, ArgEffect(StopTracking));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "CGBitmapContextCreateWithData") {
|
|
|
|
// FIXES: <rdar://problem/7358899>
|
|
|
|
// Eventually this can be improved by recognizing that 'releaseInfo'
|
|
|
|
// passed to CGBitmapContextCreateWithData is released via
|
|
|
|
// a callback and doing full IPA to make sure this is done correctly.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 8, ArgEffect(ArgEffect(StopTracking)));
|
|
|
|
return getPersistentSummary(RetEffect::MakeOwned(ObjKind::CF), ScratchArgs,
|
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "CVPixelBufferCreateWithPlanarBytes") {
|
|
|
|
// FIXES: <rdar://problem/7283567>
|
|
|
|
// Eventually this can be improved by recognizing that the pixel
|
|
|
|
// buffer passed to CVPixelBufferCreateWithPlanarBytes is released
|
|
|
|
// via a callback and doing full IPA to make sure this is done
|
|
|
|
// correctly.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 12, ArgEffect(StopTracking));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "VTCompressionSessionEncodeFrame") {
|
|
|
|
// The context argument passed to VTCompressionSessionEncodeFrame()
|
|
|
|
// is passed to the callback specified when creating the session
|
|
|
|
// (e.g. with VTCompressionSessionCreate()) which can release it.
|
|
|
|
// To account for this possibility, conservatively stop tracking
|
|
|
|
// the context.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 5, ArgEffect(StopTracking));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName == "dispatch_set_context" ||
|
|
|
|
FName == "xpc_connection_set_context") {
|
|
|
|
// <rdar://problem/11059275> - The analyzer currently doesn't have
|
|
|
|
// a good way to reason about the finalizer function for libdispatch.
|
|
|
|
// If we pass a context object that is memory managed, stop tracking it.
|
|
|
|
// <rdar://problem/13783514> - Same problem, but for XPC.
|
|
|
|
// FIXME: this hack should possibly go away once we can handle
|
|
|
|
// libdispatch and XPC finalizers.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 1, ArgEffect(StopTracking));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
} else if (FName.startswith("NSLog")) {
|
|
|
|
return getDoNothingSummary();
|
|
|
|
} else if (FName.startswith("NS") &&
|
|
|
|
(FName.find("Insert") != StringRef::npos)) {
|
|
|
|
// Whitelist NSXXInsertXX, for example NSMapInsertIfAbsent, since they can
|
|
|
|
// be deallocated by NSMapRemove. (radar://11152419)
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 1, ArgEffect(StopTracking));
|
|
|
|
ScratchArgs = AF.add(ScratchArgs, 2, ArgEffect(StopTracking));
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs, ArgEffect(DoNothing),
|
|
|
|
ArgEffect(DoNothing));
|
2018-08-18 05:41:37 +08:00
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
if (RetTy->isPointerType()) {
|
2018-08-30 04:28:33 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
// For CoreFoundation ('CF') types.
|
|
|
|
if (cocoa::isRefType(RetTy, "CF", FName)) {
|
2018-08-18 05:42:05 +08:00
|
|
|
if (isRetain(FD, FName)) {
|
2018-12-08 04:21:51 +08:00
|
|
|
// CFRetain isn't supposed to be annotated. However, this may as
|
|
|
|
// well be a user-made "safe" CFRetain function that is incorrectly
|
2018-08-18 05:41:37 +08:00
|
|
|
// annotated as cf_returns_retained due to lack of better options.
|
|
|
|
// We want to ignore such annotation.
|
|
|
|
AllowAnnotations = false;
|
|
|
|
|
2019-01-11 02:14:38 +08:00
|
|
|
return getUnarySummary(FT, IncRef);
|
2018-08-18 05:42:05 +08:00
|
|
|
} else if (isAutorelease(FD, FName)) {
|
2018-08-18 05:41:37 +08:00
|
|
|
// The headers use cf_consumed, but we can fully model CFAutorelease
|
|
|
|
// ourselves.
|
|
|
|
AllowAnnotations = false;
|
|
|
|
|
2019-01-11 02:14:38 +08:00
|
|
|
return getUnarySummary(FT, Autorelease);
|
2018-08-18 05:42:59 +08:00
|
|
|
} else if (isMakeCollectable(FName)) {
|
|
|
|
AllowAnnotations = false;
|
2019-01-11 02:14:38 +08:00
|
|
|
return getUnarySummary(FT, DoNothing);
|
2018-08-18 05:41:37 +08:00
|
|
|
} else {
|
|
|
|
return getCFCreateGetRuleSummary(FD);
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
2018-08-18 05:41:37 +08:00
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
// For CoreGraphics ('CG') and CoreVideo ('CV') types.
|
|
|
|
if (cocoa::isRefType(RetTy, "CG", FName) ||
|
|
|
|
cocoa::isRefType(RetTy, "CV", FName)) {
|
2018-08-18 05:42:05 +08:00
|
|
|
if (isRetain(FD, FName))
|
2019-01-11 02:14:38 +08:00
|
|
|
return getUnarySummary(FT, IncRef);
|
2018-08-18 05:41:37 +08:00
|
|
|
else
|
|
|
|
return getCFCreateGetRuleSummary(FD);
|
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
// For all other CF-style types, use the Create/Get
|
|
|
|
// rule for summaries but don't support Retain functions
|
|
|
|
// with framework-specific prefixes.
|
|
|
|
if (coreFoundation::isCFObjectRef(RetTy)) {
|
|
|
|
return getCFCreateGetRuleSummary(FD);
|
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
if (FD->hasAttr<CFAuditedTransferAttr>()) {
|
|
|
|
return getCFCreateGetRuleSummary(FD);
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
2018-08-18 05:41:37 +08:00
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
// Check for release functions, the only kind of functions that we care
|
|
|
|
// about that don't return a pointer type.
|
2018-12-08 04:21:51 +08:00
|
|
|
if (FName.startswith("CG") || FName.startswith("CF")) {
|
2018-08-18 05:41:37 +08:00
|
|
|
// Test for 'CGCF'.
|
|
|
|
FName = FName.substr(FName.startswith("CGCF") ? 4 : 2);
|
|
|
|
|
2018-08-18 05:42:05 +08:00
|
|
|
if (isRelease(FD, FName))
|
2019-01-11 02:14:38 +08:00
|
|
|
return getUnarySummary(FT, DecRef);
|
2018-08-18 05:41:37 +08:00
|
|
|
else {
|
|
|
|
assert(ScratchArgs.isEmpty());
|
|
|
|
// Remaining CoreFoundation and CoreGraphics functions.
|
|
|
|
// We use to assume that they all strictly followed the ownership idiom
|
|
|
|
// and that ownership cannot be transferred. While this is technically
|
|
|
|
// correct, many methods allow a tracked object to escape. For example:
|
|
|
|
//
|
|
|
|
// CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
|
|
|
|
// CFDictionaryAddValue(y, key, x);
|
|
|
|
// CFRelease(x);
|
|
|
|
// ... it is okay to use 'x' since 'y' has a reference to it
|
|
|
|
//
|
|
|
|
// We handle this and similar cases with the follow heuristic. If the
|
|
|
|
// function name contains "InsertValue", "SetValue", "AddValue",
|
|
|
|
// "AppendValue", or "SetAttribute", then we assume that arguments may
|
|
|
|
// "escape." This means that something else holds on to the object,
|
|
|
|
// allowing it be used even after its local retain count drops to 0.
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffectKind E =
|
|
|
|
(StrInStrNoCase(FName, "InsertValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "AddValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "SetValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "AppendValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "SetAttribute") != StringRef::npos)
|
|
|
|
? MayEscape
|
|
|
|
: DoNothing;
|
2018-08-18 05:41:37 +08:00
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(), ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(E, ObjKind::CF));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 04:21:51 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::generateSummary(const FunctionDecl *FD,
|
|
|
|
bool &AllowAnnotations) {
|
|
|
|
// We generate "stop" summaries for implicitly defined functions.
|
|
|
|
if (FD->isImplicit())
|
|
|
|
return getPersistentStopSummary();
|
|
|
|
|
|
|
|
const IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
|
|
|
|
StringRef FName = II ? II->getName() : "";
|
|
|
|
|
|
|
|
// Strip away preceding '_'. Doing this here will effect all the checks
|
|
|
|
// down below.
|
|
|
|
FName = FName.substr(FName.find_first_not_of('_'));
|
|
|
|
|
|
|
|
// Inspect the result type. Strip away any typedefs.
|
|
|
|
const auto *FT = FD->getType()->getAs<FunctionType>();
|
|
|
|
QualType RetTy = FT->getReturnType();
|
|
|
|
|
|
|
|
if (TrackOSObjects)
|
|
|
|
if (const RetainSummary *S = getSummaryForOSObject(FD, FName, RetTy))
|
|
|
|
return S;
|
|
|
|
|
|
|
|
if (TrackObjCAndCFObjects)
|
|
|
|
if (const RetainSummary *S =
|
|
|
|
getSummaryForObjCOrCFObject(FD, FName, RetTy, FT, AllowAnnotations))
|
|
|
|
return S;
|
|
|
|
|
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
|
2018-10-24 07:11:50 +08:00
|
|
|
if (!(TrackOSObjects && isOSObjectRelated(MD)))
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ArgEffects(AF.getEmptyMap()),
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing),
|
|
|
|
ArgEffect(StopTracking),
|
|
|
|
ArgEffect(DoNothing));
|
2018-08-23 08:26:59 +08:00
|
|
|
|
2018-08-18 05:41:37 +08:00
|
|
|
return getDefaultSummary();
|
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getFunctionSummary(const FunctionDecl *FD) {
|
|
|
|
// If we don't know what function we're calling, use our default summary.
|
|
|
|
if (!FD)
|
|
|
|
return getDefaultSummary();
|
|
|
|
|
|
|
|
// Look up a summary in our cache of FunctionDecls -> Summaries.
|
|
|
|
FuncSummariesTy::iterator I = FuncSummaries.find(FD);
|
|
|
|
if (I != FuncSummaries.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
// No summary? Generate one.
|
|
|
|
bool AllowAnnotations = true;
|
|
|
|
const RetainSummary *S = generateSummary(FD, AllowAnnotations);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
// Annotations override defaults.
|
|
|
|
if (AllowAnnotations)
|
|
|
|
updateSummaryFromAnnotations(S, FD);
|
|
|
|
|
|
|
|
FuncSummaries[FD] = S;
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Summary creation for functions (largely uses of Core Foundation).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ArgEffect getStopTrackingHardEquivalent(ArgEffect E) {
|
2019-01-11 02:13:59 +08:00
|
|
|
switch (E.getKind()) {
|
2018-08-18 05:41:07 +08:00
|
|
|
case DoNothing:
|
|
|
|
case Autorelease:
|
|
|
|
case DecRefBridgedTransferred:
|
|
|
|
case IncRef:
|
|
|
|
case UnretainedOutParameter:
|
|
|
|
case RetainedOutParameter:
|
2019-01-12 07:35:17 +08:00
|
|
|
case RetainedOutParameterOnZero:
|
|
|
|
case RetainedOutParameterOnNonZero:
|
2018-08-18 05:41:07 +08:00
|
|
|
case MayEscape:
|
|
|
|
case StopTracking:
|
|
|
|
case StopTrackingHard:
|
2019-01-11 02:13:59 +08:00
|
|
|
return E.withKind(StopTrackingHard);
|
2018-08-18 05:41:07 +08:00
|
|
|
case DecRef:
|
|
|
|
case DecRefAndStopTrackingHard:
|
2019-01-11 02:13:59 +08:00
|
|
|
return E.withKind(DecRefAndStopTrackingHard);
|
2018-08-18 05:41:07 +08:00
|
|
|
case Dealloc:
|
2019-01-11 02:13:59 +08:00
|
|
|
return E.withKind(Dealloc);
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown ArgEffect kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RetainSummaryManager::updateSummaryForCall(const RetainSummary *&S,
|
2019-01-25 09:24:04 +08:00
|
|
|
AnyCall C,
|
|
|
|
bool HasNonZeroCallbackArg,
|
|
|
|
bool IsReceiverUnconsumedSelf) {
|
|
|
|
|
|
|
|
if (HasNonZeroCallbackArg) {
|
2018-08-18 05:41:07 +08:00
|
|
|
ArgEffect RecEffect =
|
|
|
|
getStopTrackingHardEquivalent(S->getReceiverEffect());
|
|
|
|
ArgEffect DefEffect =
|
|
|
|
getStopTrackingHardEquivalent(S->getDefaultArgEffect());
|
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
ArgEffects ScratchArgs(AF.getEmptyMap());
|
2018-08-18 05:41:07 +08:00
|
|
|
ArgEffects CustomArgEffects = S->getArgEffects();
|
|
|
|
for (ArgEffects::iterator I = CustomArgEffects.begin(),
|
|
|
|
E = CustomArgEffects.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ArgEffect Translated = getStopTrackingHardEquivalent(I->second);
|
2019-01-11 02:13:59 +08:00
|
|
|
if (Translated.getKind() != DefEffect.getKind())
|
2018-08-18 05:41:07 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, I->first, Translated);
|
|
|
|
}
|
|
|
|
|
|
|
|
RetEffect RE = RetEffect::MakeNoRetHard();
|
|
|
|
|
|
|
|
// Special cases where the callback argument CANNOT free the return value.
|
|
|
|
// This can generally only happen if we know that the callback will only be
|
|
|
|
// called when the return value is already being deallocated.
|
2019-01-25 09:24:04 +08:00
|
|
|
if (C.getKind() == AnyCall::Function) {
|
|
|
|
if (const IdentifierInfo *Name = C.getIdentifier()) {
|
2018-08-18 05:41:07 +08:00
|
|
|
// When the CGBitmapContext is deallocated, the callback here will free
|
|
|
|
// the associated data buffer.
|
|
|
|
// The callback in dispatch_data_create frees the buffer, but not
|
|
|
|
// the data object.
|
|
|
|
if (Name->isStr("CGBitmapContextCreateWithData") ||
|
|
|
|
Name->isStr("dispatch_data_create"))
|
|
|
|
RE = S->getRetEffect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
S = getPersistentSummary(RE, ScratchArgs, RecEffect, DefEffect);
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case '[super init];' and '[self init];'
|
|
|
|
//
|
|
|
|
// Even though calling '[super init]' without assigning the result to self
|
|
|
|
// and checking if the parent returns 'nil' is a bad pattern, it is common.
|
|
|
|
// Additionally, our Self Init checker already warns about it. To avoid
|
|
|
|
// overwhelming the user with messages from both checkers, we model the case
|
|
|
|
// of '[super init]' in cases when it is not consumed by another expression
|
|
|
|
// as if the call preserves the value of 'self'; essentially, assuming it can
|
|
|
|
// never fail and return 'nil'.
|
|
|
|
// Note, we don't want to just stop tracking the value since we want the
|
|
|
|
// RetainCount checker to report leaks and use-after-free if SelfInit checker
|
|
|
|
// is turned off.
|
2019-01-25 09:24:04 +08:00
|
|
|
if (IsReceiverUnconsumedSelf) {
|
|
|
|
RetainSummaryTemplate ModifiableSummaryTemplate(S, *this);
|
|
|
|
ModifiableSummaryTemplate->setReceiverEffect(ArgEffect(DoNothing));
|
|
|
|
ModifiableSummaryTemplate->setRetEffect(RetEffect::MakeNoRet());
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
2019-01-25 09:24:04 +08:00
|
|
|
RetainSummaryManager::getSummary(AnyCall C,
|
|
|
|
bool HasNonZeroCallbackArg,
|
|
|
|
bool IsReceiverUnconsumedSelf,
|
2018-08-21 11:09:02 +08:00
|
|
|
QualType ReceiverType) {
|
2018-08-18 05:41:07 +08:00
|
|
|
const RetainSummary *Summ;
|
2019-01-25 09:24:04 +08:00
|
|
|
switch (C.getKind()) {
|
|
|
|
case AnyCall::Function:
|
|
|
|
case AnyCall::Constructor:
|
|
|
|
case AnyCall::Allocator:
|
|
|
|
case AnyCall::Deallocator:
|
|
|
|
Summ = getFunctionSummary(cast_or_null<FunctionDecl>(C.getDecl()));
|
2018-10-26 07:38:41 +08:00
|
|
|
break;
|
2019-01-25 09:24:04 +08:00
|
|
|
case AnyCall::Block:
|
|
|
|
case AnyCall::Destructor:
|
2018-08-18 05:41:07 +08:00
|
|
|
// FIXME: These calls are currently unsupported.
|
|
|
|
return getPersistentStopSummary();
|
2019-01-25 09:24:04 +08:00
|
|
|
case AnyCall::ObjCMethod: {
|
2019-01-30 03:29:33 +08:00
|
|
|
const auto *ME = cast_or_null<ObjCMessageExpr>(C.getExpr());
|
|
|
|
if (!ME) {
|
|
|
|
return getMethodSummary(cast<ObjCMethodDecl>(C.getDecl()));
|
|
|
|
} else if (ME->isInstanceMessage()) {
|
2019-01-25 09:24:04 +08:00
|
|
|
Summ = getInstanceMethodSummary(ME, ReceiverType);
|
2019-01-30 03:29:33 +08:00
|
|
|
} else {
|
2019-01-25 09:24:04 +08:00
|
|
|
Summ = getClassMethodSummary(ME);
|
2019-01-30 03:29:33 +08:00
|
|
|
}
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 09:24:04 +08:00
|
|
|
updateSummaryForCall(Summ, C, HasNonZeroCallbackArg,
|
|
|
|
IsReceiverUnconsumedSelf);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
assert(Summ && "Unknown call type?");
|
|
|
|
return Summ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getCFCreateGetRuleSummary(const FunctionDecl *FD) {
|
|
|
|
if (coreFoundation::followsCreateRule(FD))
|
|
|
|
return getCFSummaryCreateRule(FD);
|
|
|
|
|
|
|
|
return getCFSummaryGetRule(FD);
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:42:05 +08:00
|
|
|
bool RetainSummaryManager::isTrustedReferenceCountImplementation(
|
|
|
|
const FunctionDecl *FD) {
|
|
|
|
return hasRCAnnotation(FD, "rc_ownership_trusted_implementation");
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:38:07 +08:00
|
|
|
Optional<RetainSummaryManager::BehaviorSummary>
|
|
|
|
RetainSummaryManager::canEval(const CallExpr *CE, const FunctionDecl *FD,
|
|
|
|
bool &hasTrustedImplementationAnnotation) {
|
2018-08-18 05:42:05 +08:00
|
|
|
|
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II)
|
2018-10-26 07:38:07 +08:00
|
|
|
return None;
|
2018-08-18 05:42:05 +08:00
|
|
|
|
|
|
|
StringRef FName = II->getName();
|
|
|
|
FName = FName.substr(FName.find_first_not_of('_'));
|
|
|
|
|
|
|
|
QualType ResultTy = CE->getCallReturnType(Ctx);
|
2018-08-18 05:42:59 +08:00
|
|
|
if (ResultTy->isObjCIdType()) {
|
2018-10-26 07:38:07 +08:00
|
|
|
if (II->isStr("NSMakeCollectable"))
|
|
|
|
return BehaviorSummary::Identity;
|
2018-08-18 05:42:59 +08:00
|
|
|
} else if (ResultTy->isPointerType()) {
|
2018-08-18 05:42:05 +08:00
|
|
|
// Handle: (CF|CG|CV)Retain
|
|
|
|
// CFAutorelease
|
|
|
|
// It's okay to be a little sloppy here.
|
2018-12-21 08:18:58 +08:00
|
|
|
if (FName == "CMBufferQueueDequeueAndRetain" ||
|
|
|
|
FName == "CMBufferQueueDequeueIfDataReadyAndRetain") {
|
|
|
|
// Part of: <rdar://problem/39390714>.
|
|
|
|
// These are not retain. They just return something and retain it.
|
|
|
|
return None;
|
|
|
|
}
|
2018-08-18 05:42:05 +08:00
|
|
|
if (cocoa::isRefType(ResultTy, "CF", FName) ||
|
|
|
|
cocoa::isRefType(ResultTy, "CG", FName) ||
|
|
|
|
cocoa::isRefType(ResultTy, "CV", FName))
|
2018-10-26 07:38:07 +08:00
|
|
|
if (isRetain(FD, FName) || isAutorelease(FD, FName) ||
|
|
|
|
isMakeCollectable(FName))
|
|
|
|
return BehaviorSummary::Identity;
|
|
|
|
|
|
|
|
// safeMetaCast is called by OSDynamicCast.
|
|
|
|
// We assume that OSDynamicCast is either an identity (cast is OK,
|
|
|
|
// the input was non-zero),
|
|
|
|
// or that it returns zero (when the cast failed, or the input
|
|
|
|
// was zero).
|
2019-01-23 03:50:47 +08:00
|
|
|
if (TrackOSObjects) {
|
|
|
|
if (isOSObjectDynamicCast(FName) && FD->param_size() >= 1) {
|
|
|
|
return BehaviorSummary::IdentityOrZero;
|
|
|
|
} else if (isOSObjectThisCast(FName) && isa<CXXMethodDecl>(FD) &&
|
|
|
|
!cast<CXXMethodDecl>(FD)->isStatic()) {
|
|
|
|
return BehaviorSummary::IdentityThis;
|
|
|
|
}
|
2018-10-24 07:11:30 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:43:27 +08:00
|
|
|
const FunctionDecl* FDD = FD->getDefinition();
|
|
|
|
if (FDD && isTrustedReferenceCountImplementation(FDD)) {
|
|
|
|
hasTrustedImplementationAnnotation = true;
|
2018-10-26 07:38:07 +08:00
|
|
|
return BehaviorSummary::Identity;
|
2018-08-18 05:42:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 07:11:30 +08:00
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
const CXXRecordDecl *Parent = MD->getParent();
|
|
|
|
if (TrackOSObjects && Parent && isOSObjectSubclass(Parent))
|
2018-10-26 07:38:07 +08:00
|
|
|
if (FName == "release" || FName == "retain")
|
|
|
|
return BehaviorSummary::NoOp;
|
2018-10-24 07:11:30 +08:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:38:07 +08:00
|
|
|
return None;
|
2018-08-18 05:42:05 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getUnarySummary(const FunctionType* FT,
|
2019-01-11 02:14:38 +08:00
|
|
|
ArgEffectKind AE) {
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
// Unary functions have no arg effects by definition.
|
|
|
|
ArgEffects ScratchArgs(AF.getEmptyMap());
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
// Sanity check that this is *really* a unary function. This can
|
|
|
|
// happen if people do weird things.
|
|
|
|
const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
|
|
|
|
if (!FTP || FTP->getNumParams() != 1)
|
|
|
|
return getPersistentStopSummary();
|
|
|
|
|
2019-01-11 02:14:38 +08:00
|
|
|
ArgEffect Effect(AE, ObjKind::CF);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
ScratchArgs = AF.add(ScratchArgs, 0, Effect);
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
ScratchArgs,
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 08:26:59 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getOSSummaryRetainRule(const FunctionDecl *FD) {
|
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
2018-12-25 02:45:18 +08:00
|
|
|
AF.getEmptyMap(),
|
2019-01-11 02:13:59 +08:00
|
|
|
/*ReceiverEff=*/ArgEffect(DoNothing),
|
|
|
|
/*DefaultEff=*/ArgEffect(DoNothing),
|
|
|
|
/*ThisEff=*/ArgEffect(IncRef, ObjKind::OS));
|
2018-08-23 08:26:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getOSSummaryReleaseRule(const FunctionDecl *FD) {
|
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
2018-12-25 02:45:18 +08:00
|
|
|
AF.getEmptyMap(),
|
2019-01-11 02:13:59 +08:00
|
|
|
/*ReceiverEff=*/ArgEffect(DoNothing),
|
|
|
|
/*DefaultEff=*/ArgEffect(DoNothing),
|
|
|
|
/*ThisEff=*/ArgEffect(DecRef, ObjKind::OS));
|
2018-08-23 08:26:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-01 04:43:42 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getOSSummaryFreeRule(const FunctionDecl *FD) {
|
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
2018-12-25 02:45:18 +08:00
|
|
|
AF.getEmptyMap(),
|
2019-01-11 02:13:59 +08:00
|
|
|
/*ReceiverEff=*/ArgEffect(DoNothing),
|
|
|
|
/*DefaultEff=*/ArgEffect(DoNothing),
|
|
|
|
/*ThisEff=*/ArgEffect(Dealloc, ObjKind::OS));
|
2018-12-01 04:43:42 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 08:26:59 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getOSSummaryCreateRule(const FunctionDecl *FD) {
|
2019-01-11 02:13:46 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeOwned(ObjKind::OS),
|
2018-12-25 02:45:18 +08:00
|
|
|
AF.getEmptyMap());
|
2018-08-23 08:26:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getOSSummaryGetRule(const FunctionDecl *FD) {
|
2019-01-11 02:13:46 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNotOwned(ObjKind::OS),
|
2018-12-25 02:45:18 +08:00
|
|
|
AF.getEmptyMap());
|
2018-08-23 08:26:59 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getCFSummaryCreateRule(const FunctionDecl *FD) {
|
2019-01-11 02:13:46 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeOwned(ObjKind::CF),
|
2018-12-25 02:45:18 +08:00
|
|
|
ArgEffects(AF.getEmptyMap()));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getCFSummaryGetRule(const FunctionDecl *FD) {
|
2019-01-11 02:13:46 +08:00
|
|
|
return getPersistentSummary(RetEffect::MakeNotOwned(ObjKind::CF),
|
2018-12-25 02:45:18 +08:00
|
|
|
ArgEffects(AF.getEmptyMap()),
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(DoNothing), ArgEffect(DoNothing));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Summary creation for Selectors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Optional<RetEffect>
|
|
|
|
RetainSummaryManager::getRetEffectFromAnnotations(QualType RetTy,
|
|
|
|
const Decl *D) {
|
2019-01-11 02:14:51 +08:00
|
|
|
if (hasAnyEnabledAttrOf<NSReturnsRetainedAttr>(D, RetTy))
|
|
|
|
return ObjCAllocRetE;
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2019-01-11 02:14:51 +08:00
|
|
|
if (auto K = hasAnyEnabledAttrOf<CFReturnsRetainedAttr, OSReturnsRetainedAttr,
|
|
|
|
GeneralizedReturnsRetainedAttr>(D, RetTy))
|
|
|
|
return RetEffect::MakeOwned(*K);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2019-01-11 02:14:51 +08:00
|
|
|
if (auto K = hasAnyEnabledAttrOf<
|
|
|
|
CFReturnsNotRetainedAttr, OSReturnsNotRetainedAttr,
|
|
|
|
GeneralizedReturnsNotRetainedAttr, NSReturnsNotRetainedAttr,
|
|
|
|
NSReturnsAutoreleasedAttr>(D, RetTy))
|
|
|
|
return RetEffect::MakeNotOwned(*K);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
2018-12-06 02:34:54 +08:00
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
|
|
|
|
for (const auto *PD : MD->overridden_methods())
|
|
|
|
if (auto RE = getRetEffectFromAnnotations(RetTy, PD))
|
|
|
|
return RE;
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:35:17 +08:00
|
|
|
/// \return Whether the chain of typedefs starting from {@code QT}
|
|
|
|
/// has a typedef with a given name {@code Name}.
|
|
|
|
static bool hasTypedefNamed(QualType QT,
|
|
|
|
StringRef Name) {
|
|
|
|
while (auto *T = dyn_cast<TypedefType>(QT)) {
|
|
|
|
const auto &Context = T->getDecl()->getASTContext();
|
|
|
|
if (T->getDecl()->getIdentifier() == &Context.Idents.get(Name))
|
|
|
|
return true;
|
|
|
|
QT = T->getDecl()->getUnderlyingType();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QualType getCallableReturnType(const NamedDecl *ND) {
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
|
|
|
|
return FD->getReturnType();
|
|
|
|
} else if (const auto *MD = dyn_cast<ObjCMethodDecl>(ND)) {
|
|
|
|
return MD->getReturnType();
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected decl");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 02:15:30 +08:00
|
|
|
bool RetainSummaryManager::applyParamAnnotationEffect(
|
|
|
|
const ParmVarDecl *pd, unsigned parm_idx, const NamedDecl *FD,
|
2018-12-25 02:45:18 +08:00
|
|
|
RetainSummaryTemplate &Template) {
|
2019-01-11 02:14:51 +08:00
|
|
|
QualType QT = pd->getType();
|
|
|
|
if (auto K =
|
|
|
|
hasAnyEnabledAttrOf<NSConsumedAttr, CFConsumedAttr, OSConsumedAttr,
|
|
|
|
GeneralizedConsumedAttr>(pd, QT)) {
|
|
|
|
Template->addArg(AF, parm_idx, ArgEffect(DecRef, *K));
|
2019-01-11 02:13:59 +08:00
|
|
|
return true;
|
2019-01-12 07:35:17 +08:00
|
|
|
} else if (auto K = hasAnyEnabledAttrOf<
|
|
|
|
CFReturnsRetainedAttr, OSReturnsRetainedAttr,
|
|
|
|
OSReturnsRetainedOnNonZeroAttr, OSReturnsRetainedOnZeroAttr,
|
|
|
|
GeneralizedReturnsRetainedAttr>(pd, QT)) {
|
|
|
|
|
|
|
|
// For OSObjects, we try to guess whether the object is created based
|
|
|
|
// on the return value.
|
|
|
|
if (K == ObjKind::OS) {
|
|
|
|
QualType QT = getCallableReturnType(FD);
|
|
|
|
|
|
|
|
bool HasRetainedOnZero = pd->hasAttr<OSReturnsRetainedOnZeroAttr>();
|
|
|
|
bool HasRetainedOnNonZero = pd->hasAttr<OSReturnsRetainedOnNonZeroAttr>();
|
|
|
|
|
|
|
|
// The usual convention is to create an object on non-zero return, but
|
|
|
|
// it's reverted if the typedef chain has a typedef kern_return_t,
|
|
|
|
// because kReturnSuccess constant is defined as zero.
|
|
|
|
// The convention can be overwritten by custom attributes.
|
|
|
|
bool SuccessOnZero =
|
|
|
|
HasRetainedOnZero ||
|
|
|
|
(hasTypedefNamed(QT, "kern_return_t") && !HasRetainedOnNonZero);
|
|
|
|
bool ShouldSplit = !QT.isNull() && !QT->isVoidType();
|
|
|
|
ArgEffectKind AK = RetainedOutParameter;
|
|
|
|
if (ShouldSplit && SuccessOnZero) {
|
|
|
|
AK = RetainedOutParameterOnZero;
|
|
|
|
} else if (ShouldSplit && (!SuccessOnZero || HasRetainedOnNonZero)) {
|
|
|
|
AK = RetainedOutParameterOnNonZero;
|
|
|
|
}
|
|
|
|
Template->addArg(AF, parm_idx, ArgEffect(AK, ObjKind::OS));
|
|
|
|
}
|
|
|
|
|
|
|
|
// For others:
|
|
|
|
// Do nothing. Retained out parameters will either point to a +1 reference
|
|
|
|
// or NULL, but the way you check for failure differs depending on the
|
|
|
|
// API. Consequently, we don't have a good way to track them yet.
|
2018-12-06 02:34:54 +08:00
|
|
|
return true;
|
2019-01-12 07:35:17 +08:00
|
|
|
} else if (auto K = hasAnyEnabledAttrOf<CFReturnsNotRetainedAttr,
|
|
|
|
OSReturnsNotRetainedAttr,
|
|
|
|
GeneralizedReturnsNotRetainedAttr>(
|
|
|
|
pd, QT)) {
|
2019-01-11 02:14:51 +08:00
|
|
|
Template->addArg(AF, parm_idx, ArgEffect(UnretainedOutParameter, *K));
|
2019-01-11 02:13:59 +08:00
|
|
|
return true;
|
2019-01-12 07:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
for (const auto *OD : MD->overridden_methods()) {
|
|
|
|
const ParmVarDecl *OP = OD->parameters()[parm_idx];
|
|
|
|
if (applyParamAnnotationEffect(OP, parm_idx, OD, Template))
|
|
|
|
return true;
|
2018-12-06 02:34:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
void
|
|
|
|
RetainSummaryManager::updateSummaryFromAnnotations(const RetainSummary *&Summ,
|
|
|
|
const FunctionDecl *FD) {
|
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(Summ && "Must have a summary to add annotations to.");
|
|
|
|
RetainSummaryTemplate Template(Summ, *this);
|
|
|
|
|
|
|
|
// Effects on the parameters.
|
|
|
|
unsigned parm_idx = 0;
|
2018-11-30 10:18:50 +08:00
|
|
|
for (auto pi = FD->param_begin(),
|
2019-01-11 02:15:30 +08:00
|
|
|
pe = FD->param_end(); pi != pe; ++pi, ++parm_idx)
|
|
|
|
applyParamAnnotationEffect(*pi, parm_idx, FD, Template);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
QualType RetTy = FD->getReturnType();
|
|
|
|
if (Optional<RetEffect> RetE = getRetEffectFromAnnotations(RetTy, FD))
|
|
|
|
Template->setRetEffect(*RetE);
|
2018-12-07 06:07:12 +08:00
|
|
|
|
2019-01-11 02:14:51 +08:00
|
|
|
if (hasAnyEnabledAttrOf<OSConsumesThisAttr>(FD, RetTy))
|
2019-01-11 02:13:59 +08:00
|
|
|
Template->setThisEffect(ArgEffect(DecRef, ObjKind::OS));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RetainSummaryManager::updateSummaryFromAnnotations(const RetainSummary *&Summ,
|
|
|
|
const ObjCMethodDecl *MD) {
|
|
|
|
if (!MD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(Summ && "Must have a valid summary to add annotations to");
|
|
|
|
RetainSummaryTemplate Template(Summ, *this);
|
|
|
|
|
|
|
|
// Effects on the receiver.
|
2019-01-11 02:14:51 +08:00
|
|
|
if (hasAnyEnabledAttrOf<NSConsumesSelfAttr>(MD, MD->getReturnType()))
|
2019-01-11 02:14:12 +08:00
|
|
|
Template->setReceiverEffect(ArgEffect(DecRef, ObjKind::ObjC));
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
// Effects on the parameters.
|
|
|
|
unsigned parm_idx = 0;
|
2019-01-11 02:14:51 +08:00
|
|
|
for (auto pi = MD->param_begin(), pe = MD->param_end(); pi != pe;
|
2019-01-11 02:15:30 +08:00
|
|
|
++pi, ++parm_idx)
|
|
|
|
applyParamAnnotationEffect(*pi, parm_idx, MD, Template);
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
QualType RetTy = MD->getReturnType();
|
|
|
|
if (Optional<RetEffect> RetE = getRetEffectFromAnnotations(RetTy, MD))
|
|
|
|
Template->setRetEffect(*RetE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getStandardMethodSummary(const ObjCMethodDecl *MD,
|
|
|
|
Selector S, QualType RetTy) {
|
|
|
|
// Any special effects?
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect ReceiverEff = ArgEffect(DoNothing, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
RetEffect ResultEff = RetEffect::MakeNoRet();
|
|
|
|
|
|
|
|
// Check the method family, and apply any default annotations.
|
|
|
|
switch (MD ? MD->getMethodFamily() : S.getMethodFamily()) {
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_initialize:
|
|
|
|
case OMF_performSelector:
|
|
|
|
// Assume all Objective-C methods follow Cocoa Memory Management rules.
|
|
|
|
// FIXME: Does the non-threaded performSelector family really belong here?
|
|
|
|
// The selector could be, say, @selector(copy).
|
|
|
|
if (cocoa::isCocoaObjectRef(RetTy))
|
2019-01-11 02:13:46 +08:00
|
|
|
ResultEff = RetEffect::MakeNotOwned(ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
else if (coreFoundation::isCFObjectRef(RetTy)) {
|
|
|
|
// ObjCMethodDecl currently doesn't consider CF objects as valid return
|
|
|
|
// values for alloc, new, copy, or mutableCopy, so we have to
|
|
|
|
// double-check with the selector. This is ugly, but there aren't that
|
|
|
|
// many Objective-C methods that return CF objects, right?
|
|
|
|
if (MD) {
|
|
|
|
switch (S.getMethodFamily()) {
|
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_new:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_mutableCopy:
|
2019-01-11 02:13:46 +08:00
|
|
|
ResultEff = RetEffect::MakeOwned(ObjKind::CF);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
default:
|
2019-01-11 02:13:46 +08:00
|
|
|
ResultEff = RetEffect::MakeNotOwned(ObjKind::CF);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-11 02:13:46 +08:00
|
|
|
ResultEff = RetEffect::MakeNotOwned(ObjKind::CF);
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OMF_init:
|
|
|
|
ResultEff = ObjCInitRetE;
|
2019-01-11 02:14:12 +08:00
|
|
|
ReceiverEff = ArgEffect(DecRef, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_new:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_mutableCopy:
|
|
|
|
if (cocoa::isCocoaObjectRef(RetTy))
|
|
|
|
ResultEff = ObjCAllocRetE;
|
|
|
|
else if (coreFoundation::isCFObjectRef(RetTy))
|
2019-01-11 02:13:46 +08:00
|
|
|
ResultEff = RetEffect::MakeOwned(ObjKind::CF);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
case OMF_autorelease:
|
2019-01-11 02:13:59 +08:00
|
|
|
ReceiverEff = ArgEffect(Autorelease, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
case OMF_retain:
|
2019-01-11 02:14:12 +08:00
|
|
|
ReceiverEff = ArgEffect(IncRef, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
case OMF_release:
|
2019-01-11 02:14:12 +08:00
|
|
|
ReceiverEff = ArgEffect(DecRef, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
case OMF_dealloc:
|
2019-01-11 02:13:59 +08:00
|
|
|
ReceiverEff = ArgEffect(Dealloc, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
break;
|
|
|
|
case OMF_self:
|
|
|
|
// -self is handled specially by the ExprEngine to propagate the receiver.
|
|
|
|
break;
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_finalize:
|
|
|
|
// These methods don't return objects.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If one of the arguments in the selector has the keyword 'delegate' we
|
|
|
|
// should stop tracking the reference count for the receiver. This is
|
|
|
|
// because the reference count is quite possibly handled by a delegate
|
|
|
|
// method.
|
|
|
|
if (S.isKeywordSelector()) {
|
|
|
|
for (unsigned i = 0, e = S.getNumArgs(); i != e; ++i) {
|
|
|
|
StringRef Slot = S.getNameForSlot(i);
|
|
|
|
if (Slot.substr(Slot.size() - 8).equals_lower("delegate")) {
|
|
|
|
if (ResultEff == ObjCInitRetE)
|
|
|
|
ResultEff = RetEffect::MakeNoRetHard();
|
|
|
|
else
|
2019-01-11 02:13:59 +08:00
|
|
|
ReceiverEff = ArgEffect(StopTrackingHard, ObjKind::ObjC);
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 02:13:59 +08:00
|
|
|
if (ReceiverEff.getKind() == DoNothing &&
|
2018-08-18 05:41:07 +08:00
|
|
|
ResultEff.getKind() == RetEffect::NoRet)
|
|
|
|
return getDefaultSummary();
|
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
return getPersistentSummary(ResultEff, ArgEffects(AF.getEmptyMap()),
|
2019-01-11 02:13:59 +08:00
|
|
|
ArgEffect(ReceiverEff), ArgEffect(MayEscape));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
2019-01-25 09:24:04 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getClassMethodSummary(const ObjCMessageExpr *ME) {
|
|
|
|
assert(!ME->isInstanceMessage());
|
|
|
|
const ObjCInterfaceDecl *Class = ME->getReceiverInterface();
|
|
|
|
|
|
|
|
return getMethodSummary(ME->getSelector(), Class, ME->getMethodDecl(),
|
|
|
|
ME->getType(), ObjCClassMethodSummaries);
|
|
|
|
}
|
|
|
|
|
2018-08-21 11:09:02 +08:00
|
|
|
const RetainSummary *RetainSummaryManager::getInstanceMethodSummary(
|
2019-01-25 09:24:04 +08:00
|
|
|
const ObjCMessageExpr *ME,
|
2018-12-25 02:45:18 +08:00
|
|
|
QualType ReceiverType) {
|
2018-08-18 05:41:07 +08:00
|
|
|
const ObjCInterfaceDecl *ReceiverClass = nullptr;
|
|
|
|
|
|
|
|
// We do better tracking of the type of the object than the core ExprEngine.
|
|
|
|
// See if we have its type in our private state.
|
2018-08-21 11:09:02 +08:00
|
|
|
if (!ReceiverType.isNull())
|
|
|
|
if (const auto *PT = ReceiverType->getAs<ObjCObjectPointerType>())
|
|
|
|
ReceiverClass = PT->getInterfaceDecl();
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
// If we don't know what kind of object this is, fall back to its static type.
|
|
|
|
if (!ReceiverClass)
|
2019-01-25 09:24:04 +08:00
|
|
|
ReceiverClass = ME->getReceiverInterface();
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
// FIXME: The receiver could be a reference to a class, meaning that
|
|
|
|
// we should use the class method.
|
|
|
|
// id x = [NSObject class];
|
|
|
|
// [x performSelector:... withObject:... afterDelay:...];
|
2019-01-25 09:24:04 +08:00
|
|
|
Selector S = ME->getSelector();
|
|
|
|
const ObjCMethodDecl *Method = ME->getMethodDecl();
|
2018-08-18 05:41:07 +08:00
|
|
|
if (!Method && ReceiverClass)
|
|
|
|
Method = ReceiverClass->getInstanceMethod(S);
|
|
|
|
|
2019-01-25 09:24:04 +08:00
|
|
|
return getMethodSummary(S, ReceiverClass, Method, ME->getType(),
|
2018-08-18 05:41:07 +08:00
|
|
|
ObjCMethodSummaries);
|
|
|
|
}
|
|
|
|
|
|
|
|
const RetainSummary *
|
2018-12-25 02:45:18 +08:00
|
|
|
RetainSummaryManager::getMethodSummary(Selector S,
|
|
|
|
const ObjCInterfaceDecl *ID,
|
2018-08-18 05:41:07 +08:00
|
|
|
const ObjCMethodDecl *MD, QualType RetTy,
|
|
|
|
ObjCMethodSummariesTy &CachedSummaries) {
|
|
|
|
|
2018-12-08 04:21:51 +08:00
|
|
|
// Objective-C method summaries are only applicable to ObjC and CF objects.
|
|
|
|
if (!TrackObjCAndCFObjects)
|
|
|
|
return getDefaultSummary();
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
// Look up a summary in our summary cache.
|
|
|
|
const RetainSummary *Summ = CachedSummaries.find(ID, S);
|
|
|
|
|
|
|
|
if (!Summ) {
|
|
|
|
Summ = getStandardMethodSummary(MD, S, RetTy);
|
|
|
|
|
|
|
|
// Annotations override defaults.
|
|
|
|
updateSummaryFromAnnotations(Summ, MD);
|
|
|
|
|
|
|
|
// Memoize the summary.
|
|
|
|
CachedSummaries[ObjCSummaryKey(ID, S)] = Summ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Summ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RetainSummaryManager::InitializeClassMethodSummaries() {
|
2018-12-25 02:45:18 +08:00
|
|
|
ArgEffects ScratchArgs = AF.getEmptyMap();
|
|
|
|
|
2018-08-18 05:41:07 +08:00
|
|
|
// Create the [NSAssertionHandler currentHander] summary.
|
|
|
|
addClassMethSummary("NSAssertionHandler", "currentHandler",
|
2019-01-11 02:13:46 +08:00
|
|
|
getPersistentSummary(RetEffect::MakeNotOwned(ObjKind::ObjC),
|
2018-12-25 02:45:18 +08:00
|
|
|
ScratchArgs));
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
// Create the [NSAutoreleasePool addObject:] summary.
|
2019-01-11 02:13:59 +08:00
|
|
|
ScratchArgs = AF.add(ScratchArgs, 0, ArgEffect(Autorelease));
|
2018-08-18 05:41:07 +08:00
|
|
|
addClassMethSummary("NSAutoreleasePool", "addObject",
|
2019-01-11 02:13:59 +08:00
|
|
|
getPersistentSummary(RetEffect::MakeNoRet(), ScratchArgs,
|
|
|
|
ArgEffect(DoNothing),
|
|
|
|
ArgEffect(Autorelease)));
|
2018-08-18 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RetainSummaryManager::InitializeMethodSummaries() {
|
|
|
|
|
2018-12-25 02:45:18 +08:00
|
|
|
ArgEffects ScratchArgs = AF.getEmptyMap();
|
2018-08-18 05:41:07 +08:00
|
|
|
// Create the "init" selector. It just acts as a pass-through for the
|
|
|
|
// receiver.
|
2019-01-11 02:14:12 +08:00
|
|
|
const RetainSummary *InitSumm = getPersistentSummary(
|
|
|
|
ObjCInitRetE, ScratchArgs, ArgEffect(DecRef, ObjKind::ObjC));
|
2018-08-18 05:41:07 +08:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
|
|
|
|
|
|
|
|
// awakeAfterUsingCoder: behaves basically like an 'init' method. It
|
|
|
|
// claims the receiver and returns a retained object.
|
|
|
|
addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
|
|
|
|
InitSumm);
|
|
|
|
|
|
|
|
// The next methods are allocators.
|
2018-12-25 02:45:18 +08:00
|
|
|
const RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE,
|
|
|
|
ScratchArgs);
|
2018-08-18 05:41:07 +08:00
|
|
|
const RetainSummary *CFAllocSumm =
|
2019-01-11 02:13:46 +08:00
|
|
|
getPersistentSummary(RetEffect::MakeOwned(ObjKind::CF), ScratchArgs);
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
// Create the "retain" selector.
|
|
|
|
RetEffect NoRet = RetEffect::MakeNoRet();
|
2019-01-11 02:14:12 +08:00
|
|
|
const RetainSummary *Summ = getPersistentSummary(
|
|
|
|
NoRet, ScratchArgs, ArgEffect(IncRef, ObjKind::ObjC));
|
2018-08-18 05:41:07 +08:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
|
|
|
|
|
|
|
|
// Create the "release" selector.
|
2019-01-11 02:14:12 +08:00
|
|
|
Summ = getPersistentSummary(NoRet, ScratchArgs,
|
|
|
|
ArgEffect(DecRef, ObjKind::ObjC));
|
2018-08-18 05:41:07 +08:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
|
|
|
|
|
|
|
|
// Create the -dealloc summary.
|
2019-01-11 02:14:12 +08:00
|
|
|
Summ = getPersistentSummary(NoRet, ScratchArgs, ArgEffect(Dealloc,
|
|
|
|
ObjKind::ObjC));
|
2018-08-18 05:41:07 +08:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
|
|
|
|
|
|
|
|
// Create the "autorelease" selector.
|
2019-01-11 02:14:12 +08:00
|
|
|
Summ = getPersistentSummary(NoRet, ScratchArgs, ArgEffect(Autorelease,
|
|
|
|
ObjKind::ObjC));
|
2018-08-18 05:41:07 +08:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
|
|
|
|
|
|
|
|
// For NSWindow, allocated objects are (initially) self-owned.
|
|
|
|
// FIXME: For now we opt for false negatives with NSWindow, as these objects
|
|
|
|
// self-own themselves. However, they only do this once they are displayed.
|
|
|
|
// Thus, we need to track an NSWindow's display status.
|
|
|
|
// This is tracked in <rdar://problem/6062711>.
|
|
|
|
// See also http://llvm.org/bugs/show_bug.cgi?id=3714.
|
2019-01-11 02:13:59 +08:00
|
|
|
const RetainSummary *NoTrackYet =
|
|
|
|
getPersistentSummary(RetEffect::MakeNoRet(), ScratchArgs,
|
|
|
|
ArgEffect(StopTracking), ArgEffect(StopTracking));
|
2018-08-18 05:41:07 +08:00
|
|
|
|
|
|
|
addClassMethSummary("NSWindow", "alloc", NoTrackYet);
|
|
|
|
|
|
|
|
// For NSPanel (which subclasses NSWindow), allocated objects are not
|
|
|
|
// self-owned.
|
|
|
|
// FIXME: For now we don't track NSPanels. object for the same reason
|
|
|
|
// as for NSWindow objects.
|
|
|
|
addClassMethSummary("NSPanel", "alloc", NoTrackYet);
|
|
|
|
|
|
|
|
// For NSNull, objects returned by +null are singletons that ignore
|
|
|
|
// retain/release semantics. Just don't track them.
|
|
|
|
// <rdar://problem/12858915>
|
|
|
|
addClassMethSummary("NSNull", "null", NoTrackYet);
|
|
|
|
|
|
|
|
// Don't track allocated autorelease pools, as it is okay to prematurely
|
|
|
|
// exit a method.
|
|
|
|
addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
|
|
|
|
addClassMethSummary("NSAutoreleasePool", "allocWithZone", NoTrackYet, false);
|
|
|
|
addClassMethSummary("NSAutoreleasePool", "new", NoTrackYet);
|
|
|
|
|
|
|
|
// Create summaries QCRenderer/QCView -createSnapShotImageOfType:
|
|
|
|
addInstMethSummary("QCRenderer", AllocSumm, "createSnapshotImageOfType");
|
|
|
|
addInstMethSummary("QCView", AllocSumm, "createSnapshotImageOfType");
|
|
|
|
|
|
|
|
// Create summaries for CIContext, 'createCGImage' and
|
|
|
|
// 'createCGLayerWithSize'. These objects are CF objects, and are not
|
|
|
|
// automatically garbage collected.
|
|
|
|
addInstMethSummary("CIContext", CFAllocSumm, "createCGImage", "fromRect");
|
|
|
|
addInstMethSummary("CIContext", CFAllocSumm, "createCGImage", "fromRect",
|
|
|
|
"format", "colorSpace");
|
|
|
|
addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize", "info");
|
|
|
|
}
|
2018-08-21 11:09:02 +08:00
|
|
|
|
2019-01-25 09:24:04 +08:00
|
|
|
const RetainSummary *
|
|
|
|
RetainSummaryManager::getMethodSummary(const ObjCMethodDecl *MD) {
|
|
|
|
const ObjCInterfaceDecl *ID = MD->getClassInterface();
|
|
|
|
Selector S = MD->getSelector();
|
|
|
|
QualType ResultTy = MD->getReturnType();
|
|
|
|
|
|
|
|
ObjCMethodSummariesTy *CachedSummaries;
|
|
|
|
if (MD->isInstanceMethod())
|
|
|
|
CachedSummaries = &ObjCMethodSummaries;
|
|
|
|
else
|
|
|
|
CachedSummaries = &ObjCClassMethodSummaries;
|
|
|
|
|
|
|
|
return getMethodSummary(S, ID, MD, ResultTy, *CachedSummaries);
|
|
|
|
}
|