diff options
| author | Akira Hatanaka <ahatanaka@apple.com> | 2018-04-27 04:21:51 +0000 |
|---|---|---|
| committer | Akira Hatanaka <ahatanaka@apple.com> | 2018-04-27 04:21:51 +0000 |
| commit | e712374496147f61ff701ee46bdd9932366521c6 (patch) | |
| tree | ad3b905195500be4f8400e38a45b6977533793aa /clang/lib/CodeGen | |
| parent | fa7fd13cf8b33f879c2f3b80dc7e91ac0cace0bd (diff) | |
| download | bcm5719-llvm-e712374496147f61ff701ee46bdd9932366521c6.tar.gz bcm5719-llvm-e712374496147f61ff701ee46bdd9932366521c6.zip | |
[CodeGen] Avoid destructing a callee-destructued struct type in a
function if a function delegates to another function.
Fix a bug introduced in r328731, which caused a struct with ObjC __weak
fields that was passed to a function to be destructed twice, once in the
callee function and once in another function the callee function
delegates to. To prevent this, keep track of the callee-destructed
structs passed to a function and disable their cleanups at the point of
the call to the delegated function.
rdar://problem/39194693
Differential Revision: https://reviews.llvm.org/D45382
llvm-svn: 331016
Diffstat (limited to 'clang/lib/CodeGen')
| -rw-r--r-- | clang/lib/CodeGen/CGCall.cpp | 16 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGCleanup.cpp | 6 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGDecl.cpp | 2 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.h | 14 |
4 files changed, 35 insertions, 3 deletions
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 392c7b1ded5..d4374ff6e09 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -3063,6 +3063,22 @@ void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, } else { args.add(convertTempToRValue(local, type, loc), type); } + + // Deactivate the cleanup for the callee-destructed param that was pushed. + if (hasAggregateEvaluationKind(type) && + getContext().isParamDestroyedInCallee(type)) { + EHScopeStack::stable_iterator cleanup = + CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param)); + if (cleanup.isValid()) { + // This unreachable is a temporary marker which will be removed later. + llvm::Instruction *isActive = Builder.CreateUnreachable(); + args.addArgCleanupDeactivation(cleanup, isActive); + } else + // A param cleanup should have been pushed unless we are code-generating + // a thunk. + assert(CurFuncIsThunk && + "cleanup for callee-destructed param not recorded"); + } } static bool isProvablyNull(llvm::Value *addr) { diff --git a/clang/lib/CodeGen/CGCleanup.cpp b/clang/lib/CodeGen/CGCleanup.cpp index 526def2cc29..c5f935bdef5 100644 --- a/clang/lib/CodeGen/CGCleanup.cpp +++ b/clang/lib/CodeGen/CGCleanup.cpp @@ -1233,8 +1233,10 @@ void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C, EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C)); assert(Scope.isActive() && "double deactivation"); - // If it's the top of the stack, just pop it. - if (C == EHStack.stable_begin()) { + // If it's the top of the stack, just pop it, but do so only if it belongs + // to the current RunCleanupsScope. + if (C == EHStack.stable_begin() && + CurrentCleanupScopeDepth.strictlyEncloses(C)) { // If it's a normal cleanup, we need to pretend that the // fallthrough is unreachable. CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index c9b80e38d4b..75251df55bd 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -1962,6 +1962,8 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg, DtorKind == QualType::DK_nontrivial_c_struct) && "unexpected destructor type"); pushDestroy(DtorKind, DeclPtr, Ty); + CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] = + EHStack.stable_begin(); } } } else { diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 091fdc7ab7a..7889d089074 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -587,7 +587,7 @@ public: /// \brief Enters a new scope for capturing cleanups, all of which /// will be executed once the scope is exited. class RunCleanupsScope { - EHScopeStack::stable_iterator CleanupStackDepth; + EHScopeStack::stable_iterator CleanupStackDepth, OldCleanupScopeDepth; size_t LifetimeExtendedCleanupStackSize; bool OldDidCallStackSave; protected: @@ -610,6 +610,8 @@ public: CGF.LifetimeExtendedCleanupStack.size(); OldDidCallStackSave = CGF.DidCallStackSave; CGF.DidCallStackSave = false; + OldCleanupScopeDepth = CGF.CurrentCleanupScopeDepth; + CGF.CurrentCleanupScopeDepth = CleanupStackDepth; } /// \brief Exit this cleanup scope, emitting any accumulated cleanups. @@ -635,9 +637,14 @@ public: CGF.PopCleanupBlocks(CleanupStackDepth, LifetimeExtendedCleanupStackSize, ValuesToReload); PerformCleanup = false; + CGF.CurrentCleanupScopeDepth = OldCleanupScopeDepth; } }; + // Cleanup stack depth of the RunCleanupsScope that was pushed most recently. + EHScopeStack::stable_iterator CurrentCleanupScopeDepth = + EHScopeStack::stable_end(); + class LexicalScope : public RunCleanupsScope { SourceRange Range; SmallVector<const LabelDecl*, 4> Labels; @@ -1095,6 +1102,11 @@ private: /// decls. DeclMapTy LocalDeclMap; + // Keep track of the cleanups for callee-destructed parameters pushed to the + // cleanup stack so that they can be deactivated later. + llvm::DenseMap<const ParmVarDecl *, EHScopeStack::stable_iterator> + CalleeDestructedParamCleanups; + /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this /// will contain a mapping from said ParmVarDecl to its implicit "object_size" /// parameter. |

