Author: ahatanak Date: Mon Oct 24 16:45:54 2016 New Revision: 285031 URL: http://llvm.org/viewvc/llvm-project?rev=285031&view=rev Log: [Sema][ObjC] Warn about implicitly autoreleasing out-parameters captured by blocks.
Add a new warning "-Wblock-capture-autoreleasing". The warning warns about implicitly autoreleasing out-parameters captured by blocks which can introduce use-after-free bugs that are hard to debug. rdar://problem/15377548 Differential Revision: https://reviews.llvm.org/D25844 Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td cfe/trunk/lib/Sema/SemaExpr.cpp cfe/trunk/test/SemaObjC/arc.m Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=285031&r1=285030&r2=285031&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Mon Oct 24 16:45:54 2016 @@ -498,6 +498,7 @@ def AutomaticReferenceCounting : DiagGro def ARCRepeatedUseOfWeakMaybe : DiagGroup<"arc-maybe-repeated-use-of-weak">; def ARCRepeatedUseOfWeak : DiagGroup<"arc-repeated-use-of-weak", [ARCRepeatedUseOfWeakMaybe]>; +def BlockCaptureAutoReleasing : DiagGroup<"block-capture-autoreleasing">; def ObjCBridge : DiagGroup<"bridge-cast">; def DeallocInCategory:DiagGroup<"dealloc-in-category">; Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=285031&r1=285030&r2=285031&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Oct 24 16:45:54 2016 @@ -5077,6 +5077,16 @@ def err_arc_inconsistent_property_owners } // end "ARC and @properties" category +def warn_block_capture_autoreleasing : Warning< + "block captures an autoreleasing out-parameter, which may result in " + "use-after-free bugs">, + InGroup<BlockCaptureAutoReleasing>, DefaultIgnore; +def note_declare_parameter_autoreleasing : Note< + "declare the parameter __autoreleasing explicitly to suppress this warning">; +def note_declare_parameter_strong : Note< + "declare the parameter __strong or capture a __block __strong variable to " + "keep values alive across autorelease pools">; + def err_arc_atomic_ownership : Error< "cannot perform atomic operation on a pointer to type %0: type has " "non-trivial ownership">; Modified: cfe/trunk/lib/Sema/SemaExpr.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=285031&r1=285030&r2=285031&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaExpr.cpp (original) +++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Oct 24 16:45:54 2016 @@ -13502,6 +13502,23 @@ static bool captureInBlock(BlockScopeInf } return false; } + + // Warn about implicitly autoreleasing indirect parameters captured by blocks. + if (auto *PT = dyn_cast<PointerType>(CaptureType)) { + QualType PointeeTy = PT->getPointeeType(); + if (isa<ObjCObjectPointerType>(PointeeTy.getCanonicalType()) && + PointeeTy.getObjCLifetime() == Qualifiers::OCL_Autoreleasing && + !isa<AttributedType>(PointeeTy)) { + if (BuildAndDiagnose) { + SourceLocation VarLoc = Var->getLocation(); + S.Diag(Loc, diag::warn_block_capture_autoreleasing); + S.Diag(VarLoc, diag::note_declare_parameter_autoreleasing) << + FixItHint::CreateInsertion(VarLoc, "__autoreleasing"); + S.Diag(VarLoc, diag::note_declare_parameter_strong); + } + } + } + const bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); if (HasBlocksAttr || CaptureType->isReferenceType() || (S.getLangOpts().OpenMP && S.IsOpenMPCapturedDecl(Var))) { Modified: cfe/trunk/test/SemaObjC/arc.m URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc.m?rev=285031&r1=285030&r2=285031&view=diff ============================================================================== --- cfe/trunk/test/SemaObjC/arc.m (original) +++ cfe/trunk/test/SemaObjC/arc.m Mon Oct 24 16:45:54 2016 @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify -Wno-objc-root-class %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -fblocks -verify -Wno-objc-root-class -Wblock-capture-autoreleasing %s typedef unsigned long NSUInteger; typedef const void * CFTypeRef; @@ -808,3 +808,10 @@ int garf() { TKAssertEqual(object, nil); TKAssertEqual(object, (id)nil); } + +void block_capture_autoreleasing(A * __autoreleasing *a, A **b) { // expected-note {{declare the parameter __autoreleasing explicitly to suppress this warning}} expected-note {{declare the parameter __strong or capture a __block __strong variable to keep values alive across autorelease pools}} + ^{ + (void)*a; + (void)*b; // expected-warning {{block captures an autoreleasing out-parameter, which may result in use-after-free bugs}} + }(); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits