-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
area-ExceptionHandling-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.
Description
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:
-
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
-
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
- Create a .NET 9 console application
- Add the code below
- 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
Labels
area-ExceptionHandling-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.