runtime: Invalid profiler callbacks on exception handling since .NET 9 preview 3

Description

There is some mess with profiling callbacks related to exception handling since .NET 9 preview 3. As far as I understand, it’s caused by the switch to the new exception handling subsystem.

Reproduction Steps

The issue is reproduced on the following test application:

using System;
using System.Runtime.InteropServices;

internal static partial class Kernel32
{
  [LibraryImport("kernel32.dll", EntryPoint = "GetFileAttributesW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
  internal static partial uint GetFileAttributes(string fileName);
}

internal static partial class LibC
{
  // ReSharper disable once InconsistentNaming
  [LibraryImport("libc", EntryPoint = "access", SetLastError = true, StringMarshalling = StringMarshalling.Utf8)]
  internal static partial int access(string fileName, int mode);
}

internal static class Program
{
  private static int Main()
  {
    try
    {
      const string fileName = "file-that-should-not-exist.txt";
      Console.WriteLine("[{0}] Create", DateTime.Now);

      var fileNotFound = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
        ? IsFileNotFoundWindows(fileName)
        : IsFileNotFoundLinuxMacOs(fileName);
      Console.WriteLine("[{0}] Check fileNotFound? {1}! fileName {2}", DateTime.Now, fileNotFound, fileName);
      if (!fileNotFound)
        throw new Exception("File-not-found error is expected");

      Console.WriteLine("[{0}] Done", DateTime.Now);
      return 0;
    }
    catch (Exception e)
    {
      Console.Error.WriteLine(e);
      return 1;
    }
  }

  private static bool IsFileNotFoundWindows(string fileName)
  {
    // ReSharper disable InconsistentNaming
    const uint INVALID_FILE_ATTRIBUTES = unchecked((uint)-1);
    const int ERROR_FILE_NOT_FOUND = 2;
    // ReSharper restore InconsistentNaming
    return Kernel32.GetFileAttributes(fileName) == INVALID_FILE_ATTRIBUTES && Marshal.GetLastPInvokeError() == ERROR_FILE_NOT_FOUND;
  }

  private static bool IsFileNotFoundLinuxMacOs(string fileName)
  {
    // ReSharper disable InconsistentNaming
    const int F_OK = 0;
    const uint ENOENT = 2;
    // ReSharper restore InconsistentNaming
    return LibC.access(fileName, F_OK) == -1 && Marshal.GetLastPInvokeError() == ENOENT;
  }
}

Expected behavior

Profiler callbacks should be called in accordance with the state machine for exception handling, see https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/profiling.md#exceptions

Actual behavior

In some cases I receive ExceptionUnwindFunctionLeave notification after ExceptionSearchFunctionEnter (on the same thread, of course).

In other cases I receive notifications in a proper order, but id of the function received in ExceptionUnwindFunctionLeave doesn’t match the top frame of the callstack.

Regression?

Yes, everything worked fine in previous versions, inclduing .NET 9 preview 1-2.

Known Workarounds

No workarounds

Configuration

.NET 9 preview 3-5 Linux x64, Ubuntu 22.04

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

@k15tfu the exception callbacks report only functions whose frames the exception handling processes when searching for the handler. The methods on System.Runtime.EH, System.Runtime.StackFrameIterator and System.Runtime.ExceptionServices.InternalCalls are never processed during the EH process, we start on the frame that has thrown the exception, so that should be the first function you’d get reported in ExceptionSearchFunctionEnter/Leave in the first pass / ExceptionUnwindFunctionEnter in the second pass. So I guess the ETL FuntionEnter/FunctionLeave would report the function with the same function id as the one that will be reported by the first ExceptionSearchFunctionEnter, then it would report the internal managed frames on System.Runtime.EH, System.Runtime.StackFrameIterator and System.Runtime.ExceptionServices.InternalCalls and then the actual ExceptionSearchFunctionEnter would be called.

Is there any documentation saying that the last frame reported by the ETL FuntionEnter before an ExceptionSearchFunctionEnter is the one that is throwing the exception?

@mangod9 I have checked, and tests work fine after the switch to the legacy exception handling. So, the problem is indeed caused by the new exception handling.

new EH can be disabled using DOTNET_LegacyExceptionHandling=1