-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute.DisableRuntimeMarshalling #60639
Comments
@jkoritzinsky I like the global assembly level indication. Two things comes to mind though.
|
Yes, this will impact customers who are consuming a public P/Invoke defined in an assembly with this attribute. There is no plan to use some sort of call-site checking to determine behavior based on the caller's assembly having this attribute in case they differ.
If we decide that an opt-out would be desirable, I'd suggest that we should reconsider the opt-in design options. An opt-out design (instead of a single all-or-nothing switch) makes the "can Mono not ship the built-in system" decision nearly as difficult as the opt-in mechanisms. Even if we decide to add an opt-out mechanism, I'd suggest that we use the alternative opt-in designs as a basis to design the opt-out system. |
I much prefer the global level indiciation ( Blittable bindings are blittable and one benefit of that is that they are more readable. Forcing users who want to be blittable to add additional magic annotations everywhere is a major pain point; while opting an entire assembly out via an attribute would not be. It would be great if this came with an analyzer (or even compiler level warning/error) if |
That seems a bit extreme for cases that should ideally be few. The concern here is for consumers that have perhaps a couple of P/Invokes that would an impediment to adopt the source generator for the vast majority of their assembly. This is similar thinking to @tannergooding's |
@AaronRobinsonMSFT, are you saying they might have a situation where most of their |
You should be able to workaround limitations or bugs by writing the marshaler manually or tweaking the signature today. I do not think we should be promoting this attribute for this purpose.
Yes, I think the DllImportGenerator should produce hard errors (not warnings) for all non-blitable DllImports and unmanaged function pointer calls when it sees this attribute. |
@tannergooding Yes. Consider the assembly owner that has a library that exposes an API that requires
That is fair. |
There are a few places in the libraries where the blitability concept leaks out. Should we consider relaxing the argument validation in |
I think removing the validation for |
Just leaving a comment to track... We should consider how this impacts |
I believe, we also need to consider the case where the type loader may reorder or pack the type if it is determined to be non-blittable. If I recall during type load the layout of the type is managed auto and not managed sequential. This means if we apply the proposed attribute to a P/Invoke argument but the type was loaded with auto the native side must adhere to how the type loader decided to lay out the type. @jkoritzinsky Can likely provide a better example but I think the gist here is the type loader has the option of doing something that will make just passing through a type difficult in some cases. |
Yes, it is possible today and it makes the field layout algorithm very complex. We should replace the IsBlittable checks in field layout with is IsUnmanaged` (ie does not contain object references). It was attempted in #51416 (comment) , but we run out of time to stabilize that change for .NET 6. The plan is to do that in .NET 7. We should make this proposal dependent on that. cc @trylek |
namespace System.Runtime.CompilerServices
{
[AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
public sealed class DisableRuntimeMarshallingAttribute : Attribute
{
}
} |
Made a tracking issue for Mono #61685 |
This is something I've wanted for quite some time, very excited to see this scheduled for .NET 7! One concern I do have though is how this could be confusing when function pointers are allowed to cross between assembly boundaries. For example, imagine I have these two assemblies: // MyCoolLibraryWrapper
[assembly: DisableRuntimeMarshalling]
public unsafe static class MyCoolLibrary
{
// Returns an unmanaged function pointer which takes a 16 bit UTF16 character and returns a proper 8 bit bool.
// (IE: No marshaling is necessary or desired to invoke the returned function pointer.)
[DllImport("MyCoolLibrary.dll")]
public static extern delegate* unmanaged<char, bool> GetFilter();
public static bool PassesFilter(char c)
=> GetFilter()(c);
} // MyCoolApp, depends on MyCoolLibraryWrapper
// Note that DisableRuntimeMarshalling is not applied.
const char c = 'a';
Console.WriteLine(MyCoolLibrary.PassesFilter(c));
Console.WriteLine(MyCoolLibrary.GetFilter()(c)); For the above:
Would it be reasonable to provide an in-box analyzer which warns if a non- |
Background and motivation
As the interop team is working on a source-generated DllImport solution, we've decided to take an extra look at the built-in marshalling rules per feedback from @stephentoub.
In the built-in marshalling system today, the runtime looks at all member fields of a type at runtime and uses this runtime type information to determine if a type is blittable and how to marshal it. For compile-time source generators, this sort of design is untenable for performance and feasibility reasons (we need a static type in the final "native call" signature to get correct calling-convention behavior). See https://github.com/dotnet/runtime/blob/main/docs/design/libraries/DllImportGenerator/StructMarshalling.md for a more information.
A complicating factor in this scenario is that the default marshalling rules for
bool
andchar
are non-blittable. In particular,bool
maps to a 4-byte Windows APIBOOL
, andchar
maps to an ANSI (based on the current code page) 1-byte character. As a result, the C# language concept of "unmanaged" is not the same as the runtime concept of "blittable". In the proposal above, we took the route of building a new attribute and analysis system to ensure that a type marked as blittable is actually blittable.@stephentoub and @jkotas recommended that we could instead investigate changing the rules in source-generated P/Invokes to make the unmanaged and blittable concepts equivalent. To enable this mechanism at the interop boundary, we need to provide some mechanism to disable the built in marshalling rules. We've explored a few options in #59824, and I've selected one of the options to bring up for API review here (the other options are covered in Alternative Designs).
We propose adding a new property to
RuntimeCompatibilityAttribute
that disables the built-in marshalling system for all P/Invoke and Reverse P/Invoke scenarios defined in the assembly (all methods marked[DllImport]
, all delegates that are defined in the assembly when used in interop scenarios, and allUnmanagedCallersOnly
-attributed methods). When this property is set totrue
, all unmanaged types are considered blittable, and all non-blittable types (in/ref/out
parameters as well since they require pinning) are disallowed.We like the idea of disabling across a whole assembly for a few reasons:
API Proposal
namespace System.Runtime.CompilerServices { public class RuntimeCompatibilityAttribute { + public bool DisableRuntimeMarshalling { get; set; } } }
Additionally, we propose a behavior change for
GCHandle.Alloc
to support all unmanaged types for allocating a pinned GC handle. There is no API change required for this portion of the request, as this only changes an exceptional scenario (non-blittable unmanaged types throw today in this case, and all other unmanaged types are blittable and as such succeed).API Usage
Alternative Designs
Alternative Design 1:
System.Runtime.InteropServices.UnmanagedType.UnmanagedBlittable
namespace System.Runtime.InteropServices { public enum UnmanagedType { + UnmanagedBlittable = /* TBD */ } }
In this design, we'd either explicitly require or implicitly emit a
[MarshalAs(UnmanagedType.UnmanagedBlittable)]
attribute on every unmanaged parameter or return type that doesn't have any marshalling attributes already applied.Pros:
Cons:
UnmanagedCallersOnly
, which is the direction the interop team has been pushing.Alternative design 2:
System.Runtime.CompilerServices.CallConvUnmanagedBlittable
namespace System.Runtime.CompilerServices { + public sealed class CallConvUnmanagedBlittable {} }
In this design, we introduce a new "calling convention" type that specifies the "unmanaged == blittable" behavior for all parameters and return values, and we'd implicitly apply it in source-generated scenarios.
Pros:
Cons:
Alternative design 3:
System.Runtime.CompilerServices.CallConvNoMarshalling
namespace System.Runtime.CompilerServices { + public sealed class CallConvNoMarshalling {} }
In this design, we introduce a new "calling convention" type that disables the built-in marshalling behavior for all parameters and return values, and we'd implicitly apply it in source-generated scenarios.
Pros:
Cons:
Risks
Today, Roslyn emits the equivalent to
[assembly:RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)]
into every assembly if theRuntimeCompatibilityAttribute
is not defined by the user. If the user manually provides theRuntimeCompatibilityAttribute
attribution themselves, they may forget to setWrapNonExceptionThrows=true
. As scenarios whereWrapNonExceptionThrows=true
actually kicks in (requires manually written IL or C++/CLI to my knowledge), this shouldn't be much of an issue. If we view this as a problem, we can do one or both of the following things:RuntimeCompatibilityAttribute
attribute application in scenarios where we'd recommend applying it (ie. with the DllImportGenerator or other interop source-generators).RuntimeCompatibilityAttribute
with theDisableRuntimeMarshalling = true
property itself.The text was updated successfully, but these errors were encountered: