-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[NativeAOT] Can't call a function pointer with a ref return value #72316
Comments
I managed to make it work by adding
So that's my next question I guess 😄 |
You should not need to manually apply |
@jkotas The unmanaged function comes from a C++ library that expects the caller to use a return buffer (the returned struct is in fact a C++ object with a non-trivial destructor, which is a special case in the System V ABI), so I'm trying to force the runtime to use this calling convention. I'm not aware of an attribute that can be used to force that behavior, so I'm trying other things. I had good hope for the ref struct but I guess I'll need to use something else. I found a special case when the struct has no fields (https://cs.github.com/dotnet/runtime/blob/eb170a0523717094a921a38ac45c1c8e401ef8b6/src/coreclr/vm/methodtable.cpp#L2458) so I'll be using that for now but it's not great. |
@EgorBo yeah that's what I ended up doing 👍 |
The main problem is that your unmanaged function pointer returns a managed reference. As the runtime error indicated, this requires marshalling which is expected to be handled by the public struct TestStruct
{
}
public static unsafe void CreateFunctionPointer(IntPtr address)
{
var functionPointer = (delegate* unmanaged[Cdecl, MemberFunction]<IntPtr, TestStruct*>)address;
functionPointer(IntPtr.Zero);
} Source-generated marshalling of function pointer calls is tracked in #63590. I also tested that the source generator does not support ref returns at all, causing |
Contributes to dotnet#72316
Description
In some cases with the System V ABI, when a "complex" type is returned, the caller is expected to allocate memory for the return value, and pass a pointer to that memory as a hidden first argument.
I'm trying to call a function that uses that convention, so I looked at how to instruct the runtime to do so. It looks like that may be the case when returning by ref:
runtime/src/coreclr/vm/callingconvention.h
Line 1784 in 0547344
(note: I may be misinterpreting this code, since I haven't been able to try)
However, when trying to call an unmanaged pointer with a
ref
return value, the NativeAOT compilation fails.Reproduction Steps
Create a new project, declare a function pointer with a
ref
return value, and try calling it:Then try compiling it with NativeAOT:
Expected behavior
The call should work.
Actual behavior
An error is thrown during compilation:
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: