Skip to content

[Runtime] Fix missing memcpy in handleSingleRefCountInitWithCopy #69632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions stdlib/public/runtime/BytecodeLayouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ static void singlePayloadEnumGeneric(const Metadata *metadata,
if (SWIFT_LIKELY(xiType)) {
auto tag = xiType->vw_getEnumTagSinglePayload(
(const OpaqueValue *)(addr + addrOffset + xiTagBytesOffset),
numEmptyCases);
xiType->vw_getNumExtraInhabitants());
if (SWIFT_LIKELY(tag == 0)) {
return;
}
Expand Down Expand Up @@ -501,7 +501,7 @@ static void singlePayloadEnumGeneric(const Metadata *metadata,
if (SWIFT_LIKELY(xiType)) {
auto tag = xiType->vw_getEnumTagSinglePayload(
(const OpaqueValue *)(src + addrOffset + xiTagBytesOffset),
numEmptyCases);
xiType->vw_getNumExtraInhabitants());
if (SWIFT_LIKELY(tag == 0)) {
return;
}
Expand Down Expand Up @@ -1317,7 +1317,12 @@ static void handleSingleRefCountInitWithCopy(const Metadata *metadata,
uint8_t *dest,
uint8_t *src) {
auto tag = reader.readBytes<uint64_t>();
addrOffset += (tag & ~(0xFFULL << 56));
auto _addrOffset = addrOffset;
auto offset = (tag & ~(0xFFULL << 56));
if (SWIFT_UNLIKELY(offset)) {
memcpy(dest + _addrOffset, src + _addrOffset, offset);
}
addrOffset = _addrOffset + offset;
tag >>= 56;
if (SWIFT_UNLIKELY(tag == 0)) {
return;
Expand Down Expand Up @@ -1490,13 +1495,13 @@ static void singlePayloadEnumGenericAssignWithCopy(const Metadata *metadata,
if (!srcTag) {
srcTag = xiType->vw_getEnumTagSinglePayload(
(const OpaqueValue *)(src + addrOffset + xiTagBytesOffset),
numEmptyCases);
xiType->vw_getNumExtraInhabitants());
}

if (!destTag) {
destTag = xiType->vw_getEnumTagSinglePayload(
(const OpaqueValue *)(dest + addrOffset + xiTagBytesOffset),
numEmptyCases);
xiType->vw_getNumExtraInhabitants());
}
}

Expand Down Expand Up @@ -2059,7 +2064,8 @@ swift_singlePayloadEnumGeneric_getEnumTag(swift::OpaqueValue *address,
uint8_t numExtraTagBytes) -> unsigned {
if (xiType) {
return xiType->vw_getEnumTagSinglePayload(
(const OpaqueValue *)(addr + xiTagBytesOffset), numEmptyCases);
(const OpaqueValue *)(addr + xiTagBytesOffset),
xiType->vw_getNumExtraInhabitants());
}

return 0;
Expand Down
10 changes: 10 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ public enum PrespecializedMultiPayloadEnum<T> {
case nonEmpty1(T, Int)
}

public enum SinglePayloadEnumExistential {
case a(SomeProtocol, AnyObject)
case b
case c
}

@inline(never)
public func consume<T>(_ x: T.Type) {
withExtendedLifetime(x) {}
Expand Down Expand Up @@ -575,6 +581,10 @@ public func testAssign<T>(_ ptr: UnsafeMutablePointer<T>, from x: UnsafeMutableP
ptr.assign(from: x, count: 1)
}

public func testAssignCopy<T>(_ ptr: UnsafeMutablePointer<T>, from x: inout T) {
ptr.update(from: &x, count: 1)
}

@inline(never)
public func testInit<T>(_ ptr: UnsafeMutablePointer<T>, to x: T) {
ptr.initialize(to: x)
Expand Down
37 changes: 37 additions & 0 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,43 @@ func testSinglePayloadEnumManyXIEmpty() {

testSinglePayloadEnumManyXIEmpty()

func testEnumWithExistential() {
// Regression test for rdar://117755666
// A missing call to memcpy in `handleSingleRefCountInitWithCopy`
// was causing unexpected behavior like wrong enum cases, crashes etc.
// Without the fix, this test would not release the references.
struct SomeProtocolImpl: SomeProtocol {
let x: AnyObject
}

let ptr = UnsafeMutablePointer<SinglePayloadEnumExistential>.allocate(capacity: 1)

do {
let x = SinglePayloadEnumExistential.b
testInit(ptr, to: x)
}

do {
var z = SinglePayloadEnumExistential.a(SomeProtocolImpl(x: SimpleClass(x: 23)), SimpleClass(x: 43))

// CHECK-NEXT: Before deinit
print("Before deinit")

testAssignCopy(ptr, from: &z)
}

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
// CHECK-NEXT: SimpleClass deinitialized!
testDestroy(ptr)

ptr.deallocate()
}

testEnumWithExistential()

#if os(macOS)
func testObjc() {
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)
Expand Down