summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Safar <marek.safar@gmail.com>2019-01-27 02:08:53 +0100
committerJan Kotas <jkotas@microsoft.com>2019-01-26 17:08:53 -0800
commit2876addb7a425776470ca3ebac6e705e5adceb80 (patch)
tree2a69b08fa10951fdfcac82a5f72016ad4a9eb137
parent3038b7484330012fb418dae61cfbdb54542719fd (diff)
downloadcoreclr-2876addb7a425776470ca3ebac6e705e5adceb80.tar.gz
coreclr-2876addb7a425776470ca3ebac6e705e5adceb80.tar.bz2
coreclr-2876addb7a425776470ca3ebac6e705e5adceb80.zip
Move Marshal::ZeroFree* to shared partition (#22230)
* Move Marshal::ZeroFree* to shared partition * Add managed version of SysStringByteLen
-rw-r--r--src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs60
-rw-r--r--src/System.Private.CoreLib/shared/System/Security/SafeBSTRHandle.cs4
-rw-r--r--src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs3
-rw-r--r--src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs60
-rw-r--r--src/System.Private.CoreLib/src/System/StubHelpers.cs2
5 files changed, 63 insertions, 66 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs
index 3929523589..bb03656bcb 100644
--- a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs
+++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs
@@ -771,5 +771,65 @@ namespace System.Runtime.InteropServices
}
public static IntPtr /* IDispatch */ GetIDispatchForObject(object o) => throw new PlatformNotSupportedException();
+
+ public static void ZeroFreeBSTR(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)SysStringByteLen(s));
+ FreeBSTR(s);
+ }
+
+ public unsafe static void ZeroFreeCoTaskMemAnsi(IntPtr s)
+ {
+ ZeroFreeCoTaskMemUTF8(s);
+ }
+
+ public static unsafe void ZeroFreeCoTaskMemUnicode(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
+ FreeCoTaskMem(s);
+ }
+
+ public static unsafe void ZeroFreeCoTaskMemUTF8(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
+ FreeCoTaskMem(s);
+ }
+
+ public unsafe static void ZeroFreeGlobalAllocAnsi(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
+ FreeHGlobal(s);
+ }
+
+ public static unsafe void ZeroFreeGlobalAllocUnicode(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
+ FreeHGlobal(s);
+ }
+
+ internal static unsafe uint SysStringByteLen(IntPtr s)
+ {
+ return *(((uint*)s) - 1);
+ }
}
}
diff --git a/src/System.Private.CoreLib/shared/System/Security/SafeBSTRHandle.cs b/src/System.Private.CoreLib/shared/System/Security/SafeBSTRHandle.cs
index bc93fecef1..dd52f42cf7 100644
--- a/src/System.Private.CoreLib/shared/System/Security/SafeBSTRHandle.cs
+++ b/src/System.Private.CoreLib/shared/System/Security/SafeBSTRHandle.cs
@@ -26,7 +26,7 @@ namespace System.Security
override protected bool ReleaseHandle()
{
- RuntimeImports.RhZeroMemory(handle, (UIntPtr)(Interop.OleAut32.SysStringLen(handle) * sizeof(char)));
+ RuntimeImports.RhZeroMemory(handle, (UIntPtr)Marshal.SysStringByteLen(handle));
Interop.OleAut32.SysFreeString(handle);
return true;
}
@@ -37,7 +37,7 @@ namespace System.Security
try
{
AcquirePointer(ref bufferPtr);
- RuntimeImports.RhZeroMemory((IntPtr)bufferPtr, (UIntPtr)(Interop.OleAut32.SysStringLen((IntPtr)bufferPtr) * sizeof(char)));
+ RuntimeImports.RhZeroMemory((IntPtr)bufferPtr, (UIntPtr)Marshal.SysStringByteLen((IntPtr)bufferPtr));
}
finally
{
diff --git a/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs b/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs
index d2007d8681..65047b8684 100644
--- a/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs
+++ b/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs
@@ -141,9 +141,6 @@ namespace Microsoft.Win32
[DllImport(Interop.Libraries.OleAut32)]
internal static extern IntPtr SysAllocStringByteLen(byte[] str, uint len); // BSTR
- [DllImport(Interop.Libraries.OleAut32)]
- internal static extern uint SysStringByteLen(IntPtr bstr); // BSTR
-
[DllImport(Interop.Libraries.Kernel32, SetLastError = true)]
internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
index 0dcda5932c..b1f6212220 100644
--- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
+++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
@@ -957,65 +957,5 @@ namespace System.Runtime.InteropServices
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetFunctionPointerForDelegateInternal(Delegate d);
-
- public static void ZeroFreeBSTR(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.SysStringLen(s) * 2));
- FreeBSTR(s);
- }
-
- public unsafe static void ZeroFreeCoTaskMemAnsi(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
- FreeCoTaskMem(s);
- }
-
- public static unsafe void ZeroFreeCoTaskMemUnicode(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
- FreeCoTaskMem(s);
- }
-
- public static unsafe void ZeroFreeCoTaskMemUTF8(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
- FreeCoTaskMem(s);
- }
-
- public unsafe static void ZeroFreeGlobalAllocAnsi(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
- FreeHGlobal(s);
- }
-
- public static unsafe void ZeroFreeGlobalAllocUnicode(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
- FreeHGlobal(s);
- }
}
}
diff --git a/src/System.Private.CoreLib/src/System/StubHelpers.cs b/src/System.Private.CoreLib/src/System/StubHelpers.cs
index 8bf9c56f06..84ba25b282 100644
--- a/src/System.Private.CoreLib/src/System/StubHelpers.cs
+++ b/src/System.Private.CoreLib/src/System/StubHelpers.cs
@@ -285,7 +285,7 @@ namespace System.StubHelpers
}
else
{
- uint length = Win32Native.SysStringByteLen(bstr);
+ uint length = Marshal.SysStringByteLen(bstr);
// Intentionally checking the number of bytes not characters to match the behavior
// of ML marshalers. This prevents roundtripping of very large strings as the check