Skip to content

[Crossgen2] PInvoke stubs with EH blocks cause a runtime assert when an exception is thrown #248

Closed

Description

@fadimounir

Simple repro (make NativeMethod2 throw a divide-by-zero exception):

        [DllImport("nativelib")]
        public extern static int NativeMethod1(int a);
        [DllImport("nativelib")]
        [MethodImpl(MethodImplOptions.NoInlining)]
        public extern static int NativeMethod2(int a);
        [DllImport("nativelib")]
        [MethodImpl(MethodImplOptions.NoInlining)]
        public extern static int NativeMethod3(int a);

        static void TestPInvokeSimple()
        {
            for (int i = 0; i < 4; i++)
            {
                var a = NativeMethod1(1);
                Console.WriteLine("NativeMethod1 = " + a);

                try
                {
                    a = NativeMethod2(0);
                    Console.WriteLine("NativeMethod2 = " + a);
                }
                catch (Exception ex) 
                {
                    Console.WriteLine(ex); 
                }

                a = NativeMethod3(1);
                Console.WriteLine("NativeMethod3 = " + a);
            }
        }

Causes the following assert after the catch block executes:

Assert failure(PID 20440 [0x00004fd8], Thread: 4548 [0x11c4]): false

CORECLR! ExceptionTracker::UpdateNonvolatileRegisters + 0x17B (0x00007fff`0284fa0b)
CORECLR! FixNonvolatileRegisters + 0x287 (0x00007fff`028430a7)
CORECLR! ProcessCLRException + 0xE43 (0x00007fff`02852683)
NTDLL! chkstk + 0x19F (0x00007fff`8e5c127f)
NTDLL! RtlUnwindEx + 0x522 (0x00007fff`8e53da32)
CORECLR! ClrUnwindEx + 0x88 (0x00007fff`0283e768)
CORECLR! ProcessCLRException + 0xBE1 (0x00007fff`02852421)
NTDLL! chkstk + 0x11F (0x00007fff`8e5c11ff)
NTDLL! RtlRaiseException + 0x399 (0x00007fff`8e58a289)
NTDLL! KiUserExceptionDispatcher + 0x2E (0x00007fff`8e5bfe6e)
    File: D:\GIT\runtime\src\coreclr\src\vm\exceptionhandling.cpp Line: 494

Activity

  1. self-assigned this
    on Nov 25, 2019
  2. added
    untriaged
    on Nov 25, 2019
  3. added and removed
    untriaged
    on Nov 25, 2019
  4. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions