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...
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: master
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Katelyn Gadd
Depends on:
Reported: 2014-04-10 21:11 UTC by Riley Labrecque
Modified: 2018-03-19 13:37 UTC (History)
8 users (show)

Tags: bugpool-archived
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

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

public struct EmptyStruct {

public struct EmptySequentialStruct {

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)));

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

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.

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
Comment 6 Ludovic Henry 2018-01-24 21:13:27 UTC
I can reproduce with Mono (master/117468d740a)
Comment 7 Katelyn Gadd 2018-02-16 03:18:57 UTC
We also don't handle explicit sizes correctly. Mono will honor this attribute:
[StructLayout(LayoutKind.Explicit, Size = 0)]

While .NET framework silently upgrades the size to 1. Size = 1 works on both runtimes.
Comment 8 Ludovic Henry 2018-02-20 19:19:53 UTC
Fixed with https://github.com/mono/mono/pull/7142
Comment 9 Ludovic Henry 2018-02-23 16:53:16 UTC
Merged in master with https://github.com/mono/mono/commit/798c5efa52ada6cd4057f385b2bc7b78b57003ed