This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
arraynative.cpp
1446 lines (1139 loc) · 44.6 KB
/
arraynative.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// File: ArrayNative.cpp
//
//
// This file contains the native methods that support the Array class
//
#include "common.h"
#include "arraynative.h"
#include "excep.h"
#include "field.h"
#include "invokeutil.h"
#include "arraynative.inl"
FCIMPL1(INT32, ArrayNative::GetRank, ArrayBase* array)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array == NULL)
FCThrow(kNullReferenceException);
return array->GetRank();
}
FCIMPLEND
FCIMPL2(INT32, ArrayNative::GetLowerBound, ArrayBase* array, unsigned int dimension)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array == NULL)
FCThrow(kNullReferenceException);
if (dimension != 0)
{
// Check the dimension is within our rank
unsigned int rank = array->GetRank();
if (dimension >= rank)
FCThrowRes(kIndexOutOfRangeException, W("IndexOutOfRange_ArrayRankIndex"));
}
return array->GetLowerBoundsPtr()[dimension];
}
FCIMPLEND
// Get inclusive upper bound
FCIMPL2(INT32, ArrayNative::GetUpperBound, ArrayBase* array, unsigned int dimension)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array == NULL)
FCThrow(kNullReferenceException);
if (dimension != 0)
{
// Check the dimension is within our rank
unsigned int rank = array->GetRank();
if (dimension >= rank)
FCThrowRes(kIndexOutOfRangeException, W("IndexOutOfRange_ArrayRankIndex"));
}
return array->GetBoundsPtr()[dimension] + array->GetLowerBoundsPtr()[dimension] - 1;
}
FCIMPLEND
FCIMPL2(INT32, ArrayNative::GetLength, ArrayBase* array, unsigned int dimension)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array==NULL)
FCThrow(kNullReferenceException);
if (dimension != 0)
{
// Check the dimension is within our rank
unsigned int rank = array->GetRank();
if (dimension >= rank)
FCThrow(kIndexOutOfRangeException);
}
return array->GetBoundsPtr()[dimension];
}
FCIMPLEND
FCIMPL1(INT32, ArrayNative::GetLengthNoRank, ArrayBase* array)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array==NULL)
FCThrow(kNullReferenceException);
SIZE_T numComponents = array->GetNumComponents();
if (numComponents > INT32_MAX)
FCThrow(kOverflowException);
return (INT32)numComponents;
}
FCIMPLEND
FCIMPL1(INT64, ArrayNative::GetLongLengthNoRank, ArrayBase* array)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array==NULL)
FCThrow(kNullReferenceException);
return array->GetNumComponents();
}
FCIMPLEND
FCIMPL1(INT32, ArrayNative::GetDataPtrOffsetInternal, ArrayBase* array)
{
FCALL_CONTRACT;
VALIDATEOBJECT(array);
if (array == NULL)
FCThrow(kNullReferenceException);
return ArrayBase::GetDataPtrOffset(array->GetMethodTable());
}
FCIMPLEND
// array is GC protected by caller
void ArrayInitializeWorker(ARRAYBASEREF * arrayRef,
MethodTable* pArrayMT,
MethodTable* pElemMT)
{
STATIC_CONTRACT_MODE_COOPERATIVE;
STATIC_CONTRACT_SO_INTOLERANT;
// Ensure that the array element type is fully loaded before executing its code
pElemMT->EnsureInstanceActive();
//can not use contract here because of SEH
_ASSERTE(IsProtectedByGCFrame (arrayRef));
SIZE_T offset = ArrayBase::GetDataPtrOffset(pArrayMT);
SIZE_T size = pArrayMT->GetComponentSize();
SIZE_T cElements = (*arrayRef)->GetNumComponents();
MethodTable * pCanonMT = pElemMT->GetCanonicalMethodTable();
WORD slot = pCanonMT->GetDefaultConstructorSlot();
PCODE ctorFtn = pCanonMT->GetSlot(slot);
#if defined(_TARGET_X86_) && !defined(FEATURE_PAL)
BEGIN_CALL_TO_MANAGED();
for (SIZE_T i = 0; i < cElements; i++)
{
// Since GetSlot() is not idempotent and may have returned
// a non-optimal entry-point the first time round.
if (i == 1)
{
ctorFtn = pCanonMT->GetSlot(slot);
}
BYTE* thisPtr = (((BYTE*) OBJECTREFToObject (*arrayRef)) + offset);
#ifdef _DEBUG
__asm {
mov ECX, thisPtr
mov EDX, pElemMT // Instantiation argument if the type is generic
call [ctorFtn]
nop // Mark the fact that we can call managed code
}
#else // _DEBUG
typedef void (__fastcall * CtorFtnType)(BYTE*, BYTE*);
(*(CtorFtnType)ctorFtn)(thisPtr, (BYTE*)pElemMT);
#endif // _DEBUG
offset += size;
}
END_CALL_TO_MANAGED();
#else // _TARGET_X86_ && !FEATURE_PAL
//
// This is quite a bit slower, but it is portable.
//
for (SIZE_T i =0; i < cElements; i++)
{
// Since GetSlot() is not idempotent and may have returned
// a non-optimal entry-point the first time round.
if (i == 1)
{
ctorFtn = pCanonMT->GetSlot(slot);
}
BYTE* thisPtr = (((BYTE*) OBJECTREFToObject (*arrayRef)) + offset);
PREPARE_NONVIRTUAL_CALLSITE_USING_CODE(ctorFtn);
DECLARE_ARGHOLDER_ARRAY(args, 2);
args[ARGNUM_0] = PTR_TO_ARGHOLDER(thisPtr);
args[ARGNUM_1] = PTR_TO_ARGHOLDER(pElemMT); // Instantiation argument if the type is generic
CALL_MANAGED_METHOD_NORET(args);
offset += size;
}
#endif // !_TARGET_X86_ || FEATURE_PAL
}
FCIMPL1(void, ArrayNative::Initialize, ArrayBase* array)
{
FCALL_CONTRACT;
if (array == NULL)
{
FCThrowVoid(kNullReferenceException);
}
MethodTable* pArrayMT = array->GetMethodTable();
TypeHandle thElem = pArrayMT->GetApproxArrayElementTypeHandle();
if (thElem.IsTypeDesc())
return;
MethodTable * pElemMT = thElem.AsMethodTable();
if (!pElemMT->HasDefaultConstructor() || !pElemMT->IsValueType())
return;
ARRAYBASEREF arrayRef (array);
HELPER_METHOD_FRAME_BEGIN_1(arrayRef);
ArrayInitializeWorker(&arrayRef, pArrayMT, pElemMT);
HELPER_METHOD_FRAME_END();
}
FCIMPLEND
// Returns an enum saying whether you can copy an array of srcType into destType.
ArrayNative::AssignArrayEnum ArrayNative::CanAssignArrayTypeNoGC(const BASEARRAYREF pSrc, const BASEARRAYREF pDest)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
SO_TOLERANT;
PRECONDITION(pSrc != NULL);
PRECONDITION(pDest != NULL);
}
CONTRACTL_END;
// The next 50 lines are a little tricky. Change them with great care.
//
// This first bit is a minor optimization: e.g. when copying byte[] to byte[]
// we do not need to call GetArrayElementTypeHandle().
MethodTable *pSrcMT = pSrc->GetMethodTable();
MethodTable *pDestMT = pDest->GetMethodTable();
if (pSrcMT == pDestMT)
return AssignWillWork;
TypeHandle srcTH = pSrcMT->GetApproxArrayElementTypeHandle();
TypeHandle destTH = pDestMT->GetApproxArrayElementTypeHandle();
if (srcTH == destTH) // This check kicks for different array kind or dimensions
return AssignWillWork;
// Value class boxing
if (srcTH.IsValueType() && !destTH.IsValueType())
{
switch (srcTH.CanCastToNoGC(destTH))
{
case TypeHandle::CanCast : return AssignBoxValueClassOrPrimitive;
case TypeHandle::CannotCast : return AssignWrongType;
default : return AssignDontKnow;
}
}
// Value class unboxing.
if (!srcTH.IsValueType() && destTH.IsValueType())
{
if (srcTH.CanCastToNoGC(destTH) == TypeHandle::CanCast)
return AssignUnboxValueClass;
else if (destTH.CanCastToNoGC(srcTH) == TypeHandle::CanCast) // V extends IV. Copying from IV to V, or Object to V.
return AssignUnboxValueClass;
else
return AssignDontKnow;
}
const CorElementType srcElType = srcTH.GetVerifierCorElementType();
const CorElementType destElType = destTH.GetVerifierCorElementType();
_ASSERTE(srcElType < ELEMENT_TYPE_MAX);
_ASSERTE(destElType < ELEMENT_TYPE_MAX);
// Copying primitives from one type to another
if (CorTypeInfo::IsPrimitiveType_NoThrow(srcElType) && CorTypeInfo::IsPrimitiveType_NoThrow(destElType))
{
if (srcElType == destElType)
return AssignWillWork;
if (InvokeUtil::CanPrimitiveWiden(destElType, srcElType))
return AssignPrimitiveWiden;
else
return AssignWrongType;
}
// dest Object extends src
if (srcTH.CanCastToNoGC(destTH) == TypeHandle::CanCast)
return AssignWillWork;
// src Object extends dest
if (destTH.CanCastToNoGC(srcTH) == TypeHandle::CanCast)
return AssignMustCast;
// class X extends/implements src and implements dest.
if (destTH.IsInterface() && srcElType != ELEMENT_TYPE_VALUETYPE)
return AssignMustCast;
// class X implements src and extends/implements dest
if (srcTH.IsInterface() && destElType != ELEMENT_TYPE_VALUETYPE)
return AssignMustCast;
return AssignDontKnow;
}
// Returns an enum saying whether you can copy an array of srcType into destType.
ArrayNative::AssignArrayEnum ArrayNative::CanAssignArrayType(const BASEARRAYREF pSrc, const BASEARRAYREF pDest)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
PRECONDITION(pSrc != NULL);
PRECONDITION(pDest != NULL);
}
CONTRACTL_END;
// The next 50 lines are a little tricky. Change them with great care.
//
// This first bit is a minor optimization: e.g. when copying byte[] to byte[]
// we do not need to call GetArrayElementTypeHandle().
MethodTable *pSrcMT = pSrc->GetMethodTable();
MethodTable *pDestMT = pDest->GetMethodTable();
if (pSrcMT == pDestMT)
return AssignWillWork;
TypeHandle srcTH = pSrcMT->GetApproxArrayElementTypeHandle();
TypeHandle destTH = pDestMT->GetApproxArrayElementTypeHandle();
if (srcTH == destTH) // This check kicks for different array kind or dimensions
return AssignWillWork;
// Value class boxing
if (srcTH.IsValueType() && !destTH.IsValueType())
{
if (srcTH.CanCastTo(destTH))
return AssignBoxValueClassOrPrimitive;
else
return AssignWrongType;
}
// Value class unboxing.
if (!srcTH.IsValueType() && destTH.IsValueType())
{
if (srcTH.CanCastTo(destTH))
return AssignUnboxValueClass;
else if (destTH.CanCastTo(srcTH)) // V extends IV. Copying from IV to V, or Object to V.
return AssignUnboxValueClass;
else
return AssignWrongType;
}
const CorElementType srcElType = srcTH.GetVerifierCorElementType();
const CorElementType destElType = destTH.GetVerifierCorElementType();
_ASSERTE(srcElType < ELEMENT_TYPE_MAX);
_ASSERTE(destElType < ELEMENT_TYPE_MAX);
// Copying primitives from one type to another
if (CorTypeInfo::IsPrimitiveType_NoThrow(srcElType) && CorTypeInfo::IsPrimitiveType_NoThrow(destElType))
{
if (srcElType == destElType)
return AssignWillWork;
if (InvokeUtil::CanPrimitiveWiden(destElType, srcElType))
return AssignPrimitiveWiden;
else
return AssignWrongType;
}
// dest Object extends src
if (srcTH.CanCastTo(destTH))
return AssignWillWork;
// src Object extends dest
if (destTH.CanCastTo(srcTH))
return AssignMustCast;
// class X extends/implements src and implements dest.
if (destTH.IsInterface() && srcElType != ELEMENT_TYPE_VALUETYPE)
return AssignMustCast;
// class X implements src and extends/implements dest
if (srcTH.IsInterface() && destElType != ELEMENT_TYPE_VALUETYPE)
return AssignMustCast;
return AssignWrongType;
}
// Casts and assigns each element of src array to the dest array type.
void ArrayNative::CastCheckEachElement(const BASEARRAYREF pSrcUnsafe, const unsigned int srcIndex, BASEARRAYREF pDestUnsafe, unsigned int destIndex, const unsigned int len)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
PRECONDITION(pSrcUnsafe != NULL);
PRECONDITION(srcIndex >= 0);
PRECONDITION(pDestUnsafe != NULL);
PRECONDITION(len > 0);
}
CONTRACTL_END;
// pSrc is either a PTRARRAYREF or a multidimensional array.
TypeHandle destTH = pDestUnsafe->GetArrayElementTypeHandle();
struct _gc
{
OBJECTREF obj;
BASEARRAYREF pDest;
BASEARRAYREF pSrc;
} gc;
gc.obj = NULL;
gc.pDest = pDestUnsafe;
gc.pSrc = pSrcUnsafe;
GCPROTECT_BEGIN(gc);
for(unsigned int i=srcIndex; i<srcIndex + len; ++i)
{
gc.obj = ObjectToOBJECTREF(*((Object**) gc.pSrc->GetDataPtr() + i));
// Now that we have grabbed obj, we are no longer subject to races from another
// mutator thread.
if (gc.obj != NULL && !ObjIsInstanceOf(OBJECTREFToObject(gc.obj), destTH))
COMPlusThrow(kInvalidCastException, W("InvalidCast_DownCastArrayElement"));
OBJECTREF * destData = (OBJECTREF*)(gc.pDest->GetDataPtr()) + i - srcIndex + destIndex;
SetObjectReference(destData, gc.obj, gc.pDest->GetAppDomain());
}
GCPROTECT_END();
return;
}
// Will box each element in an array of value classes or primitives into an array of Objects.
void ArrayNative::BoxEachElement(BASEARRAYREF pSrc, unsigned int srcIndex, BASEARRAYREF pDest, unsigned int destIndex, unsigned int length)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
PRECONDITION(pSrc != NULL);
PRECONDITION(srcIndex >= 0);
PRECONDITION(pDest != NULL);
PRECONDITION(length > 0);
}
CONTRACTL_END;
// pDest is either a PTRARRAYREF or a multidimensional array.
_ASSERTE(pSrc!=NULL && srcIndex>=0 && pDest!=NULL && destIndex>=0 && length>=0);
TypeHandle srcTH = pSrc->GetArrayElementTypeHandle();
#ifdef _DEBUG
TypeHandle destTH = pDest->GetArrayElementTypeHandle();
#endif
_ASSERTE(srcTH.GetSignatureCorElementType() == ELEMENT_TYPE_CLASS || srcTH.GetSignatureCorElementType() == ELEMENT_TYPE_VALUETYPE || CorTypeInfo::IsPrimitiveType(pSrc->GetArrayElementType()));
_ASSERTE(!destTH.GetMethodTable()->IsValueType());
// Get method table of type we're copying from - we need to allocate objects of that type.
MethodTable * pSrcMT = srcTH.GetMethodTable();
PREFIX_ASSUME(pSrcMT != NULL);
if (!pSrcMT->IsClassInited())
{
BASEARRAYREF pSrcTmp = pSrc;
BASEARRAYREF pDestTmp = pDest;
GCPROTECT_BEGIN (pSrcTmp);
GCPROTECT_BEGIN (pDestTmp);
pSrcMT->CheckRunClassInitThrowing();
pSrc = pSrcTmp;
pDest = pDestTmp;
GCPROTECT_END ();
GCPROTECT_END ();
}
const unsigned int srcSize = pSrcMT->GetNumInstanceFieldBytes();
unsigned int srcArrayOffset = srcIndex * srcSize;
struct _gc
{
BASEARRAYREF src;
BASEARRAYREF dest;
OBJECTREF obj;
} gc;
gc.src = pSrc;
gc.dest = pDest;
gc.obj = NULL;
void* srcPtr = 0;
GCPROTECT_BEGIN(gc);
GCPROTECT_BEGININTERIOR(srcPtr);
for (unsigned int i=destIndex; i < destIndex+length; i++, srcArrayOffset += srcSize)
{
srcPtr = (BYTE*)gc.src->GetDataPtr() + srcArrayOffset;
gc.obj = pSrcMT->FastBox(&srcPtr);
OBJECTREF * destData = (OBJECTREF*)((gc.dest)->GetDataPtr()) + i;
SetObjectReference(destData, gc.obj, gc.dest->GetAppDomain());
}
GCPROTECT_END();
GCPROTECT_END();
}
// Unboxes from an Object[] into a value class or primitive array.
void ArrayNative::UnBoxEachElement(BASEARRAYREF pSrc, unsigned int srcIndex, BASEARRAYREF pDest, unsigned int destIndex, unsigned int length)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
PRECONDITION(pSrc != NULL);
PRECONDITION(srcIndex >= 0);
PRECONDITION(pDest != NULL);
PRECONDITION(destIndex >= 0);
PRECONDITION(length > 0);
}
CONTRACTL_END;
#ifdef _DEBUG
TypeHandle srcTH = pSrc->GetArrayElementTypeHandle();
#endif
TypeHandle destTH = pDest->GetArrayElementTypeHandle();
_ASSERTE(destTH.GetSignatureCorElementType() == ELEMENT_TYPE_CLASS || destTH.GetSignatureCorElementType() == ELEMENT_TYPE_VALUETYPE || CorTypeInfo::IsPrimitiveType(pDest->GetArrayElementType()));
_ASSERTE(!srcTH.GetMethodTable()->IsValueType());
MethodTable * pDestMT = destTH.GetMethodTable();
PREFIX_ASSUME(pDestMT != NULL);
const unsigned int destSize = pDestMT->GetNumInstanceFieldBytes();
BYTE* srcData = (BYTE*) pSrc->GetDataPtr() + srcIndex * sizeof(OBJECTREF);
BYTE* data = (BYTE*) pDest->GetDataPtr() + destIndex * destSize;
for(; length>0; length--, srcData += sizeof(OBJECTREF), data += destSize)
{
OBJECTREF obj = ObjectToOBJECTREF(*(Object**)srcData);
// Now that we have retrieved the element, we are no longer subject to race
// conditions from another array mutator.
if (!pDestMT->UnBoxInto(data, obj))
goto fail;
}
return;
fail:
COMPlusThrow(kInvalidCastException, W("InvalidCast_DownCastArrayElement"));
}
// Widen primitive types to another primitive type.
void ArrayNative::PrimitiveWiden(BASEARRAYREF pSrc, unsigned int srcIndex, BASEARRAYREF pDest, unsigned int destIndex, unsigned int length)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
PRECONDITION(pSrc != NULL);
PRECONDITION(srcIndex >= 0);
PRECONDITION(pDest != NULL);
PRECONDITION(destIndex >= 0);
PRECONDITION(length > 0);
}
CONTRACTL_END;
// Get appropriate sizes, which requires method tables.
TypeHandle srcTH = pSrc->GetArrayElementTypeHandle();
TypeHandle destTH = pDest->GetArrayElementTypeHandle();
const CorElementType srcElType = srcTH.GetVerifierCorElementType();
const CorElementType destElType = destTH.GetVerifierCorElementType();
const unsigned int srcSize = GetSizeForCorElementType(srcElType);
const unsigned int destSize = GetSizeForCorElementType(destElType);
BYTE* srcData = (BYTE*) pSrc->GetDataPtr() + srcIndex * srcSize;
BYTE* data = (BYTE*) pDest->GetDataPtr() + destIndex * destSize;
_ASSERTE(srcElType != destElType); // We shouldn't be here if these are the same type.
_ASSERTE(CorTypeInfo::IsPrimitiveType_NoThrow(srcElType) && CorTypeInfo::IsPrimitiveType_NoThrow(destElType));
for(; length>0; length--, srcData += srcSize, data += destSize)
{
// We pretty much have to do some fancy datatype mangling every time here, for
// converting w/ sign extension and floating point conversions.
switch (srcElType)
{
case ELEMENT_TYPE_U1:
switch (destElType)
{
case ELEMENT_TYPE_R4:
*(float*)data = *(UINT8*)srcData;
break;
case ELEMENT_TYPE_R8:
*(double*)data = *(UINT8*)srcData;
break;
#ifndef BIGENDIAN
default:
*(UINT8*)data = *(UINT8*)srcData;
memset(data+1, 0, destSize - 1);
break;
#else // BIGENDIAN
case ELEMENT_TYPE_CHAR:
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
*(INT16*)data = *(UINT8*)srcData;
break;
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
*(INT32*)data = *(UINT8*)srcData;
break;
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
*(INT64*)data = *(UINT8*)srcData;
break;
default:
_ASSERTE(!"Array.Copy from U1 to another type hit unsupported widening conversion");
#endif // BIGENDIAN
}
break;
case ELEMENT_TYPE_I1:
switch (destElType)
{
case ELEMENT_TYPE_I2:
*(INT16*)data = *(INT8*)srcData;
break;
case ELEMENT_TYPE_I4:
*(INT32*)data = *(INT8*)srcData;
break;
case ELEMENT_TYPE_I8:
*(INT64*)data = *(INT8*)srcData;
break;
case ELEMENT_TYPE_R4:
*(float*)data = *(INT8*)srcData;
break;
case ELEMENT_TYPE_R8:
*(double*)data = *(INT8*)srcData;
break;
default:
_ASSERTE(!"Array.Copy from I1 to another type hit unsupported widening conversion");
}
break;
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_CHAR:
switch (destElType)
{
case ELEMENT_TYPE_R4:
*(float*)data = *(UINT16*)srcData;
break;
case ELEMENT_TYPE_R8:
*(double*)data = *(UINT16*)srcData;
break;
#ifndef BIGENDIAN
default:
*(UINT16*)data = *(UINT16*)srcData;
memset(data+2, 0, destSize - 2);
break;
#else // BIGENDIAN
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_CHAR:
*(UINT16*)data = *(UINT16*)srcData;
break;
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
*(UINT32*)data = *(UINT16*)srcData;
break;
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
*(UINT64*)data = *(UINT16*)srcData;
break;
default:
_ASSERTE(!"Array.Copy from U1 to another type hit unsupported widening conversion");
#endif // BIGENDIAN
}
break;
case ELEMENT_TYPE_I2:
switch (destElType)
{
case ELEMENT_TYPE_I4:
*(INT32*)data = *(INT16*)srcData;
break;
case ELEMENT_TYPE_I8:
*(INT64*)data = *(INT16*)srcData;
break;
case ELEMENT_TYPE_R4:
*(float*)data = *(INT16*)srcData;
break;
case ELEMENT_TYPE_R8:
*(double*)data = *(INT16*)srcData;
break;
default:
_ASSERTE(!"Array.Copy from I2 to another type hit unsupported widening conversion");
}
break;
case ELEMENT_TYPE_I4:
switch (destElType)
{
case ELEMENT_TYPE_I8:
*(INT64*)data = *(INT32*)srcData;
break;
case ELEMENT_TYPE_R4:
*(float*)data = (float)*(INT32*)srcData;
break;
case ELEMENT_TYPE_R8:
*(double*)data = *(INT32*)srcData;
break;
default:
_ASSERTE(!"Array.Copy from I4 to another type hit unsupported widening conversion");
}
break;
case ELEMENT_TYPE_U4:
switch (destElType)
{
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
*(INT64*)data = *(UINT32*)srcData;
break;
case ELEMENT_TYPE_R4:
*(float*)data = (float)*(UINT32*)srcData;
break;
case ELEMENT_TYPE_R8:
*(double*)data = *(UINT32*)srcData;
break;
default:
_ASSERTE(!"Array.Copy from U4 to another type hit unsupported widening conversion");
}
break;
case ELEMENT_TYPE_I8:
if (destElType == ELEMENT_TYPE_R4)
{
*(float*) data = (float) *(INT64*)srcData;
}
else
{
_ASSERTE(destElType==ELEMENT_TYPE_R8);
*(double*) data = (double) *(INT64*)srcData;
}
break;
case ELEMENT_TYPE_U8:
if (destElType == ELEMENT_TYPE_R4)
{
//*(float*) data = (float) *(UINT64*)srcData;
INT64 srcVal = *(INT64*)srcData;
float f = (float) srcVal;
if (srcVal < 0)
f += 4294967296.0f * 4294967296.0f; // This is 2^64
*(float*) data = f;
}
else
{
_ASSERTE(destElType==ELEMENT_TYPE_R8);
//*(double*) data = (double) *(UINT64*)srcData;
INT64 srcVal = *(INT64*)srcData;
double d = (double) srcVal;
if (srcVal < 0)
d += 4294967296.0 * 4294967296.0; // This is 2^64
*(double*) data = d;
}
break;
case ELEMENT_TYPE_R4:
*(double*) data = *(float*)srcData;
break;
default:
_ASSERTE(!"Fell through outer switch in PrimitiveWiden! Unknown primitive type for source array!");
}
}
}
//
// This is a GC safe variant of the memmove intrinsic. It sets the cards, and guarantees that the object references in the GC heap are
// updated atomically.
//
// The CRT version of memmove does not always guarantee that updates of aligned fields stay atomic (e.g. it is using "rep movsb" in some cases).
// Type safety guarantees and background GC scanning requires object references in GC heap to be updated atomically.
//
void memmoveGCRefs(void *dest, const void *src, size_t len)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
SO_TOLERANT;
}
CONTRACTL_END;
_ASSERTE(dest != nullptr);
_ASSERTE(src != nullptr);
// Make sure everything is pointer aligned
_ASSERTE(IS_ALIGNED(dest, sizeof(SIZE_T)));
_ASSERTE(IS_ALIGNED(src, sizeof(SIZE_T)));
_ASSERTE(IS_ALIGNED(len, sizeof(SIZE_T)));
_ASSERTE(CheckPointer(dest));
_ASSERTE(CheckPointer(src));
if (len != 0 && dest != src)
{
InlinedMemmoveGCRefsHelper(dest, src, len);
}
}
void ArrayNative::ArrayCopyNoTypeCheck(BASEARRAYREF pSrc, unsigned int srcIndex, BASEARRAYREF pDest, unsigned int destIndex, unsigned int length)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
SO_TOLERANT;
PRECONDITION(pSrc != NULL);
PRECONDITION(srcIndex >= 0);
PRECONDITION(pDest != NULL);
PRECONDITION(length > 0);
}
CONTRACTL_END;
BYTE *src = (BYTE*)pSrc->GetDataPtr();
BYTE *dst = (BYTE*)pDest->GetDataPtr();
SIZE_T size = pSrc->GetComponentSize();
src += srcIndex * size;
dst += destIndex * size;
if (pDest->GetMethodTable()->ContainsPointers())
{
memmoveGCRefs(dst, src, length * size);
}
else
{
memmove(dst, src, length * size);
}
}
FCIMPL6(void, ArrayNative::ArrayCopy, ArrayBase* m_pSrc, INT32 m_iSrcIndex, ArrayBase* m_pDst, INT32 m_iDstIndex, INT32 m_iLength, CLR_BOOL reliable)
{
FCALL_CONTRACT;
BYTE *src;
BYTE *dst;
SIZE_T size;
struct _gc
{
BASEARRAYREF pSrc;
BASEARRAYREF pDst;
} gc;
gc.pSrc = (BASEARRAYREF)m_pSrc;
gc.pDst = (BASEARRAYREF)m_pDst;
//
// creating a HelperMethodFrame is quite expensive,
// so we want to delay this for the most common case which doesn't trigger a GC.
// FCThrow is needed to throw an exception without a HelperMethodFrame
//
// cannot pass null for source or destination
if (gc.pSrc == NULL || gc.pDst == NULL) {
FCThrowArgumentNullVoid(gc.pSrc==NULL ? W("sourceArray") : W("destinationArray"));
}
// source and destination must be arrays
_ASSERTE(gc.pSrc->GetMethodTable()->IsArray());
_ASSERTE(gc.pDst->GetMethodTable()->IsArray());
// Equal method tables should imply equal rank
_ASSERTE(!(gc.pSrc->GetMethodTable() == gc.pDst->GetMethodTable() && gc.pSrc->GetRank() != gc.pDst->GetRank()));
// Which enables us to avoid touching the EEClass in simple cases
if (gc.pSrc->GetMethodTable() != gc.pDst->GetMethodTable() && gc.pSrc->GetRank() != gc.pDst->GetRank()) {
FCThrowResVoid(kRankException, W("Rank_MustMatch"));
}
g_IBCLogger.LogMethodTableAccess(gc.pSrc->GetMethodTable());
g_IBCLogger.LogMethodTableAccess(gc.pDst->GetMethodTable());
int srcLB = gc.pSrc->GetLowerBoundsPtr()[0];
int destLB = gc.pDst->GetLowerBoundsPtr()[0];
// array bounds checking
const unsigned int srcLen = gc.pSrc->GetNumComponents();
const unsigned int destLen = gc.pDst->GetNumComponents();
if (m_iLength < 0)
FCThrowArgumentOutOfRangeVoid(W("length"), W("ArgumentOutOfRange_NeedNonNegNum"));
if (m_iSrcIndex < srcLB || (m_iSrcIndex - srcLB < 0))
FCThrowArgumentOutOfRangeVoid(W("sourceIndex"), W("ArgumentOutOfRange_ArrayLB"));
if (m_iDstIndex < destLB || (m_iDstIndex - destLB < 0))
FCThrowArgumentOutOfRangeVoid(W("destinationIndex"), W("ArgumentOutOfRange_ArrayLB"));
if ((DWORD)(m_iSrcIndex - srcLB + m_iLength) > srcLen)
FCThrowArgumentVoid(W("sourceArray"), W("Arg_LongerThanSrcArray"));
if ((DWORD)(m_iDstIndex - destLB + m_iLength) > destLen)
FCThrowArgumentVoid(W("destinationArray"), W("Arg_LongerThanDestArray"));
int r = 0;