|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Buffers.Binary; |
4 | 5 | using System.Diagnostics;
|
5 | 6 | using System.Diagnostics.CodeAnalysis;
|
6 | 7 | using System.Reflection;
|
7 |
| -using System.Runtime.CompilerServices; |
8 | 8 | using System.Runtime.InteropServices;
|
9 | 9 | using System.Runtime.Serialization;
|
10 | 10 | using System.Runtime.Versioning;
|
11 |
| -using System.Threading; |
12 | 11 |
|
13 | 12 | namespace System.Runtime.CompilerServices
|
14 | 13 | {
|
15 | 14 | public static partial class RuntimeHelpers
|
16 | 15 | {
|
17 | 16 | [Intrinsic]
|
18 |
| - [MethodImpl(MethodImplOptions.InternalCall)] |
19 |
| - public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle); |
| 17 | + public static unsafe void InitializeArray(Array array, RuntimeFieldHandle fldHandle) |
| 18 | + { |
| 19 | + if (array is null) |
| 20 | + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); |
20 | 21 |
|
21 |
| - [MethodImpl(MethodImplOptions.InternalCall)] |
22 |
| - private static extern unsafe void* GetSpanDataFrom( |
| 22 | + if (fldHandle.IsNullHandle()) |
| 23 | + throw new ArgumentException(SR.Argument_InvalidHandle); |
| 24 | + |
| 25 | + IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo(); |
| 26 | + |
| 27 | + if (!RuntimeFieldHandle.GetRVAFieldInfo(fldInfo.Value, out void* address, out uint size)) |
| 28 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 29 | + |
| 30 | + // Note that we do not check that the field is actually in the PE file that is initializing |
| 31 | + // the array. Basically, the data being published can be accessed by anyone with the proper |
| 32 | + // permissions (C# marks these as assembly visibility, and thus are protected from outside |
| 33 | + // snooping) |
| 34 | + |
| 35 | + MethodTable* pMT = GetMethodTable(array); |
| 36 | + TypeHandle elementTH = pMT->GetArrayElementTypeHandle(); |
| 37 | + |
| 38 | + if (elementTH.IsTypeDesc || !elementTH.AsMethodTable()->IsPrimitive) // Enum is included |
| 39 | + throw new ArgumentException(SR.Argument_BadArrayForInitializeArray); |
| 40 | + |
| 41 | + nuint totalSize = pMT->ComponentSize * array.NativeLength; |
| 42 | + |
| 43 | + // make certain you don't go off the end of the rva static |
| 44 | + if (totalSize > size) |
| 45 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 46 | + |
| 47 | + ref byte src = ref *(byte*)address; // Ref is extending the lifetime of the static field. |
| 48 | + GC.KeepAlive(fldInfo); |
| 49 | + |
| 50 | + ref byte dst = ref MemoryMarshal.GetArrayDataReference(array); |
| 51 | + |
| 52 | + Debug.Assert(!elementTH.AsMethodTable()->ContainsGCPointers); |
| 53 | + |
| 54 | + if (BitConverter.IsLittleEndian) |
| 55 | + { |
| 56 | + SpanHelpers.Memmove(ref dst, ref src, totalSize); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + switch (pMT->ComponentSize) |
| 61 | + { |
| 62 | + case sizeof(byte): |
| 63 | + SpanHelpers.Memmove(ref dst, ref src, totalSize); |
| 64 | + break; |
| 65 | + case sizeof(ushort): |
| 66 | + BinaryPrimitives.ReverseEndianness( |
| 67 | + new ReadOnlySpan<ushort>(ref Unsafe.As<byte, ushort>(ref src), array.Length), |
| 68 | + new Span<ushort>(ref Unsafe.As<byte, ushort>(ref dst), array.Length)); |
| 69 | + break; |
| 70 | + case sizeof(uint): |
| 71 | + BinaryPrimitives.ReverseEndianness( |
| 72 | + new ReadOnlySpan<uint>(ref Unsafe.As<byte, uint>(ref src), array.Length), |
| 73 | + new Span<uint>(ref Unsafe.As<byte, uint>(ref dst), array.Length)); |
| 74 | + break; |
| 75 | + case sizeof(ulong): |
| 76 | + BinaryPrimitives.ReverseEndianness( |
| 77 | + new ReadOnlySpan<ulong>(ref Unsafe.As<byte, ulong>(ref src), array.Length), |
| 78 | + new Span<ulong>(ref Unsafe.As<byte, ulong>(ref dst), array.Length)); |
| 79 | + break; |
| 80 | + default: |
| 81 | + Debug.Fail("Incorrect primitive type size!"); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static unsafe ref byte GetSpanDataFrom( |
23 | 88 | RuntimeFieldHandle fldHandle,
|
24 | 89 | RuntimeTypeHandle targetTypeHandle,
|
25 |
| - out int count); |
| 90 | + out int count) |
| 91 | + { |
| 92 | + if (fldHandle.IsNullHandle()) |
| 93 | + throw new ArgumentException(SR.Argument_InvalidHandle); |
| 94 | + |
| 95 | + IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo(); |
| 96 | + |
| 97 | + if (!RuntimeFieldHandle.GetRVAFieldInfo(fldInfo.Value, out void* data, out uint totalSize)) |
| 98 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 99 | + |
| 100 | + TypeHandle th = targetTypeHandle.GetNativeTypeHandle(); |
| 101 | + Debug.Assert(!th.IsTypeDesc); // TypeDesc can't be used as generic parameter |
| 102 | + MethodTable* targetMT = th.AsMethodTable(); |
| 103 | + |
| 104 | + if (!targetMT->IsPrimitive) // Enum is included |
| 105 | + throw new ArgumentException(SR.Argument_BadArrayForInitializeArray); |
| 106 | + |
| 107 | + uint targetTypeSize = targetMT->GetNumInstanceFieldBytes(); |
| 108 | + Debug.Assert(uint.IsPow2(targetTypeSize)); |
| 109 | + |
| 110 | + if (((nuint)data & (targetTypeSize - 1)) != 0) |
| 111 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 112 | + |
| 113 | + if (!BitConverter.IsLittleEndian) |
| 114 | + { |
| 115 | + throw new PlatformNotSupportedException(); |
| 116 | + } |
| 117 | + |
| 118 | + count = (int)(totalSize / targetTypeSize); |
| 119 | + ref byte dataRef = ref *(byte*)data; // Ref is extending the lifetime of the static field. |
| 120 | + GC.KeepAlive(fldInfo); |
| 121 | + |
| 122 | + return ref dataRef; |
| 123 | + } |
26 | 124 |
|
27 | 125 | // GetObjectValue is intended to allow value classes to be manipulated as 'Object'
|
28 | 126 | // but have aliasing behavior of a value class. The intent is that you would use
|
@@ -655,6 +753,8 @@ public int MultiDimensionalArrayRank
|
655 | 753 | // Warning! UNLIKE the similarly named Reflection api, this method also returns "true" for Enums.
|
656 | 754 | public bool IsPrimitive => (Flags & enum_flag_Category_Mask) is enum_flag_Category_PrimitiveValueType or enum_flag_Category_TruePrimitive;
|
657 | 755 |
|
| 756 | + public bool IsTruePrimitive => (Flags & enum_flag_Category_Mask) is enum_flag_Category_TruePrimitive; |
| 757 | + |
658 | 758 | public bool HasInstantiation => (Flags & enum_flag_HasComponentSize) == 0 && (Flags & enum_flag_GenericsMask) != enum_flag_GenericsMask_NonGeneric;
|
659 | 759 |
|
660 | 760 | public bool IsGenericTypeDefinition => (Flags & (enum_flag_HasComponentSize | enum_flag_GenericsMask)) == enum_flag_GenericsMask_TypicalInst;
|
@@ -684,6 +784,13 @@ public TypeHandle GetArrayElementTypeHandle()
|
684 | 784 |
|
685 | 785 | [MethodImpl(MethodImplOptions.InternalCall)]
|
686 | 786 | public extern uint GetNumInstanceFieldBytes();
|
| 787 | + |
| 788 | + /// <summary> |
| 789 | + /// Get the <see cref="CorElementType"/> representing primitive-like type. Enums are represented by underlying type. |
| 790 | + /// </summary> |
| 791 | + /// <remarks>This method should only be called when <see cref="IsPrimitive"/> returns <see langword="true"/>.</remarks> |
| 792 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 793 | + public extern CorElementType GetPrimitiveCorElementType(); |
687 | 794 | }
|
688 | 795 |
|
689 | 796 | // Subset of src\vm\methodtable.h
|
|
0 commit comments