-
Notifications
You must be signed in to change notification settings - Fork 565
[illink] set $(StartupHookSupport)=false *only* for Release mode
#10670
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,13 +165,50 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args) | |
| } | ||
|
|
||
| args->propagateUncaughtExceptionFn = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, IntPtr, void>)&PropagateUncaughtException; | ||
|
|
||
| RunStartupHooksIfNeeded (); | ||
| SetSynchronizationContext (); | ||
| } | ||
|
|
||
| [DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern unsafe void xamarin_app_init (IntPtr env, delegate* unmanaged <int, int, int, IntPtr*, void> get_function_pointer); | ||
|
|
||
| static void RunStartupHooksIfNeeded () | ||
| { | ||
| // Return if startup hooks are disabled or not CoreCLR | ||
| if (!RuntimeFeature.IsCoreClrRuntime) | ||
| return; | ||
| if (!RuntimeFeature.StartupHookSupport) | ||
| return; | ||
|
|
||
| RunStartupHooks (); | ||
| } | ||
|
|
||
| [RequiresUnreferencedCode ("Uses reflection to access System.StartupHookProvider.")] | ||
| static void RunStartupHooks () | ||
| { | ||
| const string typeName = "System.StartupHookProvider"; | ||
| const string methodName = "ProcessStartupHooks"; | ||
|
|
||
| var type = typeof(object).Assembly.GetType (typeName, throwOnError: false); | ||
| if (type is null) { | ||
| RuntimeNativeMethods.monodroid_log (LogLevel.Warn, LogCategories.Default, | ||
| $"Could not load type '{typeName}'. Skipping startup hooks."); | ||
| return; | ||
| } | ||
|
|
||
| var method = type.GetMethod (methodName, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to use the [UnsafeAccessor] attribute to avoid using reflection here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you can only do that if a type is public, and it's internal in |
||
| BindingFlags.NonPublic | BindingFlags.Static, null, [ typeof(string) ], null); | ||
| if (method is null) { | ||
| RuntimeNativeMethods.monodroid_log (LogLevel.Warn, LogCategories.Default, | ||
| $"Could not load method '{typeName}.{methodName}'. Skipping startup hooks."); | ||
| return; | ||
| } | ||
|
|
||
| // Pass empty string for diagnosticStartupHooks parameter | ||
| // The method will read STARTUP_HOOKS from AppContext internally | ||
| method.Invoke (null, [ "" ]); | ||
| } | ||
|
|
||
| static void SetSynchronizationContext () => | ||
| SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
| using System.Reflection; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace SystemTests | ||
| { | ||
| [TestFixture] | ||
| public class StartupHookTest | ||
| { | ||
| [Test] | ||
| public void IsInitialized () | ||
| { | ||
| var type = Type.GetType ("StartupHook, StartupHook", throwOnError: true); | ||
| Assert.IsNotNull (type, "StartupHook type should be loaded"); | ||
|
|
||
| var property = type.GetProperty ("IsInitialized", BindingFlags.Public | BindingFlags.Static); | ||
| Assert.IsNotNull (property, "IsInitialized property should exist"); | ||
|
|
||
| var value = (bool) property.GetValue (null); | ||
| Assert.IsTrue (value, "StartupHook.Initialize() should have been called"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # Environment Variables and system properties | ||
| # debug.mono.log=gref,default | ||
| debug.mono.debug=1 | ||
| DOTNET_STARTUP_HOOKS=StartupHook |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System; | ||
|
|
||
| internal static class StartupHook | ||
| { | ||
| public static bool IsInitialized { get; private set; } | ||
|
|
||
| public static void Initialize () | ||
| { | ||
| Console.WriteLine ("StartupHook.Initialize() called"); | ||
|
|
||
| IsInitialized = true; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk" > | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(DotNetAndroidTargetFramework)</TargetFramework> | ||
| <SupportedOSPlatformVersion>$(AndroidMinimumDotNetApiLevel)</SupportedOSPlatformVersion> | ||
| <GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
| <AndroidGenerateResourceDesigner>false</AndroidGenerateResourceDesigner> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a test to ensure this method is really trimmed away for a Release build (or when startuphook support is not enabled)?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we run the new test in both trimmed, AOT, all runtimes, etc.
They have a line in dotnet/runtime to preserve it, too:
https://github.com/dotnet/runtime/blob/eaf635955da0410b94cb8607f6e598c51245f830/src/mono/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.xml#L656-L663