Bug 34598 - Crash when accessing static fields
Summary: Crash when accessing static fields
Status: VERIFIED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Windows
: Normal blocker
Target Milestone: ---
Assignee: Alex Rønne Petersen
URL:
Depends on:
Blocks:
 
Reported: 2015-10-06 14:23 UTC by Mark Sojoonei
Modified: 2016-04-14 05:24 UTC (History)
8 users (show)

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


Attachments
test program (434 bytes, text/plain)
2015-10-06 14:23 UTC, Mark Sojoonei
Details


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:
Status:
VERIFIED FIXED

Description Mark Sojoonei 2015-10-06 14:23:31 UTC
Commits
https://github.com/mono/mono/commit/d2efa789ac432bbcc92483d8f6e0b17c3b23efff
and
https://github.com/mono/mono/commit/0686c213583a70bde97c8d04e0748e47416bfaf9
break code generation when using static fields in a class with attribute StructLayout(LayoutKind.Explicit).
Attached is a test program that crashes in the .cctor when trying to set up the static field.
Comment 1 Mark Sojoonei 2015-10-06 14:23:53 UTC
Created attachment 13208 [details]
test program
Comment 2 Alex Rønne Petersen 2015-10-08 16:19:29 UTC
Does this only happen on Windows? I can't repro on Linux (amd64) with Mono master.
Comment 3 Mark Sojoonei 2015-10-09 06:13:22 UTC
It does not seem to crash in Linux, but it's not working correctly either. If you add a
Console.WriteLine(Test.Zero.Value);
in the test program, it will print a bogus value.
It seems that the static field is being treated as a special (thread/context) static in the Mono code, despite not being one.
Comment 4 Mark Sojoonei 2015-10-09 06:23:56 UTC
This line:
https://github.com/mono/mono/blob/cf5f99d83aa67f2a8e05d65d0be6949aaf904e89/mono/metadata/class.c#L1622
seems to be turning every static field in a StructLayout(LayoutKind.Explicit) struct into a special static (-1 offset).
Comment 5 Arpit Jha 2015-10-09 07:18:19 UTC
I have checked this issue on Mac and windows machine and not able to reproduce this issue.

Steps I have folowed to reproduce this issue:
1.Create a Console App in XS.
2.Implement the mentioned code in https://bugzilla.xamarin.com/attachment.cgi?id=13208

Screencast:
Window:http://www.screencast.com/t/ZsvHza7ea
Mac: http://www.screencast.com/t/KzafOvrPf4

Please let me know If I missed anything to reproduce this issue and could you please provide us the build info on which this issue is occuring

Environment Info:
MAC:https://gist.github.com/Arpit360/7a628039537e53dc4e5a
Windows: https://gist.github.com/Arpit360/c86c2cd62c239198fadc
Comment 6 Mark Sojoonei 2015-10-09 08:00:14 UTC
This one is more obvious:
http://pastie.org/private/96ngqkookod14smig63p3a
http://i.imgur.com/3jHiAyq.png
Comment 7 Arpit Jha 2015-10-12 09:23:43 UTC
I have checked this issue with latest master build and able to reproduce this issue with the help of comment 6 
MonoFramework-MDK-4.3.0.1564.macos10.xamarin.x86_0421c0cff36b33a58c16930d61227b74633c1e99
XamarinStudio-5.11.0.291_a7fd52f69188903a1f7d6a9a4748b88f41bc509b

Terminal output: https://gist.github.com/Arpit360/51f7074fabf80d5de224
Screencast:http://www.screencast.com/t/ahSA9kTcb 

I have also checked this issue with Cycle 6 latest build but I am not observing the issue:
MonoFramework-MDK-4.2.1.69.macos10.xamarin.x86_d8b68fd5787b116201969176d88deb08924059d8
XamarinStudio-5.10.0.815_1f1910169d5433415cac632094c57272080ef9df

Terminal output: https://gist.github.com/Arpit360/3a06cd0df9c20d885100
Screencast: http://www.screencast.com/t/sP0cUqCNGFCj
Comment 8 Arpit Jha 2015-10-12 09:29:45 UTC
An update to comment 7:
Environment Info:
OSX : 10.10.5 (14F27)
Comment 9 Zoltan Varga 2015-10-12 14:37:30 UTC
I reverted 0686c213583a70bde97c8d04e0748e47416bfaf9/d2efa789ac432bbcc92483d8f6e0b17c3b23efff
until this is fixed, since it also seems to break Xamarin.Android.
Comment 10 Rodrigo Kumpera 2015-10-26 16:08:48 UTC
This was reverted on master and it does affect 4.0 / 4.2.

Alexp, please update this bug once the proper fix lands on master.
Comment 11 Alex Rønne Petersen 2015-10-28 05:28:18 UTC
Since this was just a small optimization and I have more important stuff on my plate, let's just leave it reverted for now. I might get back to it at some point.

The commits were never pushed to 4.2, so it won't affect it.
Comment 13 Zoltan Varga 2015-12-09 21:06:42 UTC
This bug is caused by the commit in comment #9 which was reverted, so it should be fixed.
Comment 14 Peter Collins 2015-12-09 22:03:20 UTC
Sorry my mistake. Do we want to use this bug to track the pending work/optimization mentioned in Comment #10 and Comment #11? Or is something else tracking that?
Comment 15 Zoltan Varga 2015-12-09 23:24:09 UTC
I think we should just close it.
Comment 16 Peter Collins 2015-12-09 23:31:10 UTC
Resolving as fixed as per comment #13 and #15, this was reopened by mistake.