Skip to content

Incorrect marshalling of Bool return types in new exception handling #113574

Closed

Description

@filipnavara

The managed prototypes for RhpSfiInit, RhpSfiNext and few other methods specify UnmanagedType.Bool for the return values:

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "SfiInit")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static unsafe partial bool RhpSfiInit(ref StackFrameIterator pThis, void* pStackwalkCtx, [MarshalAs(UnmanagedType.Bool)] bool instructionFault, bool* fIsExceptionIntercepted);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "SfiNext")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static unsafe partial bool RhpSfiNext(ref StackFrameIterator pThis, uint* uExCollideClauseIdx, bool* fUnwoundReversePInvoke, bool* fIsExceptionIntercepted);

This is a four byte Win32 BOOL type. However, the native code uses 1-byte C++ bool type.

In the debug version of the runtime with MSVC compiler the contracts often trash the high bytes of the RAX/EAX register and thus an incorrect return value is marshalled.

/cc @janvorli

Activity

  1. ghost added
    needs-area-label
    on Mar 15, 2025
  2. added
    untriaged
    on Mar 15, 2025
  3. added
    area-ExceptionHandling-coreclr
    and removed
    needs-area-label
    on Mar 15, 2025
  4. changed the title [-]Incorrect matshalling of Bool return types in new exception handling[/-] [+]Incorrect marshalling of Bool return types in new exception handling[/+] on Mar 15, 2025
  5. added
    in-pr
    on Mar 16, 2025
  6. removed
    untriaged
    on Mar 16, 2025
  7. added this to the 10.0.0 milestone on Mar 16, 2025
  8. janvorli commented on Mar 17, 2025

    @janvorli
    Member

    In the debug version of the runtime with MSVC compiler the contracts often trash the high bytes of the RAX/EAX register and thus an incorrect return value is marshalled.

    @filipnavara thank you for spotting this. I am surprised though that you have seen a problem occur, as I've never hit it in my testing that's always with debug / checked build.

  9. filipnavara commented on Mar 17, 2025

    @filipnavara
    MemberAuthor

    It's probably dependent on a lot of factors including compiler versions. I initially spotted it when testing my win-x86 branch with funclets where the compiler was more prone to generate the patterns that only restored al register. That said, I was regularly comparing the runs with win-x64 and at least on one occasion I hit the problem there as well.

    I don't remember which test hit the scenario but I was running the NativeAOT Exceptions test on CoreCLR which is normally excluded in the runs. That test explicitly runs throw new ...() inside the Main method and expects to hit the unhandled exception handler. It depends on the SfiNext return value to be correct to terminate the exception dispatch loop in the right place.

  10. locked and limited conversation to collaborators on Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-ExceptionHandling-coreclrin-pr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions