Skip to content

Commit cf97c2d

Browse files
committed
SILCombine: don't remove dead Array adoptStorage call
If followed by a dead infinite loop, the array initialization might have beed removed. Therefore when inserting a compensating destroy of the array buffer can lead to a crash. #85851 rdar://165876726
1 parent cac74c3 commit cf97c2d

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,14 +1561,6 @@ SILInstruction *SILCombiner::legacyVisitApplyInst(ApplyInst *AI) {
15611561
}
15621562

15631563
if (SF) {
1564-
if (SF->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED)) {
1565-
UserListTy Users;
1566-
// If the uninitialized array is only written into then it can be removed.
1567-
if (recursivelyCollectARCUsers(Users, AI)) {
1568-
if (eraseApply(AI, Users))
1569-
return nullptr;
1570-
}
1571-
}
15721564
if (SF->hasSemanticsAttr(semantics::ARRAY_GET_CONTIGUOUSARRAYSTORAGETYPE)) {
15731565
auto silTy = AI->getType();
15741566
auto storageTy = AI->getType().getASTType();

test/SILOptimizer/sil_combine_apply.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,3 +1037,22 @@ bb3:
10371037
%rv = tuple()
10381038
return %rv : $()
10391039
}
1040+
1041+
1042+
sil [_semantics "array.uninitialized"] [ossa] @adoptStorage : $@convention(thin) (@owned _ContiguousArrayStorage<Any>, Int) -> (@owned Array<Any>, UnsafeMutablePointer<Any>)
1043+
1044+
// CHECK-LABEL: sil [ossa] @dont_remove_dead_adopt_storage :
1045+
// CHECK-NOT: destroy_value
1046+
// CHECK-LABEL: } // end sil function 'dont_remove_dead_adopt_storage'
1047+
sil [ossa] @dont_remove_dead_adopt_storage : $@convention(thin) (Int) -> () {
1048+
bb0(%0 : $Int):
1049+
%1 = integer_literal $Builtin.Word, 1
1050+
%2 = alloc_ref [tail_elems $Any * %1] $_ContiguousArrayStorage<Any>
1051+
%3 = function_ref @adoptStorage : $@convention(thin) (@owned _ContiguousArrayStorage<Any>, Int) -> (@owned Array<Any>, UnsafeMutablePointer<Any>)
1052+
%4 = apply %3(%2, %0) : $@convention(thin) (@owned _ContiguousArrayStorage<Any>, Int) -> (@owned Array<Any>, UnsafeMutablePointer<Any>)
1053+
// Array buffer is not initialized due to dead-end loop -> will lead to crash if buffer is destroyed here
1054+
br bb1
1055+
bb1:
1056+
br bb1
1057+
}
1058+

0 commit comments

Comments
 (0)