Skip to content
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

Q: How to use UnmanagedCallersOnlyAttribute? #37930

Closed
hypeartist opened this issue Jun 15, 2020 · 5 comments
Closed

Q: How to use UnmanagedCallersOnlyAttribute? #37930

hypeartist opened this issue Jun 15, 2020 · 5 comments
Labels
area-System.Runtime.InteropServices question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@hypeartist
Copy link

Is there any info on how to use that new attribute?
All I have found is:

/// Any method marked with <see cref="System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute" /> can be directly called from

I have tried to apply it to some static POD method of a class library but what next? It doesn't show up in exports (used some third-party tools to check) and also I can't obtain its address having dll's handle via GetProcAddress.

Thanks in advance for any clarification.
PS: using .NET 5 Preview 7

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jun 15, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@jkotas
Copy link
Member

jkotas commented Jun 15, 2020

cc @AaronRobinsonMSFT

@jkotas jkotas added the question Answer questions and provide assistance, not an issue with source code or documentation. label Jun 15, 2020
@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented Jun 15, 2020

@hypeartist The UnmanagedCallersOnlyAttribute is used to produce the most lite weight version of a function pointer to a managed function that can be call outside the runtime. The ability to create function pointers in C# was recently exposed in C# 9 - see proposal and tracking issue dotnet/roslyn#39865. Consuming function pointers isn't the only way this feature can be used, but is definitely the most obvious utilization of this attribute.

Another way to use this attribute is via the low level hosting APIs - load_assembly_and_get_function_pointer (PR #35763). Leveraging this low level API is how the DNNE package creates a native export.

This attribute is really a low level building block for tooling or providing symmetry for C# function pointers.

@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Jun 15, 2020
@tomrus88
Copy link

So this attribute itself doesn't create native export and you need to go extra mile if you need one?

Is it safe to use C# function pointers feature to get a pointer to C# method and manually pass that pointer to native code (using pinvoke or something)?

What's the difference compared to Marshal.GetFunctionPointerForDelegate?

@AaronRobinsonMSFT
Copy link
Member

So this attribute itself doesn't create native export and you need to go extra mile if you need one?

Correct. It is an attribute that indicates to the JIT that this function will only be called from a non-managed context and therefore you should add that transition logic.

Is it safe to use C# function pointers feature to get a pointer to C# method and manually pass that pointer to native code (using pinvoke or something)?

Yes. That is exactly the intent - the function itself and subsequently the pointer can only be dispatched to in an unmanaged way. Note that the calli instruction does permit an unmanaged dispatch.

What's the difference compared to Marshal.GetFunctionPointerForDelegate?

Mostly performance and an allocation. Using Marshal.GetFunctionPointerForDelegate requires a delegate allocation but using the C# function pointers update to address-of (i.e. &) the address can be taken of a manged function with the above attribute and then immediately passed to native instead of allocating a delegate.

@jkotas jkotas closed this as completed Jun 17, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

5 participants