-
Notifications
You must be signed in to change notification settings - Fork 5k
Exception Handling Problem with Null Function Pointer Invocation #115043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Invoking null function pointer is invalid operation. It is not expected to result into a nice catchable NullReferenceException. Function pointers are unsafe construct. You have to make sure to use them correctly. |
Is dereferencing null pointer similarly undefined? In current implementation they are treated the same with managed reference. |
Dereferencing null unmanaged pointer produces deterministic NullReferenceException in CoreCLR. I am not sure whether it is the case across all .NET runtimes (Mono, Unity). |
Would the recommended best practice be to always perform explicit null checks before invoking function pointers? Something like: if (somePtr != null)
{
return somePtr(arg1, arg2);
}
else
{
// Handle null pointer case
throw new InvalidOperationException("Function pointer cannot be null");
// or return default value, etc.
} |
I'd like to check specification about it. Even if undefined, producing deterministic NullReferenceException for function pointer is helpful for debugging. |
I would expect well-written unsafe code to be tight and avoid these kind of error conditions by construction. I think If your unsafe code ends up with calling null function pointers unexpectedly, you may have more fundamental problems with memory safety. There may be situations where InvalidOperationException like you have shown is appropriate, but I would expect those situations to be very rare. |
It is not possible to produce deterministic NullReferenceException for null function pointer calls, without degrading performance of function pointer calls, or without compromising security/diagnosability. It may be possible to improve debugging experience for null function pointer calls under Visual Studio debugger. If you care about that, it should be filled using VS feedback. |
Is there any meaningful difference between calling a function at 0 and reading from 0? I’d assume the only difference is the selector used (CS vs DS). |
The observable behavior is different. When you are calling a function at 0, you won't get the fault at the call instruction. You will get a fault from trying to execute code at address 0. |
Can't that be handled by lookup of the return address in such case? |
If the call was optimized into a tailcall, the return address will point to code that has nothing to do with the null reference exception. |
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<>
):When invoking a null managed function pointer (
delegate*<>
):Steps to Reproduce
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
The text was updated successfully, but these errors were encountered: