Skip to content

Commit ea0cd51

Browse files
committed
[CodeGen][ObjC] Fix a memory leak that occurs when a non-trivial C
struct property is set using dot notation Make sure the destructor is called if needed. Differential Revision: https://reviews.llvm.org/D136639
1 parent 9e06d18 commit ea0cd51

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

clang/lib/CodeGen/CGExprAgg.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,16 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
201201
return EmitFinalDestCopy(E->getType(), LV);
202202
}
203203

204-
CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
204+
AggValueSlot Slot = EnsureSlot(E->getType());
205+
bool NeedsDestruction =
206+
!Slot.isExternallyDestructed() &&
207+
E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct;
208+
if (NeedsDestruction)
209+
Slot.setExternallyDestructed();
210+
CGF.EmitPseudoObjectRValue(E, Slot);
211+
if (NeedsDestruction)
212+
CGF.pushDestroy(QualType::DK_nontrivial_c_struct, Slot.getAddress(),
213+
E->getType());
205214
}
206215

207216
void VisitVAArgExpr(VAArgExpr *E);

clang/test/CodeGenObjC/nontrivial-c-struct-property.m

+36
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,39 @@ -(void)setP1:(S0)s0 {
6868
// CHECK: call void @objc_copyCppObjectAtomic({{.*}}, {{.*}}, ptr noundef @__move_assignment_8_8_s0)
6969
// CHECK-NOT: call
7070
// CHECK: ret void
71+
72+
// CHECK: define void @test0(ptr noundef %[[C:.*]], ptr noundef %[[A:.*]])
73+
// CHECK: %[[C_ADDR:.*]] = alloca ptr, align 8
74+
// CHECK: %[[A_ADDR:.*]] = alloca ptr, align 8
75+
// CHECK: %[[AGG_TMP_ENSURED:.*]] = alloca %[[STRUCT_S0]], align 8
76+
// CHECK: %[[AGG_TMP:.*]] = alloca %[[STRUCT_S0]], align 8
77+
// CHECK: store ptr null, ptr %[[C_ADDR]], align 8
78+
// CHECK: call void @llvm.objc.storeStrong(ptr %[[C_ADDR]], ptr %[[C]])
79+
// CHECK: store ptr %[[A]], ptr %[[A_ADDR]], align 8
80+
// CHECK: %[[V0:.*]] = load ptr, ptr %[[A_ADDR]], align 8
81+
// CHECK: call void @__copy_constructor_8_8_s0(ptr %[[AGG_TMP_ENSURED]], ptr %[[V0]])
82+
// CHECK: %[[V1:.*]] = load ptr, ptr %[[C_ADDR]], align 8
83+
// CHECK: call void @__copy_constructor_8_8_s0(ptr %[[AGG_TMP]], ptr %[[AGG_TMP_ENSURED]])
84+
// CHECK: %[[V2:.*]] = icmp eq ptr %[[V1]], null
85+
// CHECK: br i1 %[[V2]], label %[[MSGSEND_NULL:.*]], label %[[MSGSEND_CALL:.*]]
86+
87+
// CHECK: [[MSGSEND_CALL]]:
88+
// CHECK: %[[V3:.*]] = load ptr, ptr @OBJC_SELECTOR_REFERENCES_, align 8
89+
// CHECK: %[[COERCE_DIVE:.*]] = getelementptr inbounds %[[STRUCT_S0]], ptr %[[AGG_TMP]], i32 0, i32 0
90+
// CHECK: %[[V4:.*]] = load ptr, ptr %[[COERCE_DIVE]], align 8
91+
// CHECK: %[[COERCE_VAL_PI:.*]] = ptrtoint ptr %[[V4]] to i64
92+
// CHECK: call void @objc_msgSend(ptr noundef %[[V1]], ptr noundef %[[V3]], i64 %[[COERCE_VAL_PI]])
93+
// CHECK: br label %[[MSGSEND_CONT:.*]]
94+
95+
// CHECK: [[MSGSEND_NULL]]:
96+
// CHECK: call void @__destructor_8_s0(ptr %[[AGG_TMP]])
97+
// CHECK: br label %[[MSGSEND_CONT]]
98+
99+
// CHECK: [[MSGSEND_CONT]]:
100+
// CHECK: call void @__destructor_8_s0(ptr %[[AGG_TMP_ENSURED]]
101+
// CHECK: call void @llvm.objc.storeStrong(ptr %[[C_ADDR]], ptr null)
102+
// CHECK: ret void
103+
104+
void test0(C *c, S0 *a) {
105+
c.atomic0 = *a;
106+
}

0 commit comments

Comments
 (0)