aspnet / dnx

OBSOLETE - see readme
Other
963 stars 224 forks source link

DNX compilation handles fixed byte array incorrectly #3414

Closed yizhang82 closed 8 years ago

yizhang82 commented 8 years ago

Moved from https://github.com/dotnet/coreclr/issues/4854 on behalf of @jageall

If i create an unsafe struct with an explicit layout and add a fixed byte array to access the data in a lib targeting dotnet5.4, and then try to access that struct in a unit test it compiles ok, but I get a runtime error. The unit test lib target dnxcore50 as per the instructions on the xunit site. If i try and access via a method in a third lib also targeting dotnet5.4 it works fine.

I have a small repo containing a reproduction of the issue here https://github.com/jageall/DNXRuntimeIssueWithFIxedByteArray

if this behaviour is by design I would expect a compile time error, not a runtime error

yizhang82 commented 8 years ago

Following is my preliminary investigation:

I'm pretty sure this is a DNX specific issue. This is the code in question: Assert.Equal(0x34, entry.Bytes[0]); If I use csc (from VS) manually to compile FixedIssue.dll - I get the correct code in IL:

IL_0018: ldc.i4.s 52 IL_001a: ldloca.s V_0 IL_001c: ldflda valuetype [ClassLibrary1]ClassLibrary1.RuntimeIssue/'e__FixedBuffer' [ClassLibrary1]ClassLibrary1.RuntimeIssue::Bytes IL_0021: ldflda uint8 [ClassLibrary1]ClassLibrary1.RuntimeIssue/'e__FixedBuffer'::FixedElementField IL_0026: conv.u IL_0027: ldind.u1 IL_0028: call void [xunit.assert]Xunit.Assert::Equal(!!0, !!0)

However, with DNX, it generates the following code:

IL_0018: ldc.i4.s 52 IL_001a: ldloc.0 IL_001b: ldfld uint8* [ClassLibrary1]ClassLibrary1.RuntimeIssue::Bytes IL_0020: ldind.u1 IL_0021: call void [xunit.assert]Xunit.Assert::Equal(!!0, !!0)

Since the Bytes field is a opaque value type, ldfld uint8* is failing with MissingFieldException.

My best guess is that DNX is using an old version of Roslyn compiler or something along those lines.

BrennanConroy commented 8 years ago

DNX is retired