From f408cb40e289919bca355e5da01aba71305df2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Wed, 28 Feb 2024 20:36:33 +0100 Subject: [PATCH 1/4] Copy the NativeAOT AllocateTypeAssociatedMemory to Mono Found in #99019 --- .../Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs index 391bfaec6a2a46..030a24a0d3ec05 100644 --- a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs @@ -139,7 +139,13 @@ public static void RunModuleConstructor(ModuleHandle module) public static IntPtr AllocateTypeAssociatedMemory(Type type, int size) { - throw new PlatformNotSupportedException(); + if (type is not RuntimeType) + throw new ArgumentException(SR.Arg_MustBeType, nameof(type)); + + ArgumentOutOfRangeException.ThrowIfNegative(size); + + // We don't support unloading; the memory will never be freed. + return (IntPtr)NativeMemory.AllocZeroed((uint)size); } [Intrinsic] From 6fa09af4cc5b7ec67191f6f6a6977c1f27a6e73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Wed, 28 Feb 2024 20:44:50 +0100 Subject: [PATCH 2/4] Update RuntimeHelpersTests.cs --- .../System/Runtime/CompilerServices/RuntimeHelpersTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs index 0aea60fd1921e0..5148ca0c488d80 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs @@ -394,7 +394,6 @@ public static void ArrayGetSubArrayCoVarianceTest() } [Fact] - [SkipOnMono("Not presently implemented on Mono")] public static void AllocateTypeAssociatedMemoryInvalidArguments() { Assert.Throws(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(null, 10); }); @@ -402,7 +401,6 @@ public static void AllocateTypeAssociatedMemoryInvalidArguments() } [Fact] - [SkipOnMono("Not presently implemented on Mono")] public static unsafe void AllocateTypeAssociatedMemoryValidArguments() { IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32); From 3a56da71294a9812bdfa097a3f28830a9a21d35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:19:11 +0100 Subject: [PATCH 3/4] Update RuntimeHelpers.Mono.cs --- .../src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs index 030a24a0d3ec05..02aa3d25385fea 100644 --- a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; +using System.Runtime.InteropServices; using System.Runtime.Serialization; namespace System.Runtime.CompilerServices From c9c5b299fcfef932fdcacaffd64a64a61662347c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:49:22 +0100 Subject: [PATCH 4/4] Update RuntimeHelpers.Mono.cs --- .../src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs index 02aa3d25385fea..ca9ecdbe9f829c 100644 --- a/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs @@ -138,7 +138,7 @@ public static void RunModuleConstructor(ModuleHandle module) RunModuleConstructor(module.Value); } - public static IntPtr AllocateTypeAssociatedMemory(Type type, int size) + public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size) { if (type is not RuntimeType) throw new ArgumentException(SR.Arg_MustBeType, nameof(type));