Skip to content
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

Unable to bind native iOS framework that uses Async/Await in iOS 13 #13605

Closed
4brunu opened this issue Dec 20, 2021 · 12 comments · Fixed by xamarin/XamarinComponents#1311
Closed
Assignees
Labels
iOS Issues affecting Xamarin.iOS
Milestone

Comments

@4brunu
Copy link

4brunu commented Dec 20, 2021

Steps to Reproduce

  1. In the sample project open SwiftAsyncAwaitExample.sln
  2. run SwiftAsyncAwaitExample in iOS 13 or iOS 14.
  3. check the console for the Native Crash Reporting.
=================================================================
	Native Crash Reporting
=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Native stacktrace:
=================================================================
	0x1048ea2a5 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : mono_dump_native_crash_info
	0x1048de6ee - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : mono_handle_native_crash
	0x1048f15f9 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : mono_sigsegv_signal_handler_debug
	0x7fff51421e2d - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_platform.dylib : _sigtramp
	0x6000030b68c0 - Unknown
	0x7fff50b7029b - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZN5swift8Demangle9Demangler16demangleOperatorEv
	0x7fff50b6eaa8 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZN5swift8Demangle9Demangler12demangleTypeEN4llvm9StringRefE
	0x7fff50b5c375 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZL30swift_getTypeByMangledNameImplN5swift15MetadataRequestEN4llvm9StringRefEPKPKvNSt3__18functionIFPKNS_14TargetMetadataINS_9InProcessEEEjjEEENS8_IFPKNS_18TargetWitnessTableISA_EESD_jEEE
	0x7fff50b5a0e2 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZN5swift26swift_getTypeByMangledNameENS_15MetadataRequestEN4llvm9StringRefEPKPKvNSt3__18functionIFPKNS_14TargetMetadataINS_9InProcessEEEjjEEENS8_IFPKNS_18TargetWitnessTableISA_EESD_jEEE
	0x7fff50b5a1d9 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : swift_getTypeByMangledNameInContext
	0x10529f157 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/Frameworks/SwiftAsyncAwaitFramework.framework/SwiftAsyncAwa : __swift_instantiateConcreteTypeFromMangledName
	0x10529f59c - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/Frameworks/SwiftAsyncAwaitFramework.framework/SwiftAsyncAwa : __swift_instantiateConcreteTypeFromMangledName
	0x104b3b599 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : xamarin_dyn_objc_msgSend
	0x10aded290 - Unknown

=================================================================
	Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x7fff50b57e37):0x7fff50b57e27  0f 40 84 f6 74 0a 48 8b 3b be ed 00 00 00 eb 21  .@..t.H.;......!
0x7fff50b57e37  8b 02 89 c1 80 e1 1f 80 f9 03 74 0b 80 f9 04 75  ..........t....u
0x7fff50b57e47  1f 66 b8 ef 00 eb 04 66 b8 85 00 48 8b 3b 0f b7  .f.....f...H.;..
0x7fff50b57e57  f0 48 83 c4 08 5b 41 5e 41 5f 5d e9 d9 71 01 00  .H...[A^A_]..q..

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at ApiDefinitions.Messaging:void_objc_msgSend_IntPtr <0x000ef>
	  at SwiftAsyncAwaitFramework.SwiftAsyncAwait:FunctionThatCallsPrivateAsyncFunctionWithClosure <0x0028a>
	  at SwiftAsyncAwaitExample.AppDelegate:FinishedLaunching <0x001d2>
	  at <Module>:runtime_invoke_bool__this___object_object <0x00205>
	  at <unknown> <0xffffffff>
	  at UIKit.UIApplication:UIApplicationMain <0x00254>
	  at UIKit.UIApplication:Main <0x00172>
	  at SwiftAsyncAwaitExample.Application:Main <0x00092>
	  at <Module>:runtime_invoke_void_object <0x00198>
=================================================================

Expected Behavior

With the release of Xcode 13.2, Async/Await is now backwards compatible and works on iOS 13 and above.
So Async/Await is expected to work on iOS 13 and above.

Actual Behavior

Xamarin.iOS throws a Native Crash Reporting when calling a method of a native iOS framework that uses async/await on iOS 13.

