Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,6 @@ private static unsafe IntPtr GetProcAddressWithSuffix(IntPtr hModule, byte* meth
return ptr;
}

internal static unsafe void CoTaskMemFree(void* p)
{
Marshal.FreeCoTaskMem((IntPtr)p);
}

/// <summary>
/// Retrieves the function pointer for the current open static delegate that is being called
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ internal override bool CleanupRequired
internal override void EmitElementCleanup(ILCodeStream codeStream, ILEmitter emitter)
{
codeStream.Emit(ILOpcode.call, emitter.NewToken(
Context.GetHelperEntryPoint("InteropHelpers", "CoTaskMemFree")));
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));
}

protected override void AllocNativeToManaged(ILCodeStream codeStream)
Expand Down Expand Up @@ -240,9 +240,10 @@ protected override void TransformNativeToManaged(ILCodeStream codeStream)

protected override void EmitCleanupManaged(ILCodeStream codeStream)
{
ILEmitter emitter = _ilCodeStreams.Emitter;
LoadNativeValue(codeStream);
codeStream.Emit(ILOpcode.call, _ilCodeStreams.Emitter.NewToken(
Context.GetHelperEntryPoint("InteropHelpers", "CoTaskMemFree")));
codeStream.Emit(ILOpcode.call, emitter.NewToken(
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));
}
}

Expand Down
35 changes: 19 additions & 16 deletions src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,12 +1491,8 @@ internal override bool CleanupRequired

internal override void EmitElementCleanup(ILCodeStream codeStream, ILEmitter emitter)
{
#if READYTORUN
throw new NotSupportedException();
#else
codeStream.Emit(ILOpcode.call, emitter.NewToken(
Context.GetHelperEntryPoint("InteropHelpers", "CoTaskMemFree")));
#endif
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));
}

protected override void TransformManagedToNative(ILCodeStream codeStream)
Expand Down Expand Up @@ -1613,12 +1609,8 @@ internal override bool CleanupRequired

internal override void EmitElementCleanup(ILCodeStream codeStream, ILEmitter emitter)
{
#if READYTORUN
throw new NotSupportedException();
#else
codeStream.Emit(ILOpcode.call, emitter.NewToken(
Context.GetHelperEntryPoint("InteropHelpers", "CoTaskMemFree")));
#endif
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));
}

protected override void TransformManagedToNative(ILCodeStream codeStream)
Expand Down Expand Up @@ -1765,7 +1757,7 @@ protected override void EmitCleanupManaged(ILCodeStream codeStream)

LoadNativeValue(codeStream);
codeStream.Emit(ILOpcode.call, emitter.NewToken(
Context.GetHelperEntryPoint("InteropHelpers", "CoTaskMemFree")));
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));

codeStream.EmitLabel(lNullCheck);
#endif
Expand All @@ -1787,7 +1779,7 @@ internal override void EmitElementCleanup(ILCodeStream codeStream, ILEmitter emi
Debug.Assert(_marshallerInstance is null);

codeStream.Emit(ILOpcode.call, emitter.NewToken(
InteropTypes.GetMarshal(Context).GetKnownMethod("CoTaskMemFree", null)));
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));
}

protected override void TransformManagedToNative(ILCodeStream codeStream)
Expand Down Expand Up @@ -1858,11 +1850,22 @@ protected override void EmitCleanupManaged(ILCodeStream codeStream)
{
ILEmitter emitter = _ilCodeStreams.Emitter;

Debug.Assert(_marshallerInstance != null);
if (In && !Out && !IsManagedByRef)
{
Debug.Assert(_marshallerInstance != null);

codeStream.EmitLdLoca(_marshallerInstance.Value);
codeStream.Emit(ILOpcode.call, emitter.NewToken(
Marshaller.GetKnownMethod("FreeNative", null)));
codeStream.EmitLdLoca(_marshallerInstance.Value);
codeStream.Emit(ILOpcode.call, emitter.NewToken(
Marshaller.GetKnownMethod("FreeNative", null)));
}
else
{
// The marshaller instance is not guaranteed to be initialized with the latest native value.
// Free the native value directly.
LoadNativeValue(codeStream);
codeStream.Emit(ILOpcode.call, emitter.NewToken(
InteropTypes.GetMarshal(Context).GetKnownMethod("FreeCoTaskMem", null)));
}
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/coreclr/vm/ilmarshalers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,10 +2016,21 @@ void ILCUTF8Marshaler::EmitClearNative(ILCodeStream* pslILEmit)
{
STANDARD_VM_CONTRACT;

_ASSERTE(m_dwInstance != LOCAL_NUM_UNUSED);
bool bPassByValueInOnly = IsIn(m_dwMarshalFlags) && !IsOut(m_dwMarshalFlags) && !IsByref(m_dwMarshalFlags);
if (bPassByValueInOnly)
{
_ASSERTE(m_dwInstance != LOCAL_NUM_UNUSED);

pslILEmit->EmitLDLOCA(m_dwInstance);
pslILEmit->EmitCALL(METHOD__UTF8STRINGMARSHALLER__FREE_NATIVE, 1, 0);
pslILEmit->EmitLDLOCA(m_dwInstance);
pslILEmit->EmitCALL(METHOD__UTF8STRINGMARSHALLER__FREE_NATIVE, 1, 0);
}
else
{
// The marshaller instance is not guaranteed to be initialized with the latest native value.
// Free the native value directly.
EmitLoadNativeValue(pslILEmit);
pslILEmit->EmitCALL(METHOD__MARSHAL__FREE_CO_TASK_MEM, 1, 0);
}
}

LocalDesc ILCSTRMarshaler::GetManagedType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ public unsafe ref struct Utf16StringMarshaller
/// <param name="str">The string to marshal.</param>
public Utf16StringMarshaller(string? str)
{
if (str is null)
{
_nativeValue = null;
return;
}

// + 1 for null terminator
_nativeValue = (ushort*)Marshal.AllocCoTaskMem((str.Length + 1) * sizeof(ushort));

str.CopyTo(new Span<char>(_nativeValue, str.Length));
_nativeValue[str.Length] = '\0'; // null-terminate
_nativeValue = (ushort*)Marshal.StringToCoTaskMemUni(str);
Copy link
Member

Choose a reason for hiding this comment

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

Note for other reviewers. The StringToCoTaskMemUni logic is almost identical to what is being replaced here.

}

/// <summary>
Expand Down