Skip to content

[mono] Preserve FirstChanceExceptionEventArgs ctor #68235

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

Merged
merged 2 commits into from
Apr 20, 2022

Conversation

akoeplinger
Copy link
Member

@akoeplinger akoeplinger commented Apr 19, 2022

The runtime is creating the exception so we need to preserve it.
See dotnet/android#6626

While looking at this I noticed that FirstChanceExceptionEventArgs is in ExceptionNotification.cs for some reason, fixed the filename to match the type name.

Use DynamicDependency for preserving FirstChanceExceptionEventArgs and UnhandledExceptionEventArgs

The runtime is creating the exception so we need to preserve it.
See dotnet/android#6626
@marek-safar
Copy link
Contributor

marek-safar commented Apr 20, 2022

We try to avoid using descriptors if possible. I think a better fix would be to add it as DynamicDependencyAttribute to

public static event EventHandler<FirstChanceExceptionEventArgs>? FirstChanceException;

@akoeplinger
Copy link
Member Author

@marek-safar I tried but it doesn't compile:

error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations.

@marek-safar
Copy link
Contributor

I tried but it doesn't compile:

Looks like bad API design but it should be possible to use the underlying target. Try this instead

[field: DynamicDependency ()]

@akoeplinger
Copy link
Member Author

@marek-safar that worked :) I switched over UnhandledExceptionEventArgs too

@akoeplinger
Copy link
Member Author

/backport to release/6.0

@github-actions
Copy link
Contributor

Started backporting to release/6.0: https://github.com/dotnet/runtime/actions/runs/2196008785

@akoeplinger akoeplinger merged commit a6202a8 into dotnet:main Apr 20, 2022
@akoeplinger akoeplinger deleted the mono-firstchance branch April 20, 2022 18:46
directhex pushed a commit to directhex/runtime that referenced this pull request Apr 21, 2022
The runtime is creating the exception so we need to preserve it.
See dotnet/android#6626

While looking at this I noticed that `FirstChanceExceptionEventArgs` is in ExceptionNotification.cs for some reason, fixed the filename to match the type name.

Use DynamicDependency for preserving `FirstChanceExceptionEventArgs` and `UnhandledExceptionEventArgs`
jonpryor pushed a commit to dotnet/android that referenced this pull request Apr 21, 2022
)

Fixes: #6626

Context: dotnet/runtime#68235

If you create a .NET 6 app whichs ubscribes to the
[`AppDomain.FirstChanceException`][0] event:

	partial class MainActivity {
	    protected override void OnCreate (Bundle? savedInstanceState)
	    {
	        AppDomain.CurrentDomain.FirstChanceException += (o, e) => {
	            Console.WriteLine ("First chance exception!");
	       };
	    }
	}

and has a way to cause an exception to be thrown:

	partial class MainActivity {
	    public override void OnBackPressed() => throw new Exception ("wee?");
	}

and is built in Release configuration with Trimming enabled,

then the app will crash when the exception is thrown (in the above
case, by launching the app and then pressing the Back button):

	E companyname.fo: * Assertion at /__w/1/s/src/mono/mono/metadata/object.c:4179, condition `ctor' not met

The cause of the crash is that the linker removes the
[`FirstChanceExceptionEventArgs(Exception)`][1] constructor.

This was fixed in dotnet/runtime#68235 by using appropriate
`[DynamicDependency]` attributes.  However, this runtime fix is
unlikely to be released before our next RC.

Add a workaround by updating
`src/Microsoft.Android.Sdk.ILLink/PreserveLists/System.Private.CoreLib.xml`
so that the linker won't remove the
`FirstChanceExceptionEventArgs(Exception)` constructor.

[0]: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-6.0
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.firstchanceexceptioneventargs.-ctor?view=net-6.0
jonathanpeppers pushed a commit to dotnet/android that referenced this pull request Apr 21, 2022
)

Fixes: #6626

Context: dotnet/runtime#68235

If you create a .NET 6 app whichs ubscribes to the
[`AppDomain.FirstChanceException`][0] event:

	partial class MainActivity {
	    protected override void OnCreate (Bundle? savedInstanceState)
	    {
	        AppDomain.CurrentDomain.FirstChanceException += (o, e) => {
	            Console.WriteLine ("First chance exception!");
	       };
	    }
	}

and has a way to cause an exception to be thrown:

	partial class MainActivity {
	    public override void OnBackPressed() => throw new Exception ("wee?");
	}

and is built in Release configuration with Trimming enabled,

then the app will crash when the exception is thrown (in the above
case, by launching the app and then pressing the Back button):

	E companyname.fo: * Assertion at /__w/1/s/src/mono/mono/metadata/object.c:4179, condition `ctor' not met

The cause of the crash is that the linker removes the
[`FirstChanceExceptionEventArgs(Exception)`][1] constructor.

This was fixed in dotnet/runtime#68235 by using appropriate
`[DynamicDependency]` attributes.  However, this runtime fix is
unlikely to be released before our next RC.

Add a workaround by updating
`src/Microsoft.Android.Sdk.ILLink/PreserveLists/System.Private.CoreLib.xml`
so that the linker won't remove the
`FirstChanceExceptionEventArgs(Exception)` constructor.

[0]: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-6.0
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.firstchanceexceptioneventargs.-ctor?view=net-6.0
@ghost ghost locked as resolved and limited conversation to collaborators May 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants