runtime icon indicating copy to clipboard operation
runtime copied to clipboard

In mono arm64, when creating a delegate that returns 16byte-size-struct, the generated code is wrong

Open evilzhou opened this issue 5 years ago • 3 comments

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: image

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

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

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

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

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 avatar Oct 12 '21 03:10 evilzhou

@evilzhou - is this still an issue and did you have any updated PR ? Moving to next milestone, but will consider backporting the fix.

SamMonoRT avatar Aug 10 '22 19:08 SamMonoRT

cc @fanyang-mono for Arm64 visibility

SamMonoRT avatar Aug 10 '22 19:08 SamMonoRT

Closing issue, no recent repro. @evilzhou please re-open with any updated PR/repro where you hit this.

SamMonoRT avatar Jul 25 '23 15:07 SamMonoRT