C
C#3w ago
peppy

Invoking ``__fastcall`` function in x86 hosting scenario (.NET 9) by any means possible

During a reversing project of a MSVC 2012 compiled x86 binary I ran into an exported function of the following form that I must invoke:
__fastcall int fn_00a28ba0(int arg1, byte* arg2, int arg3, byte* arg4, int arg5);
__fastcall int fn_00a28ba0(int arg1, byte* arg2, int arg3, byte* arg4, int arg5);
For any which reason this calling convention is not supported in Marshal.GetDelegateForFunctionPointer or in direct function pointer invocation. (v. https://github.com/dotnet/runtime/issues/113851 and similar) I understand that this limitation is unlikely to ever be lifted, or at the very least will not be lifted any time soon. Workarounds such as using managed calling convention in some related issues (https://github.com/dotnet/runtime/pull/114037#issuecomment-2764215012 etc.) all result in AVs for this specific signature. If one were obstinate enough to insist on working around this nonetheless, what would have to be done in native code to facilitate calling this function from .NET? - Would hooking the original call with a __stdcall/__cdecl native function that simply calls through to the original (then, in .NET, getting a delegate* unmanaged[{Stdcall|Cdecl}] to this 'wrapper') suffice? - Even just inspecting the arguments to the call would be highly useful- can a reverse P/Invoke be set up from a hosting scenario, to be called from a native hook? - Is there a pure .NET workaround to this?
4 Replies
mtreit
mtreit3w ago
This sounds like a question the folks in #allow-unsafe-blocks might be able to answer. Maybe ask there...
Aaron
Aaron3w ago
I would recommend having a native component that calls this function with a supported calling convention and then using that wrapper
viceroypenguin
i'd say use a C++/CLI library to proxy it into a .net method
sibber
sibber3w ago
exporting a wrapper thay you LinraryImport is probably a lot simpler

Did you find this page helpful?