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

[Bug] MAUI templates crashing on iOS (Foundation.NSMutableData:AppendBytes) #61153

Closed
PureWeen opened this issue Oct 29, 2021 · 12 comments · Fixed by #61612
Closed

[Bug] MAUI templates crashing on iOS (Foundation.NSMutableData:AppendBytes) #61153

PureWeen opened this issue Oct 29, 2021 · 12 comments · Fixed by #61612
Assignees
Milestone

Comments

@PureWeen
Copy link
Member

PureWeen commented Oct 29, 2021

Description

Application is failing to start

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_UIntPtr <0x0006e>
	  at Foundation.NSMutableData:AppendBytes <0x0004a>
	  at Foundation.NSData:FromStream <0x000dc>
	  at Microsoft.Maui.EmbeddedFontLoader:LoadFont <0x0006a>
	  at Microsoft.Maui.FontRegistrar:LoadEmbeddedFont <0x000a0>
	  at Microsoft.Maui.FontRegistrar:LoadNativeAppFont <0x0003a>
	  at Microsoft.Maui.FontRegistrar:GetFont <0x0011a>
	  at Microsoft.Maui.FontManager:CleanseFontName <0x00022>
	  at Microsoft.Maui.FontManager:CreateFont <0x0015e>
	  at System.Collections.Concurrent.ConcurrentDictionary`2:GetOrAdd <0x000e2>
	  at Microsoft.Maui.FontManager:GetFont <0x00074>
	  at Microsoft.Maui.FontManager:GetFont <0x00032>
	  at Microsoft.Maui.LabelExtensions:UpdateFont <0x0002a>
	  at Microsoft.Maui.LabelExtensions:UpdateFont <0x0001c>
	  at Microsoft.Maui.Handlers.LabelHandler:MapFont <0x0003e>
	  at Microsoft.Maui.Controls.Label:MapFont <0x0002e>
	  at <>c__DisplayClass5_0:<Add>b__0 <0x00048>
	  at Microsoft.Maui.PropertyMapper:UpdatePropertyCore <0x00036>
	  at Microsoft.Maui.PropertyMapper:UpdateProperties <0x0005e>

Steps to Reproduce

dotnet workload install maui `
   --from-rollback-file https://aka.ms/dotnet/maui/preview.10.json `
   --source https://aka.ms/dotnet6/nuget/index.json `
   --source https://api.nuget.org/v3/index.json
  • dotnet new maui
  • launch the iOS app

Expected Behavior

doesn't crash

Actual Behavior

crashes

Basic Information

  • Version with issue: main
  • Last known good version: p10

This is acting like an issue with the iOS workloads. If you change the version of MAUI to be p.10 but still use the p.11 iOS workloads the app still crashes.

@mattleibow
Copy link
Member

mattleibow commented Oct 29, 2021

This feels like a bug in iOS since we are using the same code in maui - the only difference is the ios/runtime version.

@rolfbjarne
Copy link
Member

p.11 iOS workloads

