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

[net6][iOS] Ran out of trampolines of type 1 running on x64 simulator #68808

Closed
rolfbjarne opened this issue May 3, 2022 · 28 comments
Closed
Assignees

Comments

@rolfbjarne
Copy link
Member

From @jeromelaban on Fri, 29 Apr 2022 15:13:56 GMT

Steps to Reproduce

  1. Build and run a large app with the x64 iOS simulator

Expected Behavior

The app runs without failing.

Actual Behavior

Ran out of trampolines of type 1 in '7B5B7D87-1D1E-43DB-9BB1-12062FA2B779/data/Containers/Bundle/Application/7599D867-453F-4D78-99CA-4EDBDB515629/My.app/System.Private.CoreLib.dll' (limit 4096)

Environment

.NET 6 Mobile RC2

Build Logs

On demand.

Other details

Note that this does not reproduce on a physical device.

It tried changing this:

<MonoAOTCompilerDefaultAotArguments Include="nimt-trampolines=4000" />
<MonoAOTCompilerDefaultAotArguments Include="ntrampolines=10000" />
<MonoAOTCompilerDefaultAotArguments Include="nrgctx-fetch-trampolines=256" />
<MonoAOTCompilerDefaultAotArguments Include="ngsharedvt-trampolines=8192" />
<MonoAOTCompilerDefaultAotArguments Include="nftnptr-arg-trampolines=8000" />
<MonoAOTCompilerDefaultAotArguments Include="nrgctx-trampolines=31000" />		

But since the AOT compiler does not seem to run in this context, it's likely not using these parameters.

Related: #53033

Copied from original issue xamarin/xamarin-macios#14855

@rolfbjarne
Copy link
Member Author

From @rolfbjarne on Fri, 29 Apr 2022 15:19:55 GMT

We're going to need a way to reproduce this, would it be possible to get access to your project?

@rolfbjarne
Copy link
Member Author

From @msftbot[bot] on Fri, 29 Apr 2022 15:20:13 GMT

Hi @jeromelaban. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@rolfbjarne
Copy link
Member Author

From @jeromelaban on Fri, 29 Apr 2022 15:22:39 GMT

Yes, it's this one, from this commit: unoplatform/Uno.Gallery@233b06d.

You'll need to build Uno.Gallery/Uno.Gallery.Mobile/Uno.Gallery.Mobile.csproj for net6.0-ios. I'm doing this on windows, if that matters.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 3, 2022
@rolfbjarne
Copy link
Member Author

From @jeromelaban on Fri, 29 Apr 2022 15:26:55 GMT

One additional information I discovered, if I disable the interpreter, the application runs properly. The hint was in the stack trace:

	  at <unknown> <0xffffffff>
	  at System.Object:__icall_wrapper_ves_icall_mono_delegate_ctor_interp <0xffffffff>
	  at <>c__DisplayClass0_0:<Build>b__0 <0x003c2>
	  at Uno.Material.__Resources._mergedpages_c69cbc6d79c924ed978294ad194451a9_mergedpagesRDSC61:Build <0x005b8>
	  at <>c:<Get_3_289>b__126_2 <0x0001e>

@rolfbjarne
Copy link
Member Author

From @rolfbjarne on Tue, 03 May 2022 11:52:04 GMT

I can reproduce, here's a crash report: https://gist.github.com/rolfbjarne/8038c203eec8a33fc580bee41866d75b

I believe this is because:

  • We enable the AOT compiler when using the interpreter, because we need to AOT-compile System.Private.CoreLib.dll.
  • We're not in FullAOT mode (we're interpreting), so this block isn't executed:

if (mono_aot_only && !mono_llvm_only) {
char *code;
find_amodule_symbol (amodule, "specific_trampolines_page", (gpointer *)&code);
amodule->use_page_trampolines = code != NULL;
/*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
}

  • Which means we'll never get here, because USE_PAGE_TRAMPOLINES will be false:

code = (guint8 *)get_new_rgctx_trampoline_from_page (addr, ctx);

  • And instead we'll go here:

code = (guint8 *)get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);

  • And eventually end up here:

g_error ("Ran out of trampolines of type %d in '%s' (limit %d)\n",
tramp_type, image ? image->name : MONO_ASSEMBLY_CORLIB_NAME, amodule->info.num_trampolines [tramp_type]);

The current workaround is to disable the interpreter.

I'm moving to dotnet/runtime, since I believe that's where the issue is.

@rolfbjarne
Copy link
Member Author

CC @steveisok @vargaz

@vargaz
Copy link
Contributor

vargaz commented May 3, 2022

Is the x64 ios simulator config running in aot-only mode ?

@vargaz
Copy link
Contributor

vargaz commented May 3, 2022

The infinite trampoline stuff is not implemented on x64, so either we need to increase the default number of rgctx trampolines we emit into the corlib aot image, or enable the user to set it.

@steveisok
Copy link
Member

@rolfbjarne maybe setting the defaults higher when you aot would be the way to go?

@rolfbjarne
Copy link
Member Author

The infinite trampoline stuff is not implemented on x64

Would this be possible? What could be the timeline be (.NET 7?)?

@rolfbjarne maybe setting the defaults higher when you aot would be the way to go?

Yes, we can either allow the user to increment these values (I didn't know we didn't have infinite trampolines on all our platforms, so it's currently not possible to increment the number of trampolines manually), or set a higher default (or do both).

That said, the final fix is to implement infinite trampolines.

@rolfbjarne
Copy link
Member Author

Yes, we can either allow the user to increment these values (I didn't know we didn't have infinite trampolines on all our platforms, so it's currently not possible to increment the number of trampolines manually), or set a higher default (or do both).

I've filed xamarin/xamarin-macios#14887 so we can look at this in the meantime.

@vargaz
Copy link
Contributor

vargaz commented May 5, 2022

The infinite trampoline code might conflict with future apple security measures, so adding it could lead to problems in the future. The question is why is this configuration which rans on a JIT capable system is using aot trampolines in the first place.

@vargaz
Copy link
Contributor

vargaz commented May 5, 2022

Looks like we have a MONO_AOT_MODE_INTERP and a MONO_AOT_MODE_INTERP_ONLY configuration, the former is based on full aot so it will try to use aot trampolines, the latter doesn't.

@steveisok
Copy link
Member

This mode was introduced primarily to get interp mode working on arm64 where JIT fallback isn't realistic.

/cc @lambdageek

@vargaz
Copy link
Contributor

vargaz commented May 5, 2022

@rolfbjarne Would it be possible to use MONO_AOT_MODE_INTERP_ONLY instead MONO_AOT_MODE_INTERP on x64 ?
I.e. emit

#ifdef      __x86_64__
mono_jit_set_aot_mode (MONO_AOT_MODE_INTERP_ONLY)
#else
mono_jit_set_aot_mode (MONO_AOT_MODE_INTERP)
#endif 

We might need to modify the ios x64 simulator runtimes to not disable the jit.

@lambdageek
Copy link
Member

We need FullAOT trampolines for the interpreter on arm64 because apple doesn't allow JITing on the M1 hardware.

For the x64 Simulator we FullAOT the interpreter trampolines mostly because we can just have a single configuration for both M1 and x64.

The infinite trampoline code might conflict with future apple security measures, so adding it could lead to problems in the future. The question is why is this configuration which rans on a JIT capable system is using aot trampolines in the first place.

Do you mean on the M1 or x64? I doubt Apple would introduce additional security measures on x64 that are not already present on the M1. And if they did decide to lock down x64, I think JITing would be disallowed before page remapping.

@rolfbjarne
Copy link
Member Author

@rolfbjarne Would it be possible to use MONO_AOT_MODE_INTERP_ONLY instead MONO_AOT_MODE_INTERP on x64 ?

I can certainly try and see if it works (I'm assuming it can work without too much effort, since it did work at some point), but the question (which I can't really answer) is: do we want to do that, or implement infinite trampolines on x86_64?

@steveisok
Copy link
Member

@rolfbjarne Would it be possible to use MONO_AOT_MODE_INTERP_ONLY instead MONO_AOT_MODE_INTERP on x64 ?

I can certainly try and see if it works (I'm assuming it can work without too much effort, since it did work at some point), but the question (which I can't really answer) is: do we want to do that, or implement infinite trampolines on x86_64?

My vote: I would rather run the same configuration on both x64 and arm64.

@rolfbjarne
Copy link
Member Author

@rolfbjarne Would it be possible to use MONO_AOT_MODE_INTERP_ONLY instead MONO_AOT_MODE_INTERP on x64 ?

I can certainly try and see if it works

It didn't, it aborts with:

error: Failed to load AOT module 'System.Private.CoreLib' while running in aot-only mode: not compiled with --aot=llvmonly.

=================================================================
	Native Crash Reporting
=================================================================
Got a SIGABRT 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:
=================================================================
	0x10b22fa45 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_dump_native_crash_info
	0x10b1cf6ee - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_handle_native_crash
	0x10b22f352 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : sigabrt_signal_handler
	0x7fff6fafbdfd - /usr/lib/system/libsystem_platform.dylib : _sigtramp
	0x600003965800 - Unknown
	0x7fff2010b6b7 - /Applications/Xcode_13.3.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_c.dylib : abort
	0x10b0b69c9 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_log_write_os_log
	0x10b27d628 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : monoeg_g_logv
	0x10b27d7c2 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : monoeg_g_log
	0x10b1b2ea9 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : load_aot_module
	0x10afe14cd - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_assembly_request_load_from
	0x10afe0ecd - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_assembly_request_open
	0x10afe2ed0 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_assembly_open
	0x10aa675f1 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libxamarin-dotnet-debug.dylib : xamarin_assembly_preload_hook
	0x10afe36a7 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : invoke_assembly_preload_hook
	0x10afe3889 - /Users/rolf/Library/Developer/CoreSimulator/Devices/D21038CB-BC03-4F58-886D-D8106F87F512/data/Containers/Bundle/Application/3F282177-5E2C-4838-987B-507586C1BC52/MyInterpretedApp.app/libmonosgen-2.0.dylib : mono_assembly_load_corlib

@vargaz
Copy link
Contributor

vargaz commented May 12, 2022

Not sure what is happening here, MONO_AOT_MODE_INTERP_ONLY is the same as running with --interp.

@vargaz
Copy link
Contributor

vargaz commented May 12, 2022

Maybe there is a mismatch in the headers, this is the definition of MonoAotMode:
https://github.com/dotnet/runtime/blob/main/src/native/public/mono/jit/details/jit-types.h#L15
MONO_AOT_MODE_INTERP_ONLY has value 8.

@rolfbjarne
Copy link
Member Author

Maybe there is a mismatch in the headers,

That was the issue, once I fixed the value, using MONO_AOT_MODE_INTERP_ONLY works.

Now we just need to decide whether that's what we want to do :)

@vargaz
Copy link
Contributor

vargaz commented May 20, 2022

Implementing infinite trampolines is a lot of work, so I'd prefer this approach.

@steveisok steveisok removed the untriaged New issue has not been triaged by the area owner label Jul 5, 2022
@steveisok
Copy link
Member

Closing as I believe MONO_AOT_MODE_INTERP_ONLY is the way to go. Please reopen if anyone disagrees.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jul 8, 2022
… for x86_64.

The 'MONO_AOT_MODE_INTERP' mode is based on full AOT, which requires infinite
trampoline code to not potentially run out of trampolines. However, that mode
isn't implemented for x86_64, so in that case use 'MONO_AOT_MODE_INTERP_ONLY',
which will use the JIT as a fallback.

Ref: dotnet/runtime#68808

Fixes: xamarin#14887
rolfbjarne added a commit to xamarin/xamarin-macios that referenced this issue Jul 12, 2022
… for x86_64. (#15429)

The 'MONO_AOT_MODE_INTERP' mode is based on full AOT, which requires infinite
trampoline code to not potentially run out of trampolines. However, that mode
isn't implemented for x86_64, so in that case use 'MONO_AOT_MODE_INTERP_ONLY',
which will use the JIT as a fallback.

Ref: dotnet/runtime#68808

Fixes: #14887
@SimenCTan
Copy link

SimenCTan commented Jul 20, 2022

I have the same issue when run maui on ios.
according to this Pull request
how to add --aot arguments in MtouchExtraArgs when building a .NET project @rolfbjarne

ERR Info:

Ran out of trampolines of type 0 in
 '*/CoreSimulator/Devices/F06D2CC0-2EEA-4C06-B672-721C0A277327/
data/Containers/Bundle/Application/C9EA73F5-9801-4F0C-93D3-302108B93249/
*.app/System.Private.CoreLib.dll'  (limit 4096). 
See http://docs.xamarin.com/ios/troubleshooting for instructions on how to fix this condition.

@SimenCTan
Copy link

After updating dotnet sdk 6.0.400 this issue still exist,is there an update ?
@vargaz @rolfbjarne

@rolfbjarne
Copy link
Member Author

@SimenCTan The fix didn't make it to 6.0.400, and right now it's slated for the .NET 7 release later this fall.

Unfortunately the only workaround at the moment is to disable the interpreter.

@SimenCTan
Copy link

SimenCTan commented Aug 18, 2022

@SimenCTan The fix didn't make it to 6.0.400, and right now it's slated for the .NET 7 release later this fall.

Unfortunately the only workaround at the moment is to disable the interpreter.

thx, The temporary solution is add
<UseInterpreter Condition="'$(Configuration)' == 'Debug' AND '$(TargetFramework)' != 'net6.0-ios'">True</UseInterpreter> to the .csproj file

@ghost ghost locked as resolved and limited conversation to collaborators Sep 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants