Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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.
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.
Oh, also, if I put
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.
ENetAddress contains an int field, so it requires alignment 4. 34 aligned to 4 is 36.
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).
Could you create a testcase we can you to reproduce this ?
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.
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.
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.
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.
Regarding blittable by the way: http://msdn.microsoft.com/en-us/library/75dwhxf7.aspx
This was fixed, at least the test case that shows "This should be 42" now shows "42" as expected.