-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Handle blittable byref returns in built-in marshalling #72433
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,17 +165,14 @@ internal static TypeDesc GetNativeTypeFromMarshallerKind(TypeDesc type, | |
case MarshallerKind.LayoutClassPtr: | ||
case MarshallerKind.AsAnyA: | ||
case MarshallerKind.AsAnyW: | ||
return context.GetWellKnownType(WellKnownType.IntPtr); | ||
|
||
case MarshallerKind.ComInterface: | ||
case MarshallerKind.CustomMarshaler: | ||
case MarshallerKind.BlittableValueClassByRefReturn: | ||
return context.GetWellKnownType(WellKnownType.IntPtr); | ||
|
||
#if !READYTORUN | ||
case MarshallerKind.Variant: | ||
return InteropTypes.GetVariant(context); | ||
|
||
case MarshallerKind.CustomMarshaler: | ||
return context.GetWellKnownType(WellKnownType.IntPtr); | ||
#endif | ||
|
||
case MarshallerKind.OleCurrency: | ||
|
@@ -228,29 +225,35 @@ internal static MarshallerKind GetMarshallerKind( | |
{ | ||
elementMarshallerKind = MarshallerKind.Invalid; | ||
|
||
bool isByRef = false; | ||
TypeSystemContext context = type.Context; | ||
NativeTypeKind nativeType = NativeTypeKind.Default; | ||
bool isField = marshallerType == MarshallerType.Field; | ||
|
||
if (marshalAs != null) | ||
nativeType = marshalAs.Type; | ||
|
||
if (type.IsByRef) | ||
{ | ||
isByRef = true; | ||
|
||
type = type.GetParameterType(); | ||
|
||
if (!type.IsPrimitive && type.IsValueType && marshallerType != MarshallerType.Field | ||
if (type.IsValueType && !type.IsPrimitive && !type.IsEnum && !isField | ||
&& HasCopyConstructorCustomModifier(parameterIndex, customModifierData)) | ||
{ | ||
return MarshallerKind.BlittableValueClassWithCopyCtor; | ||
} | ||
|
||
// Compat note: CLR allows ref returning blittable structs for IJW | ||
if (isReturn) | ||
{ | ||
// Allow ref returning blittable structs for IJW | ||
if (type.IsValueType && | ||
(nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) && | ||
MarshalUtils.IsBlittableType(type)) | ||
{ | ||
return MarshallerKind.BlittableValueClassByRefReturn; | ||
} | ||
return MarshallerKind.Invalid; | ||
} | ||
} | ||
TypeSystemContext context = type.Context; | ||
NativeTypeKind nativeType = NativeTypeKind.Default; | ||
bool isField = marshallerType == MarshallerType.Field; | ||
|
||
if (marshalAs != null) | ||
nativeType = marshalAs.Type; | ||
|
||
if (nativeType == NativeTypeKind.CustomMarshaler) | ||
{ | ||
|
@@ -452,14 +455,6 @@ internal static MarshallerKind GetMarshallerKind( | |
} | ||
else if (type.IsSzArray) | ||
{ | ||
#if READYTORUN | ||
jkotas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We don't want the additional test/maintenance cost of this in R2R. | ||
if (isByRef) | ||
return MarshallerKind.Invalid; | ||
#else | ||
_ = isByRef; | ||
#endif | ||
|
||
if (nativeType == NativeTypeKind.Default) | ||
nativeType = NativeTypeKind.Array; | ||
|
||
|
@@ -515,16 +510,16 @@ internal static MarshallerKind GetMarshallerKind( | |
} | ||
else if (type.IsPointer) | ||
{ | ||
if (nativeType == NativeTypeKind.Default) | ||
type = type.GetParameterType(); | ||
|
||
if (type.IsValueType && !type.IsPrimitive && !type.IsEnum && !isField | ||
&& HasCopyConstructorCustomModifier(parameterIndex, customModifierData)) | ||
{ | ||
var pointedAtType = type.GetParameterType(); | ||
if (!pointedAtType.IsPrimitive && !type.IsEnum && marshallerType != MarshallerType.Field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
&& HasCopyConstructorCustomModifier(parameterIndex, customModifierData)) | ||
{ | ||
return MarshallerKind.BlittableValueClassWithCopyCtor; | ||
} | ||
return MarshallerKind.BlittableValue; | ||
return MarshallerKind.BlittableValueClassWithCopyCtor; | ||
} | ||
|
||
if (nativeType == NativeTypeKind.Default) | ||
return MarshallerKind.BlittableValue; | ||
else | ||
return MarshallerKind.Invalid; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,9 @@ protected static Marshaller CreateMarshaller(MarshallerKind kind) | |
return new VariantMarshaller(); | ||
case MarshallerKind.CustomMarshaler: | ||
return new CustomTypeMarshaller(); | ||
case MarshallerKind.BlittableValueClassByRefReturn: | ||
return new BlittableValueClassByRefReturn(); | ||
|
||
default: | ||
// ensures we don't throw during create marshaller. We will throw NSE | ||
// during EmitIL which will be handled and an Exception method body | ||
|
@@ -1279,4 +1282,15 @@ protected void EmitCleanUpNativeData(ILCodeStream codeStream) | |
codeStream.Emit(ILOpcode.callvirt, emitter.NewToken(cleanupNativeDataMethod)); | ||
} | ||
} | ||
|
||
class BlittableValueClassByRefReturn : Marshaller | ||
{ | ||
protected override void SetupArgumentsForReturnValueMarshalling() | ||
{ | ||
ILEmitter emitter = _ilCodeStreams.Emitter; | ||
|
||
_managedHome = new Home(emitter.NewLocal(ManagedParameterType), ManagedParameterType, isByRef: false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We just need to create locals of the right type. The default implementation works fine otherwise. |
||
_nativeHome = new Home(emitter.NewLocal(NativeType), NativeType, isByRef: false); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to make these conditions consistent and more in sync with CoreCLR logic. This path should be only ever used by managed C++ so it does not matter a whole lot anyway.