<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\Marshal.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\MarshalDirectiveException.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\PInvokeMap.cs" />
+ <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\PInvokeMarshal.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\RuntimeEnvironment.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\SEHException.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\SafeBuffer.cs" />
internal const string BCrypt = "BCrypt.dll";
internal const string Crypt32 = "crypt32.dll";
internal const string Kernel32 = "kernel32.dll";
- internal const string NtDll = "ntdll.dll";
internal const string OleAut32 = "oleaut32.dll";
}
}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System;
-using System.Runtime.InteropServices;
-using System.Security;
-
-internal partial class Interop
-{
- internal partial class NtDll
- {
- [DllImport(Libraries.NtDll, CharSet = CharSet.Unicode, EntryPoint = "RtlZeroMemory")]
- internal static extern void ZeroMemory(IntPtr address, UIntPtr length);
- }
-}
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WideCharToMultiByte.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_IntPtr.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_NativeOverlapped.cs"/>
- <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\NtDll\Interop.ZeroMemory.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysAllocStringLen.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysFreeString.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysStringLen.cs"/>
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Runtime;
using System.Diagnostics;
using System.Runtime.InteropServices;
override protected bool ReleaseHandle()
{
- Interop.NtDll.ZeroMemory(handle, (UIntPtr)(Interop.OleAut32.SysStringLen(handle) * sizeof(char)));
+ RuntimeImports.RhZeroMemory(handle, (UIntPtr)(Interop.OleAut32.SysStringLen(handle) * sizeof(char)));
Interop.OleAut32.SysFreeString(handle);
return true;
}
try
{
AcquirePointer(ref bufferPtr);
- Interop.NtDll.ZeroMemory((IntPtr)bufferPtr, (UIntPtr)(Interop.OleAut32.SysStringLen((IntPtr)bufferPtr) * sizeof(char)));
+ RuntimeImports.RhZeroMemory((IntPtr)bufferPtr, (UIntPtr)(Interop.OleAut32.SysStringLen((IntPtr)bufferPtr) * sizeof(char)));
}
finally
{
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
+using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
_buffer.Write((ulong)(index * sizeof(char)), c);
}
+ internal unsafe IntPtr MarshalToBSTR()
+ {
+ int length = _decryptedLength;
+ IntPtr ptr = IntPtr.Zero;
+ IntPtr result = IntPtr.Zero;
+ byte* bufferPtr = null;
+
+ try
+ {
+ _buffer.AcquirePointer(ref bufferPtr);
+ int resultByteLength = (length + 1) * sizeof(char);
+
+ ptr = PInvokeMarshal.AllocBSTR(length);
+
+ Buffer.MemoryCopy(bufferPtr, (byte*)ptr, resultByteLength, length * sizeof(char));
+
+ result = ptr;
+ }
+ finally
+ {
+ // If we failed for any reason, free the new buffer
+ if (result == IntPtr.Zero && ptr != IntPtr.Zero)
+ {
+ RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
+ PInvokeMarshal.FreeBSTR(ptr);
+ }
+
+ if (bufferPtr != null)
+ {
+ _buffer.ReleasePointer();
+ }
+ }
+ return result;
+ }
+
internal unsafe IntPtr MarshalToStringCore(bool globalAlloc, bool unicode)
{
int length = _decryptedLength;
// release the string if we had one.
if (stringPtr != IntPtr.Zero && result == IntPtr.Zero)
{
- UnmanagedBuffer.ZeroMemory((byte*)stringPtr, (ulong)(length * sizeof(char)));
+ RuntimeImports.RhZeroMemory(stringPtr, (UIntPtr)(length * sizeof(char)));
MarshalFree(stringPtr, globalAlloc);
}
try
{
AcquirePointer(ref ptr);
- ZeroMemory(ptr, ByteLength);
+ RuntimeImports.RhZeroMemory((IntPtr)ptr, (UIntPtr)ByteLength);
}
finally
{
Marshal.FreeHGlobal(handle);
return true;
}
-
- internal static unsafe void ZeroMemory(byte* ptr, ulong len)
- {
- for (ulong i = 0; i < len; i++) *ptr++ = 0;
- }
}
-
}
}
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
+using System.Runtime;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32;
_buffer.AcquirePointer(ref bufferPtr);
int resultByteLength = (length + 1) * sizeof(char);
- ptr = Interop.OleAut32.SysAllocStringLen(null, length);
- if (ptr == IntPtr.Zero)
- {
- throw new OutOfMemoryException();
- }
+ ptr = PInvokeMarshal.AllocBSTR(length);
Buffer.MemoryCopy(bufferPtr, (byte*)ptr, resultByteLength, length * sizeof(char));
// If we failed for any reason, free the new buffer
if (result == IntPtr.Zero && ptr != IntPtr.Zero)
{
- Interop.NtDll.ZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
- Interop.OleAut32.SysFreeString(ptr);
+ RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
+ PInvokeMarshal.FreeBSTR(ptr);
}
if (bufferPtr != null)
// If we failed for any reason, free the new buffer
if (result == IntPtr.Zero && ptr != IntPtr.Zero)
{
- Interop.NtDll.ZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
+ RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
MarshalFree(ptr, globalAlloc);
}
internal const String USER32 = "user32.dll";
internal const String OLE32 = "ole32.dll";
internal const String OLEAUT32 = "oleaut32.dll";
- internal const String NTDLL = "ntdll.dll";
#else //FEATURE_PAL
internal const String KERNEL32 = "libcoreclr";
internal const String USER32 = "libcoreclr";
internal const String OLE32 = "libcoreclr";
internal const String OLEAUT32 = "libcoreclr";
- internal const String NTDLL = "libcoreclr";
#endif //FEATURE_PAL
internal const String ADVAPI32 = "advapi32.dll";
internal const String SHELL32 = "shell32.dll";
[DllImport(KERNEL32, SetLastError = true)]
internal static extern IntPtr LocalFree(IntPtr handle);
- // MSDN says the length is a SIZE_T.
- [DllImport(NTDLL, EntryPoint = "RtlZeroMemory")]
- internal static extern void ZeroMemory(IntPtr address, UIntPtr length);
-
internal static bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX buffer)
{
buffer.length = Marshal.SizeOf(typeof(MEMORYSTATUSEX));
namespace System.Runtime.InteropServices
{
using System;
+ using System.Runtime;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
}
Contract.EndContractBlock();
-#if FEATURE_COMINTEROP
return s.MarshalToBSTR();
-#else
- throw new PlatformNotSupportedException(); // https://github.com/dotnet/coreclr/issues/10443
-#endif
}
public static IntPtr SecureStringToCoTaskMemAnsi(SecureString s)
return s.MarshalToString(globalAlloc: false, unicode: true);
}
-
-#if FEATURE_COMINTEROP
+
public static void ZeroFreeBSTR(IntPtr s)
{
- Win32Native.ZeroMemory(s, (UIntPtr)(Win32Native.SysStringLen(s) * 2));
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.SysStringLen(s) * 2));
FreeBSTR(s);
}
-#endif
public static void ZeroFreeCoTaskMemAnsi(IntPtr s)
{
- Win32Native.ZeroMemory(s, (UIntPtr)(Win32Native.lstrlenA(s)));
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.lstrlenA(s)));
FreeCoTaskMem(s);
}
public static void ZeroFreeCoTaskMemUnicode(IntPtr s)
{
- Win32Native.ZeroMemory(s, (UIntPtr)(Win32Native.lstrlenW(s) * 2));
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.lstrlenW(s) * 2));
FreeCoTaskMem(s);
}
unsafe public static void ZeroFreeCoTaskMemUTF8(IntPtr s)
{
- Win32Native.ZeroMemory(s, (UIntPtr)System.StubHelpers.StubHelpers.strlen((sbyte*)s));
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)System.StubHelpers.StubHelpers.strlen((sbyte*)s));
FreeCoTaskMem(s);
}
public static void ZeroFreeGlobalAllocAnsi(IntPtr s)
{
- Win32Native.ZeroMemory(s, (UIntPtr)(Win32Native.lstrlenA(s)));
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.lstrlenA(s)));
FreeHGlobal(s);
}
public static void ZeroFreeGlobalAllocUnicode(IntPtr s)
{
- Win32Native.ZeroMemory(s, (UIntPtr)(Win32Native.lstrlenW(s) * 2));
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.lstrlenW(s) * 2));
FreeHGlobal(s);
}
}
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
}
-
- public static void ZeroFreeBSTR(System.IntPtr s)
- {
- throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
- }
}
public class DispatchWrapper
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32;
+
+namespace System.Runtime.InteropServices
+{
+ internal static class PInvokeMarshal
+ {
+ public static IntPtr AllocBSTR(int length)
+ {
+ IntPtr bstr = Win32Native.SysAllocStringLen(null, length);
+ if (bstr == IntPtr.Zero)
+ throw new OutOfMemoryException();
+ return bstr;
+ }
+
+ public static void FreeBSTR(IntPtr ptr)
+ {
+ Win32Native.SysFreeString(ptr);
+ }
+ }
+}
#if BIT64
using nuint = System.UInt64;
#else
- using nuint = System.UInt32;
+using nuint = System.UInt32;
#endif
namespace System.Runtime
}
}
+ internal unsafe static void RhZeroMemory(IntPtr p, UIntPtr byteLength)
+ {
+ RhZeroMemory((void*)p, (nuint)byteLength);
+ }
+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
- extern private unsafe static void RhZeroMemory(byte* b, nuint byteLength);
+ extern private unsafe static void RhZeroMemory(void* b, nuint byteLength);
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern unsafe static void RhBulkMoveWithWriteBarrier(ref byte destination, ref byte source, nuint byteCount);
}
}
+ private unsafe void SecureStringToBSTRToString()
+ {
+ foreach (String ts in TestStrings)
+ {
+ SecureString secureString = new SecureString();
+ foreach (char character in ts)
+ {
+ secureString.AppendChar(character);
+ }
+
+ IntPtr BStr = IntPtr.Zero;
+ String str;
+
+ try
+ {
+ BStr = Marshal.SecureStringToBSTR(secureString);
+ str = Marshal.PtrToStringBSTR(BStr);
+ }
+ finally
+ {
+ if (BStr != IntPtr.Zero)
+ {
+ Marshal.ZeroFreeBSTR(BStr);
+ }
+ }
+
+ if (!str.Equals(ts))
+ {
+ throw new Exception();
+ }
+ }
+ }
+
private void StringToCoTaskMemAnsiToString()
{
foreach (String ts in TestStrings)
public bool RunTests()
{
StringToBStrToString();
+ SecureStringToBSTRToString();
StringToCoTaskMemAnsiToString();
StringToCoTaskMemUniToString();
StringToHGlobalAnsiToString();