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

[mono][swift] App crashes in AOT + interpreter setup as it cannot find System.Runtime.InteropServices.Swift types #105245

Closed
ivanpovazan opened this issue Jul 22, 2024 · 4 comments · Fixed by #105408
Assignees
Labels
Milestone

Comments

@ivanpovazan
Copy link
Member

Description

maccatalyst-arm64 (probably ios-arm64) application crashes when it references an assembly that must be interpreted in Release mode.
This is achieved by adding:

<MtouchInterpreter>-all,MyLib</MtouchInterpreter>

in the project file, referencing a method from MyLib assembly which has to be interpreted and building/running the app.

Tested with dotnet 9.0.100-preview.6.24328.19

Repro

  1. Checkout https://github.com/ivanpovazan/swift-error-repro
  2. dotnet publish -c Release -r maccatalyst-arm64 -t:Run MyMacCat/MyMacCat.csproj

Expected behavior

App displays ID1 ID2

Actual behavior

App crashes with:

=================================================================
	Native Crash Reporting
=================================================================
Got a SIGSEGV 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:
=================================================================
	0x104ee83bc - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_dump_native_crash_info
	0x104e9f82c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_handle_native_crash
	0x104e2bea4 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_sigsegv_signal_handler_debug
	0x18a1df584 - /usr/lib/system/libsystem_platform.dylib : _sigtramp
	0x104d7fd84 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_class_create_ptr
	0x104d7fd84 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_class_create_ptr
	0x104efb41c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : interp_method_compute_offsets
	0x104f0d690 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : generate
	0x104f0d250 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_interp_transform_method
	0x104ee9470 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : interp_create_method_pointer
	0x104e2acb8 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : jit_compile_method_with_opt
	0x104e2a53c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_jit_compile_method
	0x104ea1c60 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : common_call_trampoline
	0x104ea17cc - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_magic_trampoline
	0x104cbc04c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : generic_trampoline_jit
	0x1047f016c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : MyMacCat_AppDelegate_FinishedLaunching_UIKit_UIApplication_Foundation_NSDictionary
	0x1047f08c4 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : MyMacCat_AppDelegate___Registrar_Callbacks___callback_2_MyMacCat_AppDelegate_FinishedLaunching_intptr_intptr_intptr_intptr_intptr_
	0x1047f2524 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : wrapper_native_to_managed_MyMacCat_AppDelegate___Registrar_Callbacks___callback_2_MyMacCat_AppDelegate_FinishedLaunching_intptr_intptr_intptr_intptr_intptr_
	0x104f2b0e8 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : -[AppDelegate application:didFinishLaunchingWithOptions:]
	0x1b970b904 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:]
	0x1b970adc4 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:]
	0x1b97090ac - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIApplication _runWithMainScene:transitionContext:completion:]
	0x1b9708be0 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:]
	0x1b97056c8 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : _UIScenePerformActionsWithLifecycleActionMask
	0x1b970852c - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : __101-[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]_block_invoke
	0x1b97082b0 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:]
	0x1b970780c - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]
	0x1b9707508 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:]
	0x1b9705ca4 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke
	0x1b9f61ab8 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:fromCurrentState:actions:completion:]
	0x1ba054504 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : _UISceneSettingsDiffActionPerformChangesWithTransitionContextAndCompletion
	0x1b9705850 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]
	0x1b9a2245c - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke.243
	0x1b9704cec - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:]
	0x1b9704b5c - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIScene scene:didUpdateWithDiff:transitionContext:completion:]
	0x1b96fa864 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIApplication workspace:didCreateScene:withTransitionContext:completion:]
	0x1b96fa5f0 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:]
	0x19fda8ad8 - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : -[FBSScene _callOutQueue_didCreateWithTransitionContext:completion:]
	0x19fdd1284 - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : __92-[FBSWorkspaceScenesClient createSceneWithIdentity:parameters:transitionContext:completion:]_block_invoke.139
	0x19fd93048 - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:]
	0x19fdd0f98 - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : __92-[FBSWorkspaceScenesClient createSceneWithIdentity:parameters:transitionContext:completion:]_block_invoke
	0x189ffe3e8 - /usr/lib/system/libdispatch.dylib : _dispatch_client_callout
	0x18a001e80 - /usr/lib/system/libdispatch.dylib : _dispatch_block_invoke_direct
	0x19fd92f5c - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__
	0x19fdf10b4 - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : -[FBSMainRunLoopSerialQueue _targetQueue_performNextIfPossible]
	0x19fdf11e8 - /System/Library/PrivateFrameworks/FrontBoardServices.framework/Versions/A/FrontBoardServices : -[FBSMainRunLoopSerialQueue _performNextFromRunLoopSource]
	0x18a28e4d8 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
	0x18a28e46c - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRunLoopDoSource0
	0x18a28e1dc - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRunLoopDoSources0
	0x18a28cdc8 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRunLoopRun
	0x18a28c434 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : CFRunLoopRunSpecific
	0x194a3019c - /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox : RunCurrentEventLoopInMode
	0x194a2ffd8 - /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox : ReceiveNextEventCommon
	0x194a2fd30 - /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox : _BlockUntilNextEventMatchingListInModeWithFilter
	0x18daebd68 - /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit : _DPSNextEvent
	0x18e2e1808 - /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit : -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]
	0x18dadf09c - /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit : -[NSApplication run]
	0x18dab62e0 - /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit : NSApplicationMain
	0x18dd09a30 - /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit : +[NSWindow _savedFrameFromString:]
	0x1a3556f40 - /System/Library/PrivateFrameworks/UIKitMacHelper.framework/Versions/A/UIKitMacHelper : UINSApplicationMain
	0x1b96e3570 - /System/iOSSupport/System/Library/PrivateFrameworks/UIKitCore.framework/Versions/A/UIKitCore : UIApplicationMain
	0x104cee160 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : xamarin_UIApplicationMain
	0x104844db0 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : wrapper_managed_to_native_UIKit_UIApplication_xamarin_UIApplicationMain_int_intptr_intptr_intptr_intptr_
	0x104813bd4 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr
	0x104813dc8 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : UIKit_UIApplication_Main_string___System_Type_System_Type
	0x1047f009c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : Program__Main__string__
	0x104badd28 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr
	0x104e2f178 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_jit_runtime_invoke
	0x104dcf520 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_runtime_invoke_checked
	0x104dd572c - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_runtime_exec_main_checked
	0x104e83314 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : mono_jit_exec
	0x104cf8870 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : xamarin_main
	0x104f2adb0 - /Users/ivan/repros/swift-error-repro/MyMacCat/bin/Release/net9.0-maccatalyst/maccatalyst-arm64/MyMacCat.app/Contents/MacOS/MyMacCat : main
	0x189e260e0 - /usr/lib/dyld : start

