From 06dac75d8419bad1141602c9903f08e2606e2f37 Mon Sep 17 00:00:00 2001 From: Andreas Bauer Date: Thu, 1 Jul 2021 15:35:18 +0200 Subject: [PATCH 1/2] Correctly pass the alignmentMask to the Swift Runtime swift_allocObject --- Sources/Runtime/Factory/Factory.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/Runtime/Factory/Factory.swift b/Sources/Runtime/Factory/Factory.swift index d8001f8..82a5607 100644 --- a/Sources/Runtime/Factory/Factory.swift +++ b/Sources/Runtime/Factory/Factory.swift @@ -61,10 +61,15 @@ func buildClass(type: Any.Type) throws -> Any { var md = ClassMetadata(type: type) let info = md.toTypeInfo() let metadata = unsafeBitCast(type, to: UnsafeRawPointer.self) - let instanceSize = Int32(md.pointer.pointee.classSize) - let alignment = Int32(md.alignment) + let instanceSize = Int32(md.pointer.pointee.instanceSize) - guard let value = swift_allocObject(metadata, instanceSize, alignment) else { + // https://github.com/wickwirew/Runtime/issues/49 + // Docs specify that the alignment should be "always one less than a power of 2 that's at least alignof(void*)" + // https://github.com/apple/swift/blob/7123d2614b5f222d03b3762cb110d27a9dd98e24/include/swift/Runtime/HeapObject.h#L56-L57 + // We could use md.alignment and deduct 1, or just use the instanceAlignmentMask from the ClassMetadata. + let alignmentMask = Int32(md.pointer.pointee.instanceAlignmentMask) + + guard let value = swift_allocObject(metadata, instanceSize, alignmentMask) else { throw RuntimeError.unableToBuildType(type: type) } From 5910ef1318675c6acc2e050addec62b67c380a16 Mon Sep 17 00:00:00 2001 From: Andreas Bauer Date: Thu, 1 Jul 2021 15:37:45 +0200 Subject: [PATCH 2/2] Reenable tests on linux to be on par with the macOS test runs --- Tests/RuntimeTests/GetSetStructTests.swift | 1 + Tests/RuntimeTests/MetadataTests.swift | 6 ++--- Tests/RuntimeTests/XCTestManifests.swift | 31 ++++++++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Tests/RuntimeTests/GetSetStructTests.swift b/Tests/RuntimeTests/GetSetStructTests.swift index 92b4a8e..bc9c7ef 100644 --- a/Tests/RuntimeTests/GetSetStructTests.swift +++ b/Tests/RuntimeTests/GetSetStructTests.swift @@ -29,6 +29,7 @@ class GetSetStructTests: XCTestCase { static var allTests: [(String, (GetSetStructTests) -> () throws -> Void)] { return [ ("testGet", testGet), + ("testGetSimple", testGetSimple), ("testGetUntypedValue", testGetUntypedValue), ("testGetUntypedObject", testGetUntypedObject), ("testGetUntyped", testGetUntyped), diff --git a/Tests/RuntimeTests/MetadataTests.swift b/Tests/RuntimeTests/MetadataTests.swift index c69e27f..959919d 100644 --- a/Tests/RuntimeTests/MetadataTests.swift +++ b/Tests/RuntimeTests/MetadataTests.swift @@ -220,15 +220,15 @@ class MetadataTests: XCTestCase { XCTAssert(hasPayload.payloadType == Int.self) XCTAssert(hasTuplePayload.payloadType == (Bool, Int).self) } - - #if canImport(Foundation) + func testObjcEnum() { + #if canImport(Foundation) var md = EnumMetadata(type: ComparisonResult.self) let info = md.toTypeInfo() XCTAssertEqual(info.numberOfEnumCases, 3) XCTAssertEqual(info.numberOfPayloadEnumCases, 0) + #endif } - #endif func testOptional() throws { let info = try typeInfo(of: Double?.self) diff --git a/Tests/RuntimeTests/XCTestManifests.swift b/Tests/RuntimeTests/XCTestManifests.swift index 9ce4465..2ab4950 100644 --- a/Tests/RuntimeTests/XCTestManifests.swift +++ b/Tests/RuntimeTests/XCTestManifests.swift @@ -3,7 +3,9 @@ import XCTest extension FactoryTests { static let __allTests = [ ("testStruct", testStruct), - ("testStructUntyped", testStructUntyped) + ("testStructUntyped", testStructUntyped), + ("testClass", testClass), + ("testGenericClass", testGenericClass) ] } @@ -33,13 +35,14 @@ extension GetSetClassTests { ("testSetClassUntypedValue", testSetClassUntypedValue), ("testSetUntyped", testSetUntyped), ("testSetUntypedObject", testSetUntypedObject), - ("testSetUntypedValue", testSetUntypedValue) + ("testSetUntypedValue", testSetUntypedValue) ] } extension GetSetStructTests { static let __allTests = [ ("testGet", testGet), + ("testGetSimple", testGetSimple), ("testGetArray", testGetArray), ("testGetArrayUntyped", testGetArrayUntyped), ("testGetArrayUntypedObject", testGetArrayUntypedObject), @@ -64,21 +67,27 @@ extension GetSetStructTests { ("testSetStructUntypedValue", testSetStructUntypedValue), ("testSetUntyped", testSetUntyped), ("testSetUntypedObject", testSetUntypedObject), - ("testSetUntypedValue", testSetUntypedValue) + ("testSetUntypedValue", testSetUntypedValue) ] } extension MetadataTests { static let __allTests = [ ("testClass", testClass), - ("testEnum", testEnum), - ("testFunction", testFunction), - ("testFunctionThrows", testFunctionThrows), - ("testProtocol", testProtocol), + ("testResilientClass", testResilientClass), ("testStruct", testStruct), + ("testGenericStruct", testGenericStruct), + ("testNestedStruct", testNestedStruct), + ("testProtocol", testProtocol), ("testTuple", testTuple), ("testTupleNoLabels", testTupleNoLabels), - ("testVoidFunction", testVoidFunction) + ("testFunction", testFunction), + ("testFunctionThrows", testFunctionThrows), + ("testVoidFunction", testVoidFunction), + ("testEnum", testEnum), + ("testEnumTestEnumWithPayload", testEnumTestEnumWithPayload), + ("testObjcEnum", testObjcEnum), + ("testOptional", testOptional) ] } @@ -89,7 +98,7 @@ extension ValuePointerTests { ("testClassValuePointer", testClassValuePointer), ("testProtocolClassValuePointer", testProtocolClassValuePointer), ("testProtocolStructValuePointer", testProtocolStructValuePointer), - ("testStructValuePointer", testStructValuePointer) + ("testStructValuePointer", testStructValuePointer) ] } @@ -97,7 +106,7 @@ extension ValueWitnessTableTests { static let __allTests = [ ("testAlignment", testAlignment), ("testSize", testSize), - ("testStride", testStride) + ("testStride", testStride) ] } @@ -109,7 +118,7 @@ public func __allTests() -> [XCTestCaseEntry] { testCase(GetSetStructTests.__allTests), testCase(MetadataTests.__allTests), testCase(ValuePointerTests.__allTests), - testCase(ValueWitnessTableTests.__allTests) + testCase(ValueWitnessTableTests.__allTests) ] } #endif