summaryrefslogtreecommitdiff
path: root/src/vm/stubhelpers.cpp
blob: 0a5c143bf49dd64c7e35f17a20bed07fe0c93f5d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
// 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: stubhelpers.cpp
// 

//


#include "common.h"

#include "mlinfo.h"
#include "stubhelpers.h"
#include "jitinterface.h"
#include "dllimport.h"
#include "fieldmarshaler.h"
#include "comdelegate.h"
#include "security.h"
#include "eventtrace.h"
#include "comdatetime.h"
#include "gcheaputilities.h"
#include "interoputil.h"

#ifdef FEATURE_COMINTEROP
#include <oletls.h>
#include "olecontexthelpers.h"
#include "runtimecallablewrapper.h"
#include "comcallablewrapper.h"
#include "clrtocomcall.h"
#include "cominterfacemarshaler.h"
#include "winrttypenameconverter.h"
#endif

#ifdef VERIFY_HEAP

CQuickArray<StubHelpers::ByrefValidationEntry> StubHelpers::s_ByrefValidationEntries;
SIZE_T StubHelpers::s_ByrefValidationIndex = 0;
CrstStatic StubHelpers::s_ByrefValidationLock;

// static
void StubHelpers::Init()
{
    WRAPPER_NO_CONTRACT;
    s_ByrefValidationLock.Init(CrstPinnedByrefValidation);
}

// static
void StubHelpers::ValidateObjectInternal(Object *pObjUNSAFE, BOOL fValidateNextObj)
{
	CONTRACTL
	{
	NOTHROW;
	GC_NOTRIGGER;
	MODE_ANY;
	SO_TOLERANT;
}
	CONTRACTL_END;

	_ASSERTE(GCHeapUtilities::GetGCHeap()->RuntimeStructuresValid());

	// validate the object - there's no need to validate next object's
	// header since we validate the next object explicitly below
	pObjUNSAFE->Validate(/*bDeep=*/ TRUE, /*bVerifyNextHeader=*/ FALSE, /*bVerifySyncBlock=*/ TRUE);

	// and the next object as required
	if (fValidateNextObj)
	{
		Object *nextObj = GCHeapUtilities::GetGCHeap()->NextObj(pObjUNSAFE);
		if (nextObj != NULL)
		{
			// Note that the MethodTable of the object (i.e. the pointer at offset 0) can change from
			// g_pFreeObjectMethodTable to NULL, from NULL to <legal-value>, or possibly also from
			// g_pFreeObjectMethodTable to <legal-value> concurrently while executing this function.
			// Once <legal-value> is seen, we believe that the object should pass the Validate check.
			// We have to be careful and read the pointer only once to avoid "phantom reads".
			MethodTable *pMT = VolatileLoad(nextObj->GetMethodTablePtr());
			if (pMT != NULL && pMT != g_pFreeObjectMethodTable)
			{
				// do *not* verify the next object's syncblock - the next object is not guaranteed to
				// be "alive" so the finalizer thread may have already released its syncblock
				nextObj->Validate(/*bDeep=*/ TRUE, /*bVerifyNextHeader=*/ FALSE, /*bVerifySyncBlock=*/ FALSE);
			}
		}
	}
}

// static
MethodDesc *StubHelpers::ResolveInteropMethod(Object *pThisUNSAFE, MethodDesc *pMD)
{
    WRAPPER_NO_CONTRACT;

    if (pMD == NULL && pThisUNSAFE != NULL)
    {
        // if this is a call via delegate, get its Invoke method
        MethodTable *pMT = pThisUNSAFE->GetMethodTable();

        _ASSERTE(pMT->IsDelegate());
        return ((DelegateEEClass *)pMT->GetClass())->m_pInvokeMethod;
    }
    return pMD;
}

// static
void StubHelpers::FormatValidationMessage(MethodDesc *pMD, SString &ssErrorString)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;           
    }
    CONTRACTL_END;

    ssErrorString.Append(W("Detected managed heap corruption, likely culprit is interop call through "));

    if (pMD == NULL)
    {
        // the only case where we don't have interop MD is CALLI
        ssErrorString.Append(W("CALLI."));
    }
    else
    {
        ssErrorString.Append(W("method '"));

        StackSString ssClassName;
        pMD->GetMethodTable()->_GetFullyQualifiedNameForClass(ssClassName);

        ssErrorString.Append(ssClassName);
        ssErrorString.Append(NAMESPACE_SEPARATOR_CHAR);
        ssErrorString.AppendUTF8(pMD->GetName());

        ssErrorString.Append(W("'."));
    }
}

// static
void StubHelpers::ProcessByrefValidationList()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;           
    }
    CONTRACTL_END;

    StackSString errorString;
    ByrefValidationEntry entry = { NULL, NULL };

    EX_TRY
    {
        AVInRuntimeImplOkayHolder AVOkay;

        // Process all byref validation entries we have saved since the last GC. Note that EE is suspended at
        // this point so we don't have to take locks and we can safely call code:GCHeap.GetContainingObject.
        for (SIZE_T i = 0; i < s_ByrefValidationIndex; i++)
        {
            entry = s_ByrefValidationEntries[i];

            Object *pObjUNSAFE = GCHeapUtilities::GetGCHeap()->GetContainingObject(entry.pByref, false);
            ValidateObjectInternal(pObjUNSAFE, TRUE);
        }
    }
    EX_CATCH
    {
        EX_TRY
        {
            FormatValidationMessage(entry.pMD, errorString);
            EEPOLICY_HANDLE_FATAL_ERROR_WITH_MESSAGE(COR_E_EXECUTIONENGINE, errorString.GetUnicode());
        }
        EX_CATCH
        {
            EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
        }
        EX_END_CATCH_UNREACHABLE;
    }
    EX_END_CATCH_UNREACHABLE;

    s_ByrefValidationIndex = 0;
}

#endif // VERIFY_HEAP

FCIMPL1(double, StubHelpers::DateMarshaler__ConvertToNative,  INT64 managedDate)
{
    FCALL_CONTRACT;

    double retval = 0.0;
    HELPER_METHOD_FRAME_BEGIN_RET_0();
    retval = COMDateTime::TicksToDoubleDate(managedDate);
    HELPER_METHOD_FRAME_END();
    return retval;
}
FCIMPLEND

FCIMPL1_V(INT64, StubHelpers::DateMarshaler__ConvertToManaged, double nativeDate)
{
    FCALL_CONTRACT;

    INT64 retval = 0;
    HELPER_METHOD_FRAME_BEGIN_RET_0();
    retval = COMDateTime::DoubleDateToTicks(nativeDate);
    HELPER_METHOD_FRAME_END();
    return retval;
}
FCIMPLEND

FCIMPL4(void, StubHelpers::ValueClassMarshaler__ConvertToNative, LPVOID pDest, LPVOID pSrc, MethodTable* pMT, OBJECTREF *ppCleanupWorkListOnStack)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    FmtValueTypeUpdateNative(&pSrc, pMT, (BYTE*)pDest, ppCleanupWorkListOnStack);
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL3(void, StubHelpers::ValueClassMarshaler__ConvertToManaged, LPVOID pDest, LPVOID pSrc, MethodTable* pMT)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    FmtValueTypeUpdateCLR(&pDest, pMT, (BYTE*)pSrc);
    HELPER_METHOD_FRAME_END_POLL();
}
FCIMPLEND

FCIMPL2(void, StubHelpers::ValueClassMarshaler__ClearNative, LPVOID pDest, MethodTable* pMT)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    FmtClassDestroyNative(pDest, pMT);
    HELPER_METHOD_FRAME_END_POLL();
}
FCIMPLEND

#ifdef FEATURE_COMINTEROP

FORCEINLINE static void GetCOMIPFromRCW_ClearFP()
{
    LIMITED_METHOD_CONTRACT;

#ifdef _TARGET_X86_
    // As per ASURT 146699 we need to clear FP state before calling to COM
    // the following sequence was previously generated to compiled ML stubs
    // and is faster than _clearfp().
    __asm
    {
        fnstsw ax
        and    eax, 0x3F
        jz     NoNeedToClear
        fnclex
NoNeedToClear:
    }
#endif // _TARGET_X86_
}

FORCEINLINE static SOleTlsData *GetOrCreateOleTlsData()
{
    LIMITED_METHOD_CONTRACT;

    SOleTlsData *pOleTlsData;
#ifdef _TARGET_X86_
    // This saves 1 memory instruction over NtCurretTeb()->ReservedForOle because
    // NtCurrentTeb() reads _TEB.NtTib.Self which is the same as what FS:0 already
    // points to.
    pOleTlsData = (SOleTlsData *)(ULONG_PTR)__readfsdword(offsetof(TEB, ReservedForOle));
#else // _TARGET_X86_
    pOleTlsData = (SOleTlsData *)NtCurrentTeb()->ReservedForOle;
#endif // _TARGET_X86_
    if (pOleTlsData == NULL)
    {
        pOleTlsData = (SOleTlsData *)SetupOleContext();
    }
    return pOleTlsData;
}

FORCEINLINE static void *GetCOMIPFromRCW_GetTargetNoInterception(IUnknown *pUnk, ComPlusCallInfo *pComInfo)
{
    LIMITED_METHOD_CONTRACT;

#ifdef _TARGET_X86_
    _ASSERTE(pComInfo->m_pInterceptStub == NULL || pComInfo->m_pInterceptStub == (LPVOID)-1);
    _ASSERTE(!pComInfo->HasCopyCtorArgs());
#endif // _TARGET_X86_
    _ASSERTE(!NDirect::IsHostHookEnabled());

    LPVOID *lpVtbl = *(LPVOID **)pUnk;
    return lpVtbl[pComInfo->m_cachedComSlot];
}

FORCEINLINE static IUnknown *GetCOMIPFromRCW_GetIUnknownFromRCWCache(RCW *pRCW, MethodTable * pItfMT)
{
    LIMITED_METHOD_CONTRACT;

    // The code in this helper is the "fast path" that used to be generated directly
    // to compiled ML stubs. The idea is to aim for an efficient RCW cache hit.
    SOleTlsData * pOleTlsData = GetOrCreateOleTlsData();
    
    // test for free-threaded after testing for context match to optimize for apartment-bound objects
    if (pOleTlsData->pCurrentCtx == pRCW->GetWrapperCtxCookie() || pRCW->IsFreeThreaded())
    {
        for (int i = 0; i < INTERFACE_ENTRY_CACHE_SIZE; i++)
        {
            if (pRCW->m_aInterfaceEntries[i].m_pMT == pItfMT)
            {
                return pRCW->m_aInterfaceEntries[i].m_pUnknown;
            }
        }
    }

    // also search the auxiliary cache if it's available
    RCWAuxiliaryData *pAuxData = pRCW->m_pAuxiliaryData;
    if (pAuxData != NULL)
    {
        LPVOID pCtxCookie = (pRCW->IsFreeThreaded() ? NULL : pOleTlsData->pCurrentCtx);
        return pAuxData->FindInterfacePointer(pItfMT, pCtxCookie);
    }

    return NULL;
}

// Like GetCOMIPFromRCW_GetIUnknownFromRCWCache but also computes the target. This is a couple of instructions
// faster than GetCOMIPFromRCW_GetIUnknownFromRCWCache + GetCOMIPFromRCW_GetTargetNoInterception.
FORCEINLINE static IUnknown *GetCOMIPFromRCW_GetIUnknownFromRCWCache_NoInterception(RCW *pRCW, ComPlusCallInfo *pComInfo, void **ppTarget)
{
    LIMITED_METHOD_CONTRACT;

    // The code in this helper is the "fast path" that used to be generated directly
    // to compiled ML stubs. The idea is to aim for an efficient RCW cache hit.
    SOleTlsData *pOleTlsData = GetOrCreateOleTlsData();
    MethodTable *pItfMT = pComInfo->m_pInterfaceMT;
    
    // test for free-threaded after testing for context match to optimize for apartment-bound objects
    if (pOleTlsData->pCurrentCtx == pRCW->GetWrapperCtxCookie() || pRCW->IsFreeThreaded())
    {
        for (int i = 0; i < INTERFACE_ENTRY_CACHE_SIZE; i++)
        {
            if (pRCW->m_aInterfaceEntries[i].m_pMT == pItfMT)
            {
                IUnknown *pUnk = pRCW->m_aInterfaceEntries[i].m_pUnknown;
                _ASSERTE(pUnk != NULL);
                *ppTarget = GetCOMIPFromRCW_GetTargetNoInterception(pUnk, pComInfo);
                return pUnk;
            }
        }
    }

    // also search the auxiliary cache if it's available
    RCWAuxiliaryData *pAuxData = pRCW->m_pAuxiliaryData;
    if (pAuxData != NULL)
    {
        LPVOID pCtxCookie = (pRCW->IsFreeThreaded() ? NULL : pOleTlsData->pCurrentCtx);
        
        IUnknown *pUnk = pAuxData->FindInterfacePointer(pItfMT, pCtxCookie);
        if (pUnk != NULL)
        {
            *ppTarget = GetCOMIPFromRCW_GetTargetNoInterception(pUnk, pComInfo);
            return pUnk;
        }
    }

    return NULL;
}

FORCEINLINE static void *GetCOMIPFromRCW_GetTarget(IUnknown *pUnk, ComPlusCallInfo *pComInfo)
{
    LIMITED_METHOD_CONTRACT;


    LPVOID *lpVtbl = *(LPVOID **)pUnk;
    return lpVtbl[pComInfo->m_cachedComSlot];
}

NOINLINE static IUnknown* GetCOMIPFromRCWHelper(LPVOID pFCall, OBJECTREF pSrc, MethodDesc* pMD, void **ppTarget)
{
    FC_INNER_PROLOG(pFCall);

    IUnknown *pIntf = NULL;

    // This is only called in IL stubs which are in CER, so we don't need to worry about ThreadAbort
    HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_NO_THREAD_ABORT|Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, pSrc);

    SafeComHolder<IUnknown> pRetUnk;

    ComPlusCallInfo *pComInfo = ComPlusCallInfo::FromMethodDesc(pMD);
    pRetUnk = ComObject::GetComIPFromRCWThrowing(&pSrc, pComInfo->m_pInterfaceMT);

    if (pFCall == StubHelpers::GetCOMIPFromRCW_WinRT ||
        pFCall == StubHelpers::GetCOMIPFromRCW_WinRTSharedGeneric ||
        pFCall == StubHelpers::GetCOMIPFromRCW_WinRTDelegate)
    {
        pRetUnk.Release();
    }


    *ppTarget = GetCOMIPFromRCW_GetTarget(pRetUnk, pComInfo);
    _ASSERTE(*ppTarget != NULL);

    GetCOMIPFromRCW_ClearFP();

    pIntf = pRetUnk.Extract();
    
    // No exception will be thrown here (including thread abort as it is delayed in IL stubs)
    HELPER_METHOD_FRAME_END();

    FC_INNER_EPILOG();
    return pIntf;
}

//==================================================================================================================
// The GetCOMIPFromRCW helper exists in four specialized versions to optimize CLR->COM perf. Please be careful when
// changing this code as one of these methods is executed as part of every CLR->COM call so every instruction counts.
//==================================================================================================================


#include <optsmallperfcritical.h>

// This helper can handle any CLR->COM call (classic COM, WinRT, WinRT delegate, WinRT generic), it supports hosting,
// and clears FP state on x86 for compatibility with VB6.
FCIMPL4(IUnknown*, StubHelpers::GetCOMIPFromRCW, Object* pSrcUNSAFE, MethodDesc* pMD, void **ppTarget, CLR_BOOL* pfNeedsRelease)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pMD->IsComPlusCall() || pMD->IsGenericComPlusCall() || pMD->IsEEImpl());
    }
    CONTRACTL_END;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);
    *pfNeedsRelease = false;

    ComPlusCallInfo *pComInfo = ComPlusCallInfo::FromMethodDesc(pMD);
    RCW *pRCW = pSrc->PassiveGetSyncBlock()->GetInteropInfoNoCreate()->GetRawRCW();
    if (pRCW != NULL)
    {

        IUnknown * pUnk = GetCOMIPFromRCW_GetIUnknownFromRCWCache(pRCW, pComInfo->m_pInterfaceMT);
        if (pUnk != NULL)
        {
            *ppTarget = GetCOMIPFromRCW_GetTarget(pUnk, pComInfo);
            if (*ppTarget != NULL)
            {
                GetCOMIPFromRCW_ClearFP();
                return pUnk;
            }
        }
    }

    /* if we didn't find the COM interface pointer in the cache we will have to erect an HMF */
    *pfNeedsRelease = true;
    FC_INNER_RETURN(IUnknown*, GetCOMIPFromRCWHelper(StubHelpers::GetCOMIPFromRCW, pSrc, pMD, ppTarget));
}
FCIMPLEND

// This helper can handle only non-generic WinRT calls, does not support hosting/interception, and does not clear FP state.
FCIMPL3(IUnknown*, StubHelpers::GetCOMIPFromRCW_WinRT, Object* pSrcUNSAFE, MethodDesc* pMD, void** ppTarget)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pMD->IsComPlusCall());
    }
    CONTRACTL_END;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);

    ComPlusCallInfo *pComInfo = ((ComPlusCallMethodDesc *)pMD)->m_pComPlusCallInfo;
    RCW *pRCW = pSrc->PassiveGetSyncBlock()->GetInteropInfoNoCreate()->GetRawRCW();
    if (pRCW != NULL)
    {
        IUnknown *pUnk = GetCOMIPFromRCW_GetIUnknownFromRCWCache_NoInterception(pRCW, pComInfo, ppTarget);
        if (pUnk != NULL)
        {
            return pUnk;
        }
    }

    /* if we didn't find the COM interface pointer in the cache we will have to erect an HMF */
    FC_INNER_RETURN(IUnknown*, GetCOMIPFromRCWHelper(StubHelpers::GetCOMIPFromRCW_WinRT, pSrc, pMD, ppTarget));
}
FCIMPLEND

