diff --git a/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj index faf3af6a526ead..63109abed99f6f 100644 --- a/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -250,12 +250,6 @@ Common\Interop\Windows\Kernel32\Interop.HandleTypes.cs - - Common\Interop\Windows\Kernel32\Interop.LocalAlloc.cs - - - Common\Interop\Windows\Ole32\Interop.CoTaskMemAlloc.cs - Common\Interop\Windows\OleAut32\Interop.SysAllocStringByteLen.cs diff --git a/src/coreclr/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs b/src/coreclr/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs index 9a34b905608b96..e4eced1373810a 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs @@ -8,9 +8,6 @@ internal static partial class Interop internal static partial class Libraries { internal const string Kernel32 = RuntimeHelpers.QCall; - internal const string User32 = RuntimeHelpers.QCall; - internal const string Ole32 = RuntimeHelpers.QCall; internal const string OleAut32 = RuntimeHelpers.QCall; - internal const string Advapi32 = RuntimeHelpers.QCall; } } diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index e81335a3d05c10..005b73a47e4dc8 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -250,52 +250,6 @@ public static IntPtr GetHINSTANCE(Module m) [MethodImpl(MethodImplOptions.InternalCall)] internal static extern Exception GetExceptionForHRInternal(int errorCode, IntPtr errorInfo); - public static IntPtr AllocHGlobal(IntPtr cb) - { - // For backwards compatibility on 32 bit platforms, ensure we pass values between - // int.MaxValue and uint.MaxValue to Windows. If the binary has had the - // LARGEADDRESSAWARE bit set in the PE header, it may get 3 or 4 GB of user mode - // address space. It is remotely that those allocations could have succeeded, - // though I couldn't reproduce that. In either case, that means we should continue - // throwing an OOM instead of an ArgumentOutOfRangeException for "negative" amounts of memory. - UIntPtr numBytes; -#if TARGET_64BIT - numBytes = new UIntPtr(unchecked((ulong)cb.ToInt64())); -#else // 32 - numBytes = new UIntPtr(unchecked((uint)cb.ToInt32())); -#endif - - IntPtr pNewMem = Interop.Kernel32.LocalAlloc(Interop.Kernel32.LMEM_FIXED, unchecked(numBytes)); - if (pNewMem == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return pNewMem; - } - - public static void FreeHGlobal(IntPtr hglobal) - { - if (!IsNullOrWin32Atom(hglobal)) - { - if (IntPtr.Zero != Interop.Kernel32.LocalFree(hglobal)) - { - ThrowExceptionForHR(GetHRForLastWin32Error()); - } - } - } - - public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb) - { - IntPtr pNewMem = Interop.Kernel32.LocalReAlloc(pv, cb, Interop.Kernel32.LMEM_MOVEABLE); - if (pNewMem == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return pNewMem; - } - #if FEATURE_COMINTEROP /// /// Converts the CLR exception to an HRESULT. This function also sets @@ -481,83 +435,6 @@ public static IntPtr CreateAggregatedObject(IntPtr pOuter, T o) where T : not [MethodImpl(MethodImplOptions.InternalCall)] public static extern bool IsComObject(object o); -#endif // FEATURE_COMINTEROP - - public static IntPtr AllocCoTaskMem(int cb) - { - IntPtr pNewMem = Interop.Ole32.CoTaskMemAlloc(new UIntPtr((uint)cb)); - if (pNewMem == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return pNewMem; - } - - public static void FreeCoTaskMem(IntPtr ptr) - { - if (!IsNullOrWin32Atom(ptr)) - { - Interop.Ole32.CoTaskMemFree(ptr); - } - } - - public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb) - { - IntPtr pNewMem = Interop.Ole32.CoTaskMemRealloc(pv, new UIntPtr((uint)cb)); - if (pNewMem == IntPtr.Zero && cb != 0) - { - throw new OutOfMemoryException(); - } - - return pNewMem; - } - - internal static IntPtr AllocBSTR(int length) - { - IntPtr bstr = Interop.OleAut32.SysAllocStringLen(null, length); - if (bstr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - return bstr; - } - - public static void FreeBSTR(IntPtr ptr) - { - if (!IsNullOrWin32Atom(ptr)) - { - Interop.OleAut32.SysFreeString(ptr); - } - } - - public static IntPtr StringToBSTR(string? s) - { - if (s is null) - { - return IntPtr.Zero; - } - - IntPtr bstr = Interop.OleAut32.SysAllocStringLen(s, s.Length); - if (bstr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return bstr; - } - - public static string PtrToStringBSTR(IntPtr ptr) - { - if (ptr == IntPtr.Zero) - { - throw new ArgumentNullException(nameof(ptr)); - } - - return PtrToStringUni(ptr, (int)(SysStringByteLen(ptr) / sizeof(char))); - } - -#if FEATURE_COMINTEROP /// /// Release the COM component and if the reference hits 0 zombie this object. /// Further usage of this Object might throw an exception diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/StubHelpers.cs b/src/coreclr/src/System.Private.CoreLib/src/System/StubHelpers.cs index b507b28559dac2..57fcbced628806 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/StubHelpers.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/StubHelpers.cs @@ -127,7 +127,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP internal static void ClearNative(IntPtr pNative) { - Interop.Ole32.CoTaskMemFree(pNative); + Marshal.FreeCoTaskMem(pNative); } internal static unsafe void ConvertFixedToNative(int flags, string strManaged, IntPtr pNativeBuffer, int length) @@ -257,10 +257,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP internal static void ClearNative(IntPtr pNative) { - if (pNative != IntPtr.Zero) - { - Interop.Ole32.CoTaskMemFree(pNative); - } + Marshal.FreeCoTaskMem(pNative); } } @@ -412,10 +409,7 @@ internal static unsafe IntPtr ConvertToNative(string strManaged, IntPtr pNativeB internal static void ClearNative(IntPtr pNative) { - if (IntPtr.Zero != pNative) - { - Interop.OleAut32.SysFreeString(pNative); - } + Marshal.FreeBSTR(pNative); } } // class BSTRMarshaler @@ -476,7 +470,7 @@ internal static void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { - Interop.Ole32.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); + Marshal.FreeCoTaskMem((IntPtr)(((long)pNative) - sizeof(uint))); } } } // class VBByValStrMarshaler @@ -518,10 +512,7 @@ internal static IntPtr ConvertToNative(int flags, string strManaged) internal static void ClearNative(IntPtr pNative) { - if (IntPtr.Zero != pNative) - { - Interop.OleAut32.SysFreeString(pNative); - } + Marshal.FreeBSTR(pNative); } } // class AnsiBSTRMarshaler @@ -1067,7 +1058,7 @@ internal void ClearNative(IntPtr pNativeHome) // this must happen regardless of BackPropAction Marshal.DestroyStructure(pNativeHome, layoutType); } - Interop.Ole32.CoTaskMemFree(pNativeHome); + Marshal.FreeCoTaskMem(pNativeHome); } StubHelpers.DestroyCleanupList(ref cleanupWorkList); } diff --git a/src/coreclr/src/dlls/mscordac/mscordac_unixexports.src b/src/coreclr/src/dlls/mscordac/mscordac_unixexports.src index ddf36258fa3e05..aabfc46df2c4d7 100644 --- a/src/coreclr/src/dlls/mscordac/mscordac_unixexports.src +++ b/src/coreclr/src/dlls/mscordac/mscordac_unixexports.src @@ -138,7 +138,6 @@ nativeStringResourceTable_mscorrc #LoadLibraryW #LoadLibraryExW #LocalAlloc -#LocalReAlloc #LocalFree #MapViewOfFile #MoveFileExW diff --git a/src/coreclr/src/pal/inc/pal.h b/src/coreclr/src/pal/inc/pal.h index 133434dff0986d..d1642aef8d87ab 100644 --- a/src/coreclr/src/pal/inc/pal.h +++ b/src/coreclr/src/pal/inc/pal.h @@ -2776,14 +2776,6 @@ LocalAlloc( IN UINT uFlags, IN SIZE_T uBytes); -PALIMPORT -HLOCAL -PALAPI -LocalReAlloc( - IN HLOCAL hMem, - IN SIZE_T uBytes, - IN UINT uFlags); - PALIMPORT HLOCAL PALAPI diff --git a/src/coreclr/src/pal/inc/rt/palrt.h b/src/coreclr/src/pal/inc/rt/palrt.h index 4f29ea4b5ded92..47e6ffc000bcc0 100644 --- a/src/coreclr/src/pal/inc/rt/palrt.h +++ b/src/coreclr/src/pal/inc/rt/palrt.h @@ -303,7 +303,6 @@ typedef union _ULARGE_INTEGER { /******************* OLE, BSTR, VARIANT *************************/ STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemAlloc(SIZE_T cb); -STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemRealloc(LPVOID pv, SIZE_T cb); STDAPI_VIS(DLLEXPORT, void) CoTaskMemFree(LPVOID pv); typedef SHORT VARIANT_BOOL; diff --git a/src/coreclr/src/pal/src/memory/local.cpp b/src/coreclr/src/pal/src/memory/local.cpp index 8ed51b7c175712..fc62ef428b1e22 100644 --- a/src/coreclr/src/pal/src/memory/local.cpp +++ b/src/coreclr/src/pal/src/memory/local.cpp @@ -69,52 +69,6 @@ LocalAlloc( return (HLOCAL) lpRetVal; } -/*++ -Function: -LocalReAlloc - -See MSDN doc. ---*/ -HLOCAL -PALAPI -LocalReAlloc( - IN HLOCAL hMem, - IN SIZE_T uBytes, - IN UINT uFlags) -{ - LPVOID lpRetVal = NULL; - PERF_ENTRY(LocalReAlloc); - ENTRY("LocalReAlloc (hMem=%p, uBytes=%u, uFlags=%#x)\n", hMem, uBytes, uFlags); - - if (uFlags != LMEM_MOVEABLE) - { - // Currently valid iff uFlags is LMEM_MOVEABLE - ASSERT("Invalid parameter uFlags=0x%x\n", uFlags); - SetLastError(ERROR_INVALID_PARAMETER); - goto done; - } - - if (uBytes == 0) - { - // PAL's realloc behaves like free for a requested size of zero bytes. Force a nonzero size to get a valid pointer. - uBytes = 1; - } - - lpRetVal = PAL_realloc(hMem, uBytes); - - if (lpRetVal == NULL) - { - ERROR("Not enough memory\n"); - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - goto done; - } - -done: - LOGEXIT("LocalReAlloc returning %p.\n", lpRetVal); - PERF_EXIT(LocalReAlloc); - return (HLOCAL)lpRetVal; -} - /*++ Function: LocalFree diff --git a/src/coreclr/src/palrt/comem.cpp b/src/coreclr/src/palrt/comem.cpp index bb1bdf28a5fbd2..e56e720cc80c2f 100644 --- a/src/coreclr/src/palrt/comem.cpp +++ b/src/coreclr/src/palrt/comem.cpp @@ -12,15 +12,10 @@ STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb) { - return LocalAlloc(LMEM_FIXED, cb); -} - -STDAPI_(LPVOID) CoTaskMemRealloc(LPVOID pv, SIZE_T cb) -{ - return LocalReAlloc(pv, cb, LMEM_MOVEABLE); + return malloc(cb); } STDAPI_(void) CoTaskMemFree(LPVOID pv) { - LocalFree(pv); + free(pv); } diff --git a/src/coreclr/src/vm/ecalllist.h b/src/coreclr/src/vm/ecalllist.h index e29c06da095f9f..6298c95d95dc9e 100644 --- a/src/coreclr/src/vm/ecalllist.h +++ b/src/coreclr/src/vm/ecalllist.h @@ -1054,15 +1054,9 @@ FCFuncStart(gPalKernel32Funcs) QCFuncElement("CreateSemaphoreEx", CreateSemaphoreExW) QCFuncElement("FormatMessage", FormatMessageW) QCFuncElement("FreeEnvironmentStrings", FreeEnvironmentStringsW) - QCFuncElement("GetCurrentProcessId", GetCurrentProcessId) - QCFuncElement("GetCurrentThreadId", GetCurrentThreadId) QCFuncElement("GetEnvironmentStrings", GetEnvironmentStringsW) QCFuncElement("GetEnvironmentVariable", GetEnvironmentVariableW) QCFuncElement("GetStdHandle", GetStdHandle) - QCFuncElement("GetSystemInfo", GetSystemInfo) - QCFuncElement("LocalAlloc", LocalAlloc) - QCFuncElement("LocalReAlloc", LocalReAlloc) - QCFuncElement("LocalFree", LocalFree) QCFuncElement("OpenEvent", OpenEventW) QCFuncElement("OpenMutex", OpenMutexW) QCFuncElement("OpenSemaphore", OpenSemaphoreW) @@ -1074,17 +1068,8 @@ FCFuncStart(gPalKernel32Funcs) QCFuncElement("SetEvent", SetEvent) QCFuncElement("WriteFile", WriteFile) FCFuncEnd() - -FCFuncStart(gPalOle32Funcs) - QCFuncElement("CoTaskMemAlloc", CoTaskMemAlloc) - QCFuncElement("CoTaskMemRealloc", CoTaskMemRealloc) - QCFuncElement("CoTaskMemFree", CoTaskMemFree) -FCFuncEnd() - FCFuncStart(gPalOleAut32Funcs) QCFuncElement("SysAllocStringByteLen", SysAllocStringByteLen) - QCFuncElement("SysAllocStringLen", SysAllocStringLen) - QCFuncElement("SysFreeString", SysFreeString) FCFuncEnd() #endif @@ -1199,7 +1184,6 @@ FCClassElement("Object", "System", gObjectFuncs) FCClassElement("ObjectMarshaler", "System.StubHelpers", gObjectMarshalerFuncs) #endif #ifdef TARGET_UNIX -FCClassElement("Ole32", "", gPalOle32Funcs) FCClassElement("OleAut32", "", gPalOleAut32Funcs) #endif FCClassElement("OverlappedData", "System.Threading", gOverlappedFuncs) diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MemAlloc.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MemAlloc.cs new file mode 100644 index 00000000000000..b7f4bfd3742161 --- /dev/null +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MemAlloc.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal unsafe partial class Sys + { + [DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemAlloc")] + internal static extern IntPtr MemAlloc(nuint sizeInBytes); + + [DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemReAlloc")] + internal static extern IntPtr MemReAlloc(IntPtr ptr, nuint newSize); + + [DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemFree")] + internal static extern void MemFree(IntPtr ptr); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.LocalAlloc.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.LocalAlloc.cs index e21cb05caef8f4..9ef79146141be0 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.LocalAlloc.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.LocalAlloc.cs @@ -12,12 +12,12 @@ internal static partial class Kernel32 internal const uint LMEM_MOVEABLE = 0x0002; [DllImport(Libraries.Kernel32)] - internal static extern IntPtr LocalAlloc(uint uFlags, UIntPtr uBytes); + internal static extern IntPtr LocalAlloc(uint uFlags, nuint uBytes); [DllImport(Libraries.Kernel32)] - internal static extern IntPtr LocalReAlloc(IntPtr hMem, IntPtr uBytes, uint uFlags); + internal static extern IntPtr LocalReAlloc(IntPtr hMem, nuint uBytes, uint uFlags); - [DllImport(Libraries.Kernel32, SetLastError = true)] + [DllImport(Libraries.Kernel32)] internal static extern IntPtr LocalFree(IntPtr hMem); } } diff --git a/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs b/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs index 9246fd3d7a065e..9bce558be0e109 100644 --- a/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs +++ b/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs @@ -9,10 +9,10 @@ internal static partial class Interop internal static partial class Ole32 { [DllImport(Libraries.Ole32)] - internal static extern IntPtr CoTaskMemAlloc(UIntPtr cb); + internal static extern IntPtr CoTaskMemAlloc(nuint cb); [DllImport(Libraries.Ole32)] - internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, UIntPtr cb); + internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, nuint cb); [DllImport(Libraries.Ole32)] internal static extern void CoTaskMemFree(IntPtr ptr); diff --git a/src/libraries/Common/src/Interop/Windows/OleAut32/Interop.SysAllocStringLen.cs b/src/libraries/Common/src/Interop/Windows/OleAut32/Interop.SysAllocStringLen.cs index 43e1c633c9e161..0ace274e645e71 100644 --- a/src/libraries/Common/src/Interop/Windows/OleAut32/Interop.SysAllocStringLen.cs +++ b/src/libraries/Common/src/Interop/Windows/OleAut32/Interop.SysAllocStringLen.cs @@ -9,6 +9,6 @@ internal static partial class Interop internal static partial class OleAut32 { [DllImport(Libraries.OleAut32, CharSet = CharSet.Unicode)] - internal static extern IntPtr SysAllocStringLen(string? src, int len); + internal static extern IntPtr SysAllocStringLen(IntPtr src, uint len); } } diff --git a/src/libraries/Native/Unix/System.Native/pal_memory.c b/src/libraries/Native/Unix/System.Native/pal_memory.c index 6ba14f94d0f3c8..35757dff03b9dd 100644 --- a/src/libraries/Native/Unix/System.Native/pal_memory.c +++ b/src/libraries/Native/Unix/System.Native/pal_memory.c @@ -2,9 +2,26 @@ // The .NET Foundation licenses this file to you under the MIT license. #include "pal_memory.h" + +#include #include -void* SystemNative_MemSet(void *s, int c, uintptr_t n) +void* SystemNative_MemAlloc(uintptr_t size) +{ + return malloc(size); +} + +void* SystemNative_MemReAlloc(void* ptr, uintptr_t size) +{ + return realloc(ptr, size); +} + +void SystemNative_MemFree(void* ptr) +{ + free(ptr); +} + +void* SystemNative_MemSet(void* s, int c, uintptr_t n) { - return memset(s, c, (size_t)n); + return memset(s, c, n); } diff --git a/src/libraries/Native/Unix/System.Native/pal_memory.h b/src/libraries/Native/Unix/System.Native/pal_memory.h index 5ef38a60fc9bde..83fc2f2f9f48a0 100644 --- a/src/libraries/Native/Unix/System.Native/pal_memory.h +++ b/src/libraries/Native/Unix/System.Native/pal_memory.h @@ -7,8 +7,21 @@ #include "pal_types.h" /** - * Fills memory with a set byte. Implemented as shim to memset(3). - * - * Returns a pointer to the memory. + * C runtime malloc */ -PALEXPORT void* SystemNative_MemSet(void *s, int c, uintptr_t n); +PALEXPORT void* SystemNative_MemAlloc(uintptr_t size); + +/** + * C runtime realloc + */ +PALEXPORT void* SystemNative_MemReAlloc(void* ptr, uintptr_t size); + +/** + * C runtime free + */ +PALEXPORT void SystemNative_MemFree(void* ptr); + +/** + * C runtime memset + */ +PALEXPORT void* SystemNative_MemSet(void* s, int c, uintptr_t n); diff --git a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj index ae0f96d5351e87..d8ae07cc58a0f7 100644 --- a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj +++ b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj @@ -265,8 +265,6 @@ Link="Common\Interop\Unix\Interop.Stat.cs" /> - Common\Interop\Windows\Kernel32\Interop.CompletionPort.cs + + Common\Interop\Windows\Kernel32\Interop.ConditionVariable.cs + Common\Interop\Windows\Kernel32\Interop.CreateFile.cs + + Common\Interop\Windows\Kernel32\Interop.CriticalSection.cs + Common\Interop\Windows\Kernel32\Interop.ExpandEnvironmentStrings.cs @@ -1449,6 +1455,9 @@ Common\Interop\Windows\Kernel32\Interop.LoadLibraryEx_IntPtr.cs + + Common\Interop\Windows\Kernel32\Interop.LocalAlloc.cs + Common\Interop\Windows\Kernel32\Interop.LockFile.cs @@ -1470,6 +1479,9 @@ Common\Interop\Windows\Kernel32\Interop.MultiByteToWideChar.cs + + Common\Interop\Windows\Kernel32\Interop.OSVERSIONINFOEX.cs + Common\Interop\Windows\Kernel32\Interop.OutputDebugString.cs @@ -1560,12 +1572,21 @@ Common\Interop\Windows\NtDll\Interop.SYSTEM_LEAP_SECOND_INFORMATION.cs + + Common\Interop\Windows\OleAut32\Interop.SysAllocStringLen.cs + + + Common\Interop\Windows\OleAut32\Interop.SysFreeString.cs + Common\Interop\Windows\Ole32\Interop.CoCreateGuid.cs Common\Interop\Windows\Ole32\Interop.CoGetStandardMarshal.cs + + Common\Interop\Windows\Ole32\Interop.CoTaskMemAlloc.cs + Common\Interop\Windows\Secur32\Interop.GetUserNameExW.cs @@ -1640,24 +1661,12 @@ - - Common\Interop\Windows\OleAut32\Interop.SysAllocStringLen.cs - - - Common\Interop\Windows\OleAut32\Interop.SysFreeString.cs - Common\Interop\Windows\Kernel32\Interop.CloseHandle.cs - - Common\Interop\Windows\Kernel32\Interop.ConditionVariable.cs - Common\Interop\Windows\Kernel32\Interop.Constants.cs - - Common\Interop\Windows\Kernel32\Interop.CriticalSection.cs - Common\Interop\Windows\Kernel32\Interop.EventWaitHandle.cs @@ -1676,9 +1685,6 @@ Common\Interop\Windows\Kernel32\Interop.Mutex.cs - - Common\Interop\Windows\Kernel32\Interop.OSVERSIONINFOEX.cs - Common\Interop\Windows\Kernel32\Interop.Semaphore.cs @@ -1757,6 +1763,9 @@ Common\Interop\Unix\System.Native\Interop.LSeek.cs + + Common\Interop\Unix\System.Native\Interop.MemAlloc.cs + Common\Interop\Unix\System.Native\Interop.MksTemps.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs index a4e0d2a5e84326..1be70624baa9f8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs @@ -61,5 +61,105 @@ internal static unsafe void GetAnsiStringBytes(ReadOnlySpan chars, Span AllocHGlobal((nint)(uint)cb); + + public static void FreeCoTaskMem(IntPtr ptr) => FreeHGlobal(ptr); + + public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb) + { + nuint cbNative = (nuint)(uint)cb; + + if (cbNative == 0) + { + if (pv != IntPtr.Zero) + { + Interop.Sys.MemFree(pv); + return IntPtr.Zero; + } + // Avoid undefined realloc behavior by always allocating at least one byte + cbNative = 1; + } + + IntPtr pNewMem = Interop.Sys.MemReAlloc(pv, cbNative); + if (pNewMem == IntPtr.Zero) + { + throw new OutOfMemoryException(); + } + return pNewMem; + } + + internal static unsafe IntPtr AllocBSTR(int length) + { + // SysAllocString on Windows aligns the memory block size up + const nuint WIN32_ALLOC_ALIGN = 15; + + ulong cbNative = 2 * (ulong)(uint)length + (uint)sizeof(IntPtr) + (uint)sizeof(char) + WIN32_ALLOC_ALIGN; + if (cbNative > uint.MaxValue) + { + throw new OutOfMemoryException(); + } + + IntPtr p = Interop.Sys.MemAlloc((nuint)cbNative & ~WIN32_ALLOC_ALIGN); + if (p == IntPtr.Zero) + { + throw new OutOfMemoryException(); + } + + IntPtr s = p + sizeof(IntPtr); + *(((uint*)s) - 1) = (uint)(length * sizeof(char)); + + return s; + } + + public static unsafe void FreeBSTR(IntPtr ptr) + { + if (ptr != IntPtr.Zero) + { + Interop.Sys.MemFree(ptr - sizeof(IntPtr)); + } + } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Windows.cs index 9be93a4efc7a2c..13941f50fa7a22 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Windows.cs @@ -126,5 +126,86 @@ internal static unsafe void GetAnsiStringBytes(ReadOnlySpan chars, Span public static readonly int SystemMaxDBCSCharSize = GetSystemMaxDBCSCharSize(); - public static IntPtr AllocHGlobal(int cb) => AllocHGlobal((IntPtr)cb); + public static IntPtr AllocHGlobal(int cb) => AllocHGlobal((nint)cb); public static unsafe string? PtrToStringAnsi(IntPtr ptr) { @@ -966,6 +966,32 @@ public static unsafe void ZeroFreeGlobalAllocUnicode(IntPtr s) FreeHGlobal(s); } + public static unsafe IntPtr StringToBSTR(string? s) + { + if (s is null) + { + return IntPtr.Zero; + } + + IntPtr bstr = AllocBSTR(s.Length); + + fixed (char* firstChar = s) + { + string.wstrcpy((char*)bstr, firstChar, s.Length + 1); + } + return bstr; + } + + public static string PtrToStringBSTR(IntPtr ptr) + { + if (ptr == IntPtr.Zero) + { + throw new ArgumentNullException(nameof(ptr)); + } + + return PtrToStringUni(ptr, (int)(SysStringByteLen(ptr) / sizeof(char))); + } + internal static unsafe uint SysStringByteLen(IntPtr s) { return *(((uint*)s) - 1); diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocCoTaskMemTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocCoTaskMemTests.cs index 126294e55e5422..a6b1144d9d0c73 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocCoTaskMemTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocCoTaskMemTests.cs @@ -43,7 +43,6 @@ public void ReAllocCoTaskMem_Invoke_DataCopied() [InlineData(1)] [InlineData(100)] [Theory] - [SkipOnMono("Behavior differences on Mono")] public void ReAllocCoTaskMem_PositiveSize(int size) { IntPtr p = Marshal.ReAllocCoTaskMem(IntPtr.Zero, size); @@ -53,22 +52,11 @@ public void ReAllocCoTaskMem_PositiveSize(int size) Assert.NotEqual(IntPtr.Zero, p1); IntPtr p2 = Marshal.ReAllocCoTaskMem(p1, 0); - - // TODO: Behavior differs between platforms currently - if (PlatformDetection.IsWindows) - { - Assert.Equal(IntPtr.Zero, p2); - } - else - { - Assert.NotEqual(IntPtr.Zero, p2); - Marshal.FreeCoTaskMem(p2); - } + Assert.Equal(IntPtr.Zero, p2); } [Fact] [OuterLoop] - [SkipOnMono("Behavior differences on Mono")] public void ReAllocCoTaskMem_NegativeSize_ThrowsOutOfMemoryException() { // -1 is treated as (uint)-1 by ReAllocCoTaskMem. The allocation may succeed on 64-bit machines. diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocHGlobalTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocHGlobalTests.cs index b05b900423299c..c8f5468bef1561 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocHGlobalTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ReAllocHGlobalTests.cs @@ -44,19 +44,9 @@ public void ReAllocHGlobal_Invoke_DataCopied() [InlineData(1)] [InlineData(100)] [Theory] - [SkipOnMono("Behavior differences on Mono")] public void ReAllocHGlobal_PositiveSize(int size) { - IntPtr p; - // TODO: Behavior differs between platforms currently - if (PlatformDetection.IsWindows) - { - p = Marshal.AllocHGlobal(size); - } - else - { - p = Marshal.ReAllocHGlobal(IntPtr.Zero, (IntPtr)size); - } + IntPtr p = Marshal.ReAllocHGlobal(IntPtr.Zero, (IntPtr)size); Assert.NotEqual(IntPtr.Zero, p); IntPtr p1 = Marshal.ReAllocHGlobal(p, (IntPtr)(size + 1)); @@ -70,16 +60,8 @@ public void ReAllocHGlobal_PositiveSize(int size) } [Fact] - [SkipOnMono("Behavior differences on Mono")] public void ReAllocHGlobal_NegativeSize_ThrowsOutOfMemoryException() { - // TODO: Behavior differs between platforms currently - if (PlatformDetection.IsWindows) - { - // ReAllocHGlobal always throws when the original pointer is null (different from standard C/C++ realloc) - Assert.Throws(() => Marshal.ReAllocHGlobal(IntPtr.Zero, IntPtr.Zero)); - Assert.Throws(() => Marshal.ReAllocHGlobal(IntPtr.Zero, (IntPtr)1)); - } Assert.Throws(() => Marshal.ReAllocHGlobal(IntPtr.Zero, (IntPtr)(-1))); IntPtr p = Marshal.AllocHGlobal((IntPtr)1); diff --git a/src/mono/mono/metadata/icall-def-netcore.h b/src/mono/mono/metadata/icall-def-netcore.h index 60283d09e9da2c..7e4caa60d8b089 100644 --- a/src/mono/mono/metadata/icall-def-netcore.h +++ b/src/mono/mono/metadata/icall-def-netcore.h @@ -344,24 +344,15 @@ HANDLES(GCH_2, "InternalFree", ves_icall_System_GCHandle_InternalFree, void, 1, HANDLES(GCH_3, "InternalGet", ves_icall_System_GCHandle_InternalGet, MonoObject, 1, (gpointer)) HANDLES(GCH_4, "InternalSet", ves_icall_System_GCHandle_InternalSet, void, 2, (gpointer, MonoObject)) -ICALL_TYPE(MARSHAL, "System.Runtime.InteropServices.Marshal", MARSHAL_2) -NOHANDLES(ICALL(MARSHAL_2, "AllocCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem)) -NOHANDLES(ICALL(MARSHAL_3, "AllocHGlobal", ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal)) -NOHANDLES(ICALL(MARSHAL_50, "BufferToBSTR", ves_icall_System_Runtime_InteropServices_Marshal_BufferToBSTR)) +ICALL_TYPE(MARSHAL, "System.Runtime.InteropServices.Marshal", MARSHAL_4) HANDLES(MARSHAL_4, "DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure, void, 2, (gpointer, MonoReflectionType)) -NOHANDLES(ICALL(MARSHAL_5, "FreeBSTR", ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR)) -NOHANDLES(ICALL(MARSHAL_6, "FreeCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem)) -NOHANDLES(ICALL(MARSHAL_7, "FreeHGlobal", ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal)) HANDLES(MARSHAL_9, "GetDelegateForFunctionPointerInternal", ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal, MonoDelegate, 2, (gpointer, MonoReflectionType)) HANDLES(MARSHAL_10, "GetFunctionPointerForDelegateInternal", ves_icall_System_Runtime_InteropServices_Marshal_GetFunctionPointerForDelegateInternal, gpointer, 1, (MonoDelegate)) NOHANDLES(ICALL(MARSHAL_11, "GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error)) HANDLES(MARSHAL_48a, "IsPinnableType", ves_icall_System_Runtime_InteropServices_Marshal_IsPinnableType, MonoBoolean, 1, (MonoReflectionType)) HANDLES(MARSHAL_12, "OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf, int, 2, (MonoReflectionType, MonoString)) HANDLES(MARSHAL_13, "PrelinkInternal", ves_icall_System_Runtime_InteropServices_Marshal_Prelink, void, 1, (MonoReflectionMethod)) -HANDLES(MARSHAL_17, "PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR, MonoString, 1, (mono_bstr_const)) HANDLES(MARSHAL_20, "PtrToStructureInternal", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructureInternal, void, 3, (gconstpointer, MonoObject, MonoBoolean)) -NOHANDLES(ICALL(MARSHAL_43, "ReAllocCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem)) -NOHANDLES(ICALL(MARSHAL_23, "ReAllocHGlobal", ves_icall_System_Runtime_InteropServices_Marshal_ReAllocHGlobal)) NOHANDLES(ICALL(MARSHAL_29a, "SetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_SetLastWin32Error)) HANDLES(MARSHAL_30, "SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf, guint32, 1, (MonoReflectionType)) HANDLES(MARSHAL_31, "SizeOfHelper", ves_icall_System_Runtime_InteropServices_Marshal_SizeOfHelper, guint32, 2, (MonoReflectionType, MonoBoolean)) diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs index 2b1ff64b4802db..7d50fa0d28f655 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs @@ -11,21 +11,6 @@ namespace System.Runtime.InteropServices { public partial class Marshal { - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern IntPtr AllocCoTaskMem(int cb); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern IntPtr AllocHGlobal(IntPtr cb); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern void FreeBSTR(IntPtr ptr); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern void FreeCoTaskMem(IntPtr ptr); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern void FreeHGlobal(IntPtr hglobal); - [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern int GetLastWin32Error(); @@ -35,21 +20,9 @@ public partial class Marshal [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern IntPtr OffsetOf(Type t, string fieldName); - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern string PtrToStringBSTR(IntPtr ptr); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern IntPtr ReAllocCoTaskMem(IntPtr pv, int cb); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb); - [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld); - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private static extern unsafe IntPtr BufferToBSTR(char* ptr, int slen); - [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern bool IsPinnableType(Type type); @@ -342,22 +315,6 @@ public static IntPtr GetExceptionPointers() throw new PlatformNotSupportedException(); } - internal static unsafe IntPtr AllocBSTR(int length) - { - IntPtr res = BufferToBSTR((char*)IntPtr.Zero, length); - if (res == IntPtr.Zero) - throw new OutOfMemoryException(); - return res; - } - - public static unsafe IntPtr StringToBSTR(string? s) - { - if (s == null) - return IntPtr.Zero; - fixed (char* fixed_s = s) - return BufferToBSTR(fixed_s, s.Length); - } - private sealed class MarshalerInstanceKeyComparer : IEqualityComparer<(Type, string)> { public bool Equals((Type, string) lhs, (Type, string) rhs)