Skip to content

Conversation

@frabert
Copy link

@frabert frabert commented Mar 29, 2025

Fixes #113851

I was trying to figure out how to add support to calling Fastcall function pointers, and it appears to me that nothing is actually missing...?

Please do comment on all the obvious issues I missed, I am not familiar with the codebase at all.
If there is a chance of this actually becoming a viable PR, I am open to cleaning up and adding tests.

Copilot AI review requested due to automatic review settings March 29, 2025 17:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.

Files not reviewed (2)
  • src/coreclr/jit/importercalls.cpp: Language not supported
  • src/coreclr/vm/dllimport.cpp: Language not supported

@ghost ghost added the area-Interop-coreclr label Mar 29, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 29, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@frabert
Copy link
Author

frabert commented Mar 29, 2025

@dotnet-policy-service agree

@jkotas
Copy link
Member

jkotas commented Mar 29, 2025

What do you need the Windows x86 fascall calling convention for?

Fascall is 32-bit window x86 specific calling convention. Windows x86 is a legacy platform that we are not very interested in improving and adding new features to.

Please do comment on all the obvious issues I missed,

The obvious issue is that it has zero testing. Once you add comprehensive tests, you are very likely going to run into actual issues that would need to be fixed. cc @dotnet/jit-contrib Anything known?

@frabert
Copy link
Author

frabert commented Mar 29, 2025

What do you need the Windows x86 fastcall calling convention for?

The short of it is that I'm embedding CoreCLR and want to use it to detour various functions, some of which use the fastcall calling convention. Due to the nature of the application, I have no say in what architecture (x86) or calling convention (Fastcall) I can use.

it has zero testing

Noted! I have done some very preliminary testing locally, but without knowing the internal details of the JIT engine it's hard for me to know where the corner cases are, and I didn't feel like spending a bunch of time investigating this further only to then discover this is not going anywhere...

@jakobbotsch
Copy link
Member

__fastcall happens to match the managed calling convention in large parts, and the JIT falls back to the managed calling convention in this scenario. That's why it appears to work. But the calling conventions do differ in some key aspects, like handling of structs where the managed calling convention will enregister trivial wrapper structs, which fastcall should not be doing.

With that said I do not think it would be particularly hard to expand the support given recent work done in the JIT on ABI unification/clean up. It should just need some special handling in X86Classifier::Classify.

@jakobbotsch
Copy link
Member

It also looks like __fastcall does not enregister the return buffer. That's also different from the managed calling convention.

@frabert
Copy link
Author

frabert commented Mar 29, 2025

Does the fact that [UnmanagedCallersOnly(CallConvs = [typeof(CallConvFastcall)])] already works without this PR change anything in this discussion?

@jakobbotsch
Copy link
Member

Does the fact that [UnmanagedCallersOnly(CallConvs = [typeof(CallConvFastcall)])] already works without this PR change anything in this discussion?

It's most likely just an oversight that this does not throw some kind of compilation exception. If you try some of the things I pointed at above you will likely see incorrect results in interop with native code.

@AaronRobinsonMSFT
Copy link
Member

@frabert I appreciate the desire to get this to work, but it isn't something that is currently on our roadmap. The x86 target isn't something we tend to invest heavily in and to properly add support would represent a fair bit of effort. I've placed the issue in "Future" and if there is broader interest from the community we can look at this. For now I'm going to close this PR as it isn't likely to move forward without team support and at present it isn't there.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Interop-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoking an unmanaged fastcall function pointer under x86 Windows fails at runtime

4 participants