// This helper can handle only generic WinRT calls, does not support hosting, and does not clear FP state.
FCIMPL3(IUnknown*, StubHelpers::GetCOMIPFromRCW_WinRTSharedGeneric, Object* pSrcUNSAFE, MethodDesc* pMD, void** ppTarget)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pMD->IsGenericComPlusCall());
    }
    CONTRACTL_END;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);

    ComPlusCallInfo *pComInfo = pMD->AsInstantiatedMethodDesc()->IMD_GetComPlusCallInfo();
    RCW *pRCW = pSrc->PassiveGetSyncBlock()->GetInteropInfoNoCreate()->GetRawRCW();
    if (pRCW != NULL)
    {
        IUnknown *pUnk = GetCOMIPFromRCW_GetIUnknownFromRCWCache_NoInterception(pRCW, pComInfo, ppTarget);
        if (pUnk != NULL)
        {
            return pUnk;
        }
    }

    /* if we didn't find the COM interface pointer in the cache we will have to erect an HMF */
    FC_INNER_RETURN(IUnknown*, GetCOMIPFromRCWHelper(StubHelpers::GetCOMIPFromRCW_WinRTSharedGeneric, pSrc, pMD, ppTarget));
}
FCIMPLEND

// This helper can handle only delegate WinRT calls, does not support hosting, and does not clear FP state.
FCIMPL3(IUnknown*, StubHelpers::GetCOMIPFromRCW_WinRTDelegate, Object* pSrcUNSAFE, MethodDesc* pMD, void** ppTarget)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pMD->IsEEImpl());
    }
    CONTRACTL_END;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);

    ComPlusCallInfo *pComInfo = ((DelegateEEClass *)pMD->GetClass())->m_pComPlusCallInfo;
    RCW *pRCW = pSrc->PassiveGetSyncBlock()->GetInteropInfoNoCreate()->GetRawRCW();
    if (pRCW != NULL)
    {
        IUnknown *pUnk = GetCOMIPFromRCW_GetIUnknownFromRCWCache_NoInterception(pRCW, pComInfo, ppTarget);
        if (pUnk != NULL)
        {
            return pUnk;
        }
    }

    /* if we didn't find the COM interface pointer in the cache we will have to erect an HMF */
    FC_INNER_RETURN(IUnknown*, GetCOMIPFromRCWHelper(StubHelpers::GetCOMIPFromRCW_WinRTDelegate, pSrc, pMD, ppTarget));
}
FCIMPLEND

#include <optdefault.h>


NOINLINE static FC_BOOL_RET ShouldCallWinRTInterfaceHelper(RCW *pRCW, MethodTable *pItfMT)
{
    FC_INNER_PROLOG(StubHelpers::ShouldCallWinRTInterface);

    bool result = false;
    
    HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2);

    // call the GC-triggering version
    result = pRCW->SupportsWinRTInteropInterface(pItfMT);

    HELPER_METHOD_FRAME_END();
    FC_INNER_EPILOG();

    FC_RETURN_BOOL(result);
}

FCIMPL2(FC_BOOL_RET, StubHelpers::ShouldCallWinRTInterface, Object *pSrcUNSAFE, MethodDesc *pMD)
{
    FCALL_CONTRACT;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);

    ComPlusCallInfo *pComInfo = ComPlusCallInfo::FromMethodDesc(pMD);
    RCW *pRCW = pSrc->PassiveGetSyncBlock()->GetInteropInfoNoCreate()->GetRawRCW();
    if (pRCW == NULL)
    {
        // Pretend that we are not redirected WinRT type
        // We'll throw InvalidComObjectException later in GetComIPFromRCW
        return false;
    }

    TypeHandle::CastResult result = pRCW->SupportsWinRTInteropInterfaceNoGC(pComInfo->m_pInterfaceMT);
    switch (result)
    {
        case TypeHandle::CanCast:    FC_RETURN_BOOL(true);
        case TypeHandle::CannotCast: FC_RETURN_BOOL(false);
    }

    FC_INNER_RETURN(FC_BOOL_RET, ShouldCallWinRTInterfaceHelper(pRCW, pComInfo->m_pInterfaceMT));
}
FCIMPLEND

NOINLINE static DelegateObject *GetTargetForAmbiguousVariantCallHelper(RCW *pRCW, MethodTable *pMT, BOOL fIsEnumerable, CLR_BOOL *pfUseString)
{
    FC_INNER_PROLOG(StubHelpers::GetTargetForAmbiguousVariantCall);

    DelegateObject *pRetVal = NULL;
    
    HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2);

    // Note that if the call succeeds, it will set the right OBJECTHANDLE/flags on the RCW so we won't have to do this
    // next time. If the call fails, we don't care because it is an error and an exception will be thrown later.
    SafeComHolder<IUnknown> pUnk = pRCW->GetComIPFromRCW(pMT);

    WinRTInterfaceRedirector::WinRTLegalStructureBaseType baseType = WinRTInterfaceRedirector::GetStructureBaseType(pMT->GetInstantiation());

    BOOL fUseString = FALSE;
    BOOL fUseT = FALSE;
    pRetVal = (DelegateObject *)OBJECTREFToObject(pRCW->GetTargetForAmbiguousVariantCall(fIsEnumerable, baseType, &fUseString, &fUseT));

    *pfUseString = !!fUseString;

    HELPER_METHOD_FRAME_END();
    FC_INNER_EPILOG();

    return pRetVal;
}

// Performs a run-time check to see how an ambiguous variant call on an RCW should be handled. Returns a delegate which should
// be called, or sets *pfUseString to true which means that the caller should use the <string> instantiation. If NULL is returned
// and *pfUseString is false, the caller should attempt to handle the call as usual.
FCIMPL3(DelegateObject*, StubHelpers::GetTargetForAmbiguousVariantCall, Object *pSrcUNSAFE, MethodTable *pMT, CLR_BOOL *pfUseString)
{
    FCALL_CONTRACT;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);

    RCW *pRCW = pSrc->PassiveGetSyncBlock()->GetInteropInfoNoCreate()->GetRawRCW();
    if (pRCW == NULL)
    {
        // ignore this - the call we'll attempt to make later will throw the right exception 
        *pfUseString = false;
        return NULL;
    }

    BOOL fIsEnumerable = pMT->HasSameTypeDefAs(MscorlibBinder::GetExistingClass(CLASS__IENUMERABLEGENERIC));
    _ASSERTE(fIsEnumerable || pMT->HasSameTypeDefAs(MscorlibBinder::GetExistingClass(CLASS__IREADONLYLISTGENERIC)));

    WinRTInterfaceRedirector::WinRTLegalStructureBaseType baseType = WinRTInterfaceRedirector::GetStructureBaseType(pMT->GetInstantiation());

    BOOL fUseString = FALSE;
    BOOL fUseT = FALSE;
    DelegateObject *pRetVal = (DelegateObject *)OBJECTREFToObject(pRCW->GetTargetForAmbiguousVariantCall(fIsEnumerable, baseType, &fUseString, &fUseT));

    if (pRetVal != NULL || fUseT || fUseString)
    {
        *pfUseString = !!fUseString;
        return pRetVal;
    }

    // we haven't seen QI for the interface yet, trigger it now
    FC_INNER_RETURN(DelegateObject*, GetTargetForAmbiguousVariantCallHelper(pRCW, pMT, fIsEnumerable, pfUseString));
}
FCIMPLEND

FCIMPL2(void, StubHelpers::ObjectMarshaler__ConvertToNative, Object* pSrcUNSAFE, VARIANT* pDest)
{
    FCALL_CONTRACT;

    OBJECTREF pSrc = ObjectToOBJECTREF(pSrcUNSAFE);

    HELPER_METHOD_FRAME_BEGIN_1(pSrc);
    if (pDest->vt & VT_BYREF)
    {
        OleVariant::MarshalOleRefVariantForObject(&pSrc, pDest);
    }
    else
    {
        OleVariant::MarshalOleVariantForObject(&pSrc, pDest);
    }
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL1(Object*, StubHelpers::ObjectMarshaler__ConvertToManaged, VARIANT* pSrc)
{
    FCALL_CONTRACT;

    OBJECTREF retVal = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_1(retVal);
    // The IL stub is going to call ObjectMarshaler__ClearNative() afterwards.  
    // If it doesn't it's a bug in ILObjectMarshaler.    
    OleVariant::MarshalObjectForOleVariant(pSrc, &retVal);
    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(retVal);
}
FCIMPLEND

FCIMPL1(void, StubHelpers::ObjectMarshaler__ClearNative, VARIANT* pSrc)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    SafeVariantClear(pSrc);
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

#include <optsmallperfcritical.h>
FCIMPL4(IUnknown*, StubHelpers::InterfaceMarshaler__ConvertToNative, Object* pObjUNSAFE, MethodTable* pItfMT, MethodTable* pClsMT, DWORD dwFlags)
{
    FCALL_CONTRACT;

    if (NULL == pObjUNSAFE)
    {
        return NULL;
    }

    IUnknown *pIntf = NULL;
    OBJECTREF pObj = ObjectToOBJECTREF(pObjUNSAFE);

    // This is only called in IL stubs which are in CER, so we don't need to worry about ThreadAbort
    HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_NO_THREAD_ABORT, pObj);

    pIntf = MarshalObjectToInterface(&pObj, pItfMT, pClsMT, dwFlags);

    // No exception will be thrown here (including thread abort as it is delayed in IL stubs)
    HELPER_METHOD_FRAME_END();

    return pIntf;
}
FCIMPLEND

