Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
566b15e
Implement IsSimpleCopy and CanAssignArrayType in managed code
huoyaoyuan May 27, 2024
8a592b9
Fold IsSimpleCopy back to CanAssignArrayType
huoyaoyuan May 27, 2024
0dce3d4
Delete FCall and QCall definitions for copy
huoyaoyuan May 27, 2024
d29e5d8
Convert InternalSetValue to managed
huoyaoyuan May 28, 2024
5dd0e3c
Setup FCalls
huoyaoyuan May 28, 2024
527021c
Remove FCall for GetCorElementTypeOfElementType
huoyaoyuan May 28, 2024
e7d843a
Complete TryUnBox
huoyaoyuan May 28, 2024
2bb5fe7
Fix FCall definition
huoyaoyuan May 28, 2024
f7da172
Implement InitializeArray in managed
huoyaoyuan May 28, 2024
0924555
Implement GetSpanDataFrom in managed
huoyaoyuan May 28, 2024
dc85381
Remove FCall definition
huoyaoyuan May 28, 2024
f99c553
Fix RVA field address
huoyaoyuan May 28, 2024
c5e6cc4
Fix RVA assert
huoyaoyuan May 28, 2024
470b86f
Do not use hydrated RtFieldInfo
huoyaoyuan May 28, 2024
c7821e6
Use QCall for LoadSize
huoyaoyuan May 29, 2024
5225203
Fix CanAssignArrayType condition
huoyaoyuan May 29, 2024
07293e3
Fix compilation
huoyaoyuan May 29, 2024
5da88c3
Simplify AssignType enum
huoyaoyuan May 29, 2024
5222bca
Fix I and U in CanPrimitiveWiden
huoyaoyuan May 29, 2024
335b2fb
CanCastTo should be QCall
huoyaoyuan May 30, 2024
93ec9d8
Remove ThrowHelper usages that not in hot path
huoyaoyuan May 31, 2024
c3f5097
Merge branch 'main' into array-fcall
huoyaoyuan Jun 6, 2024
481e4d4
Revert the known broken InternalSetValue
huoyaoyuan Jun 6, 2024
a266a30
Revert "Revert the known broken InternalSetValue"
huoyaoyuan Jun 6, 2024
b8e4584
Fix heap overwrite
huoyaoyuan Jun 7, 2024
5465a97
Add disabled test for RVA field reflection
huoyaoyuan Jun 9, 2024
4ce4f51
Push back changes around IsSimpleCopy and CanAssignArrayType
huoyaoyuan Jun 10, 2024
30b7d8f
Move PrimitiveWiden to InvokeUtils
huoyaoyuan Jun 12, 2024
8a9c5bc
Merge coreclr specific InvokeUtils
huoyaoyuan Jun 12, 2024
f95fa34
Revert "Merge coreclr specific InvokeUtils"
huoyaoyuan Jun 13, 2024
ebfd224
Move InvokeUtils to shared
huoyaoyuan Jun 13, 2024
d9205c6
Apply suggestions from code review
huoyaoyuan Jun 18, 2024
7eef724
Merge branch 'main' into array-fcall
huoyaoyuan Jun 18, 2024
8cfb58f
Fix trailing whitespace
huoyaoyuan Jun 18, 2024
fbb9d71
Reduce QCalls
huoyaoyuan Jun 18, 2024
324df76
Reduce CorElementType overhead
huoyaoyuan Jun 18, 2024
089ae12
Separate RVA reflection change out
huoyaoyuan Jun 19, 2024
a5e7441
Revert GetCorElementTypeOfElementType
huoyaoyuan Jun 19, 2024
5552d63
Change the MethodTable FCall to be optimal for primitive and share wi…
huoyaoyuan Jun 19, 2024
133cd8a
Return ref from GetSpanDataFrom
huoyaoyuan Jun 19, 2024
e17bf0e
Handle enum conversion to underlying type
huoyaoyuan Jun 19, 2024
dd1e2dd
Update exception message
huoyaoyuan Jun 19, 2024
32b8789
Add KeepAlive
huoyaoyuan Jun 20, 2024
582eaf1
Remove unnecessary qcall handle
huoyaoyuan Jun 20, 2024
1836ae5
Fix ref src
huoyaoyuan Jun 20, 2024
cb6aa00
Update FCall name and assert
huoyaoyuan Jun 24, 2024
49cf866
Remove unused changes
huoyaoyuan Jun 24, 2024
7a0c398
Use void* for address
huoyaoyuan Jun 24, 2024
db677ad
Apply suggestions from code review
huoyaoyuan Jun 25, 2024
353715e
Remove unused enum FCall
huoyaoyuan Jun 24, 2024
8fb1c96
Use sizeof
huoyaoyuan Jun 25, 2024
26d4c80
Add some comment
huoyaoyuan Jun 25, 2024
5f7263f
Add comment to lifetime
huoyaoyuan Jun 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reduce QCalls
  • Loading branch information
huoyaoyuan committed Jun 18, 2024
commit fbb9d71b6c1aec3bdd68ca538b43c801201516ff
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static unsafe void InitializeArray(Array array, RuntimeFieldHandle fldHan
if (array is null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);

if ((RuntimeFieldHandle.GetAttributes(fldInfo.Value) & FieldAttributes.HasFieldRVA) == 0)
if (!RuntimeFieldHandle.GetRVAFieldInfo(new QCallFieldHandle(ref fldInfo), out IntPtr address, out uint size))
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

// Note that we do not check that the field is actually in the PE file that is initializing
Expand All @@ -35,13 +35,11 @@ public static unsafe void InitializeArray(Array array, RuntimeFieldHandle fldHan
MethodTable* pMT = GetMethodTable(array);
nuint totalSize = pMT->ComponentSize * array.NativeLength;

uint size = RuntimeFieldHandle.GetFieldSize(new QCallFieldHandle(ref fldInfo));

// make certain you don't go off the end of the rva static
if (totalSize > size)
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

void* src = (void*)RuntimeFieldHandle.GetStaticFieldAddress(fldInfo);
void* src = (void*)address;
ref byte dst = ref MemoryMarshal.GetArrayDataReference(array);

Debug.Assert(!pMT->GetArrayElementTypeHandle().AsMethodTable()->ContainsGCPointers);
Expand Down Expand Up @@ -86,7 +84,7 @@ public static unsafe void InitializeArray(Array array, RuntimeFieldHandle fldHan
{
IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo();

if ((RuntimeFieldHandle.GetAttributes(fldInfo.Value) & FieldAttributes.HasFieldRVA) == 0)
if (!RuntimeFieldHandle.GetRVAFieldInfo(new QCallFieldHandle(ref fldInfo), out IntPtr data, out uint totalSize))
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

TypeHandle th = new TypeHandle((void*)targetTypeHandle.Value);
Expand All @@ -95,10 +93,8 @@ public static unsafe void InitializeArray(Array array, RuntimeFieldHandle fldHan
if (!th.GetVerifierCorElementType().IsPrimitiveType()) // Enum is included
throw new ArgumentException(SR.Argument_MustBePrimitiveArray);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message talks about arrays, but there are no arrays involved.

I think throwing the generic Argument_BadFieldForInitializeArray would be just fine. These are compiler generated fields. The error messages do not have to go into great detail about what's wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is not about the field, but the type argument of the caller method CreateSpan<T>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the error should be about the CreateSpan type argument, but the error message says "The array must be array of primitive or enum type.". Where is the array that the error message talks about?


uint totalSize = RuntimeFieldHandle.GetFieldSize(new QCallFieldHandle(ref fldInfo));
uint targetTypeSize = th.AsMethodTable()->GetNumInstanceFieldBytes();

IntPtr data = RuntimeFieldHandle.GetStaticFieldAddress(fldInfo);
if (data % targetTypeSize != 0)
throw new ArgumentException(SR.Argument_BadFieldForInitializeArray);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1191,10 +1191,11 @@ internal static RuntimeType GetApproxDeclaringType(IRuntimeFieldInfo field)
internal static extern int GetInstanceFieldOffset(RtFieldInfo field);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetStaticFieldAddress(IRuntimeFieldInfo field);
internal static extern IntPtr GetStaticFieldAddress(RtFieldInfo field);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeFieldHandle_GetFieldSize")]
internal static partial uint GetFieldSize(QCallFieldHandle field);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeFieldHandle_GetRVAFieldInfo")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool GetRVAFieldInfo(QCallFieldHandle field, out IntPtr address, out uint size);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int GetToken(RtFieldInfo field);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeModule_GetScopeName)
DllImportEntry(RuntimeModule_GetFullyQualifiedName)
DllImportEntry(RuntimeModule_GetTypes)
DllImportEntry(RuntimeFieldHandle_GetFieldSize)
DllImportEntry(RuntimeFieldHandle_GetRVAFieldInfo)
DllImportEntry(StackFrame_GetMethodDescFromNativeIP)
DllImportEntry(ModuleBuilder_GetStringConstant)
DllImportEntry(ModuleBuilder_GetTypeRef)
Expand Down
20 changes: 16 additions & 4 deletions src/coreclr/vm/reflectioninvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,14 +1259,26 @@ FCIMPL1(void*, RuntimeFieldHandle::GetStaticFieldAddress, ReflectFieldObject *pF
}
FCIMPLEND

extern "C" UINT QCALLTYPE RuntimeFieldHandle_GetFieldSize(QCall::FieldHandle pField)
extern "C" BOOL QCALLTYPE RuntimeFieldHandle_GetRVAFieldInfo(QCall::FieldHandle pField, void** address, UINT* size)
{
QCALL_CONTRACT;

UINT ret = 0;
BOOL ret = FALSE;

BEGIN_QCALL;
ret = pField->LoadSize();

FieldDesc* pFieldDesc = pField.m_pField;

if (pFieldDesc != NULL && pFieldDesc->IsRVA())
{
ret = TRUE;

Module* pModule = pFieldDesc->GetModule();
*address = pModule->GetRvaField(pFieldDesc->GetOffset());

*size = pFieldDesc->LoadSize();
}

END_QCALL;

return ret;
Expand Down Expand Up @@ -2129,4 +2141,4 @@ extern "C" void QCALLTYPE ReflectionInvocation_GetBoxInfo(
pMT->EnsureInstanceActive();

END_QCALL;
}
}
2 changes: 1 addition & 1 deletion src/coreclr/vm/runtimehandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class RuntimeFieldHandle {
static FCDECL1(FC_BOOL_RET, AcquiresContextFromThis, FieldDesc *pField);
static FCDECL1(Object*, GetLoaderAllocator, FieldDesc *pField);
};
extern "C" UINT QCALLTYPE RuntimeFieldHandle_GetFieldSize(QCall::FieldHandle pField);
extern "C" BOOL QCALLTYPE RuntimeFieldHandle_GetRVAFieldInfo(QCall::FieldHandle pField, void** address, UINT* size);

class ModuleHandle {

Expand Down