Skip to content

Crossgen2 incorrectly takes Size into account for Auto layout #1770

Closed

Description

@MichalStrehovsky

Should return 4 but returns 16.

using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Auto, Size = 16)]
    struct GUID
    {
        private int align;

        static unsafe int Main()
        {
            return sizeof(GUID);
        }
    }

Activity

  1. added this to the 5.0 milestone on Jan 15, 2020
  2. added
    untriaged
    on Jan 15, 2020
  3. tannergooding commented on Jan 15, 2020

    @tannergooding
    Member

    We should update the documentation of StructLayoutAttribute to explicitly call out the cases where Size is ignored (if it is less than the actual size of the struct and apparently for LayoutKind.Auto as well).

  4. briansull commented on Jan 15, 2020

    @briansull
    Contributor

    In your example above you should name your arbitrary struct something other than GUID.
    As the size of the well known Windows type "GUID" is actually 16 bytes.

  5. MichalStrehovsky commented on Jan 15, 2020

    @MichalStrehovsky
    MemberAuthor

    I just copy pasted the name from a struct in the loader test mentioned in #1763 when I was playing with it. That struct is not a guid either but at least it's 16 bytes.

    We can rename it for the regression test to Foo, in line with good naming practices.

  6. jkotas commented on Jan 16, 2020

    @jkotas
    Member

    We should update the documentation of StructLayoutAttribute to explicitly call out the cases where Size is ignored

    It is never ignored (as long it is valid). It is always respected for the unmanaged layout of the type. The documentation says that StructLayoutAttribute controls the unmanaged layout of the type, and that it controls the managed layout only when the type is blittable.

  7. tannergooding commented on Jan 16, 2020

    @tannergooding
    Member

    Would the above case not qualify as blittable?

  8. MichalStrehovsky commented on Jan 16, 2020

    @MichalStrehovsky
    MemberAuthor

    Would the above case not qualify as blittable?

    It doesn't qualify because it doesn't have layout (only Sequential/Explicit types "have layout").

    inline BOOL EEClass::IsBlittable()
    {
    LIMITED_METHOD_CONTRACT;
    // Either we have an opaque bunch of bytes, or we have some fields that are
    // all isomorphic and explicitly layed out.
    return (HasLayout() && GetLayoutInfo()->IsBlittable());
    }

  9. tannergooding commented on Jan 16, 2020

    @tannergooding
    Member

    Wouldn't that mean that Size is effectively ignored/invalid on StructLayout=Auto then?

    Auto can't be marshalled and therefore can't have an unmanaged size, it also isn't blittable and therefore won't be impactful on the managed side either.

  10. jkotas commented on Jan 16, 2020

    @jkotas
    Member

    Wouldn't that mean that Size is effectively ignored/invalid on StructLayout=Auto then?

    Right, it is useless on StructLayout=Auto, just like all other managed/unmanaged marshaling directives.

  11. added a commit that references this issue on Jan 16, 2020
    fda184d
  12. added a commit that references this issue on Jan 17, 2020
    0fa59c9
  13. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-ReadyToRununtriaged

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions