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

AndroidStripILAfterAOT likely causing "InvalidProgramException: Invalid IL code in ..." #104156

Closed
tipa opened this issue Dec 5, 2023 · 15 comments
Assignees
Milestone

Comments

@tipa
Copy link

tipa commented Dec 5, 2023

Android application type

.NET Android (net7.0-android, net8.0-android, etc.)

Affected platform version

NET 8.0.100

Description

After adding the AndroidStripILAfterAOT=true property to my apps, I started seeing crashes (reported in AppCenter) from my apps in production, e.g.

System.InvalidProgramException: Invalid IL code in System.Drawing.Color:FromArgb (int,int,int,int): method body is empty. 

ColorConverters.FromHex(String hex)
MyApp.Project.get_ColorObject()
MyApp.Project.get_ColorStruct()
MyApp.TimerControlWidget.BuildRemoteViews(Context context, Bundle options)
MyApp.TimerControlWidget.OnUpdate(Context context, AppWidgetManager appWidgetManager, Int32[] appWidgetIds)
Android.Appwidget.AppWidgetProvider.n_OnUpdate_Landroid_content_Context_Landroid_appwidget_AppWidgetManager_arrayI(IntPtr jnienv, IntPtr native__this, IntPtr native_context, IntPtr native_appWidgetManager, IntPtr native_appWidgetIds)
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_V(_JniMarshal_PPLLL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2)

One on of the three crash reports of that app states "Crashed after: 1s", indicating that the app was just cold-booted before crashing.

In another app:

System.InvalidProgramException: Invalid IL code in SQLite.SQLiteConnection:.cctor (): method body is empty.

MyApp.DatabaseHelper.GetConnection()
MyApp.DatabaseHelper.get_Db()
MyApp.DatabaseHelper.GetEntry(DateTime date)
MyApp.StackRemoteViewsFactory.GetViewAt(Int32 position)
Android.Widget.RemoteViewsService.IRemoteViewsFactoryInvoker.n_GetViewAt_I(IntPtr jnienv, IntPtr native__this, Int32 position)
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPI_L(_JniMarshal_PPI_L callback, IntPtr jnienv, IntPtr klazz, Int32 p0)

All 4/4 crash reports I got for that app state "Crashed after: 0s".

All seven crash reports were sent from (different) Samsung devices.
All seven crash reports originated from the apps widgets.

Steps to Reproduce

My .csproj look something like this:

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <AndroidUseDefaultAotProfile>False</AndroidUseDefaultAotProfile>
    <AndroidEnableProfiledAot>True</AndroidEnableProfiledAot>
    <AndroidStripILAfterAOT>True</AndroidStripILAfterAOT>
    <AndroidLinkTool>r8</AndroidLinkTool>
    <TrimMode>full</TrimMode>
  </PropertyGroup>
  <ItemGroup>
    <AndroidAotProfile Include="custom.aprof" />
  </ItemGroup>

Did you find any workaround?

No response

Relevant log output

No response

@jonathanpeppers
Copy link
Member

@tipa are you able to share the .apk file for this one? It would be useful for us to inspect the IL & AOT images inside.

@tipa
Copy link
Author

tipa commented Dec 5, 2023

Sure - I shared the file with you in a private message

@jpobst jpobst added the bug label Dec 5, 2023
@jonathanpeppers
Copy link
Member

What is odd the app has the method it is complaining about:
image

This is inside System.Drawing.Primitives.dll.

I tried something like this so far, which seems to work:

@jonathanpeppers
Copy link
Member

Ok, @tipa sent me another .apk and it does thave this method stripped, as expected:

image

But I think the behavior is:

  • The app works when @tipa tests it locally
  • @tipa has some crash reports on the Google Play console
  • A [BroadcastReceiver] may be involved

So, it makes me wonder if the crash occurs if the app is launched in a special way (broadcast).

@charlesroddie
Copy link

charlesroddie commented Dec 13, 2023

We are getting this issue:

Invalid IL code in Polly.PolicyBuilder:.ctor (Polly.ExceptionPredicate): method body is empty.
Invalid IL code in System.Net.Http.Headers.MediaTypeHeaderValue:.ctor (string): method body is empty.

@FoggyFinder @kevcrooks to see if we can give an apk

@tipa
Copy link
Author

tipa commented Dec 15, 2023

So, it makes me wonder if the crash occurs if the app is launched in a special way (broadcast).

For me, it more and more feels like this is broadcast-related. I now also see some crash reports that originate from another BroadcastReceiver, which is implemented as follows:

[BroadcastReceiver(Exported = false)]
[IntentFilter(new[] { Intent.ActionBootCompleted, "android.intent.action.QUICKBOOT_POWERON" })]
sealed class BootReceiver : BroadcastReceiver
{
    public override async void OnReceive(Context context, Intent intent)
    {
         // do stuff
    }
}

ColorConverters.FromHex(String hex)
...
MyApp.Helper.SetupNotifications()
MyApp.BootReceiver.OnReceive(Context context, Intent intent)
System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0()
Java.Lang.Thread.RunnableImplementor.Run()
Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz)

And because the crashes don't happen repeatedly for the same users, it could be some race condition

@grendello
Copy link
Contributor

Long ago there was an issue with isolated broadcast receivers IIRC. The issue was that even though the app was running in the background, the receiver ran in a separate process which didn't have the XA+mono runtime properly initialized and, thus, everything burned and crashed. It doesn't appear to be the case here, but I wonder if it might be a race condition as suggested by @tipa, where the broadcast is received before the main app is fully initialized (e.g. during our very first call to the managed land, the static method JNIEnv.Initialize).

@charlesroddie
Copy link

We are getting this issue:

Invalid IL code in Polly.PolicyBuilder:.ctor (Polly.ExceptionPredicate): method body is empty.
Invalid IL code in System.Net.Http.Headers.MediaTypeHeaderValue:.ctor (string): method body is empty.

@jonathanpeppers can we send you an aab file for this and where should we send it to?

@jonathanpeppers
Copy link
Member

@tipa's problem doesn't occur every time, and we think it is a race condition related to a broadcast receiver.

@charlesroddie if you see this issue 100% (as if it crashes every time), can you file a new issue here?

The IL strip code lives here:

@charlesroddie
Copy link

The error doesn't occur every time and we only encountered it via an appcenter report. Ours does not involve a BroadcastReceiver, but our app can be launched directly or more rarely via an app link. Generally the bug seems v similar to this one. Assuming it is the same sort of thing going on as above, I would expect our aab file not to have the method in IL, that it will have the method in AOT code, and that something stops it accessing the latter, but I haven't investigated the aab file to confirm any of this.

@tranb3r
Copy link

tranb3r commented Feb 22, 2024

I'm still facing this issue. Any update?

@akoeplinger akoeplinger removed their assignment Apr 20, 2024
@lambdageek lambdageek removed their assignment Jun 28, 2024
@lambdageek
Copy link
Member

Possibly resolved by #103975. Our next steps with that PR are to measure the performance impact and then we will consider a net8.0 backport

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 28, 2024
@jonathanpeppers jonathanpeppers transferred this issue from dotnet/android Jun 28, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 28, 2024
@steveisok steveisok added this to the 9.0.0 milestone Jun 28, 2024
@steveisok steveisok added area-Codegen-AOT-mono and removed untriaged New issue has not been triaged by the area owner needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jun 28, 2024
@fanyang-mono fanyang-mono self-assigned this Jun 28, 2024
@fanyang-mono
Copy link
Member

This should have been fixed. If not, feel free to open a new issue.

@tipa
Copy link
Author

tipa commented Aug 15, 2024

@fanyang-mono Is the fix in the latest .NET8 release?

image

@fanyang-mono
Copy link
Member

@tipa I am afraid not. It should be included in the September release.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2024
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

10 participants