Skip to content

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

Closed

Description

@simonrozsival

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

Activity

  1. added
    runtime-mono
    on Nov 24, 2022
  2. ghost added
    untriaged
    on Nov 24, 2022
  3. added this to the 8.0.0 milestone on Nov 24, 2022
  4. ghost removed
    untriaged
    on Nov 24, 2022
  5. lambdageek commented on Nov 24, 2022

    @lambdageek
  6. simonrozsival commented on Nov 24, 2022

    @simonrozsival
    MemberAuthor

    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
    
  7. lambdageek commented on Nov 24, 2022

    @lambdageek
    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

  8. jandupej commented on Jun 2, 2023

    @jandupej
    Contributor

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

  9. simonrozsival commented on Jun 15, 2023

    @simonrozsival
    MemberAuthor

    @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.
    
  10. jandupej commented on Jul 28, 2023

    @jandupej
    Contributor

    The crashes are resolved with #89564. Closing.

  11. ghost locked as resolved and limited conversation to collaborators on Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Net.Httpos-androidruntime-mono

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions