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

Can't handle exception by AppDomain.UnhandledException when thrown in Executor thread #6211

Closed
azyobuzin opened this issue Aug 20, 2021 · 0 comments · Fixed by #6223
Closed
Assignees
Labels
Area: App Runtime Issues in `libmonodroid.so`.
Milestone

Comments

@azyobuzin
Copy link

azyobuzin commented Aug 20, 2021

Steps to Reproduce

  1. Set a handler on Application.OnCreate.

    public override void OnCreate()
    {
       AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
       {
           Console.WriteLine("!!! UnhandledException: " + e.ExceptionObject.GetType().FullName);
       };
       base.OnCreate();
    }
  2. Throw an exception on a thread made by Java.Util.Concurrent.Executors. In this example, the program is written on MainActivity.OnStart.

    protected override void OnStart()
    {
        base.OnStart();
        Java.Util.Concurrent.Executors.NewSingleThreadExecutor()
            .Execute(new Runnable(() =>
            {
                throw new Exception("Exception from Java Executor!");
            }));
    }

Project file: AndroidCrashTest.zip

Expected Behavior

The message !!! UnhandledException is written on Logcat before crash.

Actual Behavior

A crash occurs with SIGSEGV before the exception handler is called.

08-20 15:00:20.619 19214 19241 E AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
08-20 15:00:20.619 19214 19241 E AndroidRuntime: Process: com.companyname.androidcrashtest, PID: 19214
08-20 15:00:20.619 19214 19241 E AndroidRuntime: java.lang.Error: java.lang.Exception: Exception from Java Executor!
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:919)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: Caused by: java.lang.Exception: Exception from Java Executor!
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at mono.java.lang.Runnable.n_run(Native Method)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at mono.java.lang.Runnable.run(Runnable.java:30)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	... 2 more
08-20 15:00:20.619 19214 19241 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19241 (pool-1-thread-1), pid 19214 (ndroidcrashtest)

Version Information

Visual Studio 16.11.1 on Windows 10, Android Emulator (API 29, x86)

Microsoft Visual Studio Enterprise 2019
Version 16.11.1
VisualStudio.16.Release/16.11.1+31613.86
Microsoft .NET Framework
Version 4.8.04084

インストールされているバージョン:Enterprise

Visual C++ 2019   00435-60000-00000-AA703
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019   16.11.75.64347
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.11.75.64347
詳細については、https://www.asp.net/ をご覧ください

Azure App Service Tools v3.0.0   16.11.75.64347
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   16.11.75.64347
Azure Functions and Web Jobs Tools

C# ツール   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
IDE で使用する C# コンポーネント。プロジェクトの種類や設定に応じて、異なるバージョンのコンパイラを使用できます。

Common Azure Tools   1.10
Azure Mobile Services および Microsoft Azure Tools で使用する共通サービスを提供します。

Debugging Tools for Windows   10.0.18346.1000
Integrates the Windows Debugger functionality (http://go.microsoft.com/fwlink/?linkid=223405) in Visual Studio.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
ファブリックの診断イベント

GitHub.VisualStudio   2.11.106.19330
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode 拡張機能   1.0
IntelliCode Visual Studio 拡張機能の詳細情報

JetBrains ReSharper 2021.2   Build 212.0.20210730.153713
JetBrains ReSharper package for Microsoft Visual Studio. For more information about ReSharper, visit http://www.jetbrains.com/resharper. Copyright © 2021 JetBrains, Inc.

Microsoft Azure Service Fabric Tools for Visual Studio   16.10
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Tools for Visual Studio   2.9
Azure Cloud Services プロジェクトのサポート

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Visual Studio IDE 内からの Azure DevOps パイプラインの構成を簡略化しています。

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ ウィザード   1.0
Microsoft Visual C++ ウィザード

Microsoft Visual Studio Tools for Containers   1.2
ターゲット環境で ASP.NET Core アプリケーションを開発、実行、検証します。F5 キーを押してコンテナーで直接アプリケーションをデバッグ実行するか、Ctrl + F5 キーを押してコンテナーをリビルドせずにアプリを編集して更新します。

Microsoft Visual Studio VC パッケージ   1.0
Microsoft Visual Studio VC パッケージ

Mono Debugging for Visual Studio   16.10.15 (552afdf)
Support for debugging Mono processes with Visual Studio.

NuGet パッケージ マネージャー   5.11.0
Visual Studio 内の NuGet パッケージ マネージャー。NuGet の詳細については、https://docs.nuget.org/ にアクセスしてください

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Razor (ASP.NET Core)   16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   16.0.62107.28140
Microsoft SQL Server Data Tools

Syntax Visualizer   1.0
An extension for visualizing Roslyn SyntaxTrees.

TypeScript Tools   16.0.30526.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic ツール   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
IDE で使用する Visual Basic コンポーネント。プロジェクトの種類や設定に応じて、異なるバージョンのコンパイラを使用できます。

Visual F# Tools   16.11.0-beta.21322.6+488cc578cafcd261d90d748d8aaa7b8b091232dc
Microsoft Visual F# Tools

Visual Studio Code デバッグ アダプターのホスト パッケージ   1.0
Visual Studio Code デバッグ アダプターを Visual Studio でホストするための相互運用レイヤー

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

Visual Studio コンテナー ツール拡張機能   1.0
Visual Studio 内のコンテナーを表示、管理、診断します。

VisualStudio.DeviceLog   1.0
パッケージに関する情報

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Windows Driver Kit   10.0.18346.1000
Headers, libraries, and tools needed to develop, debug, and test Windows drivers (msdn.microsoft.com/en-us/windows/hardware/gg487428.aspx)

Xamarin   16.11.000.174 (d16-11@e8f56f1)
Xamarin.iOS と Xamarin.Android の開発を有効にする Visual Studio 拡張機能

Xamarin Designer   16.11.0.17 (remotes/origin/11e0001f0b17269345e80b58fb3adf1ba4efe2cd@11e0001f0)
Visual Studio で Xamarin Designer ツールを有効にするための Visual Studio 拡張機能。

Xamarin Templates   16.10.5 (355b57a)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   11.4.0.5 (d16-11/7776c9f)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: c633fe9
    Java.Interop: xamarin/java.interop/d16-11@48766c0
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.35.4@85460d3
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@683f375


Xamarin.iOS and Xamarin.Mac SDK   14.20.0.25 (3b53e529b)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

This issue can be reproduced when the project is built with both Debug and Release configuration. And also can be reproduced with the latest source code with some patches to build xamarin-android on my environment.

Log File

logcat

Investigation

Tombstone indicates the cause is null pointer dereference. I think mono_domain_get in this line returns null. https://github.com/xamarin/xamarin-android/blob/33f380858d2f7ca41f45856c552c80bca2ca0a67/src/monodroid/jni/monodroid-glue.cc#L2408

To prove this hypothesis, I added log outputs around domain usages and ran the application. The log indicates domain is null and the crash occurred in get_android_runtime_class.

08-20 15:22:04.461 19737 19763 E AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
08-20 15:22:04.461 19737 19763 E AndroidRuntime: Process: com.companyname.androidcrashtest, PID: 19737
08-20 15:22:04.461 19737 19763 E AndroidRuntime: java.lang.Error: java.lang.Exception: Exception from Java Executor!
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:919)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: Caused by: java.lang.Exception: Exception from Java Executor!
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at mono.java.lang.Runnable.n_run(Native Method)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at mono.java.lang.Runnable.run(Runnable.java:30)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	... 2 more
08-20 15:22:04.461 19737 19763 W monodroid: !!! Java_mono_android_Runtime_propagateUncaughtException domain = 0x0
08-20 15:22:04.461 19737 19763 W monodroid: !!! propagate_uncaught_exception (Legacy)
08-20 15:22:04.461 19737 19763 W monodroid: !!! propagate_uncaught_exception domain = 0x0
08-20 15:22:04.462 19737 19763 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19763 (pool-1-thread-1), pid 19737 (ndroidcrashtest)

Full Log

@azyobuzin azyobuzin added Area: App Runtime Issues in `libmonodroid.so`. needs-triage Issues that need to be assigned. labels Aug 20, 2021
grendello added a commit to grendello/xamarin-android that referenced this issue Aug 23, 2021
Fixes: dotnet#6211

Mono VM will return a valid AppDomain pointer (both in "legacy" and NET6
cases) only if the current thread is attached to some domain.  It is
possible that when managed code is called from an unattached Java
thread, `mono_domain_get()` will return a `NULL` pointer instead of a
valid one.  If we pass this pointer along to Mono, a segfault may occur
if Mono fails to validate the passed pointer.  Sample code which may
trigger the problem:

```csharp
public override void OnCreate()
{
   AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
   {
       Console.WriteLine("!!! UnhandledException: " + e.ExceptionObject.GetType().FullName);
   };
   base.OnCreate();
}

protected override void OnStart()
{
    base.OnStart();
    Java.Util.Concurrent.Executors.NewSingleThreadExecutor()
        .Execute(new Runnable(() =>
        {
            throw new Exception("Exception from Java Executor!");
        }));
}
```

