Skip to content

[Android] Application crashes when an exception is thrown after running a System.Diagnostics.Process #97209

@simonrozsival

Description

@simonrozsival

Description

Moved from an original issue in xamarin-android: dotnet/android#8645

Application does not catch exception after running a System.Diagnostics.Process in release mode, in debug mode it runs fine.

Note that it does not cause a crash on every exception for example if you throw new Exception(), this won't result in a crash in release mode.

I can reproduce the crash in the runtime directly using the Android sample app. The original report mentions that there isn't a crash in Debug mode. I can confirm that the sample app doesn't crash when I enable interpreter.

Reproduction Steps

Change the code in the Android sample project (src/mono/sample/Android/Program.cs):

#nullable enable

using System;
using System.Xml;
using System.Diagnostics;

Test();

void Test()
{
    CatchException("Before ShellCommand");

    using var _ = Process.Start(new ProcessStartInfo("/system/bin/hostname"));

    CatchException("After ShellCommand"); // this will crash the application in release mode
}

void CatchException(string name)
{
    Console.WriteLine(name);
    try
    {
        XmlAttributeCollection? attributes = null;
        _ = attributes!["foo"];
    }
    catch (Exception e)
    {
        Console.WriteLine($"EXCEPTION '{name}' {e}");
    }
}

Run the code in Release configuration on Android emulator:

make -C src/mono/sample/Android MONO_CONFIG=Release MONO_ARCH=arm64 runtimepack run

Read the app output using adb:

adb logcat -s DOTNET
adb logcat --buffer=crash

Expected behavior

The app should run without crashing and with the following output:

> adb logcat -s DOTNET
DOTNET  : Before ShellCommand
DOTNET  : EXCEPTION 'Before ShellCommand' System.NullReferenceException: Object reference not set to an instance of an object
DOTNET  :    at Program.<<Main>$>g__CatchException|0_1(String )
DOTNET  : After ShellCommand
DOTNET  : EXCEPTION 'After ShellCommand' System.NullReferenceException: Object reference not set to an instance of an object
DOTNET  :    at Program.<<Main>$>g__CatchException|0_1(String )

Actual behavior

The app crashes with the following outputs:

> adb logcat -s DOTNET
...
DOTNET  : Before ShellCommand
DOTNET  : EXCEPTION 'Before ShellCommand' System.NullReferenceException: Object reference not set to an instance of an object
DOTNET  :    at Program.<<Main>$>g__CatchException|0_1(String )
DOTNET  : After ShellCommand
> adb logcat --buffer=crash
--------- beginning of crash
libc    : exiting due to SIG_DFL handler for signal 11, ucontext 0x7394f9de20
libc    : Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 9369 (.dot.MonoRunner), pid 9354 (ot.HelloAndroid)
DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
DEBUG   : Build fingerprint: 'google/sdk_gphone64_arm64/emu64a:13/TE1A.220922.012/9302419:user/release-keys'
DEBUG   : Revision: '0'
DEBUG   : ABI: 'arm64'
DEBUG   : Timestamp: 2024-01-19 11:36:40.147858138+0100
DEBUG   : Process uptime: 2s
DEBUG   : Cmdline: net.dot.HelloAndroid
DEBUG   : pid: 9354, tid: 9369, name: .dot.MonoRunner  >>> net.dot.HelloAndroid <<<
DEBUG   : uid: 10188
DEBUG   : tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
DEBUG   : pac_enabled_keys: 000000000000000f (PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY)
DEBUG   : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
DEBUG   : Abort message: 'exiting due to SIG_DFL handler for signal 11, ucontext 0x7394f9de20'
DEBUG   :     x0  0000000000000000  x1  0000000000002499  x2  0000000000000006  x3  0000007394f9dc60
DEBUG   :     x4  0000008080808080  x5  0000008080808080  x6  0000008080808080  x7  8080808080000000
DEBUG   :     x8  00000000000000f0  x9  0000007380c2ea00  x10 0000000000000001  x11 0000007380c6cde4
DEBUG   :     x12 0101010101010101  x13 000000007fffffff  x14 000000000035c8e0  x15 0000000000000060
DEBUG   :     x16 0000007380cd1d58  x17 0000007380caec70  x18 0000007077334000  x19 000000000000248a
DEBUG   :     x20 0000000000002499  x21 00000000ffffffff  x22 0000007394f9de48  x23 0000007394ba06f8
DEBUG   :     x24 0000000000000040  x25 00000070d95be000  x26 000000000000000b  x27 0000007394ba0318
DEBUG   :     x28 0000000000000000  x29 0000007394f9dce0
DEBUG   :     lr  0000007380c5e968  sp  0000007394f9dc40  pc  0000007380c5e994  pst 0000000000001000
DEBUG   : backtrace:
DEBUG   :       #00 pc 0000000000051994  /apex/com.android.runtime/lib64/bionic/libc.so (abort+164) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b)
DEBUG   :       #01 pc 0000000000002520  /apex/com.android.art/lib64/libsigchain.so (art::SignalChain::Handler(int, siginfo*, void*)+1172) (BuildId: d5ff66d01eb05cf124e6fe95ee64abfd)
DEBUG   :       #02 pc 000000000000084c  [vdso]
DEBUG   :       #03 pc 000000000000b03c  <anonymous:7394e14000>

Regression?

I can reproduce the crash on release/7.0.

Known Workarounds

No response

Configuration

  • replicated with dotnet/runtime main (f6824bc9e8c68d3edb3d0a4c91e9bacd73d7767f)
  • OS: Android
  • arch: arm64
  • the issue seems to be specific to Android (I wasn't able to reproduce the same issue with mono on macOS), I haven't tested on Android x64 yet

Other information

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions