Bug 18941 - Calling Marshal.SizeOf() with an Empty struct using Sequential or Explicit returns 0
Summary: Calling Marshal.SizeOf() with an Empty struct using Sequential or Explicit re...
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: Interop (show other bugs)
Version: 3.2.x
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Alexander Kyte
URL:
Depends on:
Blocks:
 
Reported: 2014-04-10 21:11 UTC by Riley Labrecque
Modified: 2015-12-14 13:20 UTC (History)
6 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Riley Labrecque 2014-04-10 21:11:38 UTC
[code]using System;
using System.Runtime.InteropServices;

public struct EmptyStruct {
}

[StructLayout(LayoutKind.Sequential)]
public struct EmptySequentialStruct {
}

[StructLayout(LayoutKind.Explicit)]
public struct EmptyExplicitStruct {
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct EmptySequentialPackStruct {
}

[StructLayout(LayoutKind.Explicit, Pack = 4)]
public struct EmptyExplicitPackStruct {
}

class Program {
    static void Main(string[] args) {
        Console.WriteLine("SizeOf EmptyStruct: " + Marshal.SizeOf(typeof(EmptyStruct)));
        Console.WriteLine("SizeOf EmptySequentialStruct: " + Marshal.SizeOf(typeof(EmptySequentialStruct)));
        Console.WriteLine("SizeOf EmptyExplicitStruct: " + Marshal.SizeOf(typeof(EmptyExplicitStruct)));
        Console.WriteLine("SizeOf EmptySequentialPackStruct: " + Marshal.SizeOf(typeof(EmptySequentialPackStruct)));
        Console.WriteLine("SizeOf EmptyExplicitPackStruct: " + Marshal.SizeOf(typeof(EmptyExplicitPackStruct)));
    }
}[/code]


Microsoft .NET:
SizeOf EmptyStruct: 1
SizeOf EmptySequentialStruct: 1
SizeOf EmptyExplicitStruct: 1
SizeOf EmptySequentialPackStruct: 1
SizeOf EmptyExplicitPackStruct: 1

Mono 3.2.6 x86 on OSX:
SizeOf EmptyStruct: 1
SizeOf EmptySequentialStruct: 0
SizeOf EmptyExplicitStruct: 0
SizeOf EmptySequentialPackStruct: 0
SizeOf EmptyExplicitPackStruct: 0
Comment 1 Ken Cowan 2014-08-30 19:10:34 UTC
Hi,

I can repro the bug. I see code in mcs that is attempting to handle this case, but obviously isn't working.   In the meantime, you could add an explicit Size=1 as a workaround.

 KC
Comment 2 Marek Safar 2015-04-01 04:38:09 UTC
This is no mcs issue as same .exe works on .net and vice versa csc compiled code does not work on mono
Comment 3 Alexander Kyte 2015-04-01 11:16:14 UTC
Mareks, we don't add the default size to the attributes passed into a method on a TypeBuilder so it overwrites the size that we set elsewhere.
Comment 4 Alexander Kyte 2015-04-01 11:24:34 UTC
Mono looks up the size from the metadata table and it's 0.
Comment 5 Marek Safar 2015-04-01 13:11:40 UTC
mcs is fine, if exactly same .exe works on .net

Note You need to log in before you can comment on or make changes to this bug.