Skip to content

Commit f2750c8

Browse files
author
Erich Keane
committed
[MS-ABI]V-base dtor called more than needed when throw happens in v-base ctor in window. Need add "complete object flag" check in eh cleanup code.
The problem only happen on window ( A MS-ABI issuer ) The nature of the problem is virtual base dtor called more than it is needed after exception throw in inheriting base class(with virtual bases) ctor. The root problem is when throw happen, not all virtual base classes have been contructed, so not all virtual base dtors are need to call for ehcleanup. clang has code to handle vbase initialization: basically add check for "complete object flag" before call to v-base ctor. But that part is missing for cleanup code. To fix this add similar code as v-base init to cleanup code, same algorithm. 1> Add new routine: EmitDtorCompleteObjectHandler With corresponding to EmitCtorCompleteObjectHandler 2> In the EmitDestructorCal Call EmitDtorCompleteObjectHandler when generate ehcleanup inside ctor. Just add check for "complete object flag" before call to v-base dtor. Without my change: ehcleanup: ; preds = %ctor.skip_vbases %13 = cleanuppad within none [], !dbg !66 %14 = bitcast %struct.class_0* %this1 to i8*, !dbg !66 %15 = getelementptr inbounds i8, i8* %14, i64 8, !dbg !66 %16 = bitcast i8* %15 to %struct.class_2*, !dbg !66 call void @"\01??1class_2@@UEAA@XZ"(%struct.class_2* %16) llvm-mirror#6 [ "funclet"(token %13) ], !dbg !66 cleanupret from %13 unwind to caller, !dbg !66 with my change: ehcleanup: ; preds = %ctor.skip_vbases %13 = cleanuppad within none [], !dbg !66 %14 = bitcast %struct.class_0* %this1 to i8*, !dbg !66 %15 = getelementptr inbounds i8, i8* %14, i64 8, !dbg !66 %16 = bitcast i8* %15 to %struct.class_2*, !dbg !66 %is_complete_object4 = icmp ne i32 %is_most_derived2, 0, !dbg !66 br i1 %is_complete_object4, label %Dtor.dtor_vbase, label %Dtor.skip_vbase, !d bg !66 Dtor.dtor_vbase: ; preds = %ehcleanup call void @"\01??1class_2@@UEAA@XZ"(%struct.class_2* %16) llvm-mirror#6 [ "funclet"(token %13) ], !dbg !66 br label %Dtor.skip_vbase, !dbg !66 Dtor.skip_vbase: ; preds = %Dtor.dtor_vbase, %ehcleanup cleanupret from %13 unwind to caller, !dbg !66 Please let me know you need more info. Patch by Jennifer Yu. Differential Revision: https://reviews.llvm.org/D27358 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@288869 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d3f6540 commit f2750c8

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/CodeGen/MicrosoftCXXABI.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ class MicrosoftCXXABI : public CGCXXABI {
165165
llvm::BasicBlock *
166166
EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
167167
const CXXRecordDecl *RD) override;
168+
169+
llvm::BasicBlock *
170+
EmitDtorCompleteObjectHandler(CodeGenFunction &CGF);
168171

169172
void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
170173
const CXXRecordDecl *RD) override;
@@ -1135,6 +1138,25 @@ MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
11351138
return SkipVbaseCtorsBB;
11361139
}
11371140

1141+
llvm::BasicBlock *
1142+
MicrosoftCXXABI::EmitDtorCompleteObjectHandler(CodeGenFunction &CGF) {
1143+
llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
1144+
assert(IsMostDerivedClass &&
1145+
"ctor for a class with virtual bases must have an implicit parameter");
1146+
llvm::Value *IsCompleteObject =
1147+
CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object");
1148+
1149+
llvm::BasicBlock *CallVbaseDtorsBB = CGF.createBasicBlock("Dtor.dtor_vbases");
1150+
llvm::BasicBlock *SkipVbaseDtorsBB = CGF.createBasicBlock("Dtor.skip_vbases");
1151+
CGF.Builder.CreateCondBr(IsCompleteObject,
1152+
CallVbaseDtorsBB, SkipVbaseDtorsBB);
1153+
1154+
CGF.EmitBlock(CallVbaseDtorsBB);
1155+
// CGF will put the base dtor calls in this basic block for us later.
1156+
1157+
return SkipVbaseDtorsBB;
1158+
}
1159+
11381160
void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers(
11391161
CodeGenFunction &CGF, const CXXRecordDecl *RD) {
11401162
// In most cases, an override for a vbase virtual method can adjust
@@ -1512,11 +1534,21 @@ void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
15121534
This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type),
15131535
This, false);
15141536
}
1537+
1538+
llvm::BasicBlock *BaseDtorEndBB = nullptr;
1539+
if (ForVirtualBase && isa<CXXConstructorDecl>(CGF.CurCodeDecl)) {
1540+
BaseDtorEndBB = EmitDtorCompleteObjectHandler(CGF);
1541+
}
15151542

15161543
CGF.EmitCXXDestructorCall(DD, Callee, This.getPointer(),
15171544
/*ImplicitParam=*/nullptr,
15181545
/*ImplicitParamTy=*/QualType(), nullptr,
15191546
getFromDtorType(Type));
1547+
if (BaseDtorEndBB) {
1548+
// Complete object handler should continue to be the remaining
1549+
CGF.Builder.CreateBr(BaseDtorEndBB);
1550+
CGF.EmitBlock(BaseDtorEndBB);
1551+
}
15201552
}
15211553

15221554
void MicrosoftCXXABI::emitVTableTypeMetadata(const VPtrInfo &Info,

test/CodeGenCXX/microsoft-abi-eh-cleanups.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,38 @@ void f() {
278278
// WIN32-LIFETIME: %[[bc2:.*]] = bitcast %"struct.lifetime_marker::C"* %[[c]] to i8*
279279
// WIN32-LIFETIME: call void @llvm.lifetime.end(i64 1, i8* %[[bc2]])
280280
}
281+
282+
struct class_2 {
283+
class_2();
284+
virtual ~class_2();
285+
};
286+
struct class_1 : virtual class_2 {
287+
class_1(){throw "Unhandled exception";}
288+
virtual ~class_1() {}
289+
};
290+
struct class_0 : class_1 {
291+
class_0() ;
292+
virtual ~class_0() {}
293+
};
294+
295+
class_0::class_0() {
296+
// WIN32: define x86_thiscallcc %struct.class_0* @"\01??0class_0@@QAE@XZ"(%struct.class_0* returned %this, i32 %is_most_derived)
297+
// WIN32: store i32 %is_most_derived, i32* %[[IS_MOST_DERIVED_VAR:.*]], align 4
298+
// WIN32: %[[IS_MOST_DERIVED_VAL:.*]] = load i32, i32* %[[IS_MOST_DERIVED_VAR]]
299+
// WIN32: %[[SHOULD_CALL_VBASE_CTORS:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
300+
// WIN32: br i1 %[[SHOULD_CALL_VBASE_CTORS]], label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
301+
// WIN32: [[INIT_VBASES]]
302+
// WIN32: br label %[[SKIP_VBASES]]
303+
// WIN32: [[SKIP_VBASES]]
304+
// ehcleanup:
305+
// WIN32: %[[CLEANUPPAD:.*]] = cleanuppad within none []
306+
// WIN32-NEXT: bitcast %{{.*}}* %{{.*}} to i8*
307+
// WIN32-NEXT: getelementptr inbounds i8, i8* %{{.*}}, i{{.*}} {{.}}
308+
// WIN32-NEXT: bitcast i8* %{{.*}} to %{{.*}}*
309+
// WIN32-NEXT: %[[SHOULD_CALL_VBASE_DTOR:.*]] = icmp ne i32 %[[IS_MOST_DERIVED_VAL]], 0
310+
// WIN32-NEXT: br i1 %[[SHOULD_CALL_VBASE_DTOR]], label %[[DTOR_VBASE:.*]], label %[[SKIP_VBASE:.*]]
311+
// WIN32: [[DTOR_VBASE]]
312+
// WIN32-NEXT: call x86_thiscallcc void @"\01??1class_2@@UAE@XZ"
313+
// WIN32: br label %[[SKIP_VBASE]]
314+
// WIN32: [[SKIP_VBASE]]
315+
}

0 commit comments

Comments
 (0)