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

Move DllImportGenerator to use DisableRuntimeMarshalling for its blittable classification #64279

Merged

Conversation

jkoritzinsky
Copy link
Member

@jkoritzinsky jkoritzinsky commented Jan 25, 2022

Instead of using [BlittableType] attributes to track the runtime's blittable concept through the type system, use the new DisableRuntimeMarshalling attribute and use "non-auto-layout unmanaged" as our definition for "requires no marshalling".

This PR updates Microsoft.Interop.SourceGeneration to require [assembly:DisableRuntimeMarshalling] to be applied whenever a user defines a source-generated P/Invoke with a native parameter or return type that isn't a primitive, pointer, or function pointer. Additionally, since the current default marshalling for bool in the generator is as a Windows BOOL, allow bool to be specified for easy back-compatibility for now.

Because the runtime repo is an early adopter of the generator, this PR also updates the runtime to build with marshalling disabled where required. Some libraries (System.Drawing.Common, OleDb) use a lot of non-blittable marshalling in delegate-based interop scenarios or COM-focused marshalling and don't have any source-generated P/Invokes that require the DisableRuntimeMarshalling attribute, so those assemblies don't have the attribute applied to try to keep this PR a little smaller.

This PR also includes a few fixes that were required to get all tests passing:

  • Fixes in the managed type system and CoreCLR around enum handling
  • Fixes in Mono around unmanaged calli instructions in dynamically emitted methods
  • Improved [In, Out, MarshalAs] forwarding support for partially supported P/Invokes on downlevel platforms
  • A fix in System.Drawing.Common to handle a mis-sized struct interop issue that happened to never be a problem because of how IL-stubs work but was exposed as a bug when I made some of System.Drawing.Common a little more blittable in an ill-fated attempt to make it [DisableRuntimeMarshalling]-compliant.

@jkoritzinsky
Copy link
Member Author

runtime-libraries enterprise-linux failure will be fixed by #65027

@vargaz
Copy link
Contributor

vargaz commented Feb 9, 2022

The mono changes look ok.

…table-struct designations.

Require DisableRuntimeMarshalling for all uses of the new attributed marshalling model, not just blittable cases

Update generator tests to apply DisableRuntimeMarshalling where requried.

Allow custom type marshalling with an always-blittable primitive or pointer-sized Value property to not require disabled runtime marshalling. This allows array types to be marshalled without requiring turning off runtime marshalling.

Update some of the snippets used in the DllImportGenerator unit tests.
Apply DisableRuntimeMarshalling to System.Private.CoreLib

Convert over some more DllImports as we can now handle public blittable types since unmanaged == blittable

Get dotnet/runtime building with the move to DisableRuntimeMarshalling-based blittability rules

Use GeneratedDllImport everywhere in NativeAOT corelibs

Fix Test.CoreLib build.

Add CharSet

Fix modifier

Fix allconfigurations build

Add DisableRuntimeMarshalling to more projects.

Condition DisableRuntimeMarshalling inclusion on net7.0, not on current TFM.

Fix test build locally

Remove some cases of runtime marshalling in assemblies that have runtime marshalling disabled.

Fix build failures.

Add CharSet

Fix a few non-blittable P/Invokes.

Handle modopts/modreqs on an element in a signature

Fix adding partial

Add back the ErrorWriterDelegate

SetLastError in all the callbacks.

Handle null

Use SafeHandle instead of CriticalHandle.

Fix base class

Add manual non-blittable marshalling support for System.Diagnostics.EventLog.

Write manual marshaller for StringBuilder usage in Interop.winhttp.cs

Fix buffer size (sizeof char != 1)

Fix managed build

Make more DllImports generated in Registry/IO

Remove some dead code and make a few assemblies not disable runtime marshalling as they use a ton of it and I really don't want to rewrite all of it if I can avoid it.

Fix WebSocket P/Invokes

Make another WinHttp P/Invoke generated.

Fix libraries test build.

Fix watcher test build again

Remove extraneous [Out] on a blittable array

Fix various test failures due to incorrect marshalling info/manual marshalling.

Signed-off-by: Jeremy Koritzinsky <jekoritz@microsoft.com>
Fix and add tests for forwarding MarshalAs for arrays.
@jkoritzinsky
Copy link
Member Author

The runtime-libraries enterprise-linux failure is an unrelated networking issue.

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

Successfully merging this pull request may close these issues.

3 participants