In mono arm64, when creating a delegate that returns 16byte-size-struct, the generated code is wrong
Description
In mono arm64, when creating a delegate that returns 16byte-size-struct, the generated code is wrong
Reproduction Steps
The following code will cause crash under mono arm64: `
class Test
{
public static void Init()
{
new Thread(() =>
{
try
{
Thread.Sleep(1000 * 5);
unsafe
{
var addrGetLocalURL = XMono.Dlsym("libUE4.so" ,"_ZNK6UWorld11GetLocalURLEv");
var funcGetLocalURL = Marshal.GetDelegateForFunctionPointer(addrGetLocalURL, typeof(FuncGetLocalURL));
var funcGetLocalURLAddr = Marshal.GetFunctionPointerForDelegate(funcGetLocalURL);
var uworld = *(void**)XMono.Dlsym("libUE4.so", "GWorld");
var ret = funcGetLocalURL.DynamicInvoke(new IntPtr(uworld)); // crashed
}
}
catch(Exception e)
{
XMono.Logd(e.ToString());
}
}).Start();
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
struct FString
{
public IntPtr alloc;
public int num;
public int max;
}
delegate FString FuncGetLocalURL(IntPtr world);
}
`
Cause Analysis:
The target function is non-static and is defined as follows, where FString is a 16-bytes-struct:

When returning 16-bytes-struct, the generated code uses general registers (such as X1) to pass the struct address:

When returning 17-bytes-struct, use the X8 register to pass the struct address:

According to the arm64 ABI document, using x8 is correct:

This is also in line with the code of the target function:

I will create a PR to fix this problem, and hope to pass.
Expected behavior
Invoke is successful and returns normally
Actual behavior
crashed
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
@evilzhou - is this still an issue and did you have any updated PR ? Moving to next milestone, but will consider backporting the fix.
cc @fanyang-mono for Arm64 visibility
Closing issue, no recent repro. @evilzhou please re-open with any updated PR/repro where you hit this.