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

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

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

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 Mark Sojoonei 2015-10-06 14:23:31 UTC
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
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:
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

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:
Windows: https://gist.github.com/Arpit360/c86c2cd62c239198fadc
Comment 6 Mark Sojoonei 2015-10-09 08:00:14 UTC
This one is more obvious:
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 

Terminal output: https://gist.github.com/Arpit360/51f7074fabf80d5de224

I have also checked this issue with Cycle 6 latest build but I am not observing the issue:

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.