Skip to content

Commit

Permalink
use IntPtr instead of CriticalHandle to avoid resurrection issues. It…
Browse files Browse the repository at this point in the history
…'s ok to never free the handle

Commit migrated from dotnet/coreclr@0094d92
  • Loading branch information
adamsitnik committed Jun 14, 2019
1 parent a029f72 commit ba6160b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ internal static partial class Interop
internal static partial class Globalization
{
[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_GetSortHandle")]
internal static extern unsafe ResultCode GetSortHandle(byte[] localeName, out CriticalSortHandle sortHandle);
internal static extern unsafe ResultCode GetSortHandle(byte[] localeName, out IntPtr sortHandle);

[SuppressUnmanagedCodeSecurity]
[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_CloseSortHandle")]
internal static extern unsafe void CloseSortHandle(IntPtr handle);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_CompareString")]
internal static extern unsafe int CompareString(CriticalSortHandle sortHandle, char* lpStr1, int cwStr1Len, char* lpStr2, int cwStr2Len, CompareOptions options);
internal static extern unsafe int CompareString(IntPtr sortHandle, char* lpStr1, int cwStr1Len, char* lpStr2, int cwStr2Len, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_IndexOf")]
internal static extern unsafe int IndexOf(CriticalSortHandle sortHandle, char* target, int cwTargetLength, char* pSource, int cwSourceLength, CompareOptions options, int* matchLengthPtr);
internal static extern unsafe int IndexOf(IntPtr sortHandle, char* target, int cwTargetLength, char* pSource, int cwSourceLength, CompareOptions options, int* matchLengthPtr);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_LastIndexOf")]
internal static extern unsafe int LastIndexOf(CriticalSortHandle sortHandle, char* target, int cwTargetLength, char* pSource, int cwSourceLength, CompareOptions options);
internal static extern unsafe int LastIndexOf(IntPtr sortHandle, char* target, int cwTargetLength, char* pSource, int cwSourceLength, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_IndexOfOrdinalIgnoreCase")]
internal static extern unsafe int IndexOfOrdinalIgnoreCase(string target, int cwTargetLength, char* pSource, int cwSourceLength, bool findLast);
Expand All @@ -34,47 +33,27 @@ internal static partial class Globalization

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_StartsWith")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern unsafe bool StartsWith(CriticalSortHandle sortHandle, char* target, int cwTargetLength, char* source, int cwSourceLength, CompareOptions options);
internal static extern unsafe bool StartsWith(IntPtr sortHandle, char* target, int cwTargetLength, char* source, int cwSourceLength, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_EndsWith")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern unsafe bool EndsWith(CriticalSortHandle sortHandle, char* target, int cwTargetLength, char* source, int cwSourceLength, CompareOptions options);
internal static extern unsafe bool EndsWith(IntPtr sortHandle, char* target, int cwTargetLength, char* source, int cwSourceLength, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_StartsWith")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern unsafe bool StartsWith(CriticalSortHandle sortHandle, string target, int cwTargetLength, string source, int cwSourceLength, CompareOptions options);
internal static extern unsafe bool StartsWith(IntPtr sortHandle, string target, int cwTargetLength, string source, int cwSourceLength, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_EndsWith")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern unsafe bool EndsWith(CriticalSortHandle sortHandle, string target, int cwTargetLength, string source, int cwSourceLength, CompareOptions options);
internal static extern unsafe bool EndsWith(IntPtr sortHandle, string target, int cwTargetLength, string source, int cwSourceLength, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_GetSortKey")]
internal static extern unsafe int GetSortKey(CriticalSortHandle sortHandle, char* str, int strLength, byte* sortKey, int sortKeyLength, CompareOptions options);
internal static extern unsafe int GetSortKey(IntPtr sortHandle, char* str, int strLength, byte* sortKey, int sortKeyLength, CompareOptions options);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_CompareStringOrdinalIgnoreCase")]
internal static extern unsafe int CompareStringOrdinalIgnoreCase(char* lpStr1, int cwStr1Len, char* lpStr2, int cwStr2Len);

[DllImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_GetSortVersion")]
internal static extern int GetSortVersion(CriticalSortHandle sortHandle);

internal sealed class CriticalSortHandle : CriticalHandle
{
private CriticalSortHandle() :
base(IntPtr.Zero)
{
}

public override bool IsInvalid
{
get { return handle == IntPtr.Zero; }
}

protected override bool ReleaseHandle()
{
CloseSortHandle(handle);
SetHandle(IntPtr.Zero);
return true;
}
}
internal static extern int GetSortVersion(IntPtr sortHandle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ namespace System.Globalization
{
public partial class CompareInfo
{
[NonSerialized]
private Interop.Globalization.CriticalSortHandle _sortHandle = null!; // initialized in helper called by ctors

[NonSerialized]
private bool _isAsciiEqualityOrdinal;

Expand All @@ -33,7 +30,7 @@ private void InitSort(CultureInfo culture)
Interop.Globalization.ResultCode resultCode = Interop.Globalization.GetSortHandle(GetNullTerminatedUtf8String(_sortName), out _sortHandle);
if (resultCode != Interop.Globalization.ResultCode.Success)
{
_sortHandle.Dispose();
Interop.Globalization.CloseSortHandle(_sortHandle);

if (resultCode == Interop.Globalization.ResultCode.OutOfMemory)
throw new OutOfMemoryException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,6 @@ private unsafe bool EndsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> suffi
}

// PAL ends here
[NonSerialized]
private IntPtr _sortHandle;

private const uint LCMAP_SORTKEY = 0x00000400;
private const uint LCMAP_HASH = 0x00040000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public partial class CompareInfo : IDeserializationCallback

private int culture; // Do not rename (binary serialization). The fields sole purpose is to support Desktop serialization.

[NonSerialized]
private IntPtr _sortHandle;

internal CompareInfo(CultureInfo culture)
{
m_name = culture._name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ the general type of handle they support (null is invalid, -1 is invalid etc.)
back into memory, etc), then they can provide a finalizer that will be
guaranteed to run before the CriticalHandle's critical finalizer.
Subclasses are expected to be written as follows (note that
SuppressUnmanagedCodeSecurity should always be used on any P/Invoke methods
invoked as part of ReleaseHandle, in order to switch the security check from
runtime to jit time and thus remove a possible failure path from the
invocation of the method):
Subclasses are expected to be written as follows:
internal sealed MyCriticalHandleSubclass : CriticalHandle {
// Called by P/Invoke when returning CriticalHandles
Expand All @@ -99,7 +95,7 @@ public override bool IsInvalid {
get { return handle == IntPtr.Zero; }
}
[DllImport(Interop.Libraries.Kernel32), SuppressUnmanagedCodeSecurity, ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[DllImport(Interop.Libraries.Kernel32), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
private static extern bool CloseHandle(IntPtr handle);
override protected bool ReleaseHandle()
Expand Down

0 comments on commit ba6160b

Please sign in to comment.