runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[ARM32] Throw in catch block fails

Open parjong opened this issue 10 years ago • 2 comments

CoreCLR shows unexpected behavior on the following example

namespace Test { class Class1 {
  static public void Main(string[] args) {
    try {
      try {
        System.Console.WriteLine("In try");
        try {
          System.Console.WriteLine("In try 2, 1st throw");
          throw new System.Exception();
        } catch {
          System.Console.WriteLine("In 1st catch, 2nd throw");
          throw new System.Exception();
        }
      } catch {
        System.Console.WriteLine("In 2nd catch");
      }
    } catch {
      System.Console.WriteLine("Unreached");
    }
    System.Console.WriteLine("Done");
  }
} }

It is expected to show the following output:

In try
In try 2, 1st throw
In 1st catch, 2nd throw
In 2nd catch
Done

However, the 2nd throw is captured by the outermost try region as follows:

In try
In try 2, 1st throw
Unreached
Done

parjong avatar Apr 25 '16 23:04 parjong

It looks like dotnet/runtime#5676 is related with this issue, but not exactly same. With PR dotnet/coreclr#4503, the 1st throw is catched as expected, but the 2nd throw is not captured by any of try regions:

In try
In try 2, 1st throw
In 1st catch, 2nd throw

Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown.
   at Test.Class1.Main(String[] args)

parjong avatar Apr 25 '16 23:04 parjong

I found that CoreCLR failed to unwind the stack frame created by CallEHFunclet. When the 2nd exception is thrown, UnwindManagedExceptionPass1 tries to find the corresponding try region.

When I checked with GDB, CoreCLR correctly unwinds the managed stack created by the 1st catch block. After that, CoreCLR tries to unwind the stack frame with PAL_VirtualUnwind at src/vm/exceptionhandling.cpp:4579. At that time, the top stack frame was created by CallEHFunclet.

It seems that libunwind failed to traverse the stack frame due to the stack adjustment at the beginning of CallEHFunclet:

        // This helper enables us to call into a funclet after applying the non-volatiles
        NESTED_ENTRY CallEHFunclet, _TEXT, NoHandler

        PROLOG_PUSH  "{r4-r11, lr}"
        alloc_stack  4

After I revised the implementation of CallEHFunclet as in PR dotnet/coreclr#4581, CoreCLR behaves as expected:

In try
In try 2, 1st throw
In 1st catch, 2nd throw
In 2nd catch
Done

parjong avatar Apr 26 '16 00:04 parjong