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

Fix SafeHandle debug-only _ctorStackTrace for mono #82376

Merged
merged 4 commits into from
Feb 28, 2023

Conversation

stephentoub
Copy link
Member

Try to fix mono failure
Fixes #82308

@stephentoub stephentoub changed the title Move SafeHandle debug-only _ctorStackTrace to not be first field Fix SafeHandle debug-only _ctorStackTrace for mono Feb 21, 2023
@stephentoub stephentoub removed the NO-REVIEW Experimental/testing PR, do NOT review it label Feb 21, 2023
@stephentoub
Copy link
Member Author

/azp list

@azure-pipelines
Copy link

CI/CD Pipelines for this repository:

@stephentoub
Copy link
Member Author

/azp run runtime-community

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephentoub
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@lambdageek lambdageek left a comment

Choose a reason for hiding this comment

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

This is technically fine, but maybe we should do something a bit cleaner.

Also I really think we need

#if MONO
[StructLayout(LayoutKind.Sequential)]
#endif

Comment on lines +31 to 33
#if DEBUG && CORECLR
private readonly string? _ctorStackTrace;
#endif
Copy link
Member

Choose a reason for hiding this comment

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

@stephentoub questions:

  1. Can we move this member to the end of the set of instance members. (And I assume do the related changes on the native side in coreclr)
  2. For Mono we should really have [StructLayout(Sequential)] on any class where we care about the field layout. Otherwise we're just relying on auto layout coincidentally getting the fields in the right order.
  3. Should we make SafeHandle.Mono.cs and SafeHandle.CoreCLR.cs that just contain the instance field definitions? #if DEBUG && CORECLR accomplishes the same thing but feels more fragile.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we move this member to the end of the set of instance members. (And I assume do the related changes on the native side in coreclr)

I'd previously done that, and I believe the issues I hit were to do with alignment, though it's also possible I just messed something up or neglected to edit something I needed to edit.

For Mono we should really have [StructLayout(Sequential)] on any class where we care about the field layout. Otherwise we're just relying on auto layout coincidentally getting the fields in the right order.

This is irrespective of this change or this field, right? You're saying mono is expecting that handle is first and it may not be.

Should we make SafeHandle.Mono.cs and SafeHandle.CoreCLR.cs that just contain the instance field definitions? #if DEBUG && CORECLR accomplishes the same thing but feels more fragile.

I don't love the duplication there; that feels more fragile to me, actually. If this field is problematic, we can just remove it and I can put it back in the future should someone want to do another push like I did to drive down SafeHandle finalization debt.

Copy link
Member

Choose a reason for hiding this comment

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

This is irrespective of this change or this field, right? You're saying mono is expecting that handle is first and it may not be.

Yea, it's not required for this PR (all the other fields are non-reference so auto layout will get things right). Just might as well do some future-proofing

Copy link
Member

Choose a reason for hiding this comment

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

Overall I'm fine with the PR as is. It would be nice to get the layout attribute in here, but we could also do a separate PR for that. If splitting the file feels more fragile to you @stephentoub , I'm ok with the PR as is.

Copy link
Member Author

Choose a reason for hiding this comment

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

@lambdageek, mono actually already has StructLayout applied:

// Mono runtime relies on exact layout
[StructLayout(LayoutKind.Sequential)]
public abstract partial class SafeHandle

@stephentoub stephentoub merged commit 5e3dd8d into dotnet:main Feb 28, 2023
@stephentoub stephentoub deleted the fixmonosafehandle branch February 28, 2023 22:43
@ghost ghost locked as resolved and limited conversation to collaborators Mar 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SafeHandle marshalling broken with Mono
2 participants