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 (show other bugs)
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-02-16 03:18 UTC (History)
8 users (show)

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


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.

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