Skip to content

Exception Handling Problem with Null Function Pointer Invocation #115043

@Liangjia0411

Description

@Liangjia0411

Problem Description

In .NET 9, when invoking null function pointers (delegate*), exceptions cannot be properly caught, causing the program to crash directly. Additionally, managed and unmanaged function pointers exhibit inconsistent behaviors:

  1. When invoking a null unmanaged function pointer (delegate* unmanaged<>):

    • Exceptions cannot be caught with try/catch
    • Program crashes, console shows stack trace output but without line numbers
    • Normal exception handling is impossible
  2. When invoking a null managed function pointer (delegate*<>):

    • Exceptions cannot be caught with try/catch
    • Program crashes, console does not show any stack trace output
    • Normal exception handling is impossible

Steps to Reproduce

  1. Create a .NET 9 console application
  2. Add the code below
  3. Run the program

Expected Behavior

When invoking a null function pointer, a catchable exception (such as NullReferenceException) should be thrown, allowing the program to handle the exception normally and continue execution.

Actual Behavior

The program crashes and exceptions cannot be caught. Managed and unmanaged function pointers behave inconsistently (unmanaged pointers at least provide some stack information).

Test Code

class FunctionPointerTest
{
    // Test method
    public static unsafe void Main()
    {
        TestUnmanaged();
        TestManaged();
    }

    private static unsafe void TestUnmanaged()
    {
        Console.WriteLine("Start test delegate* unmanaged...");

        delegate* unmanaged<int, int, int> addPtr = &AddNumbers;

        int sum = addPtr(10, 20);
        Console.WriteLine($"Add Result: {sum}");

        TestUnmanagedNullFunctionPointer();
    }

    [UnmanagedCallersOnly]
    private static unsafe int AddNumbers(int a, int b)
    {
        return a + b;
    }

    private static unsafe void TestUnmanagedNullFunctionPointer()
    {
        Console.WriteLine("Testing unmanaged null function pointer:");

        delegate* unmanaged<int, int, int> nullPtr = null;

        try
        {
            int result = nullPtr(5, 10);
            Console.WriteLine($"Result: {result}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Expected exception: {ex}");
        }
    }

    private static unsafe void TestManaged()
    {
        Console.WriteLine("Start test delegate* managed...");

        delegate*<int, int, int> managedAddPtr = &ManagedAddNumbers;

        int sum = managedAddPtr(10, 20);
        Console.WriteLine($"Add Result: {sum}");

        TestManagedNullFunctionPointer();
    }

    private static int ManagedAddNumbers(int a, int b)
    {
        return a + b;
    }

    private static unsafe void TestManagedNullFunctionPointer()
    {
        Console.WriteLine("Testing managed null function pointer:");

        delegate*<int, int, int> nullPtr = null;

        try
        {
            int result = nullPtr(5, 10);
            Console.WriteLine($"Result: {result}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Expected exception: {ex}");
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-ExceptionHandling-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions