From a1553f0100688fefb150d6c0bfaa943e6f583970 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 25 Oct 2023 15:28:38 -0300 Subject: [PATCH 1/7] Change implementation of OffsetToStringData as it's done on coreclr --- .../CompilerServices/RuntimeHelpers.Mono.cs | 16 ++++++++++++++-- src/mono/mono/mini/interp/transform.c | 11 +---------- src/mono/mono/mini/intrinsics.c | 5 +---- 3 files changed, 16 insertions(+), 16 deletions(-) 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 bbe9b2e2a2fc3e..1f0a0fc731eb9e 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 @@ -32,8 +32,20 @@ public static void InitializeArray(Array array, RuntimeFieldHandle fldHandle) [Obsolete("OffsetToStringData has been deprecated. Use string.GetPinnableReference() instead.")] public static int OffsetToStringData { - [Intrinsic] - get => OffsetToStringData; + [NonVersionable] + get => + // Number of bytes from the address pointed to by a reference to + // a String to the first 16-bit character in the String. Skip + // over the MethodTable pointer, & String + // length. Of course, the String reference points to the memory + // after the sync block, so don't count that. + // This property allows C#'s fixed statement to work on Strings. + // On 64 bit platforms, this should be 12 (8+4) and on 32 bit 8 (4+4). +#if TARGET_64BIT + 20; +#else // 32 + 12; +#endif // TARGET_64BIT } [MethodImplAttribute(MethodImplOptions.InternalCall)] diff --git a/src/mono/mono/mini/interp/transform.c b/src/mono/mono/mini/interp/transform.c index 6bd43c3d8343b1..d0ee55f987165e 100644 --- a/src/mono/mono/mini/interp/transform.c +++ b/src/mono/mono/mini/interp/transform.c @@ -2305,16 +2305,7 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas *op = MINT_INITBLK; } } else if (in_corlib && !strcmp (klass_name_space, "System.Runtime.CompilerServices") && !strcmp (klass_name, "RuntimeHelpers")) { - if (!strcmp (tm, "get_OffsetToStringData")) { - g_assert (csignature->param_count == 0); - int offset = MONO_STRUCT_OFFSET (MonoString, chars); - interp_add_ins (td, MINT_LDC_I4); - WRITE32_INS (td->last_ins, 0, &offset); - push_simple_type (td, STACK_TYPE_I4); - interp_ins_set_dreg (td->last_ins, td->sp [-1].local); - td->ip += 5; - return TRUE; - } else if (!strcmp (tm, "GetHashCode") || !strcmp (tm, "InternalGetHashCode")) { + if (!strcmp (tm, "GetHashCode") || !strcmp (tm, "InternalGetHashCode")) { *op = MINT_INTRINS_GET_HASHCODE; } else if (!strcmp (tm, "TryGetHashCode")) { *op = MINT_INTRINS_TRY_GET_HASHCODE; diff --git a/src/mono/mono/mini/intrinsics.c b/src/mono/mono/mini/intrinsics.c index 2a32c27ab14d13..1c1dede53bc5e8 100644 --- a/src/mono/mono/mini/intrinsics.c +++ b/src/mono/mono/mini/intrinsics.c @@ -935,10 +935,7 @@ mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSign } else return NULL; } else if (cmethod->klass == runtime_helpers_class) { - if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) { - EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars)); - return ins; - } else if (!strcmp (cmethod->name, "GetRawData")) { + if (!strcmp (cmethod->name, "GetRawData")) { int dreg = alloc_preg (cfg); EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, args [0]->dreg, MONO_ABI_SIZEOF (MonoObject)); return ins; From ac114c470fb3a81949dd7ee72c076318d1fb1d31 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 25 Oct 2023 15:41:01 -0300 Subject: [PATCH 2/7] Fix comment --- .../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 1f0a0fc731eb9e..5a69902b089d27 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 @@ -40,7 +40,7 @@ public static int OffsetToStringData // length. Of course, the String reference points to the memory // after the sync block, so don't count that. // This property allows C#'s fixed statement to work on Strings. - // On 64 bit platforms, this should be 12 (8+4) and on 32 bit 8 (4+4). + // On 64 bit platforms, this should be 20 and on 32 bit 12. #if TARGET_64BIT 20; #else // 32 From 385991b98fca25510475cb3b50a1bb969aae643a Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 25 Oct 2023 15:44:32 -0300 Subject: [PATCH 3/7] Remove more comments --- .../System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 5 ----- 1 file changed, 5 deletions(-) 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 5a69902b089d27..de14894d7dbf75 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 @@ -34,11 +34,6 @@ public static int OffsetToStringData { [NonVersionable] get => - // Number of bytes from the address pointed to by a reference to - // a String to the first 16-bit character in the String. Skip - // over the MethodTable pointer, & String - // length. Of course, the String reference points to the memory - // after the sync block, so don't count that. // This property allows C#'s fixed statement to work on Strings. // On 64 bit platforms, this should be 20 and on 32 bit 12. #if TARGET_64BIT From 9f52da84552c140aaa2dd66150e639e51f6d3911 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 25 Oct 2023 15:49:46 -0300 Subject: [PATCH 4/7] completely remove the comments --- .../src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 2 -- 1 file changed, 2 deletions(-) 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 de14894d7dbf75..bb20381ab8dbe7 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 @@ -34,8 +34,6 @@ public static int OffsetToStringData { [NonVersionable] get => - // This property allows C#'s fixed statement to work on Strings. - // On 64 bit platforms, this should be 20 and on 32 bit 12. #if TARGET_64BIT 20; #else // 32 From f6a10287e21cd47d872ee16a0ab9d9eed5f983ed Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 25 Oct 2023 16:28:59 -0300 Subject: [PATCH 5/7] 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 bb20381ab8dbe7..127cca5542ef1c 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 @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; using System.Runtime.Serialization; +using System.Runtime.Versioning; namespace System.Runtime.CompilerServices { From b94d307bcd244340d0288cab15c31ec42f0b78ba Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Thu, 26 Oct 2023 10:19:30 -0300 Subject: [PATCH 6/7] Addressing comments --- .../src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 2 -- 1 file changed, 2 deletions(-) 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 127cca5542ef1c..089a4909accad2 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 @@ -5,7 +5,6 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; using System.Runtime.Serialization; -using System.Runtime.Versioning; namespace System.Runtime.CompilerServices { @@ -33,7 +32,6 @@ public static void InitializeArray(Array array, RuntimeFieldHandle fldHandle) [Obsolete("OffsetToStringData has been deprecated. Use string.GetPinnableReference() instead.")] public static int OffsetToStringData { - [NonVersionable] get => #if TARGET_64BIT 20; From 9695c42de387c2a4a43075cb7551514fa30ca557 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Thu, 26 Oct 2023 13:08:40 -0300 Subject: [PATCH 7/7] Addressing @lambdageek comment. --- .../Runtime/CompilerServices/RuntimeHelpers.Mono.cs | 10 +--------- .../System.Private.CoreLib/src/System/String.Mono.cs | 6 ++++++ 2 files changed, 7 insertions(+), 9 deletions(-) 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 089a4909accad2..7930da20fc7aea 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 @@ -30,15 +30,7 @@ public static void InitializeArray(Array array, RuntimeFieldHandle fldHandle) } [Obsolete("OffsetToStringData has been deprecated. Use string.GetPinnableReference() instead.")] - public static int OffsetToStringData - { - get => -#if TARGET_64BIT - 20; -#else // 32 - 12; -#endif // TARGET_64BIT - } + public static int OffsetToStringData => string.OFFSET_TO_STRING; [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern int InternalGetHashCode(object? o); diff --git a/src/mono/System.Private.CoreLib/src/System/String.Mono.cs b/src/mono/System.Private.CoreLib/src/System/String.Mono.cs index 35e2aef4f2fbbf..7314504aff9a22 100644 --- a/src/mono/System.Private.CoreLib/src/System/String.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/String.Mono.cs @@ -30,6 +30,12 @@ public static string IsInterned(string str) [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern string InternalIntern(string str); +#if TARGET_64BIT + internal const int OFFSET_TO_STRING = 20; +#else + internal const int OFFSET_TO_STRING = 12; +#endif + // TODO: Should be pointing to Buffer instead #region Runtime method-to-ir dependencies