-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
In #60639, we approved the System.Runtime.CompilerSevices.DisableRuntimeMarshallingAttribute API. This API is primarily intended to help enable using source-generated interop and to help simplify some concepts in the interop space by disabling some features of the built-in interop system.
This proposal proposes an analyzer to identify when users are using features of the built-in interop system when runtime marshalling is disabled with [assembly:DisableRuntimeMarshalling]. This analyzer will emit a warning diagnostic in the following scenarios (all of which fail at runtime when the P/Invoke or delegate would be called in an interop scenario):
SetLastErroris set totrueon a P/Invoke[LCIDConversionAttribute]is used on a P/Invoke- A type that is not a C#-unmanaged type is in a P/Invoke signature or the signature of a delegate with the
[UnmanagedFunctionPointer]attribute. - A C#-unmanaged type that has any fields with
[StructLayout(LayoutKind.Auto)]that is in a P/Invoke signature or the signature of a delegate with the[UnmanagedFunctionPointer]attribute.
The analyzer can also emit a "notice"-level diagnostic for the following scenarios that are likely user errors when [assembly:DisableRuntimeMarshalling] is applied. These scenarios use APIs that do not change behavior when [assembly:DisableRuntimeMarshalling] is applied and always respect the built-in marshalling system. I suggest a "notice" level diagnostic as it's possible that the user is calling APIs defined in other assemblies that don't have [assembly:DisableRuntimeMarshalling] applied or that the results of the APIs are correct even when marshalling is disabled.
- When
Marshal.SizeOfis used on an unmanaged type. (We could also include a code-fix here to recommend switching toUnsafe.SizeOfor thesizeofkeyword) - When
Marshal.OffsetOfis used. - When
Marshal.StructureToPtrorMarshal.PtrToStructureis used. (We can include code-fixes here to use "unsafe" code or other unsafe APIs).
cc: @dotnet/interop-contrib