Move Marshal::ZeroFree* to shared partition (#22230)
authorMarek Safar <marek.safar@gmail.com>
Sun, 27 Jan 2019 01:08:53 +0000 (02:08 +0100)
committerJan Kotas <jkotas@microsoft.com>
Sun, 27 Jan 2019 01:08:53 +0000 (17:08 -0800)
* Move Marshal::ZeroFree* to shared partition

* Add managed version of SysStringByteLen

src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs
src/System.Private.CoreLib/shared/System/Security/SafeBSTRHandle.cs
src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs
src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
src/System.Private.CoreLib/src/System/StubHelpers.cs

index 3929523..bb03656 100644 (file)
@@ -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);
+        }
     }
 }
index bc93fec..dd52f42 100644 (file)
@@ -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
             {
index d2007d8..65047b8 100644 (file)
@@ -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);
 
index 0dcda59..b1f6212 100644 (file)
@@ -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);
-        }
     }
 }
index 8bf9c56..84ba25b 100644 (file)
@@ -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