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

Incorrect alignment of reference struct within a struct on Mono #90531

Closed
UnityAlex opened this issue Aug 14, 2023 · 1 comment · Fixed by #90632 or Unity-Technologies/mono#1811
Closed
Assignees

Comments

@UnityAlex
Copy link
Contributor

Description

When creating a struct that has another struct within and reading/writing the alignment used is incorrect. This leads to incorrect memory being accessed for values .

Reproduction Steps

Build & run the following

using System.Runtime.InteropServices;

public class Program
{
    [StructLayout(LayoutKind.Explicit, Size = 8)]
    public struct StructA
    {
        [FieldOffset(0)]
        public long Field0;
    }

    public struct StructB
    {
        public int Field0;
        // padded 4
        public StructA Field1;
        // no padding
        public int Field2;
    }
    
    public static unsafe void TestAlignmentOfExplicitStruct()
    {
        void* mem = stackalloc byte[24];
        Marshal.WriteInt32((IntPtr)mem, 0, 1);
        Marshal.WriteInt64((IntPtr)mem, 8, 2);
        Marshal.WriteInt32((IntPtr)mem, 16, 3);

        var s = Marshal.PtrToStructure<StructB>((IntPtr)mem);

        Console.WriteLine($"s.Field0 {s.Field0}");
        Console.WriteLine($"s.Field1.Field0 {s.Field1.Field0}");
        Console.WriteLine($"s.Field2 {s.Field2}");
    }
    
    public static void Main()
    {
        TestAlignmentOfExplicitStruct();
    }
}

Expected behavior

Output:

s.Field0 1
s.Field1.Field0 2
s.Field2 3

Actual behavior

Output

s.Field0 1
s.Field1.Field0 8589934592
s.Field2 0

Regression?

Not that I'm aware of.

Known Workarounds

Using explicit struct layout avoids the alignment issue.

Configuration

Version: 8.0.100-preview.3.23178.7
OS and (architecture): Reproduced on both OSX (arm64) and Windows (x64)
Mono only.

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 14, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 14, 2023
@vcsjones vcsjones added area-VM-meta-mono and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Aug 14, 2023
@SamMonoRT
Copy link
Member

cc @vargaz

@vargaz vargaz self-assigned this Aug 15, 2023
vargaz added a commit to vargaz/runtime that referenced this issue Aug 15, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 15, 2023
@ghost ghost removed in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner labels Aug 16, 2023
UnityAlex pushed a commit to Unity-Technologies/mono that referenced this issue Aug 16, 2023
UnityAlex pushed a commit to Unity-Technologies/mono that referenced this issue Aug 16, 2023
UnityAlex pushed a commit to Unity-Technologies/mono that referenced this issue Aug 16, 2023
DanRandom pushed a commit to Unity-Technologies/mono that referenced this issue Aug 23, 2023
DanRandom pushed a commit to Unity-Technologies/mono that referenced this issue Aug 23, 2023
DanRandom pushed a commit to Unity-Technologies/mono that referenced this issue Aug 23, 2023
DanRandom pushed a commit to Unity-Technologies/mono that referenced this issue Aug 23, 2023
DanRandom pushed a commit to Unity-Technologies/mono that referenced this issue Aug 23, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants