wickwirew / runtime Goto Github PK
View Code? Open in Web Editor NEWA Swift Runtime library for viewing type info, and the dynamic getting and setting of properties.
License: MIT License
A Swift Runtime library for viewing type info, and the dynamic getting and setting of properties.
License: MIT License
I believe there is a memory leak with the below code:
I think what's happening is if the value is not initialised it will be fine and no leak will occur (construction) but if you're replacing an existing value the old value is nullified without being free'd since swift loses track of it.
I installed Runtime 2.1.0 using Cocoapods. When I opened Xcode and ran a clean build, I got the following build error in Runtime's Factory.swift:
Use of unresolved identifier 'swift_allocObject
.
Here are the details on my environment:
Here are the troubleshooting steps I've tried so far:
post_install
from my Podfile, and ran pod install
again after deleting the Pods/ directory and Podfile.lockIs it possible to inspect the cases of enums with associated values?
I would expect the following code to print something like $s11TestRuntime0A8ManglingCMn
. However, the actual output is simply TestMangling
.
import Runtime
class TestMangling {
}
let type = try! typeInfo(of: TestMangling.self)
print(type.mangledName)
For a few types I've tried, the output is an empty string. I'm guessing that the layout of the nominal type descriptor has changed since Runtime was first written, but I'm not sure how to verify this as I'm not sure where the struct for nominal type descriptors actually lives in the swift runtime source.
I'm using Runtime 2.1.1, Swift 5.1.
Please release a new version with the "Optional Fix", so that it can be used in other libraries. CocoaPods Podspec doesn't allow to specify a branch for a dependency.
Basically, I'm trying to provide KeyValueCoding capability including the array operations.
I'm trying to navigate the key path through a property that is an Array. If the propertyInfo could provide the type of the Array Element that may do the trick.
The following code will crash
class ViewController: UIViewController { }
let info = try? typeInfo(of: ViewController.self)
in
RelativePointer->advanced()
on
let offsetCopy = self.offset
Any ideas what would cause this?
struct UserLoginRes {
// SwiftProtobuf.Message conformance is added in an extension below. See the
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
// methods supported on all messages.
var code: Int32 {
get {return _storage._code}
set {_uniqueStorage()._code = newValue}
}
var message: String {
get {return _storage._message}
set {_uniqueStorage()._message = newValue}
}
var data: UserInfo {
get {return _storage._data ?? UserInfo()}
set {_uniqueStorage()._data = newValue}
}
/// Returns true if `data` has been explicitly set.
var hasData: Bool {return _storage._data != nil}
/// Clears the value of `data`. Subsequent reads from it will return its default value.
mutating func clearData() {_uniqueStorage()._data = nil}
var unknownFields = SwiftProtobuf.UnknownStorage()
init() {}
fileprivate var _storage = _StorageClass.defaultInstance
}
extension UserLoginRes: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
static let protoMessageName: String = _protobuf_package + ".UserLoginRes"
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1: .same(proto: "code"),
2: .same(proto: "message"),
3: .same(proto: "data"),
]
fileprivate class _StorageClass {
var _code: Int32 = 0
var _message: String = String()
var _data: UserInfo? = nil
static let defaultInstance = _StorageClass()
private init() {}
init(copying source: _StorageClass) {
_code = source._code
_message = source._message
_data = source._data
}
}
fileprivate mutating func _uniqueStorage() -> _StorageClass {
if !isKnownUniquelyReferenced(&_storage) {
_storage = _StorageClass(copying: _storage)
}
return _storage
}
....
Usage:
let realResp = try typeInfo(of: UserLoginRes.self)
**Q: Can not find property "code" and "message"? Only "unknownFields" and "_storage"? **
JSONSerialization.jsonObject
method return Foundation Objects
.
therefor, Int type is mapped to __NSCFNumber also string type is mapped to __NSCFString.. and so on
the problem is that if __NSCFNumber is mapped to Int using runtime library, it is fail because of type mismatch
Runtime / PropertyInfo.swift
private func set(value: Any, pointer: UnsafeMutableRawPointer) throws {
// type mismatch occur
if Swift.type(of: value) != self.type { return }
let valuePointer = pointer.advanced(by: offset)
let sets = setters(type: type)
sets.set(value: value, pointer: valuePointer)
}
how could I fix this problem?
thank you.
I know this is a place for issues but couldn't find any better place to ask this question.
This library is great and I would like to use for one of my projects which runs on Linux - I believe at this time ABI stability is limited to systems running Darwin. I could be wrong about this. My question is - has this library been tested and is it safe to use on Linux?
Thanks
I'm setting a property but it seems that property.set doesn't accept nil values. In this case it's an Int?
. What would I need to do to make Runtime support this?
Once #88 is merged there are still a number of ASan issues. The tests that fail are
All of these fail when accessing data in NominalMetadataType
. The first fail is in numberOfFields
.
Here is the ASan output
Test Case '-[RuntimeTests.GetSetStructTests testGetSimple]' started.
=================================================================
==6502==ERROR: AddressSanitizer: global-buffer-overflow on address 0x00010ecff141 at pc 0x00010014d0ad bp 0x7ffeefbfb170 sp 0x7ffeefbfa930
READ of size 44 at 0x00010ecff141 thread T0
#0 0x10014d0ac in wrap_memcpy+0x16c (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x1d0ac)
#1 0x10ecb6b1c in __swift_memcpy44_4+0x1c (RuntimeTests:x86_64+0xd9b1c)
#2 0x10eccb24e in NominalMetadataType.numberOfFields()+0x84e (RuntimeTests:x86_64+0xee24e)
#3 0x10eccbc84 in NominalMetadataType.fieldOffsets()+0x7e4 (RuntimeTests:x86_64+0xeec84)
#4 0x10ecccae7 in NominalMetadataType.properties()+0x527 (RuntimeTests:x86_64+0xefae7)
#5 0x10ecd1263 in StructMetadata.toTypeInfo()+0xa13 (RuntimeTests:x86_64+0xf4263)
#6 0x10ecd206f in protocol witness for TypeInfoConvertible.toTypeInfo() in conformance StructMetadata+0xcf (RuntimeTests:x86_64+0xf506f)
#7 0x10ece3b99 in typeInfo(of:)+0xa09 (RuntimeTests:x86_64+0x106b99)
#8 0x10ec1fefc in GetSetStructTests.testGetSimple() GetSetStructTests.swift:70
#9 0x10ec207d2 in @objc GetSetStructTests.testGetSimple() <compiler-generated>
#10 0x7fff205e856b in __invoking___+0x8b (CoreFoundation:x86_64h+0x6456b)
#11 0x7fff205e840f in -[NSInvocation invoke]+0x12e (CoreFoundation:x86_64h+0x6440f)
#12 0x1010a8bfc in __24-[XCTestCase invokeTest]_block_invoke_3+0x33 (XCTest:x86_64+0x30bfc)
#13 0x10117c3e8 in +[XCTSwiftErrorObservation observeErrorsInBlock:]+0x44 (XCTest:x86_64+0x1043e8)
#14 0x1010a8afe in __24-[XCTestCase invokeTest]_block_invoke_2+0x76 (XCTest:x86_64+0x30afe)
#15 0x10113fb75 in -[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:]+0x40 (XCTest:x86_64+0xc7b75)
#16 0x1010b3a3d in -[XCTestCase assertInvalidObjectsDeallocatedAfterScope:]+0x3c (XCTest:x86_64+0x3ba3d)
#17 0x1010a8a44 in __24-[XCTestCase invokeTest]_block_invoke.262+0xc6 (XCTest:x86_64+0x30a44)
#18 0x1011205d7 in -[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:]+0xb2 (XCTest:x86_64+0xa85d7)
#19 0x1010a8507 in -[XCTestCase invokeTest]+0x40c (XCTest:x86_64+0x30507)
#20 0x1010aa17d in __26-[XCTestCase performTest:]_block_invoke_2+0x2a (XCTest:x86_64+0x3217d)
#21 0x1011205d7 in -[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:]+0xb2 (XCTest:x86_64+0xa85d7)
#22 0x1010aa0b4 in __26-[XCTestCase performTest:]_block_invoke.393+0x55 (XCTest:x86_64+0x320b4)
#23 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#24 0x1010a98c1 in -[XCTestCase performTest:]+0x2bd (XCTest:x86_64+0x318c1)
#25 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#26 0x1010a164c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
#27 0x1010a14c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
#28 0x1010a0d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
#29 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#30 0x1010a0ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
#31 0x1010a0fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
#32 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#33 0x1010a164c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
#34 0x1010a14c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
#35 0x1010a0d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
#36 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#37 0x1010a0ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
#38 0x1010a0fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
#39 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#40 0x1010a164c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
#41 0x1010a14c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
#42 0x1010a0d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
#43 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#44 0x1010a0ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
#45 0x1010a0fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
#46 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#47 0x1011542ce in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke_2+0x93 (XCTest:x86_64+0xdc2ce)
#48 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#49 0x101154233 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke+0x6e (XCTest:x86_64+0xdc233)
#50 0x1011543b4 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke.87+0x5f (XCTest:x86_64+0xdc3b4)
#51 0x1010ca684 in -[XCTestObservationCenter _observeTestExecutionForBlock:]+0x144 (XCTest:x86_64+0x52684)
#52 0x101153fab in -[XCTTestRunSession runTestsAndReturnError:]+0x1d8 (XCTest:x86_64+0xdbfab)
#53 0x101085338 in -[XCTestDriver _runTests]+0x1d1 (XCTest:x86_64+0xd338)
#54 0x10112f1b7 in _XCTestMain+0x6b (XCTest:x86_64+0xb71b7)
#55 0x100006530 in main+0x194 (xctest:x86_64+0x100006530)
#56 0x7fff20528620 in start+0x0 (libdyld.dylib:x86_64+0x15620)
0x00010ecff141 is located 0 bytes to the right of global variable '' defined in '/Users/adamfowler/Library/Developer/Xcode/DerivedData/Runtime-alpyovrntiajzygbbctjlczjhope/Build/Intermediates.noindex/Runtime.build/Debug/RuntimeTests.build/Objects-normal-asan/x86_64/GetSetStructTests.o' (0x10ecff140) of size 1
'' is ascii string ''
SUMMARY: AddressSanitizer: global-buffer-overflow (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x1d0ac) in wrap_memcpy+0x16c
Shadow bytes around the buggy address:
0x100021d9fdd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fde0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fdf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fe10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x100021d9fe20: 00 00 00 00 00 00 00 00[01]f9 f9 f9 f9 f9 f9 f9
0x100021d9fe30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fe40: 00 00 00 00 00 00 00 00 00 00 00 00 01 f9 f9 f9
0x100021d9fe50: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fe60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100021d9fe70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
2021-05-13 08:05:22.047958+0100 xctest[6502:1824991] =================================================================
2021-05-13 08:05:22.048116+0100 xctest[6502:1824991] ==6502==ERROR: AddressSanitizer: global-buffer-overflow on address 0x00010ecff141 at pc 0x00010014d0ad bp 0x7ffeefbfb170 sp 0x7ffeefbfa930
2021-05-13 08:05:22.048162+0100 xctest[6502:1824991] READ of size 44 at 0x00010ecff141 thread T0
2021-05-13 08:05:22.048300+0100 xctest[6502:1824991] #0 0x10014d0ac in wrap_memcpy+0x16c (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x1d0ac)
2021-05-13 08:05:22.048459+0100 xctest[6502:1824991] #1 0x10ecb6b1c in __swift_memcpy44_4+0x1c (RuntimeTests:x86_64+0xd9b1c)
2021-05-13 08:05:22.048505+0100 xctest[6502:1824991] #2 0x10eccb24e in NominalMetadataType.numberOfFields()+0x84e (RuntimeTests:x86_64+0xee24e)
2021-05-13 08:05:22.048539+0100 xctest[6502:1824991] #3 0x10eccbc84 in NominalMetadataType.fieldOffsets()+0x7e4 (RuntimeTests:x86_64+0xeec84)
2021-05-13 08:05:22.048656+0100 xctest[6502:1824991] #4 0x10ecccae7 in NominalMetadataType.properties()+0x527 (RuntimeTests:x86_64+0xefae7)
2021-05-13 08:05:22.048715+0100 xctest[6502:1824991] #5 0x10ecd1263 in StructMetadata.toTypeInfo()+0xa13 (RuntimeTests:x86_64+0xf4263)
2021-05-13 08:05:22.048792+0100 xctest[6502:1824991] #6 0x10ecd206f in protocol witness for TypeInfoConvertible.toTypeInfo() in conformance StructMetadata+0xcf (RuntimeTests:x86_64+0xf506f)
2021-05-13 08:05:22.048825+0100 xctest[6502:1824991] #7 0x10ece3b99 in typeInfo(of:)+0xa09 (RuntimeTests:x86_64+0x106b99)
2021-05-13 08:05:22.048860+0100 xctest[6502:1824991] #8 0x10ec1fefc in GetSetStructTests.testGetSimple() GetSetStructTests.swift:70
2021-05-13 08:05:22.048928+0100 xctest[6502:1824991] #9 0x10ec207d2 in @objc GetSetStructTests.testGetSimple() <compiler-generated>
2021-05-13 08:05:22.049019+0100 xctest[6502:1824991] #10 0x7fff205e856b in __invoking___+0x8b (CoreFoundation:x86_64h+0x6456b)
2021-05-13 08:05:22.049065+0100 xctest[6502:1824991] #11 0x7fff205e840f in -[NSInvocation invoke]+0x12e (CoreFoundation:x86_64h+0x6440f)
2021-05-13 08:05:22.049102+0100 xctest[6502:1824991] #12 0x1010a8bfc in __24-[XCTestCase invokeTest]_block_invoke_3+0x33 (XCTest:x86_64+0x30bfc)
2021-05-13 08:05:22.049165+0100 xctest[6502:1824991] #13 0x10117c3e8 in +[XCTSwiftErrorObservation observeErrorsInBlock:]+0x44 (XCTest:x86_64+0x1043e8)
2021-05-13 08:05:22.049245+0100 xctest[6502:1824991] #14 0x1010a8afe in __24-[XCTestCase invokeTest]_block_invoke_2+0x76 (XCTest:x86_64+0x30afe)
2021-05-13 08:05:22.049281+0100 xctest[6502:1824991] #15 0x10113fb75 in -[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:]+0x40 (XCTest:x86_64+0xc7b75)
2021-05-13 08:05:22.049313+0100 xctest[6502:1824991] #16 0x1010b3a3d in -[XCTestCase assertInvalidObjectsDeallocatedAfterScope:]+0x3c (XCTest:x86_64+0x3ba3d)
2021-05-13 08:05:22.049356+0100 xctest[6502:1824991] #17 0x1010a8a44 in __24-[XCTestCase invokeTest]_block_invoke.262+0xc6 (XCTest:x86_64+0x30a44)
2021-05-13 08:05:22.049427+0100 xctest[6502:1824991] #18 0x1011205d7 in -[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:]+0xb2 (XCTest:x86_64+0xa85d7)
2021-05-13 08:05:22.049518+0100 xctest[6502:1824991] #19 0x1010a8507 in -[XCTestCase invokeTest]+0x40c (XCTest:x86_64+0x30507)
2021-05-13 08:05:22.049575+0100 xctest[6502:1824991] #20 0x1010aa17d in __26-[XCTestCase performTest:]_block_invoke_2+0x2a (XCTest:x86_64+0x3217d)
2021-05-13 08:05:22.049704+0100 xctest[6502:1824991] #21 0x1011205d7 in -[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:]+0xb2 (XCTest:x86_64+0xa85d7)
2021-05-13 08:05:22.049791+0100 xctest[6502:1824991] #22 0x1010aa0b4 in __26-[XCTestCase performTest:]_block_invoke.393+0x55 (XCTest:x86_64+0x320b4)
2021-05-13 08:05:22.049879+0100 xctest[6502:1824991] #23 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
2021-05-13 08:05:22.049937+0100 xctest[6502:1824991] #24 0x1010a98c1 in -[XCTestCase performTest:]+0x2bd (XCTest:x86_64+0x318c1)
2021-05-13 08:05:22.050014+0100 xctest[6502:1824991] #25 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
2021-05-13 08:05:22.050050+0100 xctest[6502:1824991] #26 0x1010a164c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
2021-05-13 08:05:22.050102+0100 xctest[6502:1824991] #27 0x1010a14c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
2021-05-13 08:05:22.050162+0100 xctest[6502:1824991] #28 0x1010a0d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
2021-05-13 08:05:22.050199+0100 xctest[6502:1824991] #29 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
2021-05-13 08:05:22.050237+0100 xctest[6502:1824991] #30 0x1010a0ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
2021-05-13 08:05:22.050273+0100 xctest[6502:1824991] #31 0x1010a0fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
2021-05-13 08:05:22.050311+0100 xctest[6502:1824991] #32 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
2021-05-13 08:05:22.050387+0100 xctest[6502:1824991] #33 0x1010a164c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
2021-05-13 08:05:22.050426+0100 xctest[6502:1824991] #34 0x1010a14c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
2021-05-13 08:05:22.050476+0100 xctest[6502:1824991] #35 0x1010a0d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
2021-05-13 08:05:22.050553+0100 xctest[6502:1824991] #36 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
2021-05-13 08:05:22.050638+0100 xctest[6502:1824991] #37 0x1010a0ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
2021-05-13 08:05:22.050716+0100 xctest[6502:1824991] #38 0x1010a0fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
2021-05-13 08:05:22.050806+0100 xctest[6502:1824991] #39 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
2021-05-13 08:05:22.050841+0100 xctest[6502:1824991] #40 0x1010a164c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
2021-05-13 08:05:22.050875+0100 xctest[6502:1824991] #41 0x1010a14c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
2021-05-13 08:05:22.050960+0100 xctest[6502:1824991] #42 0x1010a0d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
2021-05-13 08:05:22.051064+0100 xctest[6502:1824991] #43 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
2021-05-13 08:05:22.051117+0100 xctest[6502:1824991] #44 0x1010a0ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
2021-05-13 08:05:22.051153+0100 xctest[6502:1824991] #45 0x1010a0fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
2021-05-13 08:05:22.051188+0100 xctest[6502:1824991] #46 0x1010fb0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
2021-05-13 08:05:22.051224+0100 xctest[6502:1824991] #47 0x1011542ce in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke_2+0x93 (XCTest:x86_64+0xdc2ce)
2021-05-13 08:05:22.051265+0100 xctest[6502:1824991] #48 0x101134462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
2021-05-13 08:05:22.051304+0100 xctest[6502:1824991] #49 0x101154233 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke+0x6e (XCTest:x86_64+0xdc233)
2021-05-13 08:05:22.051336+0100 xctest[6502:1824991] #50 0x1011543b4 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke.87+0x5f (XCTest:x86_64+0xdc3b4)
2021-05-13 08:05:22.051371+0100 xctest[6502:1824991] #51 0x1010ca684 in -[XCTestObservationCenter _observeTestExecutionForBlock:]+0x144 (XCTest:x86_64+0x52684)
2021-05-13 08:05:22.051408+0100 xctest[6502:1824991] #52 0x101153fab in -[XCTTestRunSession runTestsAndReturnError:]+0x1d8 (XCTest:x86_64+0xdbfab)
2021-05-13 08:05:22.051441+0100 xctest[6502:1824991] #53 0x101085338 in -[XCTestDriver _runTests]+0x1d1 (XCTest:x86_64+0xd338)
2021-05-13 08:05:22.051479+0100 xctest[6502:1824991] #54 0x10112f1b7 in _XCTestMain+0x6b (XCTest:x86_64+0xb71b7)
2021-05-13 08:05:22.051514+0100 xctest[6502:1824991] #55 0x100006530 in main+0x194 (xctest:x86_64+0x100006530)
2021-05-13 08:05:22.051552+0100 xctest[6502:1824991] #56 0x7fff20528620 in start+0x0 (libdyld.dylib:x86_64+0x15620)
2021-05-13 08:05:22.051589+0100 xctest[6502:1824991]
2021-05-13 08:05:22.051626+0100 xctest[6502:1824991] 0x00010ecff141 is located 0 bytes to the right of global variable '' defined in '/Users/adamfowler/Library/Developer/Xcode/DerivedData/Runtime-alpyovrntiajzygbbctjlczjhope/Build/Intermediates.noindex/Runtime.build/Debug/RuntimeTests.build/Objects-normal-asan/x86_64/GetSetStructTests.o' (0x10ecff140) of size 1
2021-05-13 08:05:22.051664+0100 xctest[6502:1824991] '' is ascii string ''
2021-05-13 08:05:22.051700+0100 xctest[6502:1824991] SUMMARY: AddressSanitizer: global-buffer-overflow (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x1d0ac) in wrap_memcpy+0x16c
2021-05-13 08:05:22.051739+0100 xctest[6502:1824991] Shadow bytes around the buggy address:
2021-05-13 08:05:22.051772+0100 xctest[6502:1824991] 0x100021d9fdd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.051830+0100 xctest[6502:1824991] 0x100021d9fde0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.051880+0100 xctest[6502:1824991] 0x100021d9fdf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052285+0100 xctest[6502:1824991] 0x100021d9fe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052326+0100 xctest[6502:1824991] 0x100021d9fe10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052373+0100 xctest[6502:1824991] =>0x100021d9fe20: 00 00 00 00 00 00 00 00[01]f9 f9 f9 f9 f9 f9 f9
2021-05-13 08:05:22.052462+0100 xctest[6502:1824991] 0x100021d9fe30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052601+0100 xctest[6502:1824991] 0x100021d9fe40: 00 00 00 00 00 00 00 00 00 00 00 00 01 f9 f9 f9
2021-05-13 08:05:22.052683+0100 xctest[6502:1824991] 0x100021d9fe50: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052748+0100 xctest[6502:1824991] 0x100021d9fe60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052823+0100 xctest[6502:1824991] 0x100021d9fe70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2021-05-13 08:05:22.052859+0100 xctest[6502:1824991] Shadow byte legend (one shadow byte represents 8 application bytes):
2021-05-13 08:05:22.052914+0100 xctest[6502:1824991] Addressable: 00
2021-05-13 08:05:22.052951+0100 xctest[6502:1824991] Partially addressable: 01 02 03 04 05 06 07
2021-05-13 08:05:22.052996+0100 xctest[6502:1824991] Heap left redzone: fa
2021-05-13 08:05:22.053050+0100 xctest[6502:1824991] Freed heap region: fd
2021-05-13 08:05:22.053088+0100 xctest[6502:1824991] Stack left redzone: f1
2021-05-13 08:05:22.053151+0100 xctest[6502:1824991] Stack mid redzone: f2
2021-05-13 08:05:22.053220+0100 xctest[6502:1824991] Stack right redzone: f3
2021-05-13 08:05:22.053297+0100 xctest[6502:1824991] Stack after return: f5
2021-05-13 08:05:22.053351+0100 xctest[6502:1824991] Stack use after scope: f8
2021-05-13 08:05:22.053427+0100 xctest[6502:1824991] Global redzone: f9
2021-05-13 08:05:22.053495+0100 xctest[6502:1824991] Global init order: f6
2021-05-13 08:05:22.053529+0100 xctest[6502:1824991] Poisoned by user: f7
2021-05-13 08:05:22.053588+0100 xctest[6502:1824991] Container overflow: fc
2021-05-13 08:05:22.053667+0100 xctest[6502:1824991] Array cookie: ac
2021-05-13 08:05:22.053702+0100 xctest[6502:1824991] Intra object redzone: bb
2021-05-13 08:05:22.053734+0100 xctest[6502:1824991] ASan internal: fe
2021-05-13 08:05:22.053772+0100 xctest[6502:1824991] Left alloca redzone: ca
2021-05-13 08:05:22.053805+0100 xctest[6502:1824991] Right alloca redzone: cb
2021-05-13 08:05:22.053839+0100 xctest[6502:1824991] Shadow gap: cc
==6502==ABORTING
Again the thread sanitizer on Linux is crashing as well
Simply include this function definition somewhere in this project:
@_silgen_name("swift_getTypeByMangledNameInContext")
func swift_getTypeByMangledNameInContext(
_ typeNameStart: UnsafeMutablePointer<Int8>,
_ typeNameLength: Int32,
_ context: UnsafeRawPointer?,
_ genericArgs: UnsafePointer<UnsafeRawPointer?>?
) -> UnsafeRawPointer?
Can the library be installed via Carthage?
Currently trying to build the product by adding this to my Cartfile:
github "wickwirew/Runtime" == 1.1.0
I get
*** Skipped building Runtime due to the error:
Dependency "Runtime" has no shared framework schemes
I'm not 100% sure why but
testClass
and testGenericClass
are not part of the XCTestManifest and thus don't get run on Linux.
The first testClass
fails on Linux with a segfault.
Are these methods supposed to work on Linux or not?
So i've got a class where most of the properties are optional and I'm trying to set their values.
Is it possible to set the value of an optional kind property? When i currently try it throws an RuntimeError.couldNotGetPointer. Is there a workaround to cast it as non-optional or something like that?
Thanks in advance
Trying to set values with the lib but getting EXC_BAD_ACCESS (code=1, address=0x0) error
func setFieldValue(_ value: Any) {
if let editFieldName = editFieldName1, let _ = targetObject {
var object = targetObject!;
if dataItemTypeInfo == nil{
dataItemTypeInfo = try! typeInfo(of: type(of:object));
}
let property = try! dataItemTypeInfo!.property(named: editFieldName)
try! property.set(value: value, on: &object)
}
}
It crashes on the last line: try! property.set(value: value, on: &object)
and till then all values looks correct (dataItemTypeInfo, property, value, targetObject, editFieldName)
My pod:
pod 'Runtime', :git => 'https://github.com/wickwirew/Runtime.git', :branch => 'swift-4.1'
Xcode 9.3.1
Swift 4.1
Am I doing something wrong?
Hello,
wanted to report that the Runtime framework crashes (segfault) when used on linux platforms using a swift compiler >= 5.4.
To my analysis, it comes from calling typeInfo(of:)
for any class type.
I noticed this in the environment of our own project Apodini/Apodini#294, but could also easily reproduce it with the provided test cases of the Runtime framework.
The following test cases fail:
GetSetClassTests
: all test cases failMetadataTests
: only testClass()
failsFactoryTests
: testClass()
, testGenericClass()
(those fail on all line based swift 5 versions, see #49 and Actions Run)I found that any call to typeInfo(of:)
with a class type will result in a segfault (see backtrace below).
Specifically, it will most certainly fail when evaluating the mangledName()
method (inside the ClassMetadata.toTypeInfo()
call):
If you uncomment that line it will fail on the next access to the pointer.pointee.typeDescriptor.pointee
chain (e.g. in NominalMetadataType.numberOfFields
).
For context, I created some extensive GitHub Ci (see Supereg/Runtime feature/github-ci branch) which runs the tests cases in all swift 5 version for all common linux distributions available on the Swift DockerHub: GitHub CI Run Overview. It shows that since swift 5.4 every swift release on all linux platforms are affected.
I'll plan to create a pull request for that later as well.
Below is an exemplary backtrace of the segfault happening when running the shown command under xenial
.
swift test --filter GetSetClassTests
[4/4] Build complete!
Test Suite 'Selected tests' started at 2021-06-30 15:04:32.986
Test Suite 'GetSetClassTests' started at 2021-06-30 15:04:32.987
Test Case 'GetSetClassTests.testGet' started at 2021-06-30 15:04:32.987
Exited with signal code 11
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00007f1de0332263 in swift::metadataimpl::ValueWitnesses<swift::metadataimpl::NativeBox<unsigned int, 4ul, 4ul, 4ul> >::initializeWithCopy(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*) ()
from /usr/lib/swift/linux/libswiftCore.so
#1 0x000055a47e1b7dec in $s7Runtime15RelativePointerV8advancedSpyq_GyF (self=<error reading variable: Cannot access memory at address 0x28>) at /usr/src/Runtime/Sources/Runtime/Pointers/RelativePointer.swift:31
#2 0x000055a47e1ac340 in $s7Runtime19NominalMetadataTypePAAE11mangledNameSSyF (self=...) at /usr/src/Runtime/Sources/Runtime/Metadata/NominalMetadataType.swift:43
#3 0x000055a47e1a79ef in $s7Runtime13ClassMetadataV10toTypeInfoAA0eF0VyF (self=...) at /usr/src/Runtime/Sources/Runtime/Metadata/ClassMetadata.swift:88
#4 0x000055a47e1a80ab in $s7Runtime13ClassMetadataVAA19TypeInfoConvertibleA2aDP02todE0AA0dE0VyFTW ()
#5 0x000055a47e1b60c6 in $s7Runtime8typeInfo2ofAA04TypeC0VypXp_tKF (type=...) at /usr/src/Runtime/Sources/Runtime/Models/TypeInfo.swift:81
#6 0x000055a47e1c1a09 in $s12RuntimeTests011GetSetClassB0C04testC0yyKF (self=...) at /usr/src/Runtime/Tests/RuntimeTests/GetSetClassTests.swift:60
#7 0x000055a47e1e9625 in $s12RuntimeTests011GetSetClassB0C05__allB0SaySS_yyKcACctGvpZfiyyKcACcfu_yyKcfu0_ () at /usr/src/Runtime/Tests/RuntimeTests/XCTestManifests.swift:13
#8 0x000055a47e1f02af in $s12RuntimeTests011GetSetClassB0C05__allB0SaySS_yyKcACctGvpZfiyyKcACcfu_yyKcfu0_TA ()
#9 0x000055a47e1bb16f in $ss5Error_pIegzo_ytsAA_pIegrzo_TR ()
#10 0x000055a47e1ca944 in $ss5Error_pIegzo_ytsAA_pIegrzo_TRTA ()
#11 0x00007f1de0618b5c in $syts5Error_pIegrzo_sAA_pIegzo_TRTA () from /usr/lib/swift/linux/libXCTest.so
#12 0x00007f1de06189b8 in $s6XCTest4test33_3BE257A46ADB477C7BF2D39968B39F9DLLyyAA0A4CaseCKcyyKcxcAERbzlFyAEKcfU_TA () from /usr/lib/swift/linux/libXCTest.so
#13 0x00007f1de061894f in $s6XCTest0A4CaseCs5Error_pIeggzo_ACytsAD_pIegnrzo_TRTA () from /usr/lib/swift/linux/libXCTest.so
#14 0x00007f1de0618c16 in $s6XCTest0A4CaseCs5Error_pIeggzo_ACytsAD_pIegnrzo_TRTA.19 () from /usr/lib/swift/linux/libXCTest.so
#15 0x00007f1de060a628 in $s6XCTest0A4CaseCyts5Error_pIegnrzo_ACsAD_pIeggzo_TRTA () from /usr/lib/swift/linux/libXCTest.so
#16 0x00007f1de06172f8 in $s6XCTest0A4CaseC10invokeTestyyF () from /usr/lib/swift/linux/libXCTest.so
#17 0x00007f1de06170e3 in $s6XCTest0A4CaseC7performyyAA0A3RunCF () from /usr/lib/swift/linux/libXCTest.so
#18 0x00007f1de061a980 in $s6XCTestAAC3runyyF () from /usr/lib/swift/linux/libXCTest.so
#19 0x00007f1de0618eaf in $s6XCTest0A5SuiteC7performyyAA0A3RunCF () from /usr/lib/swift/linux/libXCTest.so
#20 0x00007f1de061a980 in $s6XCTestAAC3runyyF () from /usr/lib/swift/linux/libXCTest.so
#21 0x00007f1de0618eaf in $s6XCTest0A5SuiteC7performyyAA0A3RunCF () from /usr/lib/swift/linux/libXCTest.so
#22 0x00007f1de061a980 in $s6XCTestAAC3runyyF () from /usr/lib/swift/linux/libXCTest.so
#23 0x00007f1de0615d24 in $s6XCTest7XCTMain_9arguments9observerss5NeverOSayAA0A4CaseCm04testF5Class_SaySS_yAHKctG8allTeststG_SaySSGSayAA0A11Observation_pGtF () from /usr/lib/swift/linux/libXCTest.so
#24 0x00007f1de06156eb in $s6XCTest7XCTMainys5NeverOSayAA0A4CaseCm04testD5Class_SaySS_yAFKctG8allTeststGF () from /usr/lib/swift/linux/libXCTest.so
#25 0x000055a47e1bace1 in main () at /usr/src/Runtime/Tests/LinuxMain.swift:8
Happy to help if I can provide any further assistance.
Hi, Is it possible to list all swift protocols a class/struct is conforming?
Thanks in advance :)
Hey, is it possible to call a function (e.g. via String) of a class which is created by using createInstance()
?
I am creating test cases for Classes and check if a function was implemented correctly.
The idea is, that I provide an empty class, e.g. "Student" and a UML diagram which states that a Student has a function named "getStudentId()". A student now needs to implement the code.
Now I want to write a test case which tries to initialize the "Student" class and
1.) check if the function "getStudentId()" exists and
2.) check if it returns the correct value
I can check with
if let inferredClass = NSClassFromString("Student") {
...
} else {
print("no class found")
XCTFail("Student.swift is not implemented!")
}
that the class exists, but I cannot call the function.
Do you have an idea?
import Runtime
class A {
var value1: Float?
var value2: Double?
}
func test() {
let json = "{\"beans\":[{\"value1\":1.2,\"value2\":1.3},{\"value1\":2.2,\"value2\":2.3}]}"
if let data = json.data(using: .utf8) {
if let obj = try? JSONSerialization.jsonObject(with: data) {
let dictionary: Dictionary<String, Any> = obj as! Dictionary<String, Any>
if let beans = dictionary["beans"] as? Array<Dictionary<String, Any>> {
for beanDic in beans {
print("=========================================")
var bean = A()
if let info = try? typeInfo(of: type(of: bean)) {
let properties = info.properties()
for (key, value) in beanDic {
if properties.contains(key) {
if let property = try? info.property(named: key) {
print("set \(value)[type:\(type(of: value))] -> \(key)[type:\(property.type)]")
try? property.set(value: value, on: &bean)
}
}
}
}
print("-----------------------------------------")
print("\(type(of: bean.value1)):\(String(describing: bean.value1)), \(type(of: bean.value2)):\(String(describing: bean.value2))")
print("=========================================")
}
}
}
}
}
extension TypeInfo {
func properties() -> [String] {
var properties: [String] = []
for property in self.properties {
properties.append(property.name)
}
return properties
}
}
test()
got log:
=========================================
set 1.2[type:__NSCFNumber] -> value1[type:Optional<Float>]
set 1.3[type:__NSCFNumber] -> value2[type:Optional<Double>]
-----------------------------------------
Optional<Float>:nil, Optional<Double>:Optional(1.3)
=========================================
=========================================
set 2.2[type:__NSCFNumber] -> value1[type:Optional<Float>]
set 2.3[type:__NSCFNumber] -> value2[type:Optional<Double>]
-----------------------------------------
Optional<Float>:nil, Optional<Double>:Optional(2.3)
=========================================
Set Floating
value to Double
success. Set Floating
value to Float
fail.
Looks like something is wrong/different w/ the RC here.
Test Case '-[RuntimeMacTests.FactoryTests testClass]' started.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
* frame #0: 0x0000000108bfea2d libswiftCore.dylib`_swift_release_dealloc + 13
frame #1: 0x0000000103d362d4 RuntimeMacTests`FactoryTests.testClass(self=0x000000010390e4a0) at FactoryTests.swift:58
frame #2: 0x0000000103d368d5 RuntimeMacTests`@objc FactoryTests.testClass() at FactoryTests.swift:0
frame #3: 0x00007fffb972f0cc CoreFoundation`__invoking___ + 140
frame #4: 0x00007fffb972ef51 CoreFoundation`-[NSInvocation invoke] + 289
frame #5: 0x000000010034eb36 XCTest`__24-[XCTestCase invokeTest]_block_invoke.272 + 50
The new Package.swift
w/ the old dir layout doesn't run the tests anymore:
helge@ZeaPro Runtime (feature/macos2)*$ swift test
Compile Swift Module 'Runtime' (41 sources)
error: no tests found; create a target in the 'Tests' directory
Neither on macOS nor on Linux.
Getting some build errors in Swift 5, in Metadata.swift
and TypeInfo.swift
:
var typeInfoConvertible: TypeInfoConvertible
switch kind {
case .struct:
typeInfoConvertible = StructMetadata(type: type) // error: Missing argument for parameter 'metadata' in call
(Those errors pop up for every case
.)
That init is defined here: https://github.com/wickwirew/Runtime/blob/master/Sources/Runtime/Metadata/MetadataType.swift#L61
extension MetadataType {
// ...
init(type: Any.Type) {
let base = metadataPointer(type: type)
let metadata = base.advanced(by: valueWitnessTableOffset).raw.assumingMemoryBound(to: Layout.self)
self.init(type: type, metadata: metadata, base: base)
}
}
so I'm not sure why Swift 5 is refusing to see itโฆ
Note this happens when building the project from scratch, or when building as an SPM dependency.
typeInfoConvertible = StructMetadata.init(type: type)
.StructMetadata.swift
fails with a error: reference to invalid associated type 'Layout' of type 'StructMetadata'
, on the init declaration itself.typealias Layout = FooMetadata
declarations fixes those errors, and allowed me to remove the manual Foo.init(type: type)
โฆUndefined symbols for architecture x86_64:
"_swift_getFieldAt", referenced from:
closure #1 (Swift.Int) -> Runtime.PropertyInfo in Runtime.getProperties(of: Any.Type, offsets: [Swift.Int]) -> [Runtime.PropertyInfo] in GetFieldAt.o
ld: symbol(s) not found for architecture x86_64
swift build
from the terminal completes just fine; is this only a problem in Xcode? Is there a problem with my deployment target? I've rebuilt the .xcodeproj
with swift package generate-xcodeproj
โฆAnywho, here's the branch if you wanna take a look: https://github.com/noahemmet/Runtime/tree/swift5
The objCClassWrapper
case is not supported in createInstance(...)
. Specifically, URL
and NSURL
were both tried.
Interestingly, it does work if the property is Optional
.
(lldb) po kind
Runtime.Kind.objCClassWrapper
I'm trying to poking around the process of initialization of struct, looks like it does defined everything in Runtime/Factory/DefaultValue.swift, where it use a default construction value to create the instance.
struct TestStruct
{
var id : String
var name : String
var key_type : Int = 0
var min_value : Int?
var max_value : Int?
var choice_value : String?
var meta : String?
var is_using : Bool?
var activated : Bool = true
}
for activated property, no matter if I define it true or false, it will always stay as false, is there anyway I can get the default value I defined in the code? Maybe with some help of Mirror might help here?
The nominal type descriptor metadata is going away completely in 4.2. So there's no longer a way to get field names and types with the method this package uses. There is a new runtime method swift_getFieldAt to retrieve this data, however it uses C++ std::function and is not callable from Swift. You'd need to create a C++ wrapper that exposes a C API to use this. I've opened a PR on Swift to fix this (and make the method easily callable from Swift), but I'm not sure if they will do anything about it: apple/swift#15565. Thought I would share this info, thanks!
(copy of Zewo/Reflection#14)
how to get class/struct by name string?
struct Person{}
let info = typeInfo(of:"Person")
Hi,
I've been trying to get the GraphQL related https://github.com/GraphQLSwift/Graphiti project upgraded to Swift5 using the latest version of this library and came across a crash running the tests. The crash occurs in NominalMetadataType.genericArguments() when trying to get the TypeInfo for a simple Struct. I noticed that the Struct was declared nested within a closure.
It appears that NominalMetadataType.genericArguments() is coming back with a non-zero size results when it ought to be zero, and sometimes crashes.
To try and reproduce the problem within this project directly I've added a new test to MetadataTests.swift called testNestedStruct.
Please see this branch on my fork https://github.com/SportlabsTechnology/Runtime/tree/investigateCrash
Weirdly the addition of this new test causes the existing testStruct test to detect a non-zero amount of genericArguments and fail.
Any ideas ands help appreciated.
Thanks,
Mike.
Swift 4.1 apparently includes ABI changes that cause this library to fail completely. ~All tests crash with EXC_BAD_ACCESS.
Instructions for grabbing the latest Swift 4.1 snapshot here:
https://gist.github.com/tanner0101/cdb77c7f58d53af2ba2da5d39415389a
Hi, I tried to use the Runtime
and faced an issue with an object that inherits from the UIView
. Property offsets were wrong. When I dug deeper I found that offsets don't count the instance size of UIView
itself. I was able to workaround by modifying ClassMetadata
's toTypeInfo()
method like this:
mutating func toTypeInfo() -> TypeInfo {
var info = TypeInfo(metadata: self)
info.mangledName = mangledName()
info.properties = properties()
info.genericTypes = Array(genericArguments())
var _sc: ClassMetadata? = self
while var sc = _sc?.superClassMetadata()?.asClassMetadata() {
info.inheritance.append(sc.type)
let superInfo = sc.toTypeInfo()
info.properties.append(contentsOf: superInfo.properties)
_sc = sc
}
// Fix offset because of the obj-c inheritance if needed.
// Fix own properties only.
if let sc = _sc, let superClass = pointer.pointee.superClass as? AnyObject.Type {
let size = class_getInstanceSize(superClass) - class_getInstanceSize(NSObject.self)
for i in 0..<numberOfFields() {
info.properties[i].offset += numericCast(size) // <--- I also made the offset writable to simplify things
}
}
return info
}
I doubt it's the proper way of doing it and I was just curious If the issue can be fixed. Anyway, is it possible to add support for Swift objects inherited from objective-c classes?
class User: NSObject {
let id: Int = 0
let username: String = ""
let email: String = ""
}
let info = try typeInfo(of: User.self) // crash occurs
let property = try info.property(named: "username")
print("name: \(property.name)")
print("type: \(property.type)")
how fix this problem? and is there any plan to support objective-c language?
Hey @wickwirew,
Thanks for the great lib. However, it is crashing when I try to inspect a realm object. Could you please advise if I can do something about it?
Thanks in advance..
Here is a simple code:
`
@objcMembers
class TestModel: Object {
dynamic var test: String!
}
do {
let info = try typeInfo(of: TestModel.self) // crash
} catch let err {
print(err)
}
`
Hi there!
Thanks for an awesome library!
Lately I faced the problem with @propertyWrapper.
I have tried to create a @propertyWrapper:
@propertyWrapper public struct Blob {
public init(length: Int) {
self.length = length
self.wrappedValue = []
}
let length: Int
public var wrappedValue: [UInt8]
}
Then I used it in my struct just like this one:
struct MyStruct {
@Blob(length: 10) var blob: [UInt8]
...
}
And then I tried to createInstance , i got length = 0, I could not create instance with default length (=10). How to reflect the propertyWrapper and get the default length (= 10)?
Thanks
If you run
swift test --sanitize=address
You get the following error
Test Case '-[RuntimeTests.GetSetClassTests testGet]' started.
=================================================================
==68277==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffeeb309100 at pc 0x00010accef1f bp 0x7ffeeb3080f0 sp 0x7ffeeb3080e8
READ of size 8 at 0x7ffeeb309100 thread T0
#0 0x10accef1e in closure #1 in closure #1 in withExistentialValuePointer<A, B>(of:_:) Pointers.swift:54
#1 0x10accf1b1 in thunk for @callee_guaranteed (@unowned UnsafePointer<ExistentialContainer>) -> (@unowned ExistentialContainer, @error @owned Error) <compiler-generated>
#2 0x10accf493 in partial apply for thunk for @callee_guaranteed (@unowned UnsafePointer<ExistentialContainer>) -> (@unowned ExistentialContainer, @error @owned Error) <compiler-generated>
#3 0x7fff2ca94c9e in UnsafePointer.withMemoryRebound<A, B>(to:capacity:_:)+0xe (libswiftCore.dylib:x86_64+0x13ac9e)
#4 0x10accdc85 in closure #1 in withExistentialValuePointer<A, B>(of:_:) Pointers.swift:54
#5 0x10accecc1 in partial apply for closure #1 in withExistentialValuePointer<A, B>(of:_:) <compiler-generated>
#6 0x7fff2ca8ec9b in withUnsafePointer<A, B>(to:_:)+0xb (libswiftCore.dylib:x86_64+0x134c9b)
#7 0x10acccece in withExistentialValuePointer<A, B>(of:_:) Pointers.swift:53
#8 0x10accbf6d in withValuePointer<A, B>(of:_:) Pointers.swift:35
#9 0x10acc53ac in PropertyInfo.get(from:) PropertyInfo.swift:59
#10 0x10acc46fe in PropertyInfo.get<A>(from:) PropertyInfo.swift:50
#11 0x10acf03de in GetSetClassTests.testGet() GetSetClassTests.swift:63
#12 0x10acf0b72 in @objc GetSetClassTests.testGet() <compiler-generated>
#13 0x7fff2065856b in __invoking___+0x8b (CoreFoundation:x86_64h+0x6456b)
#14 0x7fff2065840f in -[NSInvocation invoke]+0x12e (CoreFoundation:x86_64h+0x6440f)
#15 0x105759bfc in __24-[XCTestCase invokeTest]_block_invoke_3+0x33 (XCTest:x86_64+0x30bfc)
#16 0x10582d3e8 in +[XCTSwiftErrorObservation observeErrorsInBlock:]+0x44 (XCTest:x86_64+0x1043e8)
#17 0x105759afe in __24-[XCTestCase invokeTest]_block_invoke_2+0x76 (XCTest:x86_64+0x30afe)
#18 0x1057f0b75 in -[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:]+0x40 (XCTest:x86_64+0xc7b75)
#19 0x105764a3d in -[XCTestCase assertInvalidObjectsDeallocatedAfterScope:]+0x3c (XCTest:x86_64+0x3ba3d)
#20 0x105759a44 in __24-[XCTestCase invokeTest]_block_invoke.262+0xc6 (XCTest:x86_64+0x30a44)
#21 0x1057d15d7 in -[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:]+0xb2 (XCTest:x86_64+0xa85d7)
#22 0x105759507 in -[XCTestCase invokeTest]+0x40c (XCTest:x86_64+0x30507)
#23 0x10575b17d in __26-[XCTestCase performTest:]_block_invoke_2+0x2a (XCTest:x86_64+0x3217d)
#24 0x1057d15d7 in -[XCTestCase(XCTIssueHandling) _caughtUnhandledDeveloperExceptionPermittingControlFlowInterruptions:caughtInterruptionException:whileExecutingBlock:]+0xb2 (XCTest:x86_64+0xa85d7)
#25 0x10575b0b4 in __26-[XCTestCase performTest:]_block_invoke.393+0x55 (XCTest:x86_64+0x320b4)
#26 0x1057e5462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#27 0x10575a8c1 in -[XCTestCase performTest:]+0x2bd (XCTest:x86_64+0x318c1)
#28 0x1057ac0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#29 0x10575264c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
#30 0x1057524c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
#31 0x105751d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
#32 0x1057e5462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#33 0x105751ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
#34 0x105751fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
#35 0x1057ac0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#36 0x10575264c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
#37 0x1057524c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
#38 0x105751d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
#39 0x1057e5462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#40 0x105751ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
#41 0x105751fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
#42 0x1057ac0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#43 0x10575264c in -[XCTestSuite runTestBasedOnRerunPolicy:testRun:]+0x9a (XCTest:x86_64+0x2964c)
#44 0x1057524c1 in __27-[XCTestSuite performTest:]_block_invoke+0xfc (XCTest:x86_64+0x294c1)
#45 0x105751d15 in __59-[XCTestSuite _performProtectedSectionForTest:testSection:]_block_invoke+0x17 (XCTest:x86_64+0x28d15)
#46 0x1057e5462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#47 0x105751ccc in -[XCTestSuite _performProtectedSectionForTest:testSection:]+0x9e (XCTest:x86_64+0x28ccc)
#48 0x105751fd0 in -[XCTestSuite performTest:]+0x121 (XCTest:x86_64+0x28fd0)
#49 0x1057ac0ae in -[XCTest runTest]+0x38 (XCTest:x86_64+0x830ae)
#50 0x1058052ce in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke_2+0x93 (XCTest:x86_64+0xdc2ce)
#51 0x1057e5462 in +[XCTContext runInContextForTestCase:markAsReportingBase:block:]+0xdb (XCTest:x86_64+0xbc462)
#52 0x105805233 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke+0x6e (XCTest:x86_64+0xdc233)
#53 0x1058053b4 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke.87+0x5f (XCTest:x86_64+0xdc3b4)
#54 0x10577b684 in -[XCTestObservationCenter _observeTestExecutionForBlock:]+0x144 (XCTest:x86_64+0x52684)
#55 0x105804fab in -[XCTTestRunSession runTestsAndReturnError:]+0x1d8 (XCTest:x86_64+0xdbfab)
#56 0x105736338 in -[XCTestDriver _runTests]+0x1d1 (XCTest:x86_64+0xd338)
#57 0x1057e01b7 in _XCTestMain+0x6b (XCTest:x86_64+0xb71b7)
#58 0x1048fa530 in main+0x194 (xctest:x86_64+0x100006530)
#59 0x7fff20598620 in start+0x0 (libdyld.dylib:x86_64+0x15620)
Address 0x7ffeeb309100 is located in stack of thread T0 at offset 256 in frame
#0 0x10acc4d0f in PropertyInfo.get(from:) PropertyInfo.swift:57
This frame has 4 object(s):
[32, 80) ''
[112, 120) ''
[144, 192) ''
[224, 256) '' <== Memory access at offset 256 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow Pointers.swift:54 in closure #1 in closure #1 in withExistentialValuePointer<A, B>(of:_:)
Shadow bytes around the buggy address:
0x1fffdd6611d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1fffdd6611e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1fffdd6611f0: 00 00 00 00 ca ca ca ca 00 00 00 cb cb cb cb cb
0x1fffdd661200: f1 f1 f1 f1 00 00 00 00 00 00 f2 f2 f2 f2 00 f2
0x1fffdd661210: f2 f2 00 00 00 00 00 00 f2 f2 f2 f2 00 00 00 00
=>0x1fffdd661220:[f3]f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
0x1fffdd661230: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1fffdd661240: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1fffdd661250: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1fffdd661260: 00 00 00 00 ca ca ca ca 00 00 cb cb cb cb cb cb
0x1fffdd661270: ca ca ca ca 00 00 cb cb cb cb cb cb f1 f1 f1 f1
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==68277==ABORTING
Looks like this is because you are assuming you can use the pointee
value outside of a withMemoryRebound
. Although if I fix this the address sanitizer just fails elsewhere.
On Linux both thread and address sanitizers fail
typeInfo(of:) crashes on optional types. e.g.
Runtime.RuntimeError.couldNotGetTypeInfo(type: Swift.Optional<Swift.Double>, kind: Runtime.Kind.optional)
Reading them as enum seems to work fine. Is there a reason why this is not supported?
Hey,
great library!
But is it also possible to call a function of an object with parameters?
This is a (possible stupid) question.
When modifying values didSet
is not called on the property, I assume this is due to the way the property itself is actually mutated and there is nothing that can be done about this?
Thanks!
@wickwirew Thanks a lot for adding #68 so quickly!
This code crashes with EXC_BAD_ACCESS (code=1, address=0x0) after merging #68:
import Foundation
import Runtime
let bridgedEnum = try typeInfo(of: ComparisonResult.self)
Environment: macOS 10.15.4, Swift 5.2 (Xcode 11.4).
I'm not sure if inspecting an enum bridged from C/Objective-C is supposed to work at all. Regardless, it should probably not cause a crash.
The crash occurs in EnumMetadata.cases()
in the call to record.pointee.type(genericContext:genericArguments:)
:
...
return Case(
name: record.pointee.fieldName(),
payloadType: record.pointee._mangledTypeName.offset == 0
? nil
: record.pointee.type( // Crashes here
genericContext: pointer.pointee.typeDescriptor,
genericArguments: genericVector
)
)
Inside FieldRecord.type(genericContext:genericArguments:)
, the crash occurs in swift_getTypeByMangledNameInContext
.
Is there a way to, given a name and a payload, manually construct an instance of an enum?
I'm looking some alternative to NSClassFromString but for structs :)
I'm not sure what's going on, but whatever it is I think I might be doing something wrong.
Here is a project that demonstrates the issue: https://github.com/FLEXTool/SwiftEX
Select the SwiftEXTests
scheme and run the testMirror()
test. It fails. My testing shows that for some reason, Value.self
inside the call to withValuePointer()
is seen as an existential instead of a class type. It appears to be correct, too, which tells me I'm the one doing something wrong.
The gist of what I'm trying to do is to wrap your APIs and make it work like a readwrite Mirror
, so that I don't have to fumble with .property(named:)
and property.set(value:on:)
or property.get(on:)
every time I want to do something.
Any help is appreciated!
Hey, first of all kudos for the great job with the library.
I do have a question regarding createInstance(of:constructor:)
api. It is currently building struct
and class
kinds. Is there any technical reason why instance creation for enum
is not supported, at least for ones with String
or Int
as RawValue
type? Since the method performs recursive calls on the properties, it fails once a nested enum property is encountered. Are there any plans in the near future to also support instance creation for enums, if this is technically possible of course?
Hi,
Could you tag the 1.0 and 1.1 releases as 1.0.0 and 1.1.0 please.
This will allow the swift package manager to resolve these versions, which it currently can't do.
Thanks!
Hello,
Is there a way to create an empty array with a generic type T where T would be obtained from type(of: something else)
?
So I'd like to do something like:
let type = type(of: X)
let arrayType = <...>
let arrayTypeInfo = try typeInfo(of: arrayType)
var array = arrayTypeInfo.createInstance()
Thank you for your help!
Hi @wickwirew ,
With #88, #94, #95, #99, #100, and #101 merged, it would be great if you can tag a new release so Swift Packages depending on Runtime can use a stable version instead of pointing to the current main branch.
I would suggest 2.2.3
as the release tag as there have been no breaking changes in the PRs mentioned above.
Thanks a lot for the time and effort in maintaining the Runtime project! ๐ช
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.