From 5e4163d853d32afab184afc7feef9fdb8c7e5823 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:33:50 +0000 Subject: [PATCH 01/11] Initial plan From 761cf2a61b5b4db63f59a6230556b5a9942389b6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:06:40 +0000 Subject: [PATCH 02/11] Replace Marshal.AllocHGlobal with NativeMemory.Alloc in cross-platform code Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- .../Internal/MemoryBlocks/NativeHeapMemoryBlock.cs | 4 ++-- .../Runtime/InteropServices/JavaScript/JSFunctionBinding.cs | 4 ++-- .../JavaScript/JSHostImplementation.Types.cs | 4 ++-- .../InteropServices/JavaScript/JSHostImplementation.cs | 4 ++-- .../Runtime/InteropServices/JavaScript/JSProxyContext.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Byte.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Double.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Int32.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.JSObject.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Object.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.String.cs | 6 +++--- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs index bbcd1a0e60b92c..ae0cef8c4d227e 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs @@ -29,7 +29,7 @@ public DisposableData(int size) finally #endif { - _pointer = Marshal.AllocHGlobal(size); + _pointer = (IntPtr)NativeMemory.Alloc((nuint)size); } } @@ -46,7 +46,7 @@ protected override void Release() IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero); if (ptr != IntPtr.Zero) { - Marshal.FreeHGlobal(ptr); + NativeMemory.Free((void*)ptr); } } } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs index b7e4adf82ce3d0..239f31c78e5614 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs @@ -417,7 +417,7 @@ internal static unsafe void DispatchJSImportAsyncPost(JSFunctionBinding signatur exc.slot.ReceiverShouldFree = true; var bytes = sizeof(JSMarshalerArgument) * arguments.Length; - void* cpy = (void*)Marshal.AllocHGlobal(bytes); + void* cpy = NativeMemory.Alloc((nuint)bytes); arguments.CopyTo(new Span(cpy, arguments.Length)); var sig = (nint)signature.Header; @@ -480,7 +480,7 @@ internal static unsafe void ResolveOrRejectPromise(JSProxyContext targetContext, // this copy is freed in SystemInteropJS_ResolveOrRejectPromise var bytes = sizeof(JSMarshalerArgument) * arguments.Length; - void* cpy = (void*)Marshal.AllocHGlobal(bytes); + void* cpy = NativeMemory.Alloc((nuint)bytes); arguments.CopyTo(new Span(cpy, arguments.Length)); // async diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs index d65421592248a2..c4052d95d979ed 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs @@ -26,7 +26,7 @@ public PromiseHolder(JSProxyContext targetContext) GCHandle = (IntPtr)InteropServices.GCHandle.Alloc(this, GCHandleType.Normal); ProxyContext = targetContext; #if FEATURE_WASM_MANAGED_THREADS - State = (PromiseHolderState*)Marshal.AllocHGlobal(sizeof(PromiseHolderState)); + State = (PromiseHolderState*)NativeMemory.Alloc((nuint)sizeof(PromiseHolderState)); Interlocked.Exchange(ref (*State).IsResolving, 0); #endif } @@ -36,7 +36,7 @@ public PromiseHolder(JSProxyContext targetContext, nint gcvHandle) GCHandle = gcvHandle; ProxyContext = targetContext; #if FEATURE_WASM_MANAGED_THREADS - State = (PromiseHolderState*)Marshal.AllocHGlobal(sizeof(PromiseHolderState)); + State = (PromiseHolderState*)NativeMemory.Alloc((nuint)sizeof(PromiseHolderState)); Interlocked.Exchange(ref (*State).IsResolving, 0); #endif } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs index a75c897458d04c..26a133fbbc6021 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs @@ -131,7 +131,7 @@ public static unsafe JSFunctionBinding GetMethodSignature(ReadOnlySpan @@ -109,7 +109,7 @@ public void ToJS(byte[]? value) } slot.Length = value.Length; slot.Type = MarshalerType.Array; - slot.IntPtrValue = Marshal.AllocHGlobal(value.Length * sizeof(byte)); + slot.IntPtrValue = (IntPtr)NativeMemory.Alloc((nuint)(value.Length * sizeof(byte))); slot.ElementType = MarshalerType.Byte; Marshal.Copy(value, 0, slot.IntPtrValue, slot.Length); } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs index b82c5f8fa842a5..96be8877bc9a89 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs @@ -95,7 +95,7 @@ public void ToManaged(out double[]? value) } value = new double[slot.Length]; Marshal.Copy(slot.IntPtrValue, value, 0, slot.Length); - Marshal.FreeHGlobal(slot.IntPtrValue); + NativeMemory.Free((void*)slot.IntPtrValue); } /// @@ -114,7 +114,7 @@ public void ToJS(double[] value) return; } slot.Type = MarshalerType.Array; - slot.IntPtrValue = Marshal.AllocHGlobal(value.Length * sizeof(double)); + slot.IntPtrValue = (IntPtr)NativeMemory.Alloc((nuint)(value.Length * sizeof(double))); slot.Length = value.Length; slot.ElementType = MarshalerType.Double; Marshal.Copy(value, 0, slot.IntPtrValue, slot.Length); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs index 1798761eeac161..f8e3dd4217a2fd 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs @@ -92,7 +92,7 @@ public void ToManaged(out int[]? value) } value = new int[slot.Length]; Marshal.Copy(slot.IntPtrValue, value, 0, slot.Length); - Marshal.FreeHGlobal(slot.IntPtrValue); + NativeMemory.Free((void*)slot.IntPtrValue); } /// @@ -108,7 +108,7 @@ public void ToJS(int[]? value) return; } slot.Type = MarshalerType.Array; - slot.IntPtrValue = Marshal.AllocHGlobal(value.Length * sizeof(int)); + slot.IntPtrValue = (IntPtr)NativeMemory.Alloc((nuint)(value.Length * sizeof(int))); slot.Length = value.Length; slot.ElementType = MarshalerType.Int32; Marshal.Copy(value, 0, slot.IntPtrValue, slot.Length); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.JSObject.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.JSObject.cs index 6bf6b9f7edcf13..0743b88b901136 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.JSObject.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.JSObject.cs @@ -88,7 +88,7 @@ public unsafe void ToManaged(out JSObject?[]? value) arg.ToManaged(out val); value[i] = val; } - Marshal.FreeHGlobal(slot.IntPtrValue); + NativeMemory.Free((void*)slot.IntPtrValue); } /// @@ -110,7 +110,7 @@ public unsafe void ToJS(JSObject?[] value) int bytes = value.Length * sizeof(JSMarshalerArgument); slot.Type = MarshalerType.Array; slot.ElementType = MarshalerType.JSObject; - JSMarshalerArgument* payload = (JSMarshalerArgument*)Marshal.AllocHGlobal(bytes); + JSMarshalerArgument* payload = (JSMarshalerArgument*)NativeMemory.Alloc((nuint)bytes); Unsafe.InitBlock(payload, 0, (uint)bytes); for (int i = 0; i < slot.Length; i++) { diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Object.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Object.cs index d4506a53ac7113..31223d01d8a5f3 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Object.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Object.cs @@ -354,7 +354,7 @@ public unsafe void ToManaged(out object?[]? value) #if !ENABLE_JS_INTEROP_BY_VALUE Interop.Runtime.DeregisterGCRoot(slot.IntPtrValue); #endif - Marshal.FreeHGlobal(slot.IntPtrValue); + NativeMemory.Free((void*)slot.IntPtrValue); } /// @@ -375,7 +375,7 @@ public unsafe void ToJS(object?[] value) slot.Length = value.Length; int bytes = value.Length * sizeof(JSMarshalerArgument); slot.Type = MarshalerType.Array; - JSMarshalerArgument* payload = (JSMarshalerArgument*)Marshal.AllocHGlobal(bytes); + JSMarshalerArgument* payload = (JSMarshalerArgument*)NativeMemory.Alloc((nuint)bytes); Unsafe.InitBlock(payload, 0, (uint)bytes); #if !ENABLE_JS_INTEROP_BY_VALUE Interop.Runtime.RegisterGCRoot(payload, bytes, IntPtr.Zero); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.String.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.String.cs index d0e2d9cb7f9f71..06d476ccdca5c0 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.String.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.String.cs @@ -24,7 +24,7 @@ public unsafe void ToManaged(out string? value) } #if ENABLE_JS_INTEROP_BY_VALUE value = Marshal.PtrToStringUni(slot.IntPtrValue, slot.Length); - Marshal.FreeHGlobal(slot.IntPtrValue); + NativeMemory.Free((void*)slot.IntPtrValue); #else fixed (void* argAsRoot = &slot.IntPtrValue) { @@ -96,7 +96,7 @@ public unsafe void ToManaged(out string?[]? value) #if !ENABLE_JS_INTEROP_BY_VALUE Interop.Runtime.DeregisterGCRoot(slot.IntPtrValue); #endif - Marshal.FreeHGlobal(slot.IntPtrValue); + NativeMemory.Free((void*)slot.IntPtrValue); } /// @@ -117,7 +117,7 @@ public unsafe void ToJS(string?[] value) slot.Length = value.Length; int bytes = value.Length * sizeof(JSMarshalerArgument); slot.Type = MarshalerType.Array; - JSMarshalerArgument* payload = (JSMarshalerArgument*)Marshal.AllocHGlobal(bytes); + JSMarshalerArgument* payload = (JSMarshalerArgument*)NativeMemory.Alloc((nuint)bytes); Unsafe.InitBlock(payload, 0, (uint)bytes); #if !ENABLE_JS_INTEROP_BY_VALUE Interop.Runtime.RegisterGCRoot(payload, bytes, IntPtr.Zero); From 4eb043ec8842f0b57a9fb83cc76c6f4b8ee950ac Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:13:49 +0000 Subject: [PATCH 03/11] Add unsafe keyword to methods using NativeMemory in JavaScript interop Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- .../Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Byte.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Double.cs | 4 ++-- .../JavaScript/Marshaling/JSMarshalerArgument.Int32.cs | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs index ae0cef8c4d227e..bbcd1a0e60b92c 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.cs @@ -29,7 +29,7 @@ public DisposableData(int size) finally #endif { - _pointer = (IntPtr)NativeMemory.Alloc((nuint)size); + _pointer = Marshal.AllocHGlobal(size); } } @@ -46,7 +46,7 @@ protected override void Release() IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero); if (ptr != IntPtr.Zero) { - NativeMemory.Free((void*)ptr); + Marshal.FreeHGlobal(ptr); } } } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Byte.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Byte.cs index 87c6189d41abb2..991407892bab08 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Byte.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Byte.cs @@ -83,7 +83,7 @@ public void ToJS(byte? value) /// It's used by JSImport code generator and should not be used by developers in source code. /// /// The value to be marshaled. - public void ToManaged(out byte[]? value) + public unsafe void ToManaged(out byte[]? value) { if (slot.Type == MarshalerType.None) { @@ -100,7 +100,7 @@ public void ToManaged(out byte[]? value) /// It's used by JSImport code generator and should not be used by developers in source code. /// /// The value to be marshaled. - public void ToJS(byte[]? value) + public unsafe void ToJS(byte[]? value) { if (value == null) { diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs index 96be8877bc9a89..b62f5f782e6be2 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Double.cs @@ -86,7 +86,7 @@ public void ToJS(double? value) #if !DEBUG [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public void ToManaged(out double[]? value) + public unsafe void ToManaged(out double[]? value) { if (slot.Type == MarshalerType.None) { @@ -106,7 +106,7 @@ public void ToManaged(out double[]? value) #if !DEBUG [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public void ToJS(double[] value) + public unsafe void ToJS(double[] value) { if (value == null) { diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs index f8e3dd4217a2fd..e1db8fa382bd5b 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Int32.cs @@ -83,7 +83,7 @@ public void ToJS(int? value) /// It's used by JSImport code generator and should not be used by developers in source code. /// /// The value to be marshaled. - public void ToManaged(out int[]? value) + public unsafe void ToManaged(out int[]? value) { if (slot.Type == MarshalerType.None) { @@ -100,7 +100,7 @@ public void ToManaged(out int[]? value) /// It's used by JSImport code generator and should not be used by developers in source code. /// /// The value to be marshaled. - public void ToJS(int[]? value) + public unsafe void ToJS(int[]? value) { if (value == null) { From 365502bdd633de0caaab80e9ea0f82e1ba867482 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:15:16 +0000 Subject: [PATCH 04/11] Replace Marshal.AllocHGlobal with NativeMemory.Alloc in JIT interface Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index 514e055929460e..2f27b159d30d82 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -3225,14 +3225,14 @@ private void reportMetadata(byte* key, void* value, nuint length) private void* allocateArray(UIntPtr cBytes) #pragma warning restore CA1822 // Mark members as static { - return (void*)Marshal.AllocHGlobal((IntPtr)(void*)cBytes); + return NativeMemory.Alloc((nuint)cBytes); } #pragma warning disable CA1822 // Mark members as static private void freeArray(void* array) #pragma warning restore CA1822 // Mark members as static { - Marshal.FreeHGlobal((IntPtr)array); + NativeMemory.Free(array); } #pragma warning disable CA1822 // Mark members as static From 1d53cdf9285d5a0c5213111f2723999325daee2d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 18:59:59 +0000 Subject: [PATCH 05/11] Replace remaining Marshal alloc/free calls with NativeMemory in CorInfoImpl.cs Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index 2f27b159d30d82..8aec98d92570a7 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -2154,14 +2154,14 @@ private uint getClassAttribsInternal(TypeDesc type) private void* LongLifetimeMalloc(UIntPtr sz) #pragma warning restore CA1822 // Mark members as static { - return (void*)Marshal.AllocCoTaskMem((int)sz); + return NativeMemory.Alloc((nuint)sz); } #pragma warning disable CA1822 // Mark members as static private void LongLifetimeFree(void* obj) #pragma warning restore CA1822 // Mark members as static { - Marshal.FreeCoTaskMem((IntPtr)obj); + NativeMemory.Free(obj); } private UIntPtr getClassStaticDynamicInfo(CORINFO_CLASS_STRUCT_* cls) @@ -3211,8 +3211,8 @@ private void getVars(CORINFO_METHOD_STRUCT_* ftn, ref uint cVars, ILVarInfo** va private void reportRichMappings(InlineTreeNode* inlineTree, uint numInlineTree, RichOffsetMapping* mappings, uint numMappings) #pragma warning restore CA1822 // Mark members as static { - Marshal.FreeHGlobal((IntPtr)inlineTree); - Marshal.FreeHGlobal((IntPtr)mappings); + NativeMemory.Free(inlineTree); + NativeMemory.Free(mappings); } #pragma warning disable CA1822 // Mark members as static From 3a104b79b9baf0ead7eb5e816aa42eb65b80f4b7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 20:50:48 +0000 Subject: [PATCH 06/11] Replace Marshal.AllocCoTaskMem with NativeMemory.Alloc in JitConfigProvider and ThunkGenerator Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs | 4 ++-- .../tools/Common/JitInterface/ThunkGenerator/Program.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs index 459e20a8b94b81..54a17aad90b2e6 100644 --- a/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs @@ -164,12 +164,12 @@ private static unsafe IntPtr CreateUnmanagedInstance() const int numCallbacks = 2; - void** callbacks = (void**)Marshal.AllocCoTaskMem(sizeof(IntPtr) * numCallbacks); + void** callbacks = (void**)NativeMemory.Alloc((nuint)(sizeof(IntPtr) * numCallbacks)); callbacks[0] = (delegate* unmanaged)&getIntConfigValue; callbacks[1] = (delegate* unmanaged)&getStringConfigValue; - IntPtr instance = Marshal.AllocCoTaskMem(sizeof(IntPtr)); + IntPtr instance = (IntPtr)NativeMemory.Alloc((nuint)sizeof(IntPtr)); *(IntPtr*)instance = (IntPtr)callbacks; return instance; diff --git a/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs b/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs index 1f0362ea7e9e9a..8780a723724f6c 100644 --- a/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs +++ b/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs @@ -349,7 +349,7 @@ internal unsafe partial class CorInfoImpl tw.WriteLine(@" private static IntPtr GetUnmanagedCallbacks() { - void** callbacks = (void**)Marshal.AllocCoTaskMem(sizeof(IntPtr) * " + total + @"); + void** callbacks = (void**)NativeMemory.Alloc(sizeof(IntPtr) * " + total + @"); "); int index = 0; From 199a963abd00e6f0f35283c176259e5b89116c2b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 21:15:00 +0000 Subject: [PATCH 07/11] Replace Marshal.FreeHGlobal with NativeMemory.Free in BindJSImportImpl Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- .../Runtime/InteropServices/JavaScript/JSFunctionBinding.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs index 239f31c78e5614..9375b83b013401 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs @@ -440,7 +440,7 @@ internal static unsafe JSFunctionBinding BindJSImportImpl(string functionName, s if (exceptionPtr != IntPtr.Zero) { var message = Marshal.PtrToStringUni(exceptionPtr)!; - Marshal.FreeHGlobal(exceptionPtr); + NativeMemory.Free((void*)exceptionPtr); throw new JSException(message); } From 711da0867e14d154577d33e6ece0a25e78193a56 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 11 Oct 2025 19:19:21 -0700 Subject: [PATCH 08/11] Update src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs --- src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs b/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs index 54a17aad90b2e6..5960572a816cea 100644 --- a/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs +++ b/src/coreclr/tools/Common/JitInterface/JitConfigProvider.cs @@ -164,7 +164,7 @@ private static unsafe IntPtr CreateUnmanagedInstance() const int numCallbacks = 2; - void** callbacks = (void**)NativeMemory.Alloc((nuint)(sizeof(IntPtr) * numCallbacks)); + void** callbacks = (void**)NativeMemory.Alloc((nuint)(sizeof(void*) * numCallbacks)); callbacks[0] = (delegate* unmanaged)&getIntConfigValue; callbacks[1] = (delegate* unmanaged)&getStringConfigValue; From 2b0ae864bba5e57eb6e4481394277a88d8bf53fe Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 11 Oct 2025 19:21:42 -0700 Subject: [PATCH 09/11] Update src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index 8aec98d92570a7..8907c595e0872c 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -3225,7 +3225,7 @@ private void reportMetadata(byte* key, void* value, nuint length) private void* allocateArray(UIntPtr cBytes) #pragma warning restore CA1822 // Mark members as static { - return NativeMemory.Alloc((nuint)cBytes); + return NativeMemory.Alloc(cBytes); } #pragma warning disable CA1822 // Mark members as static From eb8082ff0b205ed87dbc88762fc7adfc61e94a7a Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 11 Oct 2025 19:21:58 -0700 Subject: [PATCH 10/11] Update src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index 8907c595e0872c..505c9b6d742538 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -2154,7 +2154,7 @@ private uint getClassAttribsInternal(TypeDesc type) private void* LongLifetimeMalloc(UIntPtr sz) #pragma warning restore CA1822 // Mark members as static { - return NativeMemory.Alloc((nuint)sz); + return NativeMemory.Alloc(sz); } #pragma warning disable CA1822 // Mark members as static From a9b4fa5a1f8c8dca8d8e89b5547acb1a6e6b19a5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 12 Oct 2025 02:47:11 +0000 Subject: [PATCH 11/11] Use sizeof(void*) and simplify NativeMemory.Alloc calls Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs | 2 +- src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs index 8d0c83046277dd..e1065b2706224b 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs @@ -2606,7 +2606,7 @@ private static uint _getJitFlags(IntPtr thisHandle, IntPtr* ppException, CORJIT_ private static IntPtr GetUnmanagedCallbacks() { - void** callbacks = (void**)Marshal.AllocCoTaskMem(sizeof(IntPtr) * 176); + void** callbacks = (void**)NativeMemory.Alloc((nuint)(sizeof(void*) * 176)); callbacks[0] = (delegate* unmanaged)&_isIntrinsic; callbacks[1] = (delegate* unmanaged)&_notifyMethodInfoUsage; diff --git a/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs b/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs index 8780a723724f6c..172c2839f014ed 100644 --- a/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs +++ b/src/coreclr/tools/Common/JitInterface/ThunkGenerator/Program.cs @@ -349,7 +349,7 @@ internal unsafe partial class CorInfoImpl tw.WriteLine(@" private static IntPtr GetUnmanagedCallbacks() { - void** callbacks = (void**)NativeMemory.Alloc(sizeof(IntPtr) * " + total + @"); + void** callbacks = (void**)NativeMemory.Alloc((nuint)(sizeof(void*) * " + total + @")); "); int index = 0;