The p11 iOS workload contains breaking changes that may very well cause this (the last version doesn't work for other reasons).

If you attach a binlog, I can see exactly which version you built with, and check if it's a breaking change or a bug.

@PureWeen
Copy link
Member Author

@rolfbjarne
Copy link
Member

This seems to be an interpreter bug. If I disable the interpreter, the sample works.

The crash occurs in [NSConcreteMutableData appendBytes:length:], and a bit of debugging in lldb reveals that the length argument is incorrect:

(lldb) bt
* thread dotnet/maui#1, name = 'tid_103', queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x00000001142d81bd Foundation`-[NSConcreteMutableData appendBytes:length:]
    frame dotnet/maui#1: 0x000000010cb0b719 libxamarin-dotnet-debug.dylib`xamarin_dyn_objc_msgSend at trampolines-x86_64-objc_msgSend.s:15
    frame dotnet/maui#2: 0x000000010d6e9aed libmonosgen-2.0.dylib`do_icall + 333
    frame dotnet/maui#3: 0x000000010d6e8a7d libmonosgen-2.0.dylib`do_icall_wrapper + 253
    [...]
(lldb) p/x $rcx
(unsigned long) $1 = 0x0000000155738648
(lldb) x/x $rcx
0x155738648: 0x00008000

The length is supposed to be 32768 (0x8000), but instead it's a pointer to that value.

@PureWeen PureWeen transferred this issue from dotnet/maui Nov 3, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Nov 3, 2021
@rolfbjarne rolfbjarne removed their assignment Nov 3, 2021
@rolfbjarne
Copy link
Member

CC @akoeplinger @steveisok

This is breaking the default MAUI template, so it's a rather high priority.

It's rather obnoxious to reproduce, because of other breaking changes in later commits that hides this problem, but I believe it should be possible by installing .NET 6 (RTM), and then this package: https://bosstoragemirror.blob.core.windows.net/wrench/main/40db84a71e10a549cdcd3dab746120dba3f84554/5378878/package/notarized/Microsoft.iOS.Bundle.15.0.101-preview.11.279.pkg

@steveisok steveisok added the os-ios Apple iOS label Nov 3, 2021
@steveisok
Copy link
Member

/cc @BrzVlad

@ghost
Copy link

ghost commented Nov 3, 2021

Tagging subscribers to this area: @BrzVlad
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Application is failing to start

=================================================================
	Managed Stacktrace:
=================================================================
	  at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_UIntPtr <0x0006e>
	  at Foundation.NSMutableData:AppendBytes <0x0004a>
	  at Foundation.NSData:FromStream <0x000dc>
	  at Microsoft.Maui.EmbeddedFontLoader:LoadFont <0x0006a>
	  at Microsoft.Maui.FontRegistrar:LoadEmbeddedFont <0x000a0>
	  at Microsoft.Maui.FontRegistrar:LoadNativeAppFont <0x0003a>
	  at Microsoft.Maui.FontRegistrar:GetFont <0x0011a>
	  at Microsoft.Maui.FontManager:CleanseFontName <0x00022>
	  at Microsoft.Maui.FontManager:CreateFont <0x0015e>
	  at System.Collections.Concurrent.ConcurrentDictionary`2:GetOrAdd <0x000e2>
	  at Microsoft.Maui.FontManager:GetFont <0x00074>
	  at Microsoft.Maui.FontManager:GetFont <0x00032>
	  at Microsoft.Maui.LabelExtensions:UpdateFont <0x0002a>
	  at Microsoft.Maui.LabelExtensions:UpdateFont <0x0001c>
	  at Microsoft.Maui.Handlers.LabelHandler:MapFont <0x0003e>
	  at Microsoft.Maui.Controls.Label:MapFont <0x0002e>
	  at <>c__DisplayClass5_0:<Add>b__0 <0x00048>
	  at Microsoft.Maui.PropertyMapper:UpdatePropertyCore <0x00036>
	  at Microsoft.Maui.PropertyMapper:UpdateProperties <0x0005e>

Steps to Reproduce

 dotnet workload install maui `
   --from-rollback-file https://aka.ms/dotnet/maui/main.json `
   --source https://aka.ms/dotnet6/nuget/index.json `
   --source https://pkgs.dev.azure.com/dnceng/public/_packaging/darc-pub-dotnet-runtime-6f411658/nuget/v3/index.json `
   --source https://pkgs.dev.azure.com/dnceng/public/_packaging/darc-pub-dotnet-emsdk-1ec2e17f/nuget/v3/index.json
  • dotnet new maui
  • launch the iOS app

Expected Behavior

doesn't crash

Actual Behavior

crashes

Basic Information

  • Version with issue: main
  • Last known good version: p10

This is acting like an issue with the iOS workloads. If you change the version of MAUI to be p.10 but still use the p.11 iOS workloads the app still crashes.

Author: PureWeen
Assignees: -
Labels:

untriaged, area-Codegen-Interpreter-mono, os-ios

Milestone: -

@BrzVlad BrzVlad self-assigned this Nov 8, 2021
@lewing lewing added this to the 6.0.x milestone Nov 10, 2021
@marek-safar marek-safar removed the untriaged New issue has not been triaged by the area owner label Nov 10, 2021
@mattleibow mattleibow changed the title [Bug] MAUI templates crashing on iOS [Bug] MAUI templates crashing on iOS (Foundation.NSMutableData:AppendBytes) Nov 15, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 15, 2021
@SamMonoRT SamMonoRT reopened this Nov 22, 2021
@SamMonoRT
Copy link
Member

Closed by mistake -- reopening again

@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 25, 2021
Redth added a commit to dotnet/maui that referenced this issue Dec 8, 2021
Instead of loading font files into an NSData object via a .NET managed stream, load them by filename via the CGDataProvider directly (no need to marshal the file byte data).

This also mitigates the issue addressed in dotnet/runtime#61153 for the specific instance encountered by loading fonts this way.
Redth added a commit to dotnet/maui that referenced this issue Dec 8, 2021
Instead of loading font files into an NSData object via a .NET managed stream, load them by filename via the CGDataProvider directly (no need to marshal the file byte data).

This also mitigates the issue addressed in dotnet/runtime#61153 for the specific instance encountered by loading fonts this way.
Redth added a commit to dotnet/maui that referenced this issue Dec 8, 2021
Instead of loading font files into an NSData object via a .NET managed stream, load them by filename via the CGDataProvider directly (no need to marshal the file byte data).

This also mitigates the issue addressed in dotnet/runtime#61153 for the specific instance encountered by loading fonts this way.
@filipnavara
Copy link
Member

filipnavara commented Dec 18, 2021

I am not quite sure how it's possible but this bug exists in the Hot Restart code in VS 2022 when running legacy Xamarin code. I'd expect the prebuilt app to use legacy Mono and behave identically to VS 2019 but that's not the case. It crashes with a similar stack trace for the same underlying reasons:

Stack trace
	  at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_UIntPtr <0x00022>
	  at Foundation.NSMutableData:AppendBytes <0x00054>
	  at Foundation.NSData:FromStream <0x0010c>
	  at <LoadImageAsync>d__1:MoveNext <0x00160>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x00052>
	  at Xamarin.Forms.Platform.iOS.StreamImagesourceHandler:LoadImageAsync <0x00062>
	  at <GetNativeImageAsync>d__10:MoveNext <0x000c4>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x00052>
	  at Xamarin.Forms.Platform.iOS.ImageElementManager:GetNativeImageAsync <0x00052>
	  at <SetImage>d__8:MoveNext <0x00530>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x00052>
	  at Xamarin.Forms.Platform.iOS.ImageElementManager:SetImage <0x0005c>
	  at <SetImage>d__6:MoveNext <0x00034>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x00052>
	  at Xamarin.Forms.Platform.iOS.ImageRenderer:SetImage <0x0004e>
	  at <TrySetImage>d__5:MoveNext
 <0x0002c>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x00052>
	  at Xamarin.Forms.Platform.iOS.ImageRenderer:TrySetImage <0x0004e>
	  at <OnElementChanged>d__3:MoveNext <0x000a4>
	  at System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start <0x00052>
	  at Xamarin.Forms.Platform.iOS.ImageRenderer:OnElementChanged <0x0004a>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:SetElement <0x002a2>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement <0x00010>
	  at Xamarin.Forms.Platform.iOS.Platform:CreateRenderer <0x00080>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:OnChildAdded <0x00084>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Load <0x0005a>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:SetElement <0x00228>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement <0x00010>
	  at Xamarin.Forms.Platform.iOS.Platform
:CreateRenderer <0x00080>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:OnChildAdded <0x00084>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Load <0x0005a>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:SetElement <0x00228>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement <0x00010>
	  at Xamarin.Forms.Platform.iOS.Platform:CreateRenderer <0x00080>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:OnChildAdded <0x00084>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Load <0x0005a>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:SetElement <0x00228>
	  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1:Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement <0x00010>
	  at Xamarin.Forms.Platform.iOS.Platform:CreateRenderer <0x00080>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:OnChildAdded <0x00084>
	  at Xamarin.Forms.Platform.iOS.VisualElementPackager:Load <0x0005a>
	  at Xa
marin.Forms.Platform.iOS.PageRenderer:ViewDidLoad <0x0010c>
	  at System.Object:runtime_invoke_direct_void__this__ <0x0008e>
	  at <unknown> <0xffffffff>
	  at ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper <0x0001a>
	  at UIKit.UIViewController:get_View <0x0006c>
	  at Xamarin.Forms.Platform.iOS.PageRenderer:get_NativeView <0x00016>
	  at Xamarin.Forms.Platform.iOS.PageRenderer:SetElement <0x00096>
	  at Xamarin.Forms.Platform.iOS.Platform:CreateRenderer <0x00080>
	  at MailClient.Mobile.iOS.Renderers.CustomNavigationRenderer:CreateViewControllerForPage <0x00030>
	  at <OnPushAsync>d__53:MoveNext <0x00050>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:Start <0x0004a>
	  at MailClient.Mobile.iOS.Renderers.CustomNavigationRenderer:OnPushAsync <0x00062>
	  at MailClient.Mobile.iOS.Renderers.CustomNavigationRenderer:PushPageAsync <0x00018>
	  at MailClient.Mobile.iOS.Renderers.CustomNavigationRenderer:OnPushRequested <0x0006c>
	  at System.EventHandler`1:invoke_void_object_TEventArgs <0x00080>
	
  at <PushAsyncInner>d__88:MoveNext <0x00224>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x0004a>
	  at MailClient.Mobile.UI.Navigation.CustomNavigationPage:PushAsyncInner <0x00066>
	  at <PushAsync>d__65:MoveNext <0x0029a>
	  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start <0x0004a>
	  at MailClient.Mobile.UI.Navigation.CustomNavigationPage:PushAsync <0x00066>
	  at NavigationImpl:OnPushAsync <0x0001e>
	  at Xamarin.Forms.Internals.NavigationProxy:PushAsync <0x00038>
	  at Xamarin.Forms.Internals.NavigationProxy:OnPushAsync <0x00064>
	  at Xamarin.Forms.Internals.NavigationProxy:PushAsync <0x00038>
	  at <ComposeButton_Clicked>d__52:MoveNext <0x0005a>
	  at System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start <0x0004a>
	  at MailClient.Mobile.UI.Pages.MailsPage:ComposeButton_Clicked <0x00062>
	  at <Module>:invoke_void_object_EventArgs <0x00080>
	  at Xamarin.Forms.ImageButton:PropagateUpClicked <0x00028>
	  at Xamarin.Forms.ButtonElement:ElementClicked <0x000
5e>
	  at Xamarin.Forms.ImageButton:SendClicked <0x0000a>
	  at Xamarin.Forms.Platform.iOS.ButtonElementManager:OnButtonTouchUpInside <0x0002e>
	  at Xamarin.Forms.Platform.iOS.ButtonElementManager:TouchUpInside <0x00032>
	  at <Module>:invoke_void_object_EventArgs <0x000a6>
	  at UIKit.UIControlEventProxy:Activated <0x00030>
	  at System.Object:runtime_invoke_direct_void__this__ <0x0003a>
	  at <unknown> <0xffffffff>
	  at UIKit.UIApplication:UIApplicationMain <0x000b8>
	  at UIKit.UIApplication:Main <0x000b2>
	  at MailClient.Mobile.iOS.Application:Main <0x00012>
	  at <Module>:runtime_invoke_direct_void_string[] <0x00092>
	  at <unknown> <0xffffffff>
	  at System.Reflection.RuntimeMethodInfo:InternalInvoke <0x00030>
	  at System.Reflection.RuntimeMethodInfo:Invoke <0x000fc>
	  at System.Reflection.MethodBase:Invoke <0x0001a>
	  at Xamarin.iOS.HotRestart.Application:Run <0x001fa>
	  at Xamarin.PreBuilt.iOS.Applications:Main <0x0005a>
	  at <Module>:runtime_invoke_direct_void_string[] <0x00092>

The Mono backport (mono/mono#21317) was closed without an explanation. I assumed it's just not needed because the changes in runtime/X.iOS should not be affecting it but apparently some change did affect it.

Additional reports of the same issue:

cc @mauroa

@filipnavara
Copy link
Member

filipnavara commented Dec 18, 2021

The X.iOS 15.0.0.6 release from 23/09/2021 still used void_objc_msgSend_IntPtr_nuint prototype at the P/Invoke code site. Some change later (possibly dotnet/macios@c3574a5) unified it with .NET 6 code path and it started using void_objc_msgSend_IntPtr_UIntPtr. This triggered the interpreter bug both in MonoVM and legacy Mono. Hence, the backport to legacy Mono is necessary or the change in X.iOS has to be conditionally reverted.

cc @BrzVlad @rolfbjarne

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jan 13, 2022
Bump mono to get fix for dotnet/runtime#61153.

New commits in mono/mono:

* mono/mono@45efaa3b6f9 [interp] Remove hack for nint/nfloat
* mono/mono@a5d1934898b transform sgen_get_descriptor to parallel safe version in job_major_mod_union_preclean

Diff: https://github.com/mono/mono/compare/b8d7525156acaecf311ba468147caa74d8c190f6..45efaa3b6f9bb6aa5e6e2ef82d08ad849a40a34c
rolfbjarne added a commit to dotnet/macios that referenced this issue Jan 14, 2022
Bump mono to get fix for dotnet/runtime#61153.

New commits in mono/mono:

* mono/mono@45efaa3b6f9 [interp] Remove hack for nint/nfloat
* mono/mono@a5d1934898b transform sgen_get_descriptor to parallel safe version in job_major_mod_union_preclean

Diff: https://github.com/mono/mono/compare/b8d7525156acaecf311ba468147caa74d8c190f6..45efaa3b6f9bb6aa5e6e2ef82d08ad849a40a34c
vs-mobiletools-engineering-service2 pushed a commit to vs-mobiletools-engineering-service2/xamarin-macios that referenced this issue Jan 14, 2022
Bump mono to get fix for dotnet/runtime#61153.

New commits in mono/mono:

* mono/mono@45efaa3b6f9 [interp] Remove hack for nint/nfloat
* mono/mono@a5d1934898b transform sgen_get_descriptor to parallel safe version in job_major_mod_union_preclean

Diff: https://github.com/mono/mono/compare/b8d7525156acaecf311ba468147caa74d8c190f6..45efaa3b6f9bb6aa5e6e2ef82d08ad849a40a34c
dalexsoto pushed a commit to dotnet/macios that referenced this issue Jan 17, 2022
Bump mono to get fix for dotnet/runtime#61153.

New commits in mono/mono:

* mono/mono@45efaa3b6f9 [interp] Remove hack for nint/nfloat
* mono/mono@a5d1934898b transform sgen_get_descriptor to parallel safe version in job_major_mod_union_preclean

Diff: https://github.com/mono/mono/compare/b8d7525156acaecf311ba468147caa74d8c190f6..45efaa3b6f9bb6aa5e6e2ef82d08ad849a40a34c

Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
@rolfbjarne
Copy link
Member

The X.iOS 15.0.0.6 release from 23/09/2021 still used void_objc_msgSend_IntPtr_nuint prototype at the P/Invoke code site. Some change later (possibly xamarin/xamarin-macios@c3574a5) unified it with .NET 6 code path and it started using void_objc_msgSend_IntPtr_UIntPtr. This triggered the interpreter bug both in MonoVM and legacy Mono. Hence, the backport to legacy Mono is necessary or the change in X.iOS has to be conditionally reverted.

This has been fixed, and the next service release will include the fix.

@BrzVlad I believe this bug can be closed now.

@BrzVlad BrzVlad closed this as completed Jan 24, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants