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

[Mono][Android] App crashes with InvalidProgramException #78833

Closed
simonrozsival opened this issue Nov 24, 2022 · 7 comments
Closed

[Mono][Android] App crashes with InvalidProgramException #78833

simonrozsival opened this issue Nov 24, 2022 · 7 comments
Assignees
Labels
Milestone

Comments

@simonrozsival
Copy link
Member

Description

My code started crashing after pulling from main. There seems to be a some problem with the IL that's emitted by Mono on Android arm64.

Reproduction Steps

Replace src/mono/sample/Android/Program.cs with the following code and run it using make -C src/mono/sample/Android MONO_ARCH=arm64:

using System;
using System.Net.Http;

try
{
    using var handler = new SocketsHttpHandler();
    using var client = new HttpClient(handler);
    var response = await client.GetAsync("https://dot.net");
    Console.WriteLine(response);
    return 42;
}
catch (Exception ex)
{
    Console.WriteLine(ex);
    return 1;
}

Expected behavior

The HTTP response is pritnted in logcat.

Actual behavior

An exception is thrown:

System.TypeInitializationException: The type initializer for 'System.Net.CookieContainer' threw an exception.
 ---> System.InvalidProgramException: Invalid IL code in (wrapper managed-to-native) Interop/Sys:GetDomainName (byte*,int): IL_0012: calli     0x00000003
   at Interop.Sys.GetDomainName()
   at System.Net.NetworkInformation.HostInformationPal.GetDomainName()
   at System.Net.NetworkInformation.HostInformation.get_DomainName()
   at System.Net.CookieContainer.CreateFqdnMyDomain()
   at System.Net.CookieContainer..cctor()
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionSettings.CloneAndNormalize()
   at System.Net.Http.SocketsHttpHandler.SetupHandlerChain()
   at System.Net.Http.SocketsHttpHandler.SendAsync(HttpRequestMessage , CancellationToken )
   at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage , CancellationToken )
   at System.Net.Http.HttpClient.<>n__0(HttpRequestMessage , CancellationToken )
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken )
   at Program.<Main>$(String[] )

Regression?

No response

Known Workarounds

No response

Configuration

Other information

I've traced the problem to #75542. Basic hello world program works but when I try to make an HTTP request, the program crashes. The tests in CI seem to pass which is quite strange.

/cc @lambdageek @naricc

@simonrozsival simonrozsival added os-android runtime-mono specific to the Mono runtime labels Nov 24, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 24, 2022
@ghost
Copy link

ghost commented Nov 24, 2022

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

My code started crashing after pulling from main. There seems to be a some problem with the IL that's emitted by Mono on Android arm64.

Reproduction Steps

Replace src/mono/sample/Android/Program.cs with the following code and run it using make -C src/mono/sample/Android MONO_ARCH=arm64:

using System;
using System.Net.Http;

try
{
    using var handler = new SocketsHttpHandler();
    using var client = new HttpClient(handler);
    var response = await client.GetAsync("https://dot.net");
    Console.WriteLine(response);
    return 42;
}
catch (Exception ex)
{
    Console.WriteLine(ex);
    return 1;
}

Expected behavior

The HTTP response is pritnted in logcat.

Actual behavior

An exception is thrown:

System.TypeInitializationException: The type initializer for 'System.Net.CookieContainer' threw an exception.
 ---> System.InvalidProgramException: Invalid IL code in (wrapper managed-to-native) Interop/Sys:GetDomainName (byte*,int): IL_0012: calli     0x00000003
   at Interop.Sys.GetDomainName()
   at System.Net.NetworkInformation.HostInformationPal.GetDomainName()
   at System.Net.NetworkInformation.HostInformation.get_DomainName()
   at System.Net.CookieContainer.CreateFqdnMyDomain()
   at System.Net.CookieContainer..cctor()
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionSettings.CloneAndNormalize()
   at System.Net.Http.SocketsHttpHandler.SetupHandlerChain()
   at System.Net.Http.SocketsHttpHandler.SendAsync(HttpRequestMessage , CancellationToken )
   at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage , CancellationToken )
   at System.Net.Http.HttpClient.<>n__0(HttpRequestMessage , CancellationToken )
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken )
   at Program.<Main>$(String[] )

Regression?

No response

Known Workarounds

No response

Configuration

Other information

I've traced the problem to #75542. Basic hello world program works but when I try to make an HTTP request, the program crashes. The tests in CI seem to pass which is quite strange.

/cc @lambdageek @naricc

Author: simonrozsival
Assignees: -
Labels:

os-android, runtime-mono

Milestone: -

@steveisok steveisok added this to the 8.0.0 milestone Nov 24, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 24, 2022
@lambdageek

This comment was marked as outdated.

@simonrozsival
Copy link
Member Author

Hmm, so I figured out that when I run the Android sample and I explicitly include the marshal-ilgen runtime component, it works as expected:

make -C src/mono/sample/Android MONO_ARCH=arm64 RUNTIME_COMPONENTS=marshal-ilgen

@lambdageek
Copy link
Member

Hmm, so I figured out that when I run the Android sample and I explicitly include the marshal-ilgen runtime component, it works as expected:

make -C src/mono/sample/Android MONO_ARCH=arm64 RUNTIME_COMPONENTS=marshal-ilgen

ok, in that case we should figure out:

  1. Why Interop/Sys:GetDomainName needs non-legacy marshaling
  2. Why the call to GetDomainName throws an unhelpful invalid IL error message instead of something coherent from the non-legacy marshaling.

/cc @naricc

@SamMonoRT SamMonoRT assigned jandupej and unassigned naricc Mar 13, 2023
@jandupej
Copy link
Member

jandupej commented Jun 2, 2023

@simonrozsival Can you please verify that the issue still occurs?

@simonrozsival
Copy link
Member Author

@jandupej I tested it on latest main and the Android app still crashes when I don't explicitly set RUNTIME_COMPONENTS=marshal-ilgen. The log output has changed:

06-15 20:01:34.857 16236 16259 D DOTNET  : ((null) error) * Assertion at /.../dotnet/runtime/src/mono/mono/metadata/method-builder-ilgen.c:577, condition `ctor' not met
06-15 20:01:34.857 16236 16259 E DOTNET  : Exit code: 1.

@jandupej
Copy link
Member

The crashes are resolved with #89564. Closing.

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

No branches or pull requests

5 participants