Skip to content

Commit b58e37d

Browse files
authored
Change RhNewString length argument to nint/intptr_t (#117552)
The RhNewString length argument is declared as int, but the asm implementation was using the full 64 bits of the related argument register. That doesn't match the calling convention where there is no guarantee that the upper 32 bits are zeroed. That has caused problem in the interpreter that always loads full registers from the interpreter stack and so sometimes, the upper 32 bits were a garbage and the function failed because the length was seemingly too large. This change fixes it by changing signature of the RhNewString so that the length is nint in managed code / intptr_t in native code.
1 parent a257593 commit b58e37d

File tree

7 files changed

+8
-11
lines changed

7 files changed

+8
-11
lines changed

src/coreclr/nativeaot/Runtime/portable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ FCIMPL2(Array *, RhpNewArrayFast, MethodTable * pArrayEEType, int numElements)
127127
}
128128
FCIMPLEND
129129

130-
FCIMPL2(String *, RhNewString, MethodTable * pArrayEEType, int numElements)
130+
FCIMPL2(String *, RhNewString, MethodTable * pArrayEEType, intptr_t numElements)
131131
{
132132
// TODO: Implement. We tail call to RhpNewArrayFast for now since there's a bunch of TODOs in the places
133133
// that matter anyway.

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ internal static IntPtr RhHandleAllocDependent(object primary, object secondary)
396396

397397
[MethodImpl(MethodImplOptions.InternalCall)]
398398
[RuntimeImport(RuntimeLibrary, "RhNewString")]
399-
internal static extern unsafe string RhNewString(MethodTable* pEEType, int length);
399+
internal static extern unsafe string RhNewString(MethodTable* pEEType, nint length);
400400

401401
[MethodImpl(MethodImplOptions.InternalCall)]
402402
[RuntimeImport(RuntimeLibrary, "RhUnbox")]

src/coreclr/nativeaot/System.Private.CoreLib/src/System/String.NativeAot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class String
1818
[Intrinsic]
1919
public static readonly string Empty = "";
2020

21-
internal static unsafe string FastAllocateString(int length)
21+
internal static unsafe string FastAllocateString(nint length)
2222
{
2323
// We allocate one extra char as an interop convenience so that our strings are null-
2424
// terminated, however, we don't pass the extra +1 to the string allocation because the base

src/coreclr/nativeaot/Test.CoreLib/src/System/Runtime/RuntimeImports.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal static IntPtr RhGetModuleSection(TypeManagerHandle module, ReadyToRunSe
8282

8383
[MethodImpl(MethodImplOptions.InternalCall)]
8484
[RuntimeImport(RuntimeLibrary, "RhNewString")]
85-
internal static extern unsafe string RhNewString(MethodTable* pEEType, int length);
85+
internal static extern unsafe string RhNewString(MethodTable* pEEType, nint length);
8686

8787
[DllImport(RuntimeLibrary)]
8888
internal static extern unsafe void RhAllocateNewArray(MethodTable* pArrayEEType, uint numElements, uint flags, void* pResult);

src/coreclr/runtime/amd64/AllocFast.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ NESTED_END RhpNewObject, _TEXT
173173

174174
// Allocate a string.
175175
// RDI == MethodTable
176-
// ESI == character/element count
176+
// RSI == character/element count
177177
LEAF_ENTRY RhNewString, _TEXT
178178

179179
// we want to limit the element count to the non-negative 32-bit int range

src/coreclr/runtime/amd64/AllocFast.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ ENDM ; NEW_ARRAY_FAST
128128

129129
;; Allocate a string.
130130
;; RCX == MethodTable
131-
;; EDX == character/element count
131+
;; RDX == character/element count
132132
LEAF_ENTRY RhNewString, _TEXT
133133

134134
; we want to limit the element count to the non-negative 32-bit int range

src/coreclr/vm/jitinterface.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ EXTERN_C FCDECL1(void*, JIT_GetDynamicNonGCStaticBaseNoCtor_Portable, DynamicSta
149149
EXTERN_C FCDECL1(Object*, RhpNewFast, CORINFO_CLASS_HANDLE typeHnd_);
150150
EXTERN_C FCDECL2(Object*, RhpNewArrayFast, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR size);
151151
EXTERN_C FCDECL2(Object*, RhpNewPtrArrayFast, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR size);
152-
EXTERN_C FCDECL2(Object*, RhNewString, CORINFO_CLASS_HANDLE typeHnd_, DWORD stringLength);
152+
EXTERN_C FCDECL2(Object*, RhNewString, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR stringLength);
153153

154154
#if defined(FEATURE_64BIT_ALIGNMENT)
155155
EXTERN_C FCDECL1(Object*, RhpNewFastAlign8, CORINFO_CLASS_HANDLE typeHnd_);
@@ -161,17 +161,14 @@ EXTERN_C FCDECL2(Object*, RhpNewArrayFastAlign8, CORINFO_CLASS_HANDLE typeHnd_,
161161
EXTERN_C FCDECL1(Object*, RhpNewFast_UP, CORINFO_CLASS_HANDLE typeHnd_);
162162
EXTERN_C FCDECL2(Object*, RhpNewArrayFast_UP, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR size);
163163
EXTERN_C FCDECL2(Object*, RhpNewPtrArrayFast_UP, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR size);
164-
EXTERN_C FCDECL2(Object*, RhNewString_UP, CORINFO_CLASS_HANDLE typeHnd_, DWORD stringLength);
164+
EXTERN_C FCDECL2(Object*, RhNewString_UP, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR stringLength);
165165
#endif
166166

167167
EXTERN_C FCDECL1(Object*, RhpNew, CORINFO_CLASS_HANDLE typeHnd_);
168168
EXTERN_C FCDECL2(Object*, RhpNewVariableSizeObject, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR size);
169169
EXTERN_C FCDECL1(Object*, RhpNewMaybeFrozen, CORINFO_CLASS_HANDLE typeHnd_);
170170
EXTERN_C FCDECL2(Object*, RhpNewArrayMaybeFrozen, CORINFO_CLASS_HANDLE typeHnd_, INT_PTR size);
171171

172-
EXTERN_C FCDECL1(Object*, AllocateStringFast, DWORD stringLength);
173-
EXTERN_C FCDECL1(Object*, AllocateStringSlow, DWORD stringLength);
174-
175172
EXTERN_C FCDECL2(void, JITutil_MonReliableEnter, Object* obj, BYTE* pbLockTaken);
176173
EXTERN_C FCDECL3(void, JITutil_MonTryEnter, Object* obj, INT32 timeOut, BYTE* pbLockTaken);
177174
EXTERN_C FCDECL2(void, JITutil_MonReliableContention, AwareLock* awarelock, BYTE* pbLockTaken);

0 commit comments

Comments
 (0)