Implement managed exception handling on Linux and Mac
We need to implement throwing and catching exceptions in managed code for Linux and Mac platforms. This work depends on implementation of stack unwinding (https://github.com/dotnet/coreclr/issues/177) on these platforms.
@janvorli Could you please provide design proposal and name of a feature branch when you are ready to start working on this issue? Thank you.
@janvorli Whats remaining? Any chance of the design proposal @sergiy-k asked for?
Thanks!
@kangaroo @AndyAyersMS @pgavlin @jkotas Here is a brief overview of the plan. I am leaving for a week of vacation today, so I'll be able to respond to comments after I return. @sergiy-k said he might do some work on this while I'm away.
Managed exception handling on Linux
Exceptions thrown by the managed code
Managed code throws exceptions by calling IL_Throw FCALL, which uses RaiseException to perform the actual throwing. The FCALL body is wrapped between the HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL / HELPER_METHOD_FRAME_END macros that internally use INSTALL_UNWIND_AND_CONTINUE_HANDLER / UNINSTALL_UNWIND_AND_CONTINUE_HANDLER.
On Windows, the exception raised by the RaiseException is handled by the native windows unwinding, since the jitter provides Windows with unwinding info necessary for unwinding the jitted code.
On Linux, we need to catch the PAL_SEHException in the INSTALL_UNWIND_AND_CONTINUE_HANDLER (resp. its underlying INSTALL_UNWIND_AND_CONTINUE_HANDLER_NO_PROBE) and then unwind it manually by walking the managed frames using the Windows style unwinder, using EECodeInfo to get the function entry and module base data and calling ProcessCLRException on each frame until the handler is found and then executed.
There can also be a case when we walk out of the managed frames without finding a handler and reach a bunch of native frames. In this case, we need to rethrow the exception, let the native unwinding do its job. If that piece of native code was called from a managed method, we catch the exception on the border and again use the manual unwinding described above until the handler is found or native frames are reached again.
Exceptions thrown by the runtime methods called from the managed code
This is the same case as the previous one. The code in these methods also uses INSTALL_UNWIND_AND_CONTINUE_HANDLER / UNINSTALL_UNWIND_AND_CONTINUE_HANDLER, so the exception is caught and processed the same way.
Exceptions thrown by PInvoked functions
We will not try to handle these (except for cases where the PInvoke calls QCALLS which also use the INSTALL_UNWIND_AND_CONTINUE_HANDLER / UNINSTALL_UNWIND_AND_CONTINUE_HANDLER and so the exceptions are processed as described above).
Getting an exception from the general PInvoke means that a code that is out of our control has crashed and its state is unknown and cannot be relied on anymore. Trying to handle such state could potentially lead to deeper issues, so it is better to fail fast.
Exceptions thrown by managed code invoked from runtime
This is basically the same case as described above when managed stack walking walks out of the managed code without finding a handler. So the managed frames are unwound and then exception is rethrown and native exception unwinding handles it further.
Hardware exceptions
On Unix, hardware exceptions like invalid instruction, division by zero, null pointer reference and similar are reported by signals. We will write handlers for these signals that will call back into the runtime. The runtime will check whether the exception has happened in the managed code or native code. If it happened in a managed code, then the managed exception unwinding as described above will be performed. The current plan is to not to try to handle such exceptions in the native code and rather abort.
Update: After dotnet/coreclr#363 and dotnet/coreclr#435, we now have everything except of the hardware exceptions implemented.
The hardware exception handling on Linux went in as dotnet/coreclr#490.
@kangaroo Would you be willing to implement the hardware exceptions handling on OSX? There already seems to be some code in the PAL for that purpose for OSX, but I have no idea about its state and usability.
@janvorli Sure, I took a look at it before, and it doesn't look too hard. You can give this to me.
@janvorli Do you have any test cases aside from the regular NRE/FPE?
@kangaroo My primary test case was this code:
using System;
using System.Diagnostics;
namespace NullRefTest
{
class Program
{
static void Main(string[] args)
{
try
{
byte[] a = null;
unsafe
{
fixed (byte* b = a)
{
*b = 0;
}
}
}
catch (NullReferenceException ex)
{
Console.WriteLine("Caught null reference: " + ex.ToString());
}
catch
{
Console.WriteLine("Caught other exception");
}
}
}
}
PR: https://github.com/dotnet/coreclr/pull/498
As mentioned, it can be improved, but I wanted to get baseline support in to match Linux first.
basalt:mac.x64.debug plasma$ cat test2.cs
using System;
using System.Diagnostics;
namespace NullRefTest
{
class Program
{
static void Main(string[] args)
{
try
{
byte[] a = null;
unsafe
{
fixed (byte* b = a)
{
*b = 0;
}
}
}
catch (NullReferenceException ex)
{
Console.WriteLine("Caught null reference: " + ex.ToString());
}
catch
{
Console.WriteLine("Caught other exception");
}
}
}
}
basalt:mac.x64.debug plasma$ ./corerun test2.exe
Caught null reference: System.NullReferenceException: Object reference not set to an instance of an object.
at NullRefTest.Program.Main(String[] args)
+1
Completed now that dotnet/coreclr#498 went in