FCIMPL4(Object*, StubHelpers::InterfaceMarshaler__ConvertToManaged, IUnknown **ppUnk, MethodTable *pItfMT, MethodTable *pClsMT, DWORD dwFlags)
{
    FCALL_CONTRACT;

    if (NULL == *ppUnk)
    {
        return NULL;
    }
    
    OBJECTREF pObj = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_1(pObj);

    UnmarshalObjectFromInterface(&pObj, ppUnk, pItfMT, pClsMT, dwFlags);

    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(pObj);  
}
FCIMPLEND

void QCALLTYPE StubHelpers::InterfaceMarshaler__ClearNative(IUnknown * pUnk)
{
    QCALL_CONTRACT;

    BEGIN_QCALL_SO_TOLERANT;

    ULONG cbRef = SafeReleasePreemp(pUnk);
    LogInteropRelease(pUnk, cbRef, "InterfaceMarshalerBase::ClearNative: In/Out release");

    END_QCALL_SO_TOLERANT;
}
#include <optdefault.h>




FCIMPL1(StringObject*, StubHelpers::UriMarshaler__GetRawUriFromNative, ABI::Windows::Foundation::IUriRuntimeClass* pIUriRC)
{
    FCALL_CONTRACT;

    if (NULL == pIUriRC)
    {
        return NULL;
    }

    STRINGREF strRef = NULL;
    UINT32 cchRawUri = 0;
    LPCWSTR pwszRawUri = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_1(strRef);

    WinRtString hsRawUriName;

    {
        GCX_PREEMP();

        // Get the RawUri string from the WinRT URI object
        IfFailThrow(pIUriRC->get_RawUri(hsRawUriName.Address()));

        pwszRawUri = hsRawUriName.GetRawBuffer(&cchRawUri);
    }

    strRef = StringObject::NewString(pwszRawUri, cchRawUri);

    HELPER_METHOD_FRAME_END();

    return STRINGREFToObject(strRef);
}
FCIMPLEND

FCIMPL2(IUnknown*, StubHelpers::UriMarshaler__CreateNativeUriInstance, WCHAR* pRawUri, UINT strLen)
{
    CONTRACTL {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(pRawUri));
    }
    CONTRACTL_END;

    ABI::Windows::Foundation::IUriRuntimeClass* pIUriRC = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_0();

    GCX_PREEMP();
    pIUriRC = CreateWinRTUri(pRawUri, strLen);

    HELPER_METHOD_FRAME_END();

    return pIUriRC;
}
FCIMPLEND

ABI::Windows::UI::Xaml::Interop::INotifyCollectionChangedEventArgs* QCALLTYPE 
StubHelpers::EventArgsMarshaler__CreateNativeNCCEventArgsInstance
(int action, ABI::Windows::UI::Xaml::Interop::IBindableVector *newItem, ABI::Windows::UI::Xaml::Interop::IBindableVector *oldItem, int newIndex, int oldIndex)
{
    QCALL_CONTRACT;

   ABI::Windows::UI::Xaml::Interop::INotifyCollectionChangedEventArgs *pArgsRC = NULL;

    BEGIN_QCALL;

    EventArgsMarshalingInfo *marshalingInfo = GetAppDomain()->GetMarshalingData()->GetEventArgsMarshalingInfo();
    ABI::Windows::UI::Xaml::Interop::INotifyCollectionChangedEventArgsFactory *pFactory = marshalingInfo->GetNCCEventArgsFactory();

    SafeComHolderPreemp<IInspectable> pInner;
    HRESULT hr;
    hr = pFactory->CreateInstanceWithAllParameters(
        (ABI::Windows::UI::Xaml::Interop::NotifyCollectionChangedAction)action,
        (ABI::Windows::UI::Xaml::Interop::IBindableVector *)newItem,
        (ABI::Windows::UI::Xaml::Interop::IBindableVector *)oldItem,
        newIndex,
        oldIndex,
        NULL,
        &pInner,
        &pArgsRC);
    IfFailThrow(hr);

    END_QCALL;

    return pArgsRC;
}

ABI::Windows::UI::Xaml::Data::IPropertyChangedEventArgs* QCALLTYPE 
	StubHelpers::EventArgsMarshaler__CreateNativePCEventArgsInstance(HSTRING name)
{
    QCALL_CONTRACT;

    ABI::Windows::UI::Xaml::Data::IPropertyChangedEventArgs *pArgsRC = NULL;

    BEGIN_QCALL;

    EventArgsMarshalingInfo *marshalingInfo = GetAppDomain()->GetMarshalingData()->GetEventArgsMarshalingInfo();
    ABI::Windows::UI::Xaml::Data::IPropertyChangedEventArgsFactory *pFactory = marshalingInfo->GetPCEventArgsFactory();

    SafeComHolderPreemp<IInspectable> pInner;
    HRESULT hr;
    hr = pFactory->CreateInstance(
        name,
        NULL,
        &pInner,
        &pArgsRC);
    IfFailThrow(hr);

    END_QCALL;

    return pArgsRC;
}

// A helper to convert an IP to object using special flags.
FCIMPL1(Object *, StubHelpers::InterfaceMarshaler__ConvertToManagedWithoutUnboxing, IUnknown *pNative)
{
    FCALL_CONTRACT;

    OBJECTREF oref = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_1(oref);

    //
    // Get a wrapper for pNative
    // Note that we need to skip WinRT unboxing at this point because
    // 1. We never know whether GetObjectRefFromComIP went through unboxing path. 
    // For example, user could just pass a IUnknown * to T and we'll happily convert that to T
    // 2. If for some reason we end up getting something that does not implement IReference<T>, 
    // we'll get a nice message later when we do the cast in CLRIReferenceImpl.UnboxHelper
    //
    GetObjectRefFromComIP(
        &oref,
        pNative,                                        // pUnk
        g_pBaseCOMObject,                               // Use __ComObject
        NULL,                                           // pItfMT
        ObjFromComIP::CLASS_IS_HINT |                   // No cast check - we'll do cast later 
        ObjFromComIP::UNIQUE_OBJECT |                   // Do not cache the object - To ensure that the unboxing code is called on this object always
                                                        // and the object is not retrieved from the cache as an __ComObject.
                                                        // Don't call GetRuntimeClassName - I just want a RCW of __ComObject
        ObjFromComIP::IGNORE_WINRT_AND_SKIP_UNBOXING    // Skip unboxing
        );
        
    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(oref);
}
FCIMPLEND

FCIMPL2(StringObject *, StubHelpers::WinRTTypeNameConverter__ConvertToWinRTTypeName, 
    ReflectClassBaseObject *pTypeUNSAFE, CLR_BOOL *pbIsWinRTPrimitive)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(pTypeUNSAFE));
        PRECONDITION(CheckPointer(pbIsWinRTPrimitive));
    }
    CONTRACTL_END;

    REFLECTCLASSBASEREF refClass = (REFLECTCLASSBASEREF) pTypeUNSAFE;
    STRINGREF refString= NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_2(refClass, refString);

    SString strWinRTTypeName;    
    bool bIsPrimitive;
    if (WinRTTypeNameConverter::AppendWinRTTypeNameForManagedType(
        refClass->GetType(),    // thManagedType
        strWinRTTypeName,       // strWinRTTypeName to append
        FALSE,                  // for type conversion, not for GetRuntimeClassName
        &bIsPrimitive
        ))
    {
        *pbIsWinRTPrimitive = bIsPrimitive;
        refString = AllocateString(strWinRTTypeName);
    }
    else
    {
        *pbIsWinRTPrimitive = FALSE;
        refString = NULL;
    }

    HELPER_METHOD_FRAME_END();

    return STRINGREFToObject(refString);
}
FCIMPLEND

FCIMPL2(ReflectClassBaseObject *, StubHelpers::WinRTTypeNameConverter__GetTypeFromWinRTTypeName, StringObject *pWinRTTypeNameUNSAFE, CLR_BOOL *pbIsPrimitive)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(pWinRTTypeNameUNSAFE));
    }
    CONTRACTL_END;

    OBJECTREF refClass = NULL;
    STRINGREF refString = ObjectToSTRINGREF(pWinRTTypeNameUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_RET_2(refClass, refString);

    bool isPrimitive;
    TypeHandle th = WinRTTypeNameConverter::GetManagedTypeFromWinRTTypeName(refString->GetBuffer(), &isPrimitive);
    *pbIsPrimitive = isPrimitive;
    
    refClass = th.GetManagedClassObject();
    
    HELPER_METHOD_FRAME_END();

    return (ReflectClassBaseObject *)OBJECTREFToObject(refClass);    
}
FCIMPLEND

FCIMPL1(MethodDesc*, StubHelpers::GetDelegateInvokeMethod, DelegateObject *pThisUNSAFE)
{
    FCALL_CONTRACT;

    MethodDesc *pMD = NULL;
    
    OBJECTREF pThis = ObjectToOBJECTREF(pThisUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_RET_1(pThis);

    MethodTable *pDelMT = pThis->GetMethodTable();

    pMD = COMDelegate::FindDelegateInvokeMethod(pDelMT);
    if (pMD->IsSharedByGenericInstantiations())
    {
        // we need the exact MethodDesc
        pMD = InstantiatedMethodDesc::FindOrCreateExactClassMethod(pDelMT, pMD);
    }

    HELPER_METHOD_FRAME_END();

    _ASSERTE(pMD);
    return pMD;
}
FCIMPLEND

// Called from COM-to-CLR factory method stubs to get the return value (the delegating interface pointer
// corresponding to the default WinRT interface of the class which we are constructing).
FCIMPL2(IInspectable *, StubHelpers::GetWinRTFactoryReturnValue, Object *pThisUNSAFE, PCODE pCtorEntry)
{
    FCALL_CONTRACT;

    IInspectable *pInsp = NULL;

    OBJECTREF pThis = ObjectToOBJECTREF(pThisUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_RET_1(pThis);

    // COM-to-CLR stubs use the target method entry point as their stub context
    MethodDesc *pCtorMD = Entry2MethodDesc(pCtorEntry, NULL);
    MethodTable *pClassMT = pCtorMD->GetMethodTable();

    // make sure that we talk to the right CCW
    ComCallWrapperTemplate *pTemplate = ComCallWrapperTemplate::GetTemplate(TypeHandle(pClassMT));
    CCWHolder pWrap = ComCallWrapper::InlineGetWrapper(&pThis, pTemplate);

    MethodTable *pDefaultItf = pClassMT->GetDefaultWinRTInterface();
    const IID &riid = (pDefaultItf == NULL ? IID_IInspectable : IID_NULL);
    
    pInsp = static_cast<IInspectable *>(ComCallWrapper::GetComIPFromCCW(pWrap, riid, pDefaultItf, 
        GetComIPFromCCW::CheckVisibility));

    HELPER_METHOD_FRAME_END();

    return pInsp;
}
FCIMPLEND

// Called from CLR-to-COM factory method stubs to get the outer IInspectable to pass
// to the underlying factory object.
FCIMPL2(IInspectable *, StubHelpers::GetOuterInspectable, Object *pThisUNSAFE, MethodDesc *pCtorMD)
{
    FCALL_CONTRACT;

    IInspectable *pInsp = NULL;

    OBJECTREF pThis = ObjectToOBJECTREF(pThisUNSAFE);

    if (pThis->GetTrueMethodTable() != pCtorMD->GetMethodTable())
    {
        // this is a composition scenario
        HELPER_METHOD_FRAME_BEGIN_RET_1(pThis);

        // we don't have the "outer" yet, marshal the object
        pInsp = static_cast<IInspectable *>
            (MarshalObjectToInterface(&pThis, NULL, NULL, ItfMarshalInfo::ITF_MARSHAL_INSP_ITF | ItfMarshalInfo::ITF_MARSHAL_USE_BASIC_ITF));

        HELPER_METHOD_FRAME_END();
    }

    return pInsp;
}
FCIMPLEND

#ifdef MDA_SUPPORTED
FCIMPL2(ExceptionObject*, StubHelpers::TriggerExceptionSwallowedMDA, ExceptionObject* pExceptionUNSAFE, PCODE pManagedTarget)
{
    FCALL_CONTRACT;
    OBJECTREF pException = ObjectToOBJECTREF(pExceptionUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_RET_1(pException);

    // COM-to-CLR stubs use the target method entry point as their stub context
    MethodDesc * pMD = Entry2MethodDesc(pManagedTarget, NULL);

    MDA_TRIGGER_ASSISTANT(ExceptionSwallowedOnCallFromCom, ReportViolation(pMD, &pException));

    HELPER_METHOD_FRAME_END();
    return (ExceptionObject*)OBJECTREFToObject(pException);
}
FCIMPLEND
#endif // MDA_SUPPORTED

#endif // FEATURE_COMINTEROP

FCIMPL0(void, StubHelpers::SetLastError)
{
    // Make sure this is the first thing we do after returning from the target, as almost everything can cause the last error to get trashed
    DWORD lastError = ::GetLastError();

    FCALL_CONTRACT;

    GetThread()->m_dwLastError = lastError;
}
FCIMPLEND

FCIMPL0(void, StubHelpers::ClearLastError)
{
    FCALL_CONTRACT;

    ::SetLastError(0);
}
FCIMPLEND

FCIMPL1(FC_BOOL_RET, StubHelpers::IsQCall, NDirectMethodDesc* pNMD)
{
    FCALL_CONTRACT;
    FC_RETURN_BOOL(pNMD->IsQCall());
}
FCIMPLEND

NOINLINE static void InitDeclaringTypeHelper(MethodTable *pMT)
{
    FC_INNER_PROLOG(StubHelpers::InitDeclaringType);

    HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2);
    pMT->CheckRunClassInitThrowing();
    HELPER_METHOD_FRAME_END();

    FC_INNER_EPILOG();
}

// Triggers cctor of pNMD's declarer, similar to code:JIT_InitClass.
#include <optsmallperfcritical.h>
FCIMPL1(void, StubHelpers::InitDeclaringType, NDirectMethodDesc* pNMD)
{
    FCALL_CONTRACT;

    MethodTable *pMT = pNMD->GetMethodTable();
    _ASSERTE(!pMT->IsClassPreInited());

    if (pMT->GetDomainLocalModule()->IsClassInitialized(pMT))
        return;

    FC_INNER_RETURN_VOID(InitDeclaringTypeHelper(pMT));
}
FCIMPLEND
#include <optdefault.h>

FCIMPL1(void*, StubHelpers::GetNDirectTarget, NDirectMethodDesc* pNMD)
{
    FCALL_CONTRACT;

    FCUnique(0xa2);
    return pNMD->GetNDirectTarget();
}
FCIMPLEND

FCIMPL2(void*, StubHelpers::GetDelegateTarget, DelegateObject *pThisUNSAFE, UINT_PTR *ppStubArg)
{
    PCODE pEntryPoint = NULL;

#ifdef _DEBUG
    BEGIN_PRESERVE_LAST_ERROR;
#endif

    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(pThisUNSAFE));
    }
    CONTRACTL_END;

    DELEGATEREF orefThis = (DELEGATEREF)ObjectToOBJECTREF(pThisUNSAFE);

#if defined(_TARGET_X86_)
    // On x86 we wrap the call with a thunk that handles host notifications.
    SyncBlock *pSyncBlock = orefThis->PassiveGetSyncBlock();
    if (pSyncBlock != NULL)
    {
        InteropSyncBlockInfo *pInteropInfo = pSyncBlock->GetInteropInfoNoCreate();
        if (pInteropInfo != NULL)
        {
            // we return entry point to a stub that wraps the real target
            Stub *pInterceptStub = pInteropInfo->GetInterceptStub();
            if (pInterceptStub != NULL)
            {
                pEntryPoint = pInterceptStub->GetEntryPoint();
            }
        }
    }
#endif // _TARGET_X86_

#if defined(_WIN64)
    UINT_PTR target = (UINT_PTR)orefThis->GetMethodPtrAux();

    // The lowest bit is used to distinguish between MD and target on 64-bit.
    target = (target << 1) | 1;

    // On 64-bit we pass the real target to the stub-for-host through this out argument,
    // see IL code gen in NDirectStubLinker::DoNDirect for details.
    *ppStubArg = target;

#elif defined(_TARGET_ARM_)
    // @ARMTODO: Nothing to do for ARM yet since we don't support the hosted path.
#endif // _WIN64, _TARGET_ARM_

    if (pEntryPoint == NULL)
    {
        pEntryPoint = orefThis->GetMethodPtrAux();
    }

#ifdef _DEBUG
    END_PRESERVE_LAST_ERROR;
#endif

    return (PVOID)pEntryPoint;
}
FCIMPLEND



FCIMPL2(void, StubHelpers::ThrowInteropParamException, UINT resID, UINT paramIdx)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    ::ThrowInteropParamException(resID, paramIdx);
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

#ifdef FEATURE_COMINTEROP
FCIMPL1(void, StubHelpers::StubRegisterRCW, Object *unsafe_pThis)
{
    FCALL_CONTRACT;

    OBJECTREF oref = ObjectToOBJECTREF(unsafe_pThis);
    HELPER_METHOD_FRAME_BEGIN_1(oref);

#if defined(_DEBUG) && defined(FEATURE_MDA)
    // Make sure that we only get here because the MDA is turned on.
    MdaRaceOnRCWCleanup* mda = MDA_GET_ASSISTANT(RaceOnRCWCleanup);
    _ASSERTE(mda != NULL);
#endif // _DEBUG

    // RegisterRCW may throw OOM in which case we need to decrement the refcount on the RCW
    class RCWDecrementUseCountHolder
    {
    public:
        RCW *m_pRCW;

        RCWDecrementUseCountHolder(RCW *pRCW)
        {
            LIMITED_METHOD_CONTRACT;
            m_pRCW = pRCW;
        }

        ~RCWDecrementUseCountHolder()
        {
            WRAPPER_NO_CONTRACT;
            if (m_pRCW != NULL)
            {
                m_pRCW->DecrementUseCount();
            }
        }
    };

    RCWDecrementUseCountHolder holder(oref->GetSyncBlock()->GetInteropInfoNoCreate()->GetRCWAndIncrementUseCount());
    if (holder.m_pRCW == NULL)
    {
        COMPlusThrow(kInvalidComObjectException, IDS_EE_COM_OBJECT_NO_LONGER_HAS_WRAPPER);
    }

    GET_THREAD()->RegisterRCW(holder.m_pRCW);

    // if we made it here, suppress the DecrementUseCount call
    holder.m_pRCW = NULL;

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL1(void, StubHelpers::StubUnregisterRCW, Object *unsafe_pThis)
{
    FCALL_CONTRACT;

    OBJECTREF oref = ObjectToOBJECTREF(unsafe_pThis);
    HELPER_METHOD_FRAME_BEGIN_1(oref);

#if defined(_DEBUG) && defined(FEATURE_MDA)
    // Make sure that we only get here because the MDA is turned on.
    MdaRaceOnRCWCleanup* mda = MDA_GET_ASSISTANT(RaceOnRCWCleanup);
    _ASSERTE(mda != NULL);
#endif // _DEBUG

    RCW *pRCW = GET_THREAD()->UnregisterRCW(INDEBUG(oref->GetSyncBlock()));

    if (pRCW != NULL)
    {
        // Thread::RegisterRCW incremented the use count, decrement it now
        pRCW->DecrementUseCount();
    }

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

class COMInterfaceMarshalerCallback : public ICOMInterfaceMarshalerCallback
{
public :
    COMInterfaceMarshalerCallback(Thread *pThread, LPVOID pCtxCookie)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_TRIGGERS;
            MODE_ANY;
        }
        CONTRACTL_END;

        _ASSERTE(pThread != NULL);
        _ASSERTE(pCtxCookie != NULL);
        
        m_bIsFreeThreaded = false;
        m_pThread = pThread;
        m_pCtxCookie = pCtxCookie;

        m_bIsDCOMProxy = false;
    }

    virtual void OnRCWCreated(RCW *pRCW)
    {
        LIMITED_METHOD_CONTRACT;

        _ASSERTE(pRCW != NULL);
        
        if (pRCW->IsFreeThreaded())
            m_bIsFreeThreaded = true;

        if (pRCW->IsDCOMProxy())
            m_bIsDCOMProxy = true;
    }

    // Return true if ComInterfaceMarshaler should use this RCW
    // Return false if ComInterfaceMarshaler should just skip this RCW and proceed
    // to create a duplicate one instead
    virtual bool ShouldUseThisRCW(RCW *pRCW)
    {
        LIMITED_METHOD_CONTRACT;
        
        _ASSERTE(pRCW->SupportsIInspectable());

        // Is this a free threaded RCW or a context-bound RCW created in the same context
        if (pRCW->IsFreeThreaded() || 
            pRCW->GetWrapperCtxCookie() == m_pCtxCookie)
        {
            return true;
        }
        else
        {
            //
            // Now we get back a WinRT factory RCW created in a different context. This means the
            // factory is a singleton, and the returned IActivationFactory could be either one of 
            // the following:
            // 1) A raw pointer, and it acts like a free threaded object
            // 2) A proxy that is used across different contexts. It might maintain a list of contexts
            // that it is marshaled to, and will fail to be called if it is not marshaled to this 
            // context yet.
            //
            // In this case, it is unsafe to use this RCW in this context and we should proceed
            // to create a duplicated one instead. It might make sense to have a context-sensitive
            // RCW cache but I don't think this case will be common enough to justify it
            //
            return false;
        }
    }
    
    virtual void OnRCWCacheHit(RCW *pRCW)
    {
        LIMITED_METHOD_CONTRACT;    

        if (pRCW->IsFreeThreaded())
            m_bIsFreeThreaded = true;        

        if (pRCW->IsDCOMProxy())
            m_bIsDCOMProxy = true;
    }

    bool IsFreeThreaded()
    {
        LIMITED_METHOD_CONTRACT;

        return m_bIsFreeThreaded;
    }
    
    bool IsDCOMProxy()
    {
        LIMITED_METHOD_CONTRACT;

        return m_bIsDCOMProxy;
    }

private :
    Thread *m_pThread;          // Current thread
    LPVOID m_pCtxCookie;        // Current context cookie
    bool   m_bIsFreeThreaded;   // Whether we got back the RCW from a different context
    bool   m_bIsDCOMProxy;      // Is this a proxy to an object in a different process
};

//
// Retrieve cached WinRT factory RCW or create a new one, according to the MethodDesc of the .ctor
//
FCIMPL1(Object*, StubHelpers::GetWinRTFactoryObject, MethodDesc *pCMD)
{
    FCALL_CONTRACT;

    OBJECTREF refFactory = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_1(refFactory);
    
    MethodTable *pMTOfTypeToCreate = pCMD->GetMethodTable();
    AppDomain   *pDomain = GetAppDomain();

    //
    // Look up cached WinRT factory according to type to create + current context cookie
    // For each type in AppDomain, we cache only the last WinRT factory object 
    // We don't cache factory per context in order to avoid explosion of objects if there are 
    // multiple STA apartments
    //
    // Note that if cached WinRT factory is FTM, we'll get it back regardless of the supplied cookie
    //
    LPVOID lpCtxCookie = GetCurrentCtxCookie();
    refFactory = pDomain->LookupWinRTFactoryObject(pMTOfTypeToCreate, lpCtxCookie);
    if (refFactory == NULL)
    {   
        //
        // Didn't find a cached factory that matches the context
        // Time to create a new factory and wrap it in a RCW
        // 

        //
        // Creates a callback to checks for singleton WinRT factory during RCW creation
        //
        // If we get back an existing RCW from a different context, this callback
        // will make the RCW a context-agile (but not free-threaded) RCW. Being context-agile
        // in this case means RCW will not make any context transition. As long as we are only
        // calling this RCW from where we got it back (using IInspectable* as identity), we should
        // be fine (as we are supposed to call that pointer directly anyway)
        //
        // See code:COMInterfaceMarshalerCallback for more details
        //
        COMInterfaceMarshalerCallback callback(GET_THREAD(), lpCtxCookie);

        //
        // Get the activation factory instance for this WinRT type and create a RCW for it
        //
        GetNativeWinRTFactoryObject(
            pMTOfTypeToCreate,
            GET_THREAD(),
            ComPlusCallInfo::FromMethodDesc(pCMD)->m_pInterfaceMT,  // Factory interface
            FALSE,      // Don't need a unique RCW
                        // it is only needed in WindowsRuntimeMarshal.GetActivationFactory API
            &callback,
            &refFactory);

        //
        // If this is free-threaded factory RCW, set lpCtxCookie = NULL, which means
        // this RCW can be used anywhere
        // Otherwise, we can only use this RCW from current thread
        //
        if (callback.IsFreeThreaded())
            lpCtxCookie = NULL;
        
        // Cache the result in the AD-wide cache, unless this is a proxy to a DCOM object on Apollo.  In the
        // Apollo app model, out of process WinRT servers can have lifetimes independent of the application,
        // and the cache may wind up with stale pointers if we save proxies to OOP factories.  In addition,
        // their app model is such that OOP WinRT objects cannot rely on having static state as they can be
        // forcibly terminated at any point.  Therefore, not caching an OOP WinRT factory object in Apollo
        // does not lead to correctness issues.
        //
        // This is not the same on the desktop, where OOP objects may contain static state, and therefore
        // we need to keep them alive.
#ifdef FEATURE_WINDOWSPHONE 
        if (!callback.IsDCOMProxy())
#endif // FEATURE_WINDOWSPHONE
        {
            pDomain->CacheWinRTFactoryObject(pMTOfTypeToCreate, &refFactory, lpCtxCookie);
        }
    }

    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(refFactory);
}
FCIMPLEND


#endif

#ifdef MDA_SUPPORTED
NOINLINE static void CheckCollectedDelegateMDAHelper(UMEntryThunk *pEntryThunk)
{
    FC_INNER_PROLOG(StubHelpers::CheckCollectedDelegateMDA);
    HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2);

    CallbackOnCollectedDelegateHelper(pEntryThunk);

    HELPER_METHOD_FRAME_END();
    FC_INNER_EPILOG();
}

FCIMPL1(void, StubHelpers::CheckCollectedDelegateMDA, LPVOID pEntryThunk)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(pEntryThunk != NULL);
    }
    CONTRACTL_END;

    if (MDA_GET_ASSISTANT(CallbackOnCollectedDelegate) == NULL)
        return;

    // keep this FCall as fast as possible for the "MDA is off" case
    FC_INNER_RETURN_VOID(CheckCollectedDelegateMDAHelper((UMEntryThunk *)pEntryThunk));
}
FCIMPLEND
#endif // MDA_SUPPORTED

#ifdef PROFILING_SUPPORTED
FCIMPL3(SIZE_T, StubHelpers::ProfilerBeginTransitionCallback, SIZE_T pSecretParam, Thread* pThread, Object* unsafe_pThis)
{
    FCALL_CONTRACT;

    // We can get here with an ngen image generated with "/prof", 
    // even if the profiler doesn't want to track transitions.
    if (!CORProfilerTrackTransitions())
    {
        return NULL;
    }

    MethodDesc* pRealMD = NULL;

    BEGIN_PRESERVE_LAST_ERROR;

    // We must transition to preemptive GC mode before calling out to the profiler, 
    // and the transition requires us to set up a HMF.
    DELEGATEREF dref = (DELEGATEREF)ObjectToOBJECTREF(unsafe_pThis);
    HELPER_METHOD_FRAME_BEGIN_RET_1(dref);

    bool fReverseInterop = false;

    if (NULL == pThread)
    {
        // This is our signal for the reverse interop cases.
        fReverseInterop = true;
        pThread = GET_THREAD();
        // the secret param in this casee is the UMEntryThunk
        pRealMD = ((UMEntryThunk*)pSecretParam)->GetMethod();
    }
    else
    if (pSecretParam == 0)
    {
        // Secret param is null.  This is the calli pinvoke case or the unmanaged delegate case.  
        // We have an unmanaged target address but no MD.  For the unmanaged delegate case, we can
        // still retrieve the MD by looking at the "this" object.

        if (dref == NULL)
        {
            // calli pinvoke case
            pRealMD = NULL;
        }
        else
        {
            // unmanaged delegate case
            MethodTable* pMT = dref->GetMethodTable();
            _ASSERTE(pMT->IsDelegate());

            EEClass * pClass = pMT->GetClass();
            pRealMD = ((DelegateEEClass*)pClass)->m_pInvokeMethod;
            _ASSERTE(pRealMD);
        }
    }
    else
    {
        // This is either the COM interop or the pinvoke case.
        pRealMD = (MethodDesc*)pSecretParam;
    }

    {
        GCX_PREEMP_THREAD_EXISTS(pThread);

        if (fReverseInterop)
        {
            ProfilerUnmanagedToManagedTransitionMD(pRealMD, COR_PRF_TRANSITION_CALL);
        }
        else
        {
            ProfilerManagedToUnmanagedTransitionMD(pRealMD, COR_PRF_TRANSITION_CALL);
        }
    }

    HELPER_METHOD_FRAME_END();

    END_PRESERVE_LAST_ERROR;

    return (SIZE_T)pRealMD;
}
FCIMPLEND

FCIMPL2(void, StubHelpers::ProfilerEndTransitionCallback, MethodDesc* pRealMD, Thread* pThread)
{
    FCALL_CONTRACT;

    // We can get here with an ngen image generated with "/prof", 
    // even if the profiler doesn't want to track transitions.
    if (!CORProfilerTrackTransitions())
    {
        return;
    }

    BEGIN_PRESERVE_LAST_ERROR;

    // We must transition to preemptive GC mode before calling out to the profiler, 
    // and the transition requires us to set up a HMF.
    HELPER_METHOD_FRAME_BEGIN_0();
    {
        bool fReverseInterop = false;

        if (NULL == pThread)
        {
            // if pThread is null, we are doing reverse interop
            pThread = GET_THREAD();
            fReverseInterop = true;
        }
        
        GCX_PREEMP_THREAD_EXISTS(pThread);

        if (fReverseInterop)
        {
            ProfilerManagedToUnmanagedTransitionMD(pRealMD, COR_PRF_TRANSITION_RETURN);
        }
        else
        {
            ProfilerUnmanagedToManagedTransitionMD(pRealMD, COR_PRF_TRANSITION_RETURN);
        }
    }
    HELPER_METHOD_FRAME_END();

    END_PRESERVE_LAST_ERROR;
}
FCIMPLEND
#endif // PROFILING_SUPPORTED

FCIMPL1(Object*, StubHelpers::GetHRExceptionObject, HRESULT hr)
{
    FCALL_CONTRACT;

    OBJECTREF oThrowable = NULL;
    
    HELPER_METHOD_FRAME_BEGIN_RET_1(oThrowable);
    {
        // GetExceptionForHR uses equivalant logic as COMPlusThrowHR
        GetExceptionForHR(hr, &oThrowable);
    }
    HELPER_METHOD_FRAME_END();    

    return OBJECTREFToObject(oThrowable);
}
FCIMPLEND

#ifdef FEATURE_COMINTEROP
FCIMPL4(Object*, StubHelpers::GetCOMHRExceptionObject, HRESULT hr, MethodDesc *pMD, Object *unsafe_pThis, CLR_BOOL fForWinRT)
{
    FCALL_CONTRACT;

    OBJECTREF oThrowable = NULL;

    // get 'this'
    OBJECTREF oref = ObjectToOBJECTREF(unsafe_pThis);
    
    HELPER_METHOD_FRAME_BEGIN_RET_2(oref, oThrowable);
    {
        IErrorInfo *pErrInfo = NULL;

        IRestrictedErrorInfo *pResErrorInfo = NULL;
        BOOL bHasNonCLRLanguageErrorObject = FALSE;

        if (fForWinRT)
        {
            SafeGetRestrictedErrorInfo(&pResErrorInfo);
            if (pResErrorInfo != NULL)
            {
                // If we have a restricted error Info lets try and find the corresponding errorInfo,
                // bHasNonCLRLanguageErrorObject can be TRUE|FALSE depending on whether we have an associtated LanguageExceptionObject
                // and whether it is CLR exceptionObject => bHasNonCLRLanguageErrorObject = FALSE;
                // or whether it is a non-CLRExceptionObject => bHasNonCLRLanguageErrorObject = TRUE;
                pErrInfo = GetCorrepondingErrorInfo_WinRT(hr, pResErrorInfo, &bHasNonCLRLanguageErrorObject);
            }
        }
        if (pErrInfo == NULL && pMD != NULL)
        {
            // Retrieve the interface method table.
            MethodTable *pItfMT = ComPlusCallInfo::FromMethodDesc(pMD)->m_pInterfaceMT;

            // Get IUnknown pointer for this interface on this object
            IUnknown* pUnk = ComObject::GetComIPFromRCW(&oref, pItfMT);
            if (pUnk != NULL)
            {
                // Check to see if the component supports error information for this interface.
                IID ItfIID;
                pItfMT->GetGuid(&ItfIID, TRUE);
                pErrInfo = GetSupportedErrorInfo(pUnk, ItfIID, !fForWinRT);
            
                DWORD cbRef = SafeRelease(pUnk);
                LogInteropRelease(pUnk, cbRef, "IUnk to QI for ISupportsErrorInfo");
            }
        }

        GetExceptionForHR(hr, pErrInfo, fForWinRT, &oThrowable, pResErrorInfo, bHasNonCLRLanguageErrorObject);
    }
    HELPER_METHOD_FRAME_END();    

    return OBJECTREFToObject(oThrowable);
}
FCIMPLEND
#endif // FEATURE_COMINTEROP

FCIMPL3(void, StubHelpers::FmtClassUpdateNativeInternal, Object* pObjUNSAFE, BYTE* pbNative, OBJECTREF *ppCleanupWorkListOnStack)
{
    FCALL_CONTRACT;

    OBJECTREF pObj = ObjectToOBJECTREF(pObjUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_1(pObj);

    FmtClassUpdateNative(&pObj, pbNative, ppCleanupWorkListOnStack);

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL2(void, StubHelpers::FmtClassUpdateCLRInternal, Object* pObjUNSAFE, BYTE* pbNative)
{
    FCALL_CONTRACT;

    OBJECTREF pObj = ObjectToOBJECTREF(pObjUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_1(pObj);

    FmtClassUpdateCLR(&pObj, pbNative);

    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL2(void, StubHelpers::LayoutDestroyNativeInternal, BYTE* pbNative, MethodTable* pMT)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    LayoutDestroyNative(pbNative, pMT);
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL1(Object*, StubHelpers::AllocateInternal, EnregisteredTypeHandle pRegisteredTypeHnd)
{
    FCALL_CONTRACT;

    TypeHandle typeHnd = TypeHandle::FromPtr(pRegisteredTypeHnd);
    OBJECTREF objRet = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_1(objRet);

    MethodTable* pMT = typeHnd.GetMethodTable();
    objRet = pMT->Allocate();
    
    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(objRet);
}
FCIMPLEND

FCIMPL1(void, StubHelpers::DecimalCanonicalizeInternal, DECIMAL *pDec)
{
    FCALL_CONTRACT;

    if (FAILED(DecimalCanonicalize(pDec)))
    {
        FCThrowResVoid(kOverflowException, W("Overflow_Currency"));
    }
}
FCIMPLEND

FCIMPL1(int, StubHelpers::AnsiStrlen, __in_z char* pszStr)
{
    FCALL_CONTRACT;

    size_t len = strlen(pszStr);

    // the length should have been checked earlier (see StubHelpers.CheckStringLength)
    _ASSERTE(FitsInI4(len));

    return (int)len;
}
FCIMPLEND

FCIMPL3(void, StubHelpers::MarshalToUnmanagedVaListInternal, va_list va, DWORD cbVaListSize, const VARARGS* pArgIterator)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    VARARGS::MarshalToUnmanagedVaList(va, cbVaListSize, pArgIterator);
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

FCIMPL2(void, StubHelpers::MarshalToManagedVaListInternal, va_list va, VARARGS* pArgIterator)
{
    FCALL_CONTRACT;

    VARARGS::MarshalToManagedVaList(va, pArgIterator);
}
FCIMPLEND

FCIMPL3(void, StubHelpers::ValidateObject, Object *pObjUNSAFE, MethodDesc *pMD, Object *pThisUNSAFE)
{
    FCALL_CONTRACT;

#ifdef VERIFY_HEAP
    HELPER_METHOD_FRAME_BEGIN_0();

    StackSString errorString;
    EX_TRY
    {
        AVInRuntimeImplOkayHolder AVOkay;
		// don't validate the next object if a BGC is in progress.  we can race with background
	    // sweep which could make the next object a Free object underneath us if it's dead.
        ValidateObjectInternal(pObjUNSAFE, !(GCHeapUtilities::GetGCHeap()->IsConcurrentGCInProgress()));
    }
    EX_CATCH
    {
        FormatValidationMessage(ResolveInteropMethod(pThisUNSAFE, pMD), errorString);
        EEPOLICY_HANDLE_FATAL_ERROR_WITH_MESSAGE(COR_E_EXECUTIONENGINE, errorString.GetUnicode());
    }
    EX_END_CATCH_UNREACHABLE;

    HELPER_METHOD_FRAME_END();
#else // VERIFY_HEAP
    FCUnique(0xa3);
    UNREACHABLE_MSG("No validation support without VERIFY_HEAP");
#endif // VERIFY_HEAP
}
FCIMPLEND

FCIMPL3(void, StubHelpers::ValidateByref, void *pByref, MethodDesc *pMD, Object *pThisUNSAFE)
{
    FCALL_CONTRACT;

#ifdef VERIFY_HEAP
    // We cannot validate byrefs at this point as code:GCHeap.GetContainingObject could potentially race
    // with allocations on other threads. We'll just remember this byref along with the interop MD and
    // perform the validation on next GC (see code:StubHelpers.ProcessByrefValidationList).

    // Skip byref if is not pointing inside managed heap
    if (!GCHeapUtilities::GetGCHeap()->IsHeapPointer(pByref))
    {
        return;
    }
    ByrefValidationEntry entry;
    entry.pByref = pByref;
    entry.pMD = ResolveInteropMethod(pThisUNSAFE, pMD);

    HELPER_METHOD_FRAME_BEGIN_0();

    SIZE_T NumOfEntries = 0;
    {
        CrstHolder ch(&s_ByrefValidationLock);

        if (s_ByrefValidationIndex >= s_ByrefValidationEntries.Size())
        {
            // The validation list grows as necessary, for simplicity we never shrink it.
            SIZE_T newSize;
            if (!ClrSafeInt<SIZE_T>::multiply(s_ByrefValidationIndex, 2, newSize) ||
                !ClrSafeInt<SIZE_T>::addition(newSize, 1, newSize))
            {
                ThrowHR(COR_E_OVERFLOW);
            }

            s_ByrefValidationEntries.ReSizeThrows(newSize);
            _ASSERTE(s_ByrefValidationIndex < s_ByrefValidationEntries.Size());
        }

        s_ByrefValidationEntries[s_ByrefValidationIndex] = entry;
        NumOfEntries = ++s_ByrefValidationIndex;
    }

    if (NumOfEntries > BYREF_VALIDATION_LIST_MAX_SIZE)
    {
        // if the list is too big, trigger GC now
        GCHeapUtilities::GetGCHeap()->GarbageCollect(0);
    }

    HELPER_METHOD_FRAME_END();
#else // VERIFY_HEAP
    FCUnique(0xa4);
    UNREACHABLE_MSG("No validation support without VERIFY_HEAP");
#endif // VERIFY_HEAP
}
FCIMPLEND

FCIMPL0(void*, StubHelpers::GetStubContext)
{
    FCALL_CONTRACT;

    FCUnique(0xa0);
    UNREACHABLE_MSG_RET("This is a JIT intrinsic!");
}
FCIMPLEND

FCIMPL2(void, StubHelpers::LogPinnedArgument, MethodDesc *target, Object *pinnedArg)
{
    FCALL_CONTRACT;

    SIZE_T managedSize = 0;

    if (pinnedArg != NULL)
    {
        // Can pass null objects to interop, only check the size if the object is valid.
        managedSize = pinnedArg->GetSize();
    }

    if (target != NULL)
    {
        STRESS_LOG3(LF_STUBS, LL_INFO100, "Managed object %#X with size '%#X' pinned for interop to Method [%pM]\n", pinnedArg, managedSize, target);
    }
    else
    {
        STRESS_LOG2(LF_STUBS, LL_INFO100, "Managed object %#X pinned for interop with size '%#X'", pinnedArg, managedSize);
    }
}
FCIMPLEND

#ifdef _WIN64
FCIMPL0(void*, StubHelpers::GetStubContextAddr)
{
    FCALL_CONTRACT;

    FCUnique(0xa1);
    UNREACHABLE_MSG("This is a JIT intrinsic!");
}
FCIMPLEND
#endif // _WIN64

#ifdef MDA_SUPPORTED    
FCIMPL0(void, StubHelpers::TriggerGCForMDA)
{
    FCALL_CONTRACT;

    HELPER_METHOD_FRAME_BEGIN_0();
    TriggerGCForMDAInternal();    
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND
#endif // MDA_SUPPORTED

FCIMPL1(DWORD, StubHelpers::CalcVaListSize, VARARGS *varargs)
{
    FCALL_CONTRACT;

    return VARARGS::CalcVaListSize(varargs);
}
FCIMPLEND

#ifdef FEATURE_ARRAYSTUB_AS_IL
NOINLINE static void ArrayTypeCheckSlow(Object* element, PtrArray* arr)
{
    FC_INNER_PROLOG(StubHelpers::ArrayTypeCheck);
    HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2);

    if (!ObjIsInstanceOf(element, arr->GetArrayElementTypeHandle()))
        COMPlusThrow(kArrayTypeMismatchException);
    
    HELPER_METHOD_FRAME_END();

    FC_INNER_EPILOG();
}

FCIMPL2(void, StubHelpers::ArrayTypeCheck, Object* element, PtrArray* arr)
{
    FCALL_CONTRACT;

    if (ObjIsInstanceOfNoGC(element, arr->GetArrayElementTypeHandle()) == TypeHandle::CanCast)
        return;
    
    FC_INNER_RETURN_VOID(ArrayTypeCheckSlow(element, arr));
}
FCIMPLEND
#endif // FEATURE_ARRAYSTUB_AS_IL

#ifdef FEATURE_STUBS_AS_IL
FCIMPL2(void, StubHelpers::MulticastDebuggerTraceHelper, Object* element, INT32 count)
{
    FCALL_CONTRACT;
    FCUnique(0xa5);
}
FCIMPLEND
#endif // FEATURE_STUBS_AS_IL