Skip to content

Commit ee879b1

Browse files
drexinmeg-gupta
authored andcommitted
[IRGen] Reject enums with inaccessible tpye metadata in layout string… (swiftlang#65016)
* [IRGen] Reject enums with inaccessible tpye metadata in layout string generation rdar://107679697 Because we are currently handling most enums in layout strings by going through the metadata, we have to ensure that the metadata is accessible from the current module and reject the enum otherwise. * Use proper mechanism to create and reference dylib in test * Fix linking * Add rpath to test dylib
1 parent b63a3d0 commit ee879b1

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

lib/IRGen/TypeLayout.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,12 @@ bool EnumTypeLayoutEntry::refCountString(IRGenModule &IGM,
20512051
case CopyDestroyStrategy::ForwardToPayload:
20522052
return cases[0]->refCountString(IGM, B, genericSig);
20532053
case CopyDestroyStrategy::Normal: {
2054+
// TODO: this is only relevant until we properly support enums.
2055+
if (!ty.getASTType()->isLegalFormalType() ||
2056+
!IGM.getSILModule().isTypeMetadataAccessible(ty.getASTType())) {
2057+
return false;
2058+
}
2059+
20542060
auto *accessor = createMetatypeAccessorFunction(IGM, ty, genericSig);
20552061
B.addFixedEnumRefCount(accessor);
20562062
B.addSkip(fixedSize(IGM)->getValue());

test/Interpreter/Inputs/layout_string_witnesses_types.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ public struct MultiPayloadEnumWrapper {
308308
}
309309
}
310310

311-
312311
public struct ComplexNesting<A, B, C, D> {
313312
let pre: Filler = Filler()
314313
let a: NestedA<A>
@@ -365,6 +364,21 @@ public struct ComplexNesting<A, B, C, D> {
365364
}
366365
}
367366

367+
internal enum InternalEnum {
368+
case a(Int, AnyObject)
369+
case b(Int)
370+
case c(String)
371+
}
372+
373+
public struct InternalEnumWrapper {
374+
internal let x: InternalEnum
375+
internal let y: Int = 32
376+
377+
public init(x: AnyObject) {
378+
self.x = .a(23, x)
379+
}
380+
}
381+
368382

369383
@inline(never)
370384
public func testAssign<T>(_ ptr: UnsafeMutablePointer<T>, from x: T) {

test/Interpreter/layout_string_witnesses_static.swift

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// RUN: %empty-directory(%t)
22
// 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
3-
// 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
3+
4+
// NOTE: We have to build this as dylib to turn private external symbols into local symbols, so we can observe potential issues with linkage
5+
// 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
6+
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
47
// 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
58
// 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
6-
// 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
9+
// 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)
710
// RUN: %target-codesign %t/main
8-
// RUN: %target-run %t/main | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os
11+
// RUN: %target-run %t/main %t/%target-library-name(layout_string_witnesses_types) | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os
912

1013
// REQUIRES: executable_test
1114

@@ -341,6 +344,39 @@ func testForwardToPayloadEnum() {
341344

342345
testForwardToPayloadEnum()
343346

347+
struct InternalEnumWrapperWrapper {
348+
let x: InternalEnumWrapper
349+
}
350+
351+
func testInternalEnumWrapper() {
352+
let ptr = UnsafeMutablePointer<InternalEnumWrapperWrapper>.allocate(capacity: 1)
353+
354+
do {
355+
let x = InternalEnumWrapper(x: SimpleClass(x: 23))
356+
testInit(ptr, to: .init(x: x))
357+
}
358+
359+
do {
360+
let y = InternalEnumWrapper(x: SimpleClass(x: 28))
361+
362+
// CHECK: Before deinit
363+
print("Before deinit")
364+
365+
// CHECK-NEXT: SimpleClass deinitialized!
366+
testAssign(ptr, from: .init(x: y))
367+
}
368+
369+
// CHECK-NEXT: Before deinit
370+
print("Before deinit")
371+
372+
// CHECK-NEXT: SimpleClass deinitialized!
373+
testDestroy(ptr)
374+
375+
ptr.deallocate()
376+
}
377+
378+
testInternalEnumWrapper()
379+
344380
#if os(macOS)
345381
func testObjc() {
346382
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)

0 commit comments

Comments
 (0)