Environment

=== Visual Studio Enterprise 2019 for Mac ===

Version 8.10.16 (build 2)
Installation UUID: 95a87c0e-b7a0-472f-8ea5-7c2fba2958ae
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000162

=== Mono Framework MDK ===

Runtime:
	Mono 6.12.0.162 (2020-02/2ca650f1f62) (64-bit)
	Package version: 612000162

=== Roslyn (Language Service) ===

3.10.0-4.21269.26+029847714208ebe49668667c60ea5b0a294e0fcb

=== NuGet ===

Version: 5.9.0.7134

=== .NET SDK (x64) ===

SDK: /usr/local/share/dotnet/sdk/5.0.404/Sdks
SDK Versions:
	5.0.404
	3.1.416
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	5.0.13
	3.1.22

=== .NET Core 3.1 SDK ===

SDK: 3.1.416

=== .NET 5.0 SDK ===

SDK: 5.0.404

=== Xamarin.Profiler ===

Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 12.0.0.3 (Visual Studio Enterprise)
Commit: xamarin-android/d16-11/f0e3c2d
Android SDK: /Applications/android-sdk
	Supported Android versions:
		5.1 (API level 22)
		8.0 (API level 26)
		8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 31.0.3
SDK Build Tools Version: 31.0.0

Build Information: 
Mono: c633fe9
Java.Interop: xamarin/java.interop/d16-11@476bb5b
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.35.4@85460d3
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@87af37b

=== Eclipse Temurin JDK ===

Java SDK: Not Found

=== Android SDK Manager ===

Version: 16.10.0.13
Hash: 1b81df5
Branch: remotes/origin/d16-10
Build date: 2021-11-12 01:17:32 UTC

=== Android Device Manager ===

Version: 16.10.0.15
Hash: 89dcc0b
Branch: remotes/origin/d16-10
Build date: 2021-11-12 01:17:52 UTC

=== Xamarin Designer ===

Version: 16.11.0.39
Hash: cd672761d
Branch: remotes/origin/d16-11
Build date: 2021-10-06 21:09:18 UTC

=== Apple Developer Tools ===

Xcode 13.2.1 (19586)
Build 13C100

=== Xamarin.Mac ===

Xamarin.Mac not installed. Can't find /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/Version.

=== Xamarin.iOS ===

Version: 15.4.0.0 (Visual Studio Enterprise)
Hash: 8fc41ae82
Branch: xcode13.2
Build date: 2021-12-09 01:07:56-0500

=== Build Information ===

Release ID: 810160002
Git revision: 6dbc7886fa608147f685a7df3912249400c84716
Build date: 2021-12-14 15:54:37-05
Build branch: release-8.10

=== Operating System ===

Mac OS X 12.1.0
Darwin 21.2.0 Darwin Kernel Version 21.2.0
    Sun Nov 28 20:28:54 PST 2021
    root:xnu-8019.61.5~1/RELEASE_X86_64 x86_64

Build Logs

https://gist.github.com/4brunu/55e4c18a9b8455cb492c01a84026ccdc

Example Project (If Possible)

The directory SwiftAsyncAwaitFramework contains a native iOS Framework that uses Async/Await on iOS 13 and above, and also a native sample app SwiftAsyncAwaitAppExample, to test the Async/Await on iOS 13 and above, and it works.

The directory SwiftAsyncAwaitBinding contains the xamarin binding of the native iOS library.

The directory SwiftAsyncAwaitExample contains a Xamarin app to test the xamarin binding of the native iOS library.

SwiftAsyncAwaitExample.zip

@rachelkang
Copy link
Contributor

Hi, @4brunu - I don't believe that Swift functions taking closures are p-invokable. When @stephen-hawley returns next year, he will be able to confirm that. Until then, we will leave this issue open.

@chamons
Copy link
Contributor

chamons commented Dec 20, 2021

See https://forums.swift.org/t/concurrency-interoperability-with-objective-c/41616 in particular the section on using blocks. You can in theory bind that to C# with our BlockLiteral, but it's a bit complicated.

If you want to go down that road consider dropping by https://aka.ms/dotnet-discord #apple if you have questions.

