Skip to content

Commit 22c2889

Browse files
authored
Fix OOB access in SemanticARCOpts (#39351)
1 parent 3ad6d00 commit 22c2889

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/SILOptimizer/SemanticARC/LoadCopyToLoadBorrowOpt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ class StorageGuaranteesLoadVisitor
123123

124124
// If we have an inout parameter that isn't ever actually written to, return
125125
// false.
126-
if (arg->getKnownParameterInfo().isIndirectMutating()) {
126+
if (!arg->isIndirectResult() &&
127+
arg->getKnownParameterInfo().isIndirectMutating()) {
127128
auto wellBehavedWrites = ctx.addressToExhaustiveWriteListCache.get(arg);
128129
if (!wellBehavedWrites.hasValue()) {
129130
return answer(true);

test/SILOptimizer/semantic-arc-opts-loadcopy-to-loadborrow.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ final class Klass {
5656
extension Klass : MyFakeAnyObject {
5757
func myFakeMethod()
5858
}
59+
60+
struct NonTrivialStruct {
61+
var val: Klass
62+
}
63+
5964
sil @guaranteed_klass_user : $@convention(thin) (@guaranteed Klass) -> ()
6065
sil @guaranteed_fakeoptional_klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
6166
sil @guaranteed_fakeoptional_classlet_user : $@convention(thin) (@guaranteed FakeOptional<ClassLet>) -> ()
@@ -1496,3 +1501,16 @@ bb3:
14961501
%9999 = tuple()
14971502
return %9999 : $()
14981503
}
1504+
1505+
// Make sure we don't crash on this code. We used to crash for @out args on the access path to the load
1506+
sil [ossa] @test_opt_out_arg : $@convention(thin)(@in NonTrivialStruct) -> (@out NonTrivialStruct) {
1507+
bb0(%0 : $*NonTrivialStruct, %1 : $*NonTrivialStruct):
1508+
copy_addr %1 to [initialization] %0 : $*NonTrivialStruct
1509+
%ele = struct_element_addr %0 : $*NonTrivialStruct, #NonTrivialStruct.val
1510+
%ld = load [copy] %ele : $*Klass
1511+
destroy_value %ld : $Klass
1512+
destroy_addr %1 : $*NonTrivialStruct
1513+
%9999 = tuple()
1514+
return %9999 : $()
1515+
}
1516+

0 commit comments

Comments
 (0)