Skip to content

Commit 09cd036

Browse files
authored
[IRGen] Fix size computation for imported C types in type layouts (#73271)
rdar://126954341 C types don't have separate size and stride, but in type layouts we always computed the size as if they did. This could cause wrong offsets in compact value witnesses
1 parent a31d15c commit 09cd036

File tree

7 files changed

+92
-17
lines changed

7 files changed

+92
-17
lines changed

lib/IRGen/TypeLayout.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,11 @@ bool AlignedGroupEntry::isSingleRetainablePointer() const { return false; }
16331633
std::optional<Size> AlignedGroupEntry::fixedSize(IRGenModule &IGM) const {
16341634
if (_fixedSize.has_value())
16351635
return *_fixedSize;
1636+
1637+
if (fixedTypeInfo) {
1638+
return *(_fixedSize = (*fixedTypeInfo)->getFixedSize());
1639+
}
1640+
16361641
Size currentSize(0);
16371642
for (auto *entry : entries) {
16381643
if (!entry->fixedSize(IGM) || !entry->fixedAlignment(IGM)) {
@@ -1742,6 +1747,11 @@ bool AlignedGroupEntry::refCountString(IRGenModule &IGM, LayoutStringBuilder &B,
17421747
return false;
17431748
}
17441749

1750+
if (isTriviallyDestroyable()) {
1751+
B.addSkip(fixedSize(IGM)->getValue());
1752+
return true;
1753+
}
1754+
17451755
uint64_t offset = 0;
17461756
for (auto *entry : entries) {
17471757
if (offset) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef SWIFT_TEST_CTYPES_H
2+
#define SWIFT_TEST_CTYPES_H
3+
4+
struct BigAlignment {
5+
_Alignas(16) float foo[4];
6+
char b;
7+
};
8+
9+
#endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module CTypes {
2+
header "CTypes.h"
3+
}

test/Interpreter/Inputs/layout_string_witnesses_types.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Swift
2+
import CTypes
23

34
public class SimpleClass {
45
public let x: Int
@@ -39,6 +40,15 @@ public struct Simple {
3940
}
4041
}
4142

43+
public struct CTypeAligned {
44+
let x = BigAlignment()
45+
let y: SimpleClass
46+
47+
public init(_ y: SimpleClass) {
48+
self.y = y
49+
}
50+
}
51+
4252
public struct GenericStruct<T> {
4353
let x: Int = 0
4454
let y: T
@@ -57,17 +67,17 @@ public class GenericClass<T> {
5767
}
5868

5969
public struct GenericBig<T> {
60-
let x: T
61-
let x1: T
62-
let x2: T
63-
let x3: T
64-
let x4: T
65-
let x5: T
66-
let x6: T
67-
let x7: T
68-
let x8: T
69-
let x9: T
70-
let x10: T
70+
let x: T
71+
let x1: T
72+
let x2: T
73+
let x3: T
74+
let x4: T
75+
let x5: T
76+
let x6: T
77+
let x7: T
78+
let x8: T
79+
let x9: T
80+
let x10: T
7181

7282
public init(x: T) {
7383
self.x = x

test/Interpreter/layout_string_witnesses_dynamic.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -prespecialize-generic-metadata -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-type-layout -enable-autolinking-runtime-compatibility-bytecode-layouts -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-dylib(%t/%target-library-name(layout_string_witnesses_types)) -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
2+
// RUN: %target-swift-frontend -I %S/Inputs/CTypes -prespecialize-generic-metadata -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-type-layout -enable-autolinking-runtime-compatibility-bytecode-layouts -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-dylib(%t/%target-library-name(layout_string_witnesses_types)) -I %S/Inputs/CTypes -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
44
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
55
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-library-evolution -enable-autolinking-runtime-compatibility-bytecode-layouts -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift
66
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift

test/Interpreter/layout_string_witnesses_objc.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %empty-directory(%t)
22
//
33
// RUN: %target-clang -fobjc-arc %S/Inputs/ObjCClasses/ObjCClasses.m -c -o %t/ObjCClasses.o
4-
// RUN: %target-swift-frontend -prespecialize-generic-metadata -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-type-layout -enable-autolinking-runtime-compatibility-bytecode-layouts -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift
5-
// RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
4+
// RUN: %target-swift-frontend -I %S/Inputs/CTypes -prespecialize-generic-metadata -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-type-layout -enable-autolinking-runtime-compatibility-bytecode-layouts -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift
5+
// RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -I %S/Inputs/CTypes -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
66
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
77
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -parse-stdlib -module-name layout_string_witnesses_dynamic -llayout_string_witnesses_types -L%t -I %S/Inputs/ObjCClasses/ %t/ObjCClasses.o -I %t -o %t/main %s %target-rpath(%t)
88
// RUN: %target-codesign %t/main

test/Interpreter/layout_string_witnesses_static.swift

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift
2+
// RUN: %target-swift-frontend -I %S/Inputs/CTypes -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift
33

44
// 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-layout-string-value-witnesses -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
5+
// RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -I %S/Inputs/CTypes -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift
66
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
77
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -enable-library-evolution -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift
88
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift
@@ -1180,6 +1180,49 @@ func testMultiPayloadError() {
11801180

11811181
testMultiPayloadError()
11821182

1183+
func testCTypeAligned() {
1184+
let ptr = UnsafeMutablePointer<CTypeAligned>.allocate(capacity: 1)
1185+
1186+
// initWithCopy
1187+
do {
1188+
let x = CTypeAligned(SimpleClass(x: 23))
1189+
testInit(ptr, to: x)
1190+
}
1191+
1192+
// assignWithTake
1193+
do {
1194+
let y = CTypeAligned(SimpleClass(x: 1))
1195+
1196+
// CHECK-NEXT: Before deinit
1197+
print("Before deinit")
1198+
1199+
// CHECK-NEXT: SimpleClass deinitialized!
1200+
testAssign(ptr, from: y)
1201+
}
1202+
1203+
// assignWithCopy
1204+
do {
1205+
var z = CTypeAligned(SimpleClass(x: 5))
1206+
1207+
// CHECK-NEXT: Before deinit
1208+
print("Before deinit")
1209+
1210+
// CHECK-NEXT: SimpleClass deinitialized!
1211+
testAssignCopy(ptr, from: &z)
1212+
}
1213+
1214+
// CHECK-NEXT: Before deinit
1215+
print("Before deinit")
1216+
1217+
// destroy
1218+
// CHECK-NEXT: SimpleClass deinitialized!
1219+
testDestroy(ptr)
1220+
1221+
ptr.deallocate()
1222+
}
1223+
1224+
testCTypeAligned()
1225+
11831226
#if os(macOS)
11841227
func testObjc() {
11851228
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)

0 commit comments

Comments
 (0)