=================================================================
	Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x104d72310):0x104d72300  ff 83 00 d1 fd 7b 01 a9 fd 43 00 91 c0 19 00 b4  .....{...C......
0x104d72310  08 08 40 b9 08 5d 10 53 09 05 00 51 3f 75 00 71  ..@..].S...Q?u.q
0x104d72320  e8 19 00 54 6a 0e 00 f0 4a a1 35 91 8b 00 00 10  ...Tj...J.5.....
0x104d72330  4c 69 69 38 6b 09 0c 8b 60 01 1f d6 00 00 40 f9  Lii8k...`.....@.

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at <unknown> <0xffffffff>
	  at MyMacCat.AppDelegate:FinishedLaunching <0x0004b>
	  at __Registrar_Callbacks__:callback_2_MyMacCat_AppDelegate_FinishedLaunching <0x00123>
	  at __Registrar_Callbacks__:callback_2_MyMacCat_AppDelegate_FinishedLaunching <0x00073>
	  at <unknown> <0xffffffff>
	  at UIKit.UIApplication:xamarin_UIApplicationMain <0x00007>
	  at UIKit.UIApplication:UIApplicationMain <0x00063>
	  at UIKit.UIApplication:Main <0x00117>
	  at Program:<Main>$ <0x0003b>
	  at System.Object:runtime_invoke_dynamic <0x00127>
=================================================================

Workaround

Full AOT compilation does not reproduce this problem. This can be achieved by commenting out:

    <!-- <MtouchInterpreter>-all,MyLib</MtouchInterpreter> -->

Initial investigation

App seems to crash at:

MonoClass *swift_error_ptr = mono_class_create_ptr (m_class_get_this_arg (swift_error));
because swift_error is NULL. By inspecting the trimmed SPC.dll it seems like all the types from: System.Runtime.InteropServices.Swift got removed during the build, which means we need to rethink the assumptions in these cases.

@ivanpovazan ivanpovazan added this to the 9.0.0 milestone Jul 22, 2024
Copy link
Contributor

Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to 'os-maccatalyst': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

@ivanpovazan
Copy link
Member Author

/cc: @BrzVlad @kotlarmilos

@lambdageek
Copy link
Member

lambdageek commented Jul 22, 2024

The swift classes can be removed by the IL linker if they're unused.

Looking at

MonoClass *swift_error = mono_class_try_get_swift_error_class ();
MonoClass *swift_error_ptr = mono_class_create_ptr (m_class_get_this_arg (swift_error));

it needs to deal with a few things:

  1. mono_class_try_get_swift_error_class is cheap to call repeatedly - it has an internal static variable for a cache; so this is fine,
  2. but mono_class_create_ptr is a little bit expensive (it has to do a hash table lookup for the cache); it also can't really deal with a NULL input
  3. m_class_get_this_arg is not ok to call on a NULL swift_error.

So you need your own cheaper cache for swift_error_ptr (or move it into a conditional that's only used if the calling convention cares about Swift types)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants