Skip to content

Commit 0fc2e13

Browse files
committed
Create mark_dependence for array allocation in OSLogOptimization
With swiftlang#70242 mark_dependence was added while emitting uninitialized array allocation between the unsafe pointer and the array value returned. Without it, we can end up with use after free of the unsafe pointer if the array's lifetime is shortened. Along with this change, pattern matching of all array optimizations was updated to include mark_dependence. This change adds mark_dependence for uninitialized array allocation in OSLogOptimization which was left out previously. With this, potential use-after-free of the unsafe pointer is prevented and all updated array optimizations apply to the array created by OSLogOptimization. Fixes rdar://122922902
1 parent bb6de77 commit 0fc2e13

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ static SILValue emitCodeForConstantArray(ArrayRef<SILValue> elements,
529529
builder.createDestructureTuple(loc, applyInst);
530530
SILValue arraySIL = destructureInst->getResults()[0];
531531
SILValue storagePointerSIL = destructureInst->getResults()[1];
532+
storagePointerSIL = builder.createMarkDependence(
533+
loc, storagePointerSIL, arraySIL, MarkDependenceKind::Escaping);
532534

533535
if (elements.empty()) {
534536
// Nothing more to be done if we are creating an empty array.

test/SILOptimizer/OSLogMandatoryOptTest.sil

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ bb0:
407407
// CHECK: [[ALLOCATORREF:%[0-9]+]] = function_ref @$ss27_allocateUninitializedArrayySayxG_BptBwlF
408408
// CHECK: [[TUPLE:%[0-9]+]] = apply [[ALLOCATORREF]]<Int64>([[NUMELEMS]])
409409
// CHECK: ([[ARRAY:%[0-9]+]], [[STORAGEPTR:%[0-9]+]]) = destructure_tuple [[TUPLE]]
410-
// CHECK: [[STORAGEADDR:%[0-9]+]] = pointer_to_address [[STORAGEPTR]] : $Builtin.RawPointer to [strict] $*Int64
410+
// CHECK: [[MDI:%.*]] = mark_dependence [[STORAGEPTR]] : $Builtin.RawPointer on [[ARRAY]] : $Array<Int64>
411+
// CHECK: [[STORAGEADDR:%[0-9]+]] = pointer_to_address [[MDI]] : $Builtin.RawPointer to [strict] $*Int64
411412
// CHECK: store [[ELEM1INT]] to [trivial] [[STORAGEADDR]] : $*Int64
412413
// CHECK: [[INDEX1:%[0-9]+]] = integer_literal $Builtin.Word, 1
413414
// CHECK: [[INDEXADDR1:%[0-9]+]] = index_addr [[STORAGEADDR]] : $*Int64, [[INDEX1]] : $Builtin.Word
@@ -539,7 +540,8 @@ bb0:
539540
// CHECK: [[ALLOCATORREF:%[0-9]+]] = function_ref @$ss27_allocateUninitializedArrayySayxG_BptBwlF
540541
// CHECK: [[TUPLE:%[0-9]+]] = apply [[ALLOCATORREF]]<String>([[NUMELEMS]])
541542
// CHECK: ([[ARRAY:%[0-9]+]], [[STORAGEPTR:%[0-9]+]]) = destructure_tuple [[TUPLE]]
542-
// CHECK: [[STORAGEADDR:%[0-9]+]] = pointer_to_address [[STORAGEPTR]] : $Builtin.RawPointer to [strict] $*String
543+
// CHECK: [[MDI:%.*]] = mark_dependence [[STORAGEPTR]] : $Builtin.RawPointer on [[ARRAY]] : $Array<String>
544+
// CHECK: [[STORAGEADDR:%[0-9]+]] = pointer_to_address [[MDI]] : $Builtin.RawPointer to [strict] $*String
543545
// CHECK: store [[STRINGCONST]] to [init] [[STORAGEADDR]] : $*String
544546
// CHECK: [[FINALIZEREF:%[0-9]+]] = function_ref @$ss27_finalizeUninitializedArrayySayxGABnlF
545547
// CHECK: [[FINALIZED:%[0-9]+]] = apply [[FINALIZEREF]]<String>([[ARRAY]])

0 commit comments

Comments
 (0)