@4brunu
Copy link
Author

4brunu commented Dec 20, 2021

Hi @rachelkang and @chamons, thanks for reaching out 🙂

Maybe I didn't explain myself well.

The issue here is not related to calling a Swift function that takes a closure.

The issue is related to the use of Async/Await with the new backwards compatible mode, and running on iOS 13 and 14.

To sum up, the issue is not calling Async/Await code from C#, the issue is that Xamarin seems that is not injecting the new support library related to the new concurrency mode, therefore the code above works fine on iOS 15 but it crashes on iOS 13 and 14, because Async/Await was only introduced in iOS 15, and only works on iOS 13 and 14 when the new support library related to the new concurrency mode is present.

This is the code that I have inside my native iOS Framework.

@objc public class SwiftAsyncAwait: NSObject {
    @objc public static func functionThatCallsPrivateAsyncFunction(closure: @escaping (String, Error?) -> Void) {
        Task {
            do {
                let result = try await asyncFunction()
                closure(result, nil)
            } catch {
                closure("It didn't work!", error)
            }
        }
    }

    private static func asyncFunction() async throws -> String {
        let seconds = 3
        let duration = UInt64(seconds * 1_000_000_000)
        try await Task.sleep(nanoseconds: duration)

        return "It Work's!"
    }
}

The function functionThatCallsPrivateAsyncFunction is compatible with Objective-c.

But when I call the function functionThatCallsPrivateAsyncFunction from Xamarin iOS, it works on iOS 15, but it crashes on iOS 13 and 14 with the following error.

=================================================================
	Native Crash Reporting
=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Native stacktrace:
=================================================================
	0x1048ea2a5 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : mono_dump_native_crash_info
	0x1048de6ee - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : mono_handle_native_crash
	0x1048f15f9 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : mono_sigsegv_signal_handler_debug
	0x7fff51421e2d - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_platform.dylib : _sigtramp
	0x6000030b68c0 - Unknown
	0x7fff50b7029b - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZN5swift8Demangle9Demangler16demangleOperatorEv
	0x7fff50b6eaa8 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZN5swift8Demangle9Demangler12demangleTypeEN4llvm9StringRefE
	0x7fff50b5c375 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZL30swift_getTypeByMangledNameImplN5swift15MetadataRequestEN4llvm9StringRefEPKPKvNSt3__18functionIFPKNS_14TargetMetadataINS_9InProcessEEEjjEEENS8_IFPKNS_18TargetWitnessTableISA_EESD_jEEE
	0x7fff50b5a0e2 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : _ZN5swift26swift_getTypeByMangledNameENS_15MetadataRequestEN4llvm9StringRefEPKPKvNSt3__18functionIFPKNS_14TargetMetadataINS_9InProcessEEEjjEEENS8_IFPKNS_18TargetWitnessTableISA_EESD_jEEE
	0x7fff50b5a1d9 - /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib : swift_getTypeByMangledNameInContext
	0x10529f157 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/Frameworks/SwiftAsyncAwaitFramework.framework/SwiftAsyncAwa : __swift_instantiateConcreteTypeFromMangledName
	0x10529f59c - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/Frameworks/SwiftAsyncAwaitFramework.framework/SwiftAsyncAwa : __swift_instantiateConcreteTypeFromMangledName
	0x104b3b599 - /Users/user/Library/Developer/CoreSimulator/Devices/B9B09834-5F56-4157-84F2-7693D7B75FE7/data/Containers/Bundle/Application/0DA75164-3DCC-4120-8178-FF577ECDE608/SwiftAsyncAwaitExample.app/SwiftAsyncAwaitExample : xamarin_dyn_objc_msgSend
	0x10aded290 - Unknown

=================================================================
	Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x7fff50b57e37):0x7fff50b57e27  0f 40 84 f6 74 0a 48 8b 3b be ed 00 00 00 eb 21  .@..t.H.;......!