Possible crashes caused by the above code:

    08-20 15:00:20.619 19214 19241 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19241 (pool-1-thread-1), pid 19214 (ndroidcrashtest)
    08-20 15:00:20.786 19245 19245 F DEBUG   : backtrace:
    08-20 15:00:20.786  1648  1648 I hwservicemanager: getTransport: Cannot find entry [email protected]::IMapper/default in either framework or device manifest.
    08-20 15:00:20.786 19245 19245 F DEBUG   :       #00 pc 0011b72f  /apex/com.android.runtime/lib/bionic/libc.so (pthread_mutex_lock+31) (BuildId: 471745f0fbbcedb3db1553d5bd6fcd8b)
    08-20 15:00:20.786 19245 19245 F DEBUG   :       #1 pc 0015240d  /data/app/com.companyname.androidcrashtest-sqgk-Mnu8GZM5hKPu1yWEQ==/lib/x86/libmonosgen-2.0.so (mono_domain_assembly_search+61)

    08-20 15:22:04.462 19737 19763 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19763 (pool-1-thread-1), pid 19737 (ndroidcrashtest)
    08-20 15:22:04.599 19767 19767 F DEBUG   : backtrace:
    08-20 15:22:04.599 19767 19767 F DEBUG   :       #00 pc 0011b72f  /apex/com.android.runtime/lib/bionic/libc.so (pthread_mutex_lock+31) (BuildId: 471745f0fbbcedb3db1553d5bd6fcd8b)
    08-20 15:22:04.599 19767 19767 F DEBUG   :       #1 pc 0026b91a  /data/app/com.companyname.androidcrashtest-JQoYc3YFwZtEoSJlTqX_BA==/lib/x86/libmonosgen-2.0.so (mono_os_mutex_lock+42)

To avoid the above situation, wrap the `mono_domain_get()` call in a
method which checks the return value and, if it's `nullptr`, it calls
`mono_get_root_domain()` and attaches the current thread to that domain.
@grendello grendello removed the needs-triage Issues that need to be assigned. label Aug 23, 2021
@grendello grendello added this to the .NET 6 milestone Aug 23, 2021
jonpryor pushed a commit that referenced this issue Aug 26, 2021
Fixes: #6211

Mono VM will return a valid AppDomain pointer (both in "legacy" and
NET6 cases) only if the current thread is attached to some domain.
It is possible that when managed code is called from an unattached
Java thread, `mono_domain_get()` will return `nullptr` instead of a
valid one.  If we further pass `nullptr` to other Mono functions, a
segfault may occur if Mono fails to validate the passed pointer.

Sample code which may trigger the problem:

	public override void OnCreate()
	{
	   AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
	       Console.WriteLine("!!! UnhandledException: " + e.ExceptionObject.GetType().FullName);
	   };
	   base.OnCreate();
	}

	protected override void OnStart()
	{
	    base.OnStart();
	    Java.Util.Concurrent.Executors.NewSingleThreadExecutor()
	        .Execute(new Runnable(() => {
	            throw new Exception("Exception from Java Executor!");
	        }));
	}

Possible crash caused by the above code:

	F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19241 (pool-1-thread-1), pid 19214 (ndroidcrashtest)
	F DEBUG   : backtrace:
	I hwservicemanager: getTransport: Cannot find entry [email protected]::IMapper/default in either framework or device manifest.
	F DEBUG   :       #00 pc 0011b72f  /apex/com.android.runtime/lib/bionic/libc.so (pthread_mutex_lock+31) (BuildId: 471745f0fbbcedb3db1553d5bd6fcd8b)
	F DEBUG   :       #1 pc 0015240d  /data/app/com.companyname.androidcrashtest-sqgk-Mnu8GZM5hKPu1yWEQ==/lib/x86/libmonosgen-2.0.so (mono_domain_assembly_search+61)

	F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19763 (pool-1-thread-1), pid 19737 (ndroidcrashtest)
	F DEBUG   : backtrace:
	F DEBUG   :       #00 pc 0011b72f  /apex/com.android.runtime/lib/bionic/libc.so (pthread_mutex_lock+31) (BuildId: 471745f0fbbcedb3db1553d5bd6fcd8b)
	F DEBUG   :       #1 pc 0026b91a  /data/app/com.companyname.androidcrashtest-JQoYc3YFwZtEoSJlTqX_BA==/lib/x86/libmonosgen-2.0.so (mono_os_mutex_lock+42)

To avoid the above situation, wrap the `mono_domain_get()` call into
`utils.get_current_domain()` which checks the return value of
`mono_domain_get()` and, if it's `nullptr`, calls
`mono_get_root_domain()` and attaches the current thread to that
root domain.
@ghost ghost locked as resolved and limited conversation to collaborators Jun 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App Runtime Issues in `libmonodroid.so`.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants