Bug 11899 - OffsetOf identical for two cases, only one deserializes properly
Summary: OffsetOf identical for two cases, only one deserializes properly
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2013-04-23 09:11 UTC by James Bellinger
Modified: 2017-08-18 19:58 UTC (History)
4 users (show)

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

Repro case. All the contents being blittable, it is a blittable struct, and therefore this should work. It does on MS.NET. It does not on Mono. (8.18 KB, application/x-zip)
2013-04-24 09:55 UTC, James Bellinger

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 James Bellinger 2013-04-23 09:11:09 UTC

In my code (at http://www.zer7.com/files/oss/enetcs/ENet_1_3_6_3.zip, requires libenet1a package to work),

the Native.ENetPeer structure comes back wrong from P/Invoke on Mono (x86-64 Linux).

In particular, its address property is wrong. Marshal.OffsetOf suggests offset 36. There are 34 bytes before address (6-byte ENetAddress).

Now, if I use Pack=1 and put 'ushort padding;' right before address, so that it's explicitly at 36 instead of being aligned by Mono at 36, again, Marshal.OffsetOf suggests offset 36.

However, this works correctly. Pack=1 without the padding is two bytes off as one would expect.

So, clearly, Mono has two different behaviors for identical OffsetOf responses.

Please look into this. I can make a workaround, but it has affected someone using my library and it's a rather subtle thing to cause a Mono incompatibility.

Thank you

Comment 1 James Bellinger 2013-04-23 09:44:24 UTC
Oh, also, if I put

uint addressHost;
ushort addressPort;

directly into the outer struct, it works perfectly. So it's not just a question of OffsetOf I believe, but of failing to deserialize back from P/Invoke the inner structure.
Comment 2 Zoltan Varga 2013-04-24 03:14:12 UTC
ENetAddress contains an int field, so it requires alignment 4. 34 aligned to 4 is 36.
Comment 3 James Bellinger 2013-04-24 09:11:05 UTC
Yes, it's 36 on both MS.NET and Mono. However, it doesn't work if it's inside the struct. Deserializes fine if it's just int and ushort on the outer. Not even considering the port, the *host* is wrong, and not in an offset-by-two manner (Pack=1 without padding gives an offset-by-two value, inside the struct I get a value that's clearly not from either).
Comment 4 Zoltan Varga 2013-04-24 09:31:18 UTC
Could you create a testcase we can you to reproduce this ?
Comment 5 James Bellinger 2013-04-24 09:55:17 UTC
Created attachment 3865 [details]
Repro case. All the contents being blittable, it is a blittable struct, and therefore this should work. It does on MS.NET. It does not on Mono.
Comment 6 Zoltan Varga 2013-04-27 09:20:15 UTC
I see the problem. We align struct fields to 4/8 bytes instead of their natural alignment, probably for performance reasons. This should only affect the managed representation of the structure, so if you pass/return these structures normally in pinvoke, or use Marshal.PtrToStructure/StructureToPtr, it should work.
Comment 7 James Bellinger 2013-04-27 12:05:39 UTC
I see why Mono would do that. However...

The structure I have there only contains blittable elements and is therefore blittable. Blittable structs are not copied when marshaled in .NET. Therefore by implication their internal structure has to match the StructLayout.

Comment 8 James Bellinger 2013-04-27 12:06:25 UTC
This is incidentally also why one can create a managed union in .NET with FieldOffset and Explicit StructLayout. If I understand correctly, such a construction would fail to work in Mono at present.
Comment 9 James Bellinger 2013-04-27 12:08:30 UTC
Regarding blittable by the way: http://msdn.microsoft.com/en-us/library/75dwhxf7.aspx
Comment 10 Miguel de Icaza [MSFT] 2017-08-18 19:58:43 UTC
This was fixed, at least the test case that shows "This should be 42" now shows "42" as expected.