0x7fff50b57e37  8b 02 89 c1 80 e1 1f 80 f9 03 74 0b 80 f9 04 75  ..........t....u
0x7fff50b57e47  1f 66 b8 ef 00 eb 04 66 b8 85 00 48 8b 3b 0f b7  .f.....f...H.;..
0x7fff50b57e57  f0 48 83 c4 08 5b 41 5e 41 5f 5d e9 d9 71 01 00  .H...[A^A_]..q..

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at ApiDefinitions.Messaging:void_objc_msgSend_IntPtr <0x000ef>
	  at SwiftAsyncAwaitFramework.SwiftAsyncAwait:FunctionThatCallsPrivateAsyncFunctionWithClosure <0x0028a>
	  at SwiftAsyncAwaitExample.AppDelegate:FinishedLaunching <0x001d2>
	  at <Module>:runtime_invoke_bool__this___object_object <0x00205>
	  at <unknown> <0xffffffff>
	  at UIKit.UIApplication:UIApplicationMain <0x00254>
	  at UIKit.UIApplication:Main <0x00172>
	  at SwiftAsyncAwaitExample.Application:Main <0x00092>
	  at <Module>:runtime_invoke_void_object <0x00198>
=================================================================

I think the issue is that for the Swift 5.5 to support Async/Await in older versions of iOS (13 and 14) it needs a new support library related to the new concurrency mode.

But I think Xamarin is not injecting this new support library, therefore the code above works correctly on iOS 15, but not on iOS 13 and 14.

All the Async/Await code is only used internally inside the iOS native framework and only referenced in a privately inside the Framework, and never referenced directly by Xamarin iOS.

@rachelkang
Copy link
Contributor

@4brunu Thanks for elaborating. Could you try using https://www.nuget.org/packages/Xamarin.iOS.SwiftRuntimeSupport/ and let us know how that works for you?

@4brunu
Copy link
Author

4brunu commented Dec 20, 2021

@rachelkang I just tested it and it still crashes.
I think we are in the correct path to fix this, but the last version of the Swift Runtime Support in that nuget package is from 2019, the current one was release last week, on 13 Dec 2021.
So maybe that package needs to be updated?

@4brunu
Copy link
Author

4brunu commented Dec 20, 2021

I got this working by enable in Xcode the setting ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES.

This confirms that this issue is caused by the the missing Swift standard libraries.

So updating the package https://www.nuget.org/packages/Xamarin.iOS.SwiftRuntimeSupport/ to the last version should fix this issue.

@chamons
Copy link
Contributor

chamons commented Dec 20, 2021

Please give this version a try. I had some major trouble updating that library, which has been unmaintained for a few years, but I think that should work.

Xamarin.iOS.SwiftRuntimeSupport.0.2.1.nupkg.zip

You may need to add it to your local nuget feed, I can dig up instructions if needed on that.

@4brunu
Copy link
Author

4brunu commented Dec 20, 2021

It worked!
Could you please publish that update?

@chamons
Copy link
Contributor

chamons commented Dec 20, 2021

I'll look into it, but to be honest all of the relevant people may be on vacation, so there may be some delay.

@4brunu
Copy link
Author

4brunu commented Dec 20, 2021

Sure, thanks for helping 🙂

@chamons chamons added this to the Future milestone Dec 22, 2021
@chamons chamons added the iOS Issues affecting Xamarin.iOS label Dec 22, 2021
@chamons chamons self-assigned this Dec 22, 2021
@balayeluvolu
Copy link

balayeluvolu commented Feb 9, 2022

I'll look into it, but to be honest all of the relevant people may be on vacation, so there may be some delay.

First of all thanks for fix. Any tentative plan to publish this? We are having the similar issue code sign failing after upgrading to xcode 13.2.1 and your 0.2.1 package addressed that as well. Waiting for the published version. Please let us know and Thank you once again!

@balayeluvolu
Copy link

balayeluvolu commented Feb 22, 2022

I'll look into it, but to be honest all of the relevant people may be on vacation, so there may be some delay.

First of all thanks for fix. Any tentative plan to publish this? We are having the similar issue code sign failing after upgrading to xcode 13.2.1 and your 0.2.1 package addressed that as well. Waiting for the published version. Please let us know and Thank you once again!

any update on the publish date @chamons? Appreciate your help on this. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Apr 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Issues affecting Xamarin.iOS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants