Skip to content

[IRGen] Reject enums with inaccessible tpye metadata in layout string… #65016

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 4 commits into from
Apr 14, 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
6 changes: 6 additions & 0 deletions lib/IRGen/TypeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,12 @@ bool EnumTypeLayoutEntry::refCountString(IRGenModule &IGM,
case CopyDestroyStrategy::ForwardToPayload:
return cases[0]->refCountString(IGM, B, genericSig);
case CopyDestroyStrategy::Normal: {
// TODO: this is only relevant until we properly support enums.
if (!ty.getASTType()->isLegalFormalType() ||
!IGM.getSILModule().isTypeMetadataAccessible(ty.getASTType())) {
return false;
}

auto *accessor = createMetatypeAccessorFunction(IGM, ty, genericSig);
B.addFixedEnumRefCount(accessor);
B.addSkip(fixedSize(IGM)->getValue());
Expand Down
16 changes: 15 additions & 1 deletion test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ public struct MultiPayloadEnumWrapper {
}
}


public struct ComplexNesting<A, B, C, D> {
let pre: Filler = Filler()
let a: NestedA<A>
Expand Down Expand Up @@ -365,6 +364,21 @@ public struct ComplexNesting<A, B, C, D> {
}
}

internal enum InternalEnum {
case a(Int, AnyObject)
case b(Int)
case c(String)
}

public struct InternalEnumWrapper {
internal let x: InternalEnum
internal let y: Int = 32

public init(x: AnyObject) {
self.x = .a(23, x)
}
}


@inline(never)
public func testAssign<T>(_ ptr: UnsafeMutablePointer<T>, from x: T) {
Expand Down
42 changes: 39 additions & 3 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-type-layout -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift
// RUN: %target-build-swift -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -c -parse-as-library -o %t/layout_string_witnesses_types.o %S/Inputs/layout_string_witnesses_types.swift

// NOTE: We have to build this as dylib to turn private external symbols into local symbols, so we can observe potential issues with linkage
// RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-library-evolution -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -module-name layout_string_witnesses_static %t/layout_string_witnesses_types.o %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -module-name layout_string_witnesses_static -llayout_string_witnesses_types -L%t %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s %target-rpath(%t)
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os
// RUN: %target-run %t/main %t/%target-library-name(layout_string_witnesses_types) | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os

// REQUIRES: executable_test

Expand Down Expand Up @@ -341,6 +344,39 @@ func testForwardToPayloadEnum() {

testForwardToPayloadEnum()

struct InternalEnumWrapperWrapper {
let x: InternalEnumWrapper
}

func testInternalEnumWrapper() {
let ptr = UnsafeMutablePointer<InternalEnumWrapperWrapper>.allocate(capacity: 1)

do {
let x = InternalEnumWrapper(x: SimpleClass(x: 23))
testInit(ptr, to: .init(x: x))
}

do {
let y = InternalEnumWrapper(x: SimpleClass(x: 28))

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

// CHECK-NEXT: SimpleClass deinitialized!
testAssign(ptr, from: .init(x: y))
}

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

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

ptr.deallocate()
}

testInternalEnumWrapper()

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