Skip to content

Commit c537e59

Browse files
authored
[5.7][IRGen] Return null reference from IRGenFunction::emitUnmanagedAlloc … (#58621)
* [IRGen] Return null reference from IRGenFunction::emitUnmanagedAlloc when layout isKnownEmpty rdar://92418090 This fixes a runtime crash on x86 caused by allocations of size 0, which are later deallocated with swift_deallocObjectImpl, but are missing the object header. * Add executable_test requirement to test
1 parent 056dd5d commit c537e59

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/IRGen/GenHeap.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ llvm::Value *IRGenFunction::emitUnmanagedAlloc(const HeapLayout &layout,
534534
const llvm::Twine &name,
535535
llvm::Constant *captureDescriptor,
536536
const HeapNonFixedOffsets *offsets) {
537+
if (layout.isKnownEmpty())
538+
return IGM.RefCountedNull;
539+
537540
llvm::Value *metadata = layout.getPrivateMetadata(IGM, captureDescriptor);
538541
llvm::Value *size, *alignMask;
539542
if (offsets) {
@@ -1505,7 +1508,10 @@ class FixedBoxTypeInfoBase : public BoxTypeInfo {
15051508
public:
15061509
FixedBoxTypeInfoBase(IRGenModule &IGM, HeapLayout &&layout)
15071510
: BoxTypeInfo(IGM), layout(std::move(layout))
1508-
{}
1511+
{
1512+
// Empty layouts should always use EmptyBoxTypeInfo instead
1513+
assert(!layout.isKnownEmpty());
1514+
}
15091515

15101516
OwnedAddress
15111517
allocate(IRGenFunction &IGF, SILType boxedType, GenericEnvironment *env,

test/IRGen/partial_apply.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,20 @@ entry(%0 : $EmptyType, %1: $*SomeType, %3: $FixedType):
848848
%40 = tuple()
849849
return %40 : $()
850850
}
851+
852+
// Test that we don't have an alloc object with 0, because that is not allowed
853+
// CHECK-LABEL: define{{.*}} swiftcc void @my_test_case
854+
// CHECK-NOT: swift_allocObject
855+
// CHECK: ret
856+
sil @take_empty : $@convention(thin) (@in_guaranteed EmptyType) -> ()
857+
sil @my_test_case : $@convention(thin) () -> () {
858+
entry:
859+
%5 = alloc_stack $EmptyType
860+
// store % to %5
861+
%f = function_ref @take_empty : $@convention(thin) (@in_guaranteed EmptyType) -> ()
862+
%36 = partial_apply [callee_guaranteed] %f(%5) : $@convention(thin) (@in_guaranteed EmptyType) -> ()
863+
release_value %36: $@callee_guaranteed () ->()
864+
dealloc_stack %5 : $*EmptyType
865+
%t = tuple()
866+
return %t : $()
867+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-run-simple-swift(-O)
2+
3+
// REQUIRES: executable_test
4+
5+
// rdar://92418090
6+
7+
protocol P {
8+
var covariantSelfPropClosure: ((Self) -> Void) -> Void { get }
9+
}
10+
extension P {
11+
var covariantSelfPropClosure: ((Self) -> Void) -> Void { { $0(self) } }
12+
}
13+
14+
struct S: P {}
15+
16+
let p: P = S()
17+
18+
p.covariantSelfPropClosure { _ in }

0 commit comments

Comments
 (0)