|
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 | + IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo(); |
20 | 20 |
|
21 | | - [MethodImpl(MethodImplOptions.InternalCall)] |
22 | | - private static extern unsafe void* GetSpanDataFrom( |
| 21 | + if (array is null) |
| 22 | + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); |
| 23 | + |
| 24 | + if ((RuntimeFieldHandle.GetAttributes(fldInfo.Value) & FieldAttributes.HasFieldRVA) == 0) |
| 25 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 26 | + |
| 27 | + // Note that we do not check that the field is actually in the PE file that is initializing |
| 28 | + // the array. Basically the data being published is can be accessed by anyone with the proper |
| 29 | + // permissions (C# marks these as assembly visibility, and thus are protected from outside |
| 30 | + // snooping) |
| 31 | + |
| 32 | + if (!array.GetCorElementTypeOfElementType().IsPrimitiveType()) // Enum is included |
| 33 | + throw new ArgumentException(SR.Argument_MustBePrimitiveArray); |
| 34 | + |
| 35 | + MethodTable* pMT = GetMethodTable(array); |
| 36 | + nuint totalSize = pMT->ComponentSize * array.NativeLength; |
| 37 | + |
| 38 | + uint size = RuntimeFieldHandle.GetFieldSize(new QCallFieldHandle(ref fldInfo)); |
| 39 | + |
| 40 | + // make certain you don't go off the end of the rva static |
| 41 | + if (totalSize > size) |
| 42 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 43 | + |
| 44 | + void* src = (void*)RuntimeFieldHandle.GetStaticFieldAddress(fldInfo); |
| 45 | + ref byte dst = ref MemoryMarshal.GetArrayDataReference(array); |
| 46 | + |
| 47 | + Debug.Assert(!pMT->GetArrayElementTypeHandle().AsMethodTable()->ContainsGCPointers); |
| 48 | + |
| 49 | + if (BitConverter.IsLittleEndian) |
| 50 | + { |
| 51 | + SpanHelpers.Memmove(ref dst, ref *(byte*)src, totalSize); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + switch (pMT->ComponentSize) |
| 56 | + { |
| 57 | + case 1: |
| 58 | + SpanHelpers.Memmove(ref dst, ref *(byte*)src, totalSize); |
| 59 | + break; |
| 60 | + case 2: |
| 61 | + BinaryPrimitives.ReverseEndianness( |
| 62 | + new ReadOnlySpan<ushort>(src, array.Length), |
| 63 | + new Span<ushort>(ref Unsafe.As<byte, ushort>(ref dst), array.Length)); |
| 64 | + break; |
| 65 | + case 4: |
| 66 | + BinaryPrimitives.ReverseEndianness( |
| 67 | + new ReadOnlySpan<uint>(src, array.Length), |
| 68 | + new Span<uint>(ref Unsafe.As<byte, uint>(ref dst), array.Length)); |
| 69 | + break; |
| 70 | + case 8: |
| 71 | + BinaryPrimitives.ReverseEndianness( |
| 72 | + new ReadOnlySpan<ulong>(src, array.Length), |
| 73 | + new Span<ulong>(ref Unsafe.As<byte, ulong>(ref dst), array.Length)); |
| 74 | + break; |
| 75 | + default: |
| 76 | + Debug.Fail("Incorrect primitive type size!"); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static unsafe void* GetSpanDataFrom( |
23 | 83 | RuntimeFieldHandle fldHandle, |
24 | 84 | RuntimeTypeHandle targetTypeHandle, |
25 | | - out int count); |
| 85 | + out int count) |
| 86 | + { |
| 87 | + IRuntimeFieldInfo fldInfo = fldHandle.GetRuntimeFieldInfo(); |
| 88 | + |
| 89 | + if ((RuntimeFieldHandle.GetAttributes(fldInfo.Value) & FieldAttributes.HasFieldRVA) == 0) |
| 90 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 91 | + |
| 92 | + TypeHandle th = new TypeHandle((void*)targetTypeHandle.Value); |
| 93 | + Debug.Assert(!th.IsTypeDesc); // TypeDesc can't be used as generic parameter |
| 94 | + |
| 95 | + if (!th.GetVerifierCorElementType().IsPrimitiveType()) // Enum is included |
| 96 | + throw new ArgumentException(SR.Argument_MustBePrimitiveArray); |
| 97 | + |
| 98 | + uint totalSize = RuntimeFieldHandle.GetFieldSize(new QCallFieldHandle(ref fldInfo)); |
| 99 | + uint targetTypeSize = th.AsMethodTable()->GetNumInstanceFieldBytes(); |
| 100 | + |
| 101 | + IntPtr data = RuntimeFieldHandle.GetStaticFieldAddress(fldInfo); |
| 102 | + if (data % targetTypeSize != 0) |
| 103 | + throw new ArgumentException(SR.Argument_BadFieldForInitializeArray); |
| 104 | + |
| 105 | + if (!BitConverter.IsLittleEndian) |
| 106 | + { |
| 107 | + throw new PlatformNotSupportedException(); |
| 108 | + } |
| 109 | + |
| 110 | + count = (int)(totalSize / targetTypeSize); |
| 111 | + return (void*)data; |
| 112 | + } |
26 | 113 |
|
27 | 114 | // GetObjectValue is intended to allow value classes to be manipulated as 'Object' |
28 | 115 | // but have aliasing behavior of a value class. The intent is that you would use |
|
0 commit comments