Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StructLayout work on Mono 64-bit #22794

Conversation

KirillOsenkov
Copy link
Member

Need to work around a Mono 64 behavior where on 64-bit the size of the struct would be 24 and not 20 due to alignment (it ignores Size = 20, but Pack = 4 does it). Without this fix the ctor would throw because it would use sizeof(Sha1Hash) == 24.

Customer scenario

When Roslyn is hosted in VS for Mac (64-bit Mono on Mac) reading metadata references crashes with this stack:

1) TestBug58060 (MonoDevelop.CSharp.Refactoring.CSharpFindReferencesProviderTests.TestBug58060)
   System.AggregateException : One or more errors occurred.
  ----> System.ArgumentException : checksum must be a SHA-1 hash
Parameter name: checksum
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/5533/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 
  at Microsoft.CodeAnalysis.FindSymbols.SyntaxTreeIndex+<GetChecksumAsync>d__53.MoveNext () [0x00087] in <b063dbb7454e41f1a6073b72b22d712e>:0 

Workarounds, if any

None known

Risk

Low, fix verified

Performance impact

None

Is this a regression from a previous update?

Regression since June 2017

Root cause analysis:

Test Roslyn on Mono 64-bit

How was the bug found?

Adoption of Roslyn by VS for Mac team

@KirillOsenkov KirillOsenkov changed the base branch from features/vs-for-mac-packages to features/vs-for-mac-refactorings October 20, 2017 20:01
@sharwell
Copy link
Member

Looks like this needs to be rebased?

Need to work around a Mono 64 behavior where on 64-bit the size of the struct would be 24 and not 20 due to alignment (it ignores Size = 20, but Pack = 4 does it). Without this fix the ctor would throw because it would use sizeof(Sha1Hash) == 24.
@KirillOsenkov
Copy link
Member Author

Rebased.

@KirillOsenkov
Copy link
Member Author

And this is a Mono runtime bug where we discuss whether the Mono runtime needs to be updated to match the CLR behavior here: https://bugzilla.xamarin.com/show_bug.cgi?id=60298

/// struct would be 24 and not 20 due to alignment (it ignores Size = 20, but Pack = 4 does it).
/// Without this fix the ctor would throw because it would use sizeof(Sha1Hash) == 24.
/// </remarks>
[StructLayout(LayoutKind.Explicit, Size = 20, Pack = 4)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this even use StructLayout at all? Are we using this for blitting with something else? Can we update the doc to point to where that is being done?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a question for @sharwell I presume. I would prefer to avoid digging too deep into this code since I don't understand it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonmalinowski Considering this is a private nested type, the current comment and usage ~50 lines above seems to address the situation. It was certainly enough for Kirill to make the correct change in response to the Mono bug (coming in from never seeing it before).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My motto is if somebody had to ask, your comments are never sufficient. 😄 Is the blitting above the only reason? If I just rewrote the ToString to not allocate the array entirely...could I remove this?

/// struct would be 24 and not 20 due to alignment (it ignores Size = 20, but Pack = 4 does it).
/// Without this fix the ctor would throw because it would use sizeof(Sha1Hash) == 24.
/// </remarks>
[StructLayout(LayoutKind.Explicit, Size = 20, Pack = 4)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonmalinowski Considering this is a private nested type, the current comment and usage ~50 lines above seems to address the situation. It was certainly enough for Kirill to make the correct change in response to the Mono bug (coming in from never seeing it before).

@jasonmalinowski
Copy link
Member

The windows_debug_unit64_prtest failure is another instance of #21563.

@jasonmalinowski
Copy link
Member

@dotnet-bot retest windows_debug_unit64_prtest please.

@jasonmalinowski jasonmalinowski merged commit ace949b into dotnet:features/vs-for-mac-refactorings Oct 23, 2017
@KirillOsenkov KirillOsenkov deleted the FixStructLayout branch October 23, 2017 22:06
@DustinCampbell
Copy link
Member

@jasonmalinowski, @sharwell: we're seeing this same issue with Roslyn 2.6.0 on OmniSharp (which runs on Mono). Currently, OmniSharp is blocked taking Roslyn 2.6.0 and C# 7.2 because of this problem. Could we get this merged to master?

@sharwell
Copy link
Member

💭 According to ECMA-335 §II.22.8 (ClassLayout), it seems that Pack should not be specified for LayoutKind.Explicit. Do we have a status for the underlying Mono bug?

@DustinCampbell
Copy link
Member

Looking at the discussion of the Mono bug that Kirill filed, it appears that there's some dispute as to whether it is a bug or not: https://bugzilla.xamarin.com/show_bug.cgi?id=60298.

@DustinCampbell
Copy link
Member

At the moment, this completely blocks C# 7.2 in VS Code.

@sharwell
Copy link
Member

👍 Can you file a separate bug for this and assign it to me? I'm working on a solution.

@DustinCampbell
Copy link
Member

Issue submitted: #23722.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants