summaryrefslogtreecommitdiff
path: root/src/vm/comdelegate.cpp
blob: 9d92a7c6e5171f34643232ff9fdd945cf862b981 (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
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
// 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: COMDelegate.cpp 
// 

// This module contains the implementation of the native methods for the
// Delegate class.
// 


#include "common.h"
#include "comdelegate.h"
#include "invokeutil.h"
#include "excep.h"
#include "class.h"
#include "field.h"
#include "dllimportcallback.h"
#include "dllimport.h"
#include "eeconfig.h"
#include "cgensys.h"
#include "asmconstants.h"
#include "virtualcallstub.h"
#include "callingconvention.h"
#include "customattribute.h"
#include "typestring.h"
#include "../md/compiler/custattr.h"
#ifdef FEATURE_COMINTEROP
#include "comcallablewrapper.h"
#endif // FEATURE_COMINTEROP

#define DELEGATE_MARKER_UNMANAGEDFPTR -1


#ifndef DACCESS_COMPILE

#if defined(_TARGET_AMD64_) && !defined(UNIX_AMD64_ABI)

// ShuffleOfs not needed

#elif defined(_TARGET_X86_)

// Return an encoded shuffle entry describing a general register or stack offset that needs to be shuffled.
static UINT16 ShuffleOfs(INT ofs, UINT stackSizeDelta = 0)
{
    STANDARD_VM_CONTRACT;

    if (TransitionBlock::IsStackArgumentOffset(ofs))
    {
        ofs = (ofs - TransitionBlock::GetOffsetOfReturnAddress()) + stackSizeDelta;

        if (ofs >= ShuffleEntry::REGMASK)
        {
            // method takes too many stack args
            COMPlusThrow(kNotSupportedException);
        }
    }
    else
    {
        ofs -= TransitionBlock::GetOffsetOfArgumentRegisters();
        ofs |= ShuffleEntry::REGMASK;
    }

    return static_cast<UINT16>(ofs);
}

#else // Portable default implementation

// Iterator for extracting shuffle entries for argument desribed by an ArgLocDesc.
// Used when calculating shuffle array entries in GenerateShuffleArray below.
class ShuffleIterator
{
    // Argument location description
    ArgLocDesc* m_argLocDesc;

#if defined(UNIX_AMD64_ABI)
    // Current eightByte used for struct arguments in registers
    int m_currentEightByte;
#endif    
    // Current general purpose register index (relative to the ArgLocDesc::m_idxGenReg)
    int m_currentGenRegIndex;
    // Current floating point register index (relative to the ArgLocDesc::m_idxFloatReg)
    int m_currentFloatRegIndex;
    // Current stack slot index (relative to the ArgLocDesc::m_idxStack)
    int m_currentStackSlotIndex;

#if defined(UNIX_AMD64_ABI)
    // Get next shuffle offset for struct passed in registers. There has to be at least one offset left.
    UINT16 GetNextOfsInStruct()
    {
        EEClass* eeClass = m_argLocDesc->m_eeClass;
        _ASSERTE(eeClass != NULL);
        
        if (m_currentEightByte < eeClass->GetNumberEightBytes())
        {
            SystemVClassificationType eightByte = eeClass->GetEightByteClassification(m_currentEightByte);
            unsigned int eightByteSize = eeClass->GetEightByteSize(m_currentEightByte);

            m_currentEightByte++;

            int index;
            UINT16 mask = ShuffleEntry::REGMASK;

            if (eightByte == SystemVClassificationTypeSSE)
            {
                _ASSERTE(m_currentFloatRegIndex < m_argLocDesc->m_cFloatReg);
                index = m_argLocDesc->m_idxFloatReg + m_currentFloatRegIndex;
                m_currentFloatRegIndex++;

                mask |= ShuffleEntry::FPREGMASK;
                if (eightByteSize == 4)
                {
                    mask |= ShuffleEntry::FPSINGLEMASK;
                }
            }
            else
            {
                _ASSERTE(m_currentGenRegIndex < m_argLocDesc->m_cGenReg);
                index = m_argLocDesc->m_idxGenReg + m_currentGenRegIndex;
                m_currentGenRegIndex++;
            }

            return (UINT16)index | mask;
        }

        // There are no more offsets to get, the caller should not have called us
        _ASSERTE(false);
        return 0;
    }
#endif // UNIX_AMD64_ABI

public:

    // Construct the iterator for the ArgLocDesc
    ShuffleIterator(ArgLocDesc* argLocDesc)
    :
        m_argLocDesc(argLocDesc),
#if defined(UNIX_AMD64_ABI)
        m_currentEightByte(0),
#endif
        m_currentGenRegIndex(0),
        m_currentFloatRegIndex(0),
        m_currentStackSlotIndex(0)
    {
    }

    // Check if there are more offsets to shuffle
    bool HasNextOfs()
    {
        return (m_currentGenRegIndex < m_argLocDesc->m_cGenReg) || 
#if defined(UNIX_AMD64_ABI)
               (m_currentFloatRegIndex < m_argLocDesc->m_cFloatReg) ||
#endif
               (m_currentStackSlotIndex < m_argLocDesc->m_cStack);        
    }

    // Get next offset to shuffle. There has to be at least one offset left.
    UINT16 GetNextOfs()
    {
        int index;

#if defined(UNIX_AMD64_ABI)

        // Check if the argLocDesc is for a struct in registers
        EEClass* eeClass = m_argLocDesc->m_eeClass;
        if (m_argLocDesc->m_eeClass != 0)
        {
            return GetNextOfsInStruct();
        }

        // Shuffle float registers first
        if (m_currentFloatRegIndex < m_argLocDesc->m_cFloatReg)
        {        
            index = m_argLocDesc->m_idxFloatReg + m_currentFloatRegIndex;
            m_currentFloatRegIndex++;

            return (UINT16)index | ShuffleEntry::REGMASK | ShuffleEntry::FPREGMASK;
        }
#endif // UNIX_AMD64_ABI

        // Shuffle any registers first (the order matters since otherwise we could end up shuffling a stack slot
        // over a register we later need to shuffle down as well).
        if (m_currentGenRegIndex < m_argLocDesc->m_cGenReg)
        {        
            index = m_argLocDesc->m_idxGenReg + m_currentGenRegIndex;
            m_currentGenRegIndex++;

            return (UINT16)index | ShuffleEntry::REGMASK;
        }

        // If we get here we must have at least one stack slot left to shuffle (this method should only be called
        // when AnythingToShuffle(pArg) == true).
        if (m_currentStackSlotIndex < m_argLocDesc->m_cStack)
        {
            index = m_argLocDesc->m_idxStack + m_currentStackSlotIndex;
            m_currentStackSlotIndex++;

            // Delegates cannot handle overly large argument stacks due to shuffle entry encoding limitations.
            if (index >= ShuffleEntry::REGMASK)
            {
                COMPlusThrow(kNotSupportedException);
            }

            return (UINT16)index;
        }

        // There are no more offsets to get, the caller should not have called us
        _ASSERTE(false);
        return 0;
    }
};

#endif

#if defined(UNIX_AMD64_ABI)
// Return an index of argument slot. First indices are reserved for general purpose registers,
// the following ones for float registers and then the rest for stack slots.
// This index is independent of how many registers are actually used to pass arguments.
int GetNormalizedArgumentSlotIndex(UINT16 offset)
{
    int index;

    if (offset & ShuffleEntry::FPREGMASK)
    {
        index = NUM_ARGUMENT_REGISTERS + (offset & ShuffleEntry::OFSREGMASK);
    }
    else if (offset & ShuffleEntry::REGMASK)
    {
        index = offset & ShuffleEntry::OFSREGMASK;
    }
    else
    {
        // stack slot
        index = NUM_ARGUMENT_REGISTERS + NUM_FLOAT_ARGUMENT_REGISTERS + (offset & ShuffleEntry::OFSMASK);
    }

    return index;
}

// Node of a directed graph where nodes represent registers / stack slots
// and edges represent moves of data.
struct ShuffleGraphNode
{
    static const UINT16 NoNode = 0xffff;
    // Previous node (represents source of data for the register / stack of the current node)
    UINT16 prev;
    // Offset of the register / stack slot
    UINT16 ofs;
    // Set to true for nodes that are source of data for a destination node
    UINT8 isSource;
    // Nodes that are marked are either already processed or don't participate in the shuffling
    UINT8 isMarked;
};

#endif // UNIX_AMD64_ABI

VOID GenerateShuffleArray(MethodDesc* pInvoke, MethodDesc *pTargetMeth, SArray<ShuffleEntry> * pShuffleEntryArray)
{
    STANDARD_VM_CONTRACT;

    ShuffleEntry entry;
    ZeroMemory(&entry, sizeof(entry));

#if defined(_TARGET_AMD64_) && !defined(UNIX_AMD64_ABI)
    MetaSig msig(pInvoke);
    ArgIterator argit(&msig);

    if (argit.HasRetBuffArg())
    {
        if (!pTargetMeth->IsStatic())
        {
            // Use ELEMENT_TYPE_END to signal the special handling required by
            // instance method with return buffer. "this" needs to come from
            // the first argument.
            entry.argtype = ELEMENT_TYPE_END;
            pShuffleEntryArray->Append(entry);

            msig.NextArgNormalized();
        }
        else
        {
            entry.argtype = ELEMENT_TYPE_PTR;
            pShuffleEntryArray->Append(entry);
        }
    }

    CorElementType sigType;

    while ((sigType = msig.NextArgNormalized()) != ELEMENT_TYPE_END)
    {        
        ZeroMemory(&entry, sizeof(entry));
        entry.argtype = sigType;
        pShuffleEntryArray->Append(entry);
    }

    ZeroMemory(&entry, sizeof(entry));
    entry.srcofs  = ShuffleEntry::SENTINEL;
    pShuffleEntryArray->Append(entry);

#elif defined(_TARGET_X86_)
    // Must create independent msigs to prevent the argiterators from
    // interfering with other.
    MetaSig sSigSrc(pInvoke);
    MetaSig sSigDst(pTargetMeth);

    _ASSERTE(sSigSrc.HasThis());

    ArgIterator sArgPlacerSrc(&sSigSrc);
    ArgIterator sArgPlacerDst(&sSigDst);

    UINT stackSizeSrc = sArgPlacerSrc.SizeOfArgStack();
    UINT stackSizeDst = sArgPlacerDst.SizeOfArgStack();

    if (stackSizeDst > stackSizeSrc)
    {
        // we can drop arguments but we can never make them up - this is definitely not allowed
        COMPlusThrow(kVerificationException);
    }

    UINT stackSizeDelta;

#ifdef UNIX_X86_ABI
    // Stack does not shrink as UNIX_X86_ABI uses CDECL (instead of STDCALL).
    stackSizeDelta = 0;
#else
    stackSizeDelta = stackSizeSrc - stackSizeDst;
#endif

    INT ofsSrc, ofsDst;

    // if the function is non static we need to place the 'this' first
    if (!pTargetMeth->IsStatic())
    {
        entry.srcofs = ShuffleOfs(sArgPlacerSrc.GetNextOffset());
        entry.dstofs = ShuffleEntry::REGMASK | 4;
        pShuffleEntryArray->Append(entry);
    }
    else if (sArgPlacerSrc.HasRetBuffArg())
    {
        // the first register is used for 'this'
        entry.srcofs = ShuffleOfs(sArgPlacerSrc.GetRetBuffArgOffset());
        entry.dstofs = ShuffleOfs(sArgPlacerDst.GetRetBuffArgOffset(), stackSizeDelta);
        if (entry.srcofs != entry.dstofs)
            pShuffleEntryArray->Append(entry);
    }

    while (TransitionBlock::InvalidOffset != (ofsSrc = sArgPlacerSrc.GetNextOffset()))
    {
        ofsDst = sArgPlacerDst.GetNextOffset();

        int cbSize = sArgPlacerDst.GetArgSize();

        do
        {
            entry.srcofs = ShuffleOfs(ofsSrc);
            entry.dstofs = ShuffleOfs(ofsDst, stackSizeDelta);

            ofsSrc += STACK_ELEM_SIZE;
            ofsDst += STACK_ELEM_SIZE;

            if (entry.srcofs != entry.dstofs)
                pShuffleEntryArray->Append(entry);

            cbSize -= STACK_ELEM_SIZE;
        }
        while (cbSize > 0);
    }

    if (stackSizeDelta != 0)
    {
        // Emit code to move the return address
        entry.srcofs = 0;     // retaddress is assumed to be at esp
        entry.dstofs = static_cast<UINT16>(stackSizeDelta);
        pShuffleEntryArray->Append(entry);
    }

    entry.srcofs = ShuffleEntry::SENTINEL;
    entry.dstofs = static_cast<UINT16>(stackSizeDelta);
    pShuffleEntryArray->Append(entry);

#else // Portable default implementation
    MetaSig sSigSrc(pInvoke);
    MetaSig sSigDst(pTargetMeth);

    // Initialize helpers that determine how each argument for the source and destination signatures is placed
    // in registers or on the stack.
    ArgIterator sArgPlacerSrc(&sSigSrc);
    ArgIterator sArgPlacerDst(&sSigDst);

    INT ofsSrc;
    INT ofsDst;
    ArgLocDesc sArgSrc;
    ArgLocDesc sArgDst;

#if defined(UNIX_AMD64_ABI)
    int argSlots = NUM_FLOAT_ARGUMENT_REGISTERS + NUM_ARGUMENT_REGISTERS + sArgPlacerSrc.SizeOfArgStack() / sizeof(size_t);
#endif // UNIX_AMD64_ABI

    // If the target method in non-static (this happens for open instance delegates), we need to account for
    // the implicit this parameter.
    if (sSigDst.HasThis())
    {
        // The this pointer is an implicit argument for the destination signature. But on the source side it's
        // just another regular argument and needs to be iterated over by sArgPlacerSrc and the MetaSig.
        sArgPlacerSrc.GetArgLoc(sArgPlacerSrc.GetNextOffset(), &sArgSrc);

        sArgPlacerSrc.GetThisLoc(&sArgDst);

        ShuffleIterator iteratorSrc(&sArgSrc);
        ShuffleIterator iteratorDst(&sArgDst);

        entry.srcofs = iteratorSrc.GetNextOfs();
        entry.dstofs = iteratorDst.GetNextOfs();
        pShuffleEntryArray->Append(entry);
    }

    // Handle any return buffer argument.
    if (sArgPlacerDst.HasRetBuffArg())
    {
        // The return buffer argument is implicit in both signatures.

#if !defined(_TARGET_ARM64_) || !defined(CALLDESCR_RETBUFFARGREG)
        // The ifdef above disables this code if the ret buff arg is always in the same register, which
        // means that we don't need to do any shuffling for it.

        sArgPlacerSrc.GetRetBuffArgLoc(&sArgSrc);
        sArgPlacerDst.GetRetBuffArgLoc(&sArgDst);

        ShuffleIterator iteratorSrc(&sArgSrc);
        ShuffleIterator iteratorDst(&sArgDst);

        entry.srcofs = iteratorSrc.GetNextOfs();
        entry.dstofs = iteratorDst.GetNextOfs();

        // Depending on the type of target method (static vs instance) the return buffer argument may end up
        // in the same register in both signatures. So we only commit the entry (by moving the entry pointer
        // along) in the case where it's not a no-op (i.e. the source and destination ops are different).
        if (entry.srcofs != entry.dstofs)
            pShuffleEntryArray->Append(entry);
#endif // !defined(_TARGET_ARM64_) || !defined(CALLDESCR_RETBUFFARGREG)
    }

    // Iterate all the regular arguments. mapping source registers and stack locations to the corresponding
    // destination locations.
    while ((ofsSrc = sArgPlacerSrc.GetNextOffset()) != TransitionBlock::InvalidOffset)
    {
        ofsDst = sArgPlacerDst.GetNextOffset();

        // Find the argument location mapping for both source and destination signature. A single argument can
        // occupy a floating point register, a general purpose register, a pair of registers of any kind or
        // a stack slot.
        sArgPlacerSrc.GetArgLoc(ofsSrc, &sArgSrc);
        sArgPlacerDst.GetArgLoc(ofsDst, &sArgDst);

        ShuffleIterator iteratorSrc(&sArgSrc);
        ShuffleIterator iteratorDst(&sArgDst);

        // Shuffle each slot in the argument (register or stack slot) from source to destination.
        while (iteratorSrc.HasNextOfs())
        {
            // Locate the next slot to shuffle in the source and destination and encode the transfer into a
            // shuffle entry.
            entry.srcofs = iteratorSrc.GetNextOfs();
            entry.dstofs = iteratorDst.GetNextOfs();

            // Only emit this entry if it's not a no-op (i.e. the source and destination locations are
            // different).
            if (entry.srcofs != entry.dstofs)
                pShuffleEntryArray->Append(entry);
        }

        // We should have run out of slots to shuffle in the destination at the same time as the source.
        _ASSERTE(!iteratorDst.HasNextOfs());
    }


#if defined(UNIX_AMD64_ABI)
    // The Unix AMD64 ABI can cause a struct to be passed on stack for the source and in registers for the destination.
    // That can cause some arguments that are passed on stack for the destination to be passed in registers in the source.
    // An extreme example of that is e.g.:
    //   void fn(int, int, int, int, int, struct {int, double}, double, double, double, double, double, double, double, double, double, double)
    // For this signature, the shuffle needs to move slots as follows (please note the "forward" movement of xmm registers):
    //   RDI->RSI, RDX->RCX, R8->RDX, R9->R8, stack[0]->R9, xmm0->xmm1, xmm1->xmm2, ... xmm6->xmm7, xmm7->stack[0], stack[1]->xmm0, stack[2]->stack[1], stack[3]->stack[2]
    // To prevent overwriting of slots before they are moved, we need to perform the shuffling in correct order

    NewArrayHolder<ShuffleGraphNode> pGraphNodes = new ShuffleGraphNode[argSlots];

    // Initialize the graph array
    for (unsigned int i = 0; i < argSlots; i++)
    {
        pGraphNodes[i].prev = ShuffleGraphNode::NoNode;
        pGraphNodes[i].isMarked = true;
        pGraphNodes[i].isSource = false;
    }

    // Build the directed graph representing register and stack slot shuffling.
    // The links are directed from destination to source.
    // During the build also set isSource flag for nodes that are sources of data.
    // The ones that don't have the isSource flag set are beginnings of non-cyclic
    // segments of the graph.
    for (unsigned int i = 0; i < pShuffleEntryArray->GetCount(); i++)
    {
        ShuffleEntry entry = (*pShuffleEntryArray)[i];

        int srcIndex = GetNormalizedArgumentSlotIndex(entry.srcofs);
        int dstIndex = GetNormalizedArgumentSlotIndex(entry.dstofs);

        // Unmark the node to indicate that it was not processed yet
        pGraphNodes[srcIndex].isMarked = false;
        // The node contains a register / stack slot that is a source from which we move data to a destination one
        pGraphNodes[srcIndex].isSource = true;
        pGraphNodes[srcIndex].ofs = entry.srcofs;

        // Unmark the node to indicate that it was not processed yet
        pGraphNodes[dstIndex].isMarked = false;
        // Link to the previous node in the graph (source of data for the current node)
        pGraphNodes[dstIndex].prev = srcIndex;
        pGraphNodes[dstIndex].ofs = entry.dstofs;
    }

    // Now that we've built the graph, clear the array, we will regenerate it from the graph ensuring a proper order of shuffling
    pShuffleEntryArray->Clear();

    // Add all non-cyclic subgraphs to the target shuffle array and mark their nodes as visited
    for (unsigned int startIndex = 0; startIndex < argSlots; startIndex++)
    {
        unsigned int index = startIndex;

        if (!pGraphNodes[index].isMarked && !pGraphNodes[index].isSource)
        {
            // This node is not a source, that means it is an end of shuffle chain
            // Generate shuffle array entries for all nodes in the chain in a correct
            // order.
            UINT16 dstOfs = ShuffleEntry::SENTINEL;

            do
            {
                pGraphNodes[index].isMarked = true;
                if (dstOfs != ShuffleEntry::SENTINEL)
                {
                    entry.srcofs = pGraphNodes[index].ofs;
                    entry.dstofs = dstOfs;
                    pShuffleEntryArray->Append(entry);
                }

                dstOfs = pGraphNodes[index].ofs;
                index = pGraphNodes[index].prev;
            }
            while (index != ShuffleGraphNode::NoNode);
        }
    }

    // Process all cycles in the graph
    for (unsigned int startIndex = 0; startIndex < argSlots; startIndex++)
    {
        unsigned int index = startIndex;

        if (!pGraphNodes[index].isMarked)
        {
            // This node is part of a new cycle as all non-cyclic parts of the graphs were already visited

            // Move the first node register / stack slot to a helper reg
            UINT16 dstOfs = ShuffleEntry::HELPERREG;

            do
            {
                pGraphNodes[index].isMarked = true;

                entry.srcofs = pGraphNodes[index].ofs;
                entry.dstofs = dstOfs;
                pShuffleEntryArray->Append(entry);

                dstOfs = pGraphNodes[index].ofs;
                index = pGraphNodes[index].prev;
            }
            while (index != startIndex);

            // Move helper reg to the last node register / stack slot
            entry.srcofs = ShuffleEntry::HELPERREG;
            entry.dstofs = dstOfs;
            pShuffleEntryArray->Append(entry);
        }
    }

#endif // UNIX_AMD64_ABI

    entry.srcofs = ShuffleEntry::SENTINEL;
    entry.dstofs = 0;
    pShuffleEntryArray->Append(entry);
#endif
}


ShuffleThunkCache *COMDelegate::m_pShuffleThunkCache = NULL;
MulticastStubCache *COMDelegate::m_pSecureDelegateStubCache = NULL;
MulticastStubCache *COMDelegate::m_pMulticastStubCache = NULL;

CrstStatic   COMDelegate::s_DelegateToFPtrHashCrst;
PtrHashMap*  COMDelegate::s_pDelegateToFPtrHash = NULL;


// One time init.
void COMDelegate::Init()
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    s_DelegateToFPtrHashCrst.Init(CrstDelegateToFPtrHash, CRST_UNSAFE_ANYMODE);

    s_pDelegateToFPtrHash = ::new PtrHashMap();

    LockOwner lock = {&COMDelegate::s_DelegateToFPtrHashCrst, IsOwnerOfCrst};
    s_pDelegateToFPtrHash->Init(TRUE, &lock);

    m_pShuffleThunkCache = new ShuffleThunkCache(SystemDomain::GetGlobalLoaderAllocator()->GetStubHeap());
    m_pMulticastStubCache = new MulticastStubCache();
    m_pSecureDelegateStubCache = new MulticastStubCache();
}

#ifdef FEATURE_COMINTEROP
ComPlusCallInfo * COMDelegate::PopulateComPlusCallInfo(MethodTable * pDelMT)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    DelegateEEClass * pClass = (DelegateEEClass *)pDelMT->GetClass();

    // set up the ComPlusCallInfo if it does not exist already
    if (pClass->m_pComPlusCallInfo == NULL)
    {
        LoaderHeap *pHeap = pDelMT->GetLoaderAllocator()->GetHighFrequencyHeap();
        ComPlusCallInfo *pTemp = (ComPlusCallInfo *)(void *)pHeap->AllocMem(S_SIZE_T(sizeof(ComPlusCallInfo)));

        pTemp->m_cachedComSlot = ComMethodTable::GetNumExtraSlots(ifVtable);
        pTemp->InitStackArgumentSize();

        InterlockedCompareExchangeT(EnsureWritablePages(&pClass->m_pComPlusCallInfo), pTemp, NULL);
    }

    *EnsureWritablePages(&pClass->m_pComPlusCallInfo->m_pInterfaceMT) = pDelMT;

    return pClass->m_pComPlusCallInfo;
}
#endif // FEATURE_COMINTEROP

// We need a LoaderHeap that lives at least as long as the DelegateEEClass, but ideally no longer
LoaderHeap *DelegateEEClass::GetStubHeap()
{
    return GetInvokeMethod()->GetLoaderAllocator()->GetStubHeap();
}


Stub* COMDelegate::SetupShuffleThunk(MethodTable * pDelMT, MethodDesc *pTargetMeth)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM());
    }
    CONTRACTL_END;

    GCX_PREEMP();

    DelegateEEClass * pClass = (DelegateEEClass *)pDelMT->GetClass();
    
    MethodDesc *pMD = pClass->GetInvokeMethod();

    StackSArray<ShuffleEntry> rShuffleEntryArray;
    GenerateShuffleArray(pMD, pTargetMeth, &rShuffleEntryArray);

    ShuffleThunkCache* pShuffleThunkCache = m_pShuffleThunkCache;

    LoaderAllocator* pLoaderAllocator = pDelMT->GetLoaderAllocator();
    if (pLoaderAllocator->IsCollectible())
    {
        pShuffleThunkCache = ((AssemblyLoaderAllocator*)pLoaderAllocator)->GetShuffleThunkCache();
    }

    Stub* pShuffleThunk = pShuffleThunkCache->Canonicalize((const BYTE *)&rShuffleEntryArray[0]);
    if (!pShuffleThunk)
    {
        COMPlusThrowOM();
    }

    g_IBCLogger.LogEEClassCOWTableAccess(pDelMT);

    EnsureWritablePages(pClass);

    if (!pTargetMeth->IsStatic() && pTargetMeth->HasRetBuffArg() && IsRetBuffPassedAsFirstArg()) 
    {
        if (FastInterlockCompareExchangePointer(&pClass->m_pInstRetBuffCallStub, pShuffleThunk, NULL ) != NULL)
        {
            pShuffleThunk->DecRef();
            pShuffleThunk = pClass->m_pInstRetBuffCallStub;
        }
    }
    else
    {
        if (FastInterlockCompareExchangePointer(&pClass->m_pStaticCallStub, pShuffleThunk, NULL ) != NULL)
        {
            pShuffleThunk->DecRef();
            pShuffleThunk = pClass->m_pStaticCallStub;
        }
    }    

    return pShuffleThunk;
}


#ifndef CROSSGEN_COMPILE

static PCODE GetVirtualCallStub(MethodDesc *method, TypeHandle scopeType)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM()); // from MetaSig::SizeOfArgStack
    }
    CONTRACTL_END;

    //TODO: depending on what we decide for generics method we may want to move this check to better places
    if (method->IsGenericMethodDefinition() || method->HasMethodInstantiation())
    {
        COMPlusThrow(kNotSupportedException);
    }

    // need to grab a virtual dispatch stub 
    // method can be on a canonical MethodTable, we need to allocate the stub on the loader allocator associated with the exact type instantiation.
    VirtualCallStubManager *pVirtualStubManager = scopeType.GetMethodTable()->GetLoaderAllocator()->GetVirtualCallStubManager();
    PCODE pTargetCall = pVirtualStubManager->GetCallStub(scopeType, method);
    _ASSERTE(pTargetCall);
    return pTargetCall;
}

FCIMPL5(FC_BOOL_RET, COMDelegate::BindToMethodName, 
                        Object *refThisUNSAFE, 
                        Object *targetUNSAFE, 
                        ReflectClassBaseObject *pMethodTypeUNSAFE,
                        StringObject* methodNameUNSAFE, 
                        int flags)
{
    FCALL_CONTRACT;

    struct _gc
    {
        DELEGATEREF refThis;
        OBJECTREF target;
        STRINGREF methodName;
        REFLECTCLASSBASEREF refMethodType;
    } gc;

    gc.refThis    = (DELEGATEREF) ObjectToOBJECTREF(refThisUNSAFE);
    gc.target     = (OBJECTREF) targetUNSAFE;
    gc.methodName = (STRINGREF) methodNameUNSAFE;
    gc.refMethodType = (REFLECTCLASSBASEREF) ObjectToOBJECTREF(pMethodTypeUNSAFE);

    TypeHandle methodType = gc.refMethodType->GetType();

    MethodDesc *pMatchingMethod = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);

    // Caching of MethodDescs (impl and decl) for MethodTable slots provided significant
    // performance gain in some reflection emit scenarios.
    MethodTable::AllowMethodDataCaching();

    TypeHandle targetType((gc.target != NULL) ? gc.target->GetMethodTable() : NULL);
    // get the invoke of the delegate
    MethodTable * pDelegateType = gc.refThis->GetMethodTable();
    MethodDesc* pInvokeMeth = COMDelegate::FindDelegateInvokeMethod(pDelegateType);
    _ASSERTE(pInvokeMeth);

    //
    // now loop through the methods looking for a match
    //

    // get the name in UTF8 format
    SString wszName(SString::Literal, gc.methodName->GetBuffer());
    StackScratchBuffer utf8Name;
    LPCUTF8 szNameStr = wszName.GetUTF8(utf8Name);

    // pick a proper compare function
    typedef int (__cdecl *UTF8StringCompareFuncPtr)(const char *, const char *);
    UTF8StringCompareFuncPtr StrCompFunc = (flags & DBF_CaselessMatching) ? stricmpUTF8 : strcmp;
    
    // search the type hierarchy
    MethodTable *pMTOrig = methodType.GetMethodTable()->GetCanonicalMethodTable();
    for (MethodTable *pMT = pMTOrig; pMT != NULL; pMT = pMT->GetParentMethodTable())
    {
        MethodTable::MethodIterator it(pMT);
        it.MoveToEnd();
        for (; it.IsValid() && (pMT == pMTOrig || !it.IsVirtual()); it.Prev())
        {
            MethodDesc *pCurMethod = it.GetDeclMethodDesc();
            
            // We can't match generic methods (since no instantiation information has been provided).
            if (pCurMethod->IsGenericMethodDefinition())
                continue;

            if ((pCurMethod != NULL) && (StrCompFunc(szNameStr, pCurMethod->GetName()) == 0))
            {
                // found a matching string, get an associated method desc if needed
                // Use unboxing stubs for instance and virtual methods on value types.
                // If this is a open delegate to an instance method BindToMethod will rebind it to the non-unboxing method.
                // Open delegate
                //   Static: never use unboxing stub
                //     BindToMethodInfo/Name will bind to the non-unboxing stub. BindToMethod will reinforce that.
                //   Instance: We only support binding to an unboxed value type reference here, so we must never use an unboxing stub
                //     BindToMethodInfo/Name will bind to the unboxing stub. BindToMethod will rebind to the non-unboxing stub.
                //   Virtual: trivial (not allowed)
                // Closed delegate
                //   Static: never use unboxing stub
                //     BindToMethodInfo/Name will bind to the non-unboxing stub.
                //   Instance: always use unboxing stub
                //     BindToMethodInfo/Name will bind to the unboxing stub.
                //   Virtual: always use unboxing stub
                //     BindToMethodInfo/Name will bind to the unboxing stub.

                pCurMethod =
                    MethodDesc::FindOrCreateAssociatedMethodDesc(pCurMethod,
                                                                 methodType.GetMethodTable(),
                                                                 (!pCurMethod->IsStatic() && pCurMethod->GetMethodTable()->IsValueType()), 
                                                                 pCurMethod->GetMethodInstantiation(),
                                                                 false /* do not allow code with a shared-code calling convention to be returned */,
                                                                 true /* Ensure that methods on generic interfaces are returned as instantiated method descs */);
                BOOL fIsOpenDelegate;
                if (!COMDelegate::IsMethodDescCompatible((gc.target == NULL) ? TypeHandle() : gc.target->GetTrueTypeHandle(), 
                                                        methodType, 
                                                        pCurMethod, 
                                                        gc.refThis->GetTypeHandle(), 
                                                        pInvokeMeth,
                                                        flags,
                                                        &fIsOpenDelegate))
                {
                    // Signature doesn't match, skip.
                    continue;
                }

                // Found the target that matches the signature and satisfies security transparency rules
                // Initialize the delegate to point to the target method.
                BindToMethod(&gc.refThis,
                             &gc.target,
                             pCurMethod,
                             methodType.GetMethodTable(),
                             fIsOpenDelegate,
                             TRUE);

                pMatchingMethod = pCurMethod;
                goto done;
            }
        }
    }
    done:
        ;
    HELPER_METHOD_FRAME_END();

    FC_RETURN_BOOL(pMatchingMethod != NULL);
}
FCIMPLEND


FCIMPL5(FC_BOOL_RET, COMDelegate::BindToMethodInfo, Object* refThisUNSAFE, Object* targetUNSAFE, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pMethodTypeUNSAFE, int flags)
{
    FCALL_CONTRACT;

    BOOL result = TRUE;

    struct _gc
    {
        DELEGATEREF refThis;
        OBJECTREF refFirstArg;
        REFLECTCLASSBASEREF refMethodType;
        REFLECTMETHODREF refMethod;
    } gc;

    gc.refThis          = (DELEGATEREF) ObjectToOBJECTREF(refThisUNSAFE);
    gc.refFirstArg      = ObjectToOBJECTREF(targetUNSAFE);
    gc.refMethodType    = (REFLECTCLASSBASEREF) ObjectToOBJECTREF(pMethodTypeUNSAFE);
    gc.refMethod        = (REFLECTMETHODREF) ObjectToOBJECTREF(pMethodUNSAFE);

    MethodTable *pMethMT = gc.refMethodType->GetType().GetMethodTable();
    MethodDesc *method = gc.refMethod->GetMethod();

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);

    // Assert to track down VS#458689.
    _ASSERTE(gc.refThis != gc.refFirstArg);

    // A generic method had better be instantiated (we can't dispatch to an uninstantiated one).
    if (method->IsGenericMethodDefinition())
        COMPlusThrow(kArgumentException, W("Arg_DlgtTargMeth"));

    // get the invoke of the delegate
    MethodTable * pDelegateType = gc.refThis->GetMethodTable();
    MethodDesc* pInvokeMeth = COMDelegate::FindDelegateInvokeMethod(pDelegateType);
    _ASSERTE(pInvokeMeth);

    // See the comment in BindToMethodName
    method =
        MethodDesc::FindOrCreateAssociatedMethodDesc(method,
                                                     pMethMT,
                                                     (!method->IsStatic() && pMethMT->IsValueType()), 
                                                     method->GetMethodInstantiation(),
                                                     false /* do not allow code with a shared-code calling convention to be returned */,
                                                     true /* Ensure that methods on generic interfaces are returned as instantiated method descs */);

    BOOL fIsOpenDelegate;
    if (COMDelegate::IsMethodDescCompatible((gc.refFirstArg == NULL) ? TypeHandle() : gc.refFirstArg->GetTrueTypeHandle(), 
                                            TypeHandle(pMethMT), 
                                            method, 
                                            gc.refThis->GetTypeHandle(), 
                                            pInvokeMeth,
                                            flags,
                                            &fIsOpenDelegate))
    {
        // Initialize the delegate to point to the target method.
        BindToMethod(&gc.refThis,
                     &gc.refFirstArg,
                     method,
                     pMethMT,
                     fIsOpenDelegate,
                     !(flags & DBF_SkipSecurityChecks));
    }
    else
        result = FALSE;

    HELPER_METHOD_FRAME_END();

    FC_RETURN_BOOL(result);
}
FCIMPLEND

// This method is called (in the late bound case only) once a target method has been decided on. All the consistency checks
// (signature matching etc.) have been done at this point and the only major reason we could fail now is on security grounds
// (someone trying to create a delegate over a method that's not visible to them for instance). This method will initialize the
// delegate (wrapping it in a secure delegate if necessary). Upon return the delegate should be ready for invocation.
void COMDelegate::BindToMethod(DELEGATEREF   *pRefThis,
                               OBJECTREF     *pRefFirstArg,
                               MethodDesc    *pTargetMethod,
                               MethodTable   *pExactMethodType,
                               BOOL           fIsOpenDelegate,
                               BOOL           fCheckSecurity)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(pRefThis));
        PRECONDITION(CheckPointer(pRefFirstArg, NULL_OK));
        PRECONDITION(CheckPointer(pTargetMethod));
        PRECONDITION(CheckPointer(pExactMethodType));
    }
    CONTRACTL_END;
    
    // We might have to wrap the delegate in a secure delegate depending on the location of the target method. The following local
    // keeps track of the real (i.e. non-secure) delegate whether or not this is required.
    DELEGATEREF refRealDelegate = NULL;
    GCPROTECT_BEGIN(refRealDelegate);

    // Security checks (i.e. whether the creator of the delegate is allowed to access the target method) are the norm. They are only
    // disabled when:
    //   1. this is called by deserialization to recreate an existing delegate instance, where such checks are unwarranted.
    //   2. this is called from DynamicMethod.CreateDelegate which doesn't need access check.
    if (fCheckSecurity)
    {
        MethodTable *pInstanceMT = pExactMethodType;
        bool targetPossiblyRemoted = false;

        if (fIsOpenDelegate)
        {
            _ASSERTE(pRefFirstArg == NULL || *pRefFirstArg == NULL);

        }
        else
        {
            // closed-static is OK and we can check the target in the closed-instance case
            pInstanceMT = (*pRefFirstArg == NULL ? NULL : (*pRefFirstArg)->GetMethodTable());
        }

        RefSecContext sCtx(InvokeUtil::GetInvocationAccessCheckType(targetPossiblyRemoted));

        // Check visibility of the target method. If it's an instance method, we have to pass the type
        // of the instance being accessed which we get from the first argument or from the method itself.
        // The type of the instance is necessary for visibility checks of protected methods.
        InvokeUtil::CheckAccessMethod(&sCtx,
                                      pExactMethodType,
                                      pTargetMethod->IsStatic() ? NULL : pInstanceMT,
                                      pTargetMethod);
    }

    // If we didn't wrap the real delegate in a secure delegate then the real delegate is the one passed in.
    if (refRealDelegate == NULL)
    {
        if (NeedsWrapperDelegate(pTargetMethod))
            refRealDelegate = CreateSecureDelegate(*pRefThis, NULL, pTargetMethod);
        else
            refRealDelegate = *pRefThis;
    }

    pTargetMethod->EnsureActive();

    if (fIsOpenDelegate) 
    {
        _ASSERTE(pRefFirstArg == NULL || *pRefFirstArg == NULL);

        // Open delegates use themselves as the target (which handily allows their shuffle thunks to locate additional data at
        // invocation time).
        refRealDelegate->SetTarget(refRealDelegate);

        // We need to shuffle arguments for open delegates since the first argument on the calling side is not meaningful to the
        // callee.
        MethodTable * pDelegateMT = (*pRefThis)->GetMethodTable();
        DelegateEEClass *pDelegateClass = (DelegateEEClass*)pDelegateMT->GetClass();
        Stub *pShuffleThunk = NULL;

        // Look for a thunk cached on the delegate class first. Note we need a different thunk for instance methods with a
        // hidden return buffer argument because the extra argument switches place with the target when coming from the caller.
        if (!pTargetMethod->IsStatic() && pTargetMethod->HasRetBuffArg() && IsRetBuffPassedAsFirstArg()) 
            pShuffleThunk = pDelegateClass->m_pInstRetBuffCallStub;
        else
            pShuffleThunk = pDelegateClass->m_pStaticCallStub;

        // If we haven't already setup a shuffle thunk go do it now (which will cache the result automatically).
        if (!pShuffleThunk)
            pShuffleThunk = SetupShuffleThunk(pDelegateMT, pTargetMethod);

        // Indicate that the delegate will jump to the shuffle thunk rather than directly to the target method.
        refRealDelegate->SetMethodPtr(pShuffleThunk->GetEntryPoint());
            
        // Use stub dispatch for all virtuals.
        // <TODO> Investigate not using this for non-interface virtuals. </TODO>
        // The virtual dispatch stub doesn't work on unboxed value type objects which don't have MT pointers.
        // Since open instance delegates on value type methods require unboxed objects we cannot use the
        // virtual dispatch stub for them. On the other hand, virtual methods on value types don't need
        // to be dispatched because value types cannot be derived. So we treat them like non-virtual methods.
        if (pTargetMethod->IsVirtual() && !pTargetMethod->GetMethodTable()->IsValueType())
        {
            // Since this is an open delegate over a virtual method we cannot virtualize the call target now. So the shuffle thunk
            // needs to jump to another stub (this time provided by the VirtualStubManager) that will virtualize the call at
            // runtime.
            PCODE pTargetCall = GetVirtualCallStub(pTargetMethod, TypeHandle(pExactMethodType));
            refRealDelegate->SetMethodPtrAux(pTargetCall);
            refRealDelegate->SetInvocationCount((INT_PTR)(void *)pTargetMethod);
        }
        else
        {
            // <TODO> If VSD isn't compiled in this gives the wrong result for virtuals (we need run time virtualization). </TODO>
            // Reflection or the code in BindToMethodName will pass us the unboxing stub for non-static methods on value types. But
            // for open invocation on value type methods the actual reference will be passed so we need the unboxed method desc
            // instead.
            if (pTargetMethod->IsUnboxingStub())
            {
                // We want a MethodDesc which is not an unboxing stub, but is an instantiating stub if needed.
                pTargetMethod = MethodDesc::FindOrCreateAssociatedMethodDesc(
                                                        pTargetMethod,
                                                        pExactMethodType,
                                                        FALSE /* don't want unboxing entry point */,
                                                        pTargetMethod->GetMethodInstantiation(),
                                                        FALSE /* don't want MD that requires inst. arguments */,
                                                        true /* Ensure that methods on generic interfaces are returned as instantiated method descs */);
            }

            // The method must not require any extra hidden instantiation arguments.
            _ASSERTE(!pTargetMethod->RequiresInstArg());

            // Note that it is important to cache pTargetCode in local variable to avoid GC hole.
            // GetMultiCallableAddrOfCode() can trigger GC.
            PCODE pTargetCode = pTargetMethod->GetMultiCallableAddrOfCode();
            refRealDelegate->SetMethodPtrAux(pTargetCode);
        }
    }
    else
    {
        PCODE pTargetCode = NULL;

        // For virtual methods we can (and should) virtualize the call now (so we don't have to insert a thunk to do so at runtime).
        // <TODO>
        // Remove the following if we decide we won't cope with this case on late bound.
        // We can get virtual delegates closed over null through this code path, so be careful to handle that case (no need to
        // virtualize since we're just going to throw NullRefException at invocation time).
        // </TODO>
        if (pTargetMethod->IsVirtual() &&
            *pRefFirstArg != NULL &&
            pTargetMethod->GetMethodTable() != (*pRefFirstArg)->GetMethodTable())
            pTargetCode = pTargetMethod->GetMultiCallableAddrOfVirtualizedCode(pRefFirstArg, pTargetMethod->GetMethodTable());
        else
#ifdef HAS_THISPTR_RETBUF_PRECODE
        if (pTargetMethod->IsStatic() && pTargetMethod->HasRetBuffArg() && IsRetBuffPassedAsFirstArg())
            pTargetCode = pTargetMethod->GetLoaderAllocator()->GetFuncPtrStubs()->GetFuncPtrStub(pTargetMethod, PRECODE_THISPTR_RETBUF);
        else
#endif // HAS_THISPTR_RETBUF_PRECODE
            pTargetCode = pTargetMethod->GetMultiCallableAddrOfCode();
        _ASSERTE(pTargetCode);

        refRealDelegate->SetTarget(*pRefFirstArg);
        refRealDelegate->SetMethodPtr(pTargetCode);
    }

    LoaderAllocator *pLoaderAllocator = pTargetMethod->GetLoaderAllocator();

    if (pLoaderAllocator->IsCollectible())
        refRealDelegate->SetMethodBase(pLoaderAllocator->GetExposedObject());

    GCPROTECT_END();
}

// Marshals a managed method to an unmanaged callback provided the 
// managed method is static and it's parameters require no marshalling.
PCODE COMDelegate::ConvertToCallback(MethodDesc* pMD)
{
    CONTRACTL
    {
        THROWS;
    GC_TRIGGERS;
    INJECT_FAULT(COMPlusThrowOM());
    }
    CONTRACTL_END;

    PCODE pCode = NULL;

    // only static methods are allowed
    if (!pMD->IsStatic())
        COMPlusThrow(kNotSupportedException, W("NotSupported_NonStaticMethod"));

    // no generic methods
    if (pMD->IsGenericMethodDefinition())
        COMPlusThrow(kNotSupportedException, W("NotSupported_GenericMethod"));

    // Arguments 
    if (NDirect::MarshalingRequired(pMD, pMD->GetSig(), pMD->GetModule()))
        COMPlusThrow(kNotSupportedException, W("NotSupported_NonBlittableTypes"));

    // Get UMEntryThunk from the thunk cache.
    UMEntryThunk *pUMEntryThunk = pMD->GetLoaderAllocator()->GetUMEntryThunkCache()->GetUMEntryThunk(pMD);

#if defined(_TARGET_X86_) && !defined(FEATURE_STUBS_AS_IL)

    // System.Runtime.InteropServices.NativeCallableAttribute
    BYTE* pData = NULL;
    LONG cData = 0;
    CorPinvokeMap callConv = (CorPinvokeMap)0;

    HRESULT hr = pMD->GetCustomAttribute(WellKnownAttribute::NativeCallable, (const VOID **)(&pData), (ULONG *)&cData);
    IfFailThrow(hr);

    if (cData > 0)
    {
        CustomAttributeParser ca(pData, cData);
        // NativeCallable has two optional named arguments CallingConvention and EntryPoint.
        CaNamedArg namedArgs[2];
        CaTypeCtor caType(SERIALIZATION_TYPE_STRING);
        // First, the void constructor.
        IfFailThrow(ParseKnownCaArgs(ca, NULL, 0));

        // Now the optional named properties
        namedArgs[0].InitI4FieldEnum("CallingConvention", "System.Runtime.InteropServices.CallingConvention", (ULONG)callConv);
        namedArgs[1].Init("EntryPoint", SERIALIZATION_TYPE_STRING, caType);
        IfFailThrow(ParseKnownCaNamedArgs(ca, namedArgs, lengthof(namedArgs)));

        callConv = (CorPinvokeMap)(namedArgs[0].val.u4 << 8);
        // Let UMThunkMarshalInfo choose the default if calling convension not definied.
        if (namedArgs[0].val.type.tag != SERIALIZATION_TYPE_UNDEFINED)
        {
            UMThunkMarshInfo* pUMThunkMarshalInfo = pUMEntryThunk->GetUMThunkMarshInfo();
            pUMThunkMarshalInfo->SetCallingConvention(callConv);
        }
}
#endif  //_TARGET_X86_ && !FEATURE_STUBS_AS_IL

    pCode = (PCODE)pUMEntryThunk->GetCode();
    _ASSERTE(pCode != NULL);
    return pCode;
}

// Marshals a delegate to a unmanaged callback.
LPVOID COMDelegate::ConvertToCallback(OBJECTREF pDelegateObj)
{
    CONTRACTL
    { 
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        
        INJECT_FAULT(COMPlusThrowOM());
    }
    CONTRACTL_END;

    if (!pDelegateObj) 
        return NULL;

    DELEGATEREF pDelegate = (DELEGATEREF) pDelegateObj;

    PCODE pCode;
    GCPROTECT_BEGIN(pDelegate);

    MethodTable* pMT = pDelegate->GetMethodTable();
    DelegateEEClass* pClass = (DelegateEEClass*)(pMT->GetClass());

    if (pMT->HasInstantiation())
        COMPlusThrowArgumentException(W("delegate"), W("Argument_NeedNonGenericType"));

    // If we are a delegate originally created from an unmanaged function pointer, we will simply return 
    // that function pointer.
    if (DELEGATE_MARKER_UNMANAGEDFPTR == pDelegate->GetInvocationCount())
    {
        pCode = pDelegate->GetMethodPtrAux();
    }
    else
    {
        UMEntryThunk*   pUMEntryThunk   = NULL;
        SyncBlock*      pSyncBlock      = pDelegate->GetSyncBlock();
            
        InteropSyncBlockInfo* pInteropInfo = pSyncBlock->GetInteropInfo();
        
        pUMEntryThunk = (UMEntryThunk*)pInteropInfo->GetUMEntryThunk();

        if (!pUMEntryThunk) 
        {

            UMThunkMarshInfo *pUMThunkMarshInfo = pClass->m_pUMThunkMarshInfo;
            MethodDesc *pInvokeMeth = FindDelegateInvokeMethod(pMT);

            if (!pUMThunkMarshInfo) 
            {
                GCX_PREEMP();

                pUMThunkMarshInfo = new UMThunkMarshInfo();
                pUMThunkMarshInfo->LoadTimeInit(pInvokeMeth);

                g_IBCLogger.LogEEClassCOWTableAccess(pMT);
                EnsureWritablePages(pClass);
                if (FastInterlockCompareExchangePointer(&(pClass->m_pUMThunkMarshInfo),
                                                        pUMThunkMarshInfo,
                                                        NULL ) != NULL)
                {
                    delete pUMThunkMarshInfo;
                    pUMThunkMarshInfo = pClass->m_pUMThunkMarshInfo;
                }
            }

            _ASSERTE(pUMThunkMarshInfo != NULL);
            _ASSERTE(pUMThunkMarshInfo == pClass->m_pUMThunkMarshInfo);

            pUMEntryThunk = UMEntryThunk::CreateUMEntryThunk();
            Holder<UMEntryThunk *, DoNothing, UMEntryThunk::FreeUMEntryThunk> umHolder;
            umHolder.Assign(pUMEntryThunk);

            // multicast. go thru Invoke
            OBJECTHANDLE objhnd = GetAppDomain()->CreateLongWeakHandle(pDelegate);
            _ASSERTE(objhnd != NULL);

            // This target should not ever be used. We are storing it in the thunk for better diagnostics of "call on collected delegate" crashes.
            PCODE pManagedTargetForDiagnostics = (pDelegate->GetMethodPtrAux() != NULL) ? pDelegate->GetMethodPtrAux() : pDelegate->GetMethodPtr();

            // MethodDesc is passed in for profiling to know the method desc of target
            pUMEntryThunk->LoadTimeInit(
                pManagedTargetForDiagnostics,
                objhnd,
                pUMThunkMarshInfo, pInvokeMeth);

            if (!pInteropInfo->SetUMEntryThunk(pUMEntryThunk)) 
            {
                pUMEntryThunk = (UMEntryThunk*)pInteropInfo->GetUMEntryThunk();
            }
            else
            {
                umHolder.SuppressRelease();
                // Insert the delegate handle / UMEntryThunk* into the hash
                LPVOID key = (LPVOID)pUMEntryThunk;

                // Assert that the entry isn't already in the hash.
                _ASSERTE((LPVOID)INVALIDENTRY == COMDelegate::s_pDelegateToFPtrHash->LookupValue((UPTR)key, 0));
                
                {
                    CrstHolder ch(&COMDelegate::s_DelegateToFPtrHashCrst);               
                    COMDelegate::s_pDelegateToFPtrHash->InsertValue((UPTR)key, pUMEntryThunk->GetObjectHandle());
                }
            }
            
            _ASSERTE(pUMEntryThunk != NULL);
            _ASSERTE(pUMEntryThunk == (UMEntryThunk*)pInteropInfo->GetUMEntryThunk()); 

        }
        pCode = (PCODE)pUMEntryThunk->GetCode();
    }
    
    GCPROTECT_END();
    return (LPVOID)pCode;
}

// Marshals an unmanaged callback to Delegate
//static
OBJECTREF COMDelegate::ConvertToDelegate(LPVOID pCallback, MethodTable* pMT)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    if (!pCallback)
    {
        return NULL;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Check if this callback was originally a managed method passed out to unmanaged code.
    //

    UMEntryThunk* pUMEntryThunk = UMEntryThunk::Decode(pCallback);

    // Lookup the callsite in the hash, if found, we can map this call back to its managed function.
    // Otherwise, we'll treat this as an unmanaged callsite.
    // Make sure that the pointer doesn't have the value of 1 which is our hash table deleted item marker.
    LPVOID DelegateHnd = (pUMEntryThunk != NULL) && ((UPTR)pUMEntryThunk != (UPTR)1)
        ? COMDelegate::s_pDelegateToFPtrHash->LookupValue((UPTR)pUMEntryThunk, 0)
        : (LPVOID)INVALIDENTRY;

    if (DelegateHnd != (LPVOID)INVALIDENTRY)
    {
        // Found a managed callsite
        OBJECTREF pDelegate = NULL;
        GCPROTECT_BEGIN(pDelegate);

        pDelegate = ObjectFromHandle((OBJECTHANDLE)DelegateHnd);

        // Make sure we're not trying to sneak into another domain.
        SyncBlock* pSyncBlock = pDelegate->GetSyncBlock();
        _ASSERTE(pSyncBlock);
            
        InteropSyncBlockInfo* pInteropInfo = pSyncBlock->GetInteropInfo();
        _ASSERTE(pInteropInfo);
        
        pUMEntryThunk = (UMEntryThunk*)pInteropInfo->GetUMEntryThunk();
        _ASSERTE(pUMEntryThunk);

        GCPROTECT_END();
        return pDelegate;
    }
    

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // This is an unmanaged callsite. We need to create a new delegate.
    //
    // The delegate's invoke method will point to a call thunk.
    // The call thunk will internally shuffle the args, set up a DelegateTransitionFrame, marshal the args,
    //  call the UM Function located at m_pAuxField, unmarshal the args, and return.
    // Invoke -> CallThunk -> ShuffleThunk -> Frame -> Marshal -> Call AuxField -> UnMarshal
    
    DelegateEEClass*    pClass      = (DelegateEEClass*)pMT->GetClass();
    MethodDesc*         pMD         = FindDelegateInvokeMethod(pMT);

    PREFIX_ASSUME(pClass != NULL);

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Get or create the marshaling stub information
    //

    PCODE pMarshalStub = pClass->m_pMarshalStub;
    if (pMarshalStub == NULL)
    {
        GCX_PREEMP();

        pMarshalStub = GetStubForInteropMethod(pMD, 0, &(pClass->m_pForwardStubMD));

        // Save this new stub on the DelegateEEClass.       
        EnsureWritablePages(dac_cast<PVOID>(&pClass->m_pMarshalStub), sizeof(PCODE));
        InterlockedCompareExchangeT<PCODE>(&pClass->m_pMarshalStub, pMarshalStub, NULL);

        pMarshalStub = pClass->m_pMarshalStub;
    }

    // The IL marshaling stub performs the function of the shuffle thunk - it simply omits 'this' in
    // the call to unmanaged code. The stub recovers the unmanaged target from the delegate instance.

    _ASSERTE(pMarshalStub != NULL);

    ////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    // Wire up the stubs to the new delegate instance.
    // 
    
    LOG((LF_INTEROP, LL_INFO10000, "Created delegate for function pointer: entrypoint: %p\n", pMarshalStub));

    // Create the new delegate
    DELEGATEREF delObj = (DELEGATEREF) pMT->Allocate();

    {
        // delObj is not protected
        GCX_NOTRIGGER();
        
        // Wire up the unmanaged call stub to the delegate.
        delObj->SetTarget(delObj);              // We are the "this" object
        
        // For X86, we save the entry point in the delegate's method pointer and the UM Callsite in the aux pointer.
        delObj->SetMethodPtr(pMarshalStub);
        delObj->SetMethodPtrAux((PCODE)pCallback);

        // Also, mark this delegate as an unmanaged function pointer wrapper.
        delObj->SetInvocationCount(DELEGATE_MARKER_UNMANAGEDFPTR);
    }

#if defined(_TARGET_X86_)
    GCPROTECT_BEGIN(delObj);

    Stub *pInterceptStub = NULL;

    {
        GCX_PREEMP();

        MethodDesc *pStubMD = pClass->m_pForwardStubMD;
        _ASSERTE(pStubMD != NULL && pStubMD->IsILStub());

    }

    if (pInterceptStub != NULL)
    {
        // install the outer-most stub to sync block
        SyncBlock *pSyncBlock = delObj->GetSyncBlock();
            
        InteropSyncBlockInfo *pInteropInfo = pSyncBlock->GetInteropInfo();
        VERIFY(pInteropInfo->SetInterceptStub(pInterceptStub));
    }

    GCPROTECT_END();
#endif // _TARGET_X86_

    return delObj;
}

#ifdef FEATURE_COMINTEROP
// Marshals a WinRT delegate interface pointer to a managed Delegate
//static
OBJECTREF COMDelegate::ConvertWinRTInterfaceToDelegate(IUnknown *pIdentity, MethodTable* pMT)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(pIdentity));
        PRECONDITION(CheckPointer(pMT));
    }
    CONTRACTL_END;

    MethodDesc*         pMD         = FindDelegateInvokeMethod(pMT);

    if (pMD->IsSharedByGenericInstantiations())
    {
        // we need an exact MD to represent the call
        pMD = InstantiatedMethodDesc::FindOrCreateExactClassMethod(pMT, pMD);
    }
    else
    {
        // set up ComPlusCallInfo
        PopulateComPlusCallInfo(pMT);
    }

    ComPlusCallInfo *pComInfo = ComPlusCallInfo::FromMethodDesc(pMD);
    PCODE pMarshalStub = (pComInfo == NULL ? NULL : pComInfo->m_pILStub);

    if (pMarshalStub == NULL)
    {
        GCX_PREEMP();

        DWORD dwStubFlags = NDIRECTSTUB_FL_COM | NDIRECTSTUB_FL_WINRT | NDIRECTSTUB_FL_WINRTDELEGATE;

        pMarshalStub = GetStubForInteropMethod(pMD, dwStubFlags);

        // At this point we must have a non-NULL ComPlusCallInfo
        pComInfo = ComPlusCallInfo::FromMethodDesc(pMD);
        _ASSERTE(pComInfo != NULL);

        // Save this new stub on the ComPlusCallInfo
        InterlockedCompareExchangeT<PCODE>(EnsureWritablePages(&pComInfo->m_pILStub), pMarshalStub, NULL);

        pMarshalStub = pComInfo->m_pILStub;
    }

    _ASSERTE(pMarshalStub != NULL);

    ////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    // Wire up the stub to the new delegate instance.
    // 
    
    LOG((LF_INTEROP, LL_INFO10000, "Created delegate for WinRT interface: pUnk: %p\n", pIdentity));

    // Create the new delegate
    DELEGATEREF delObj = (DELEGATEREF) pMT->Allocate();

    {
        // delObj is not protected
        GCX_NOTRIGGER();
        
        // Wire up the unmanaged call stub to the delegate.
        delObj->SetTarget(delObj);              // We are the "this" object
        
        // We save the entry point in the delegate's method pointer and the identity pUnk in the aux pointer.
        delObj->SetMethodPtr(pMarshalStub);
        delObj->SetMethodPtrAux((PCODE)pIdentity);

        // Also, mark this delegate as an unmanaged function pointer wrapper.
        delObj->SetInvocationCount(DELEGATE_MARKER_UNMANAGEDFPTR);
    }

    return delObj;
}
#endif // FEATURE_COMINTEROP

void COMDelegate::ValidateDelegatePInvoke(MethodDesc* pMD)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;

        PRECONDITION(CheckPointer(pMD));
    }
    CONTRACTL_END;
    
    if (pMD->IsSynchronized())
        COMPlusThrow(kTypeLoadException, IDS_EE_NOSYNCHRONIZED);

    if (pMD->MethodDesc::IsVarArg())
        COMPlusThrow(kNotSupportedException, IDS_EE_VARARG_NOT_SUPPORTED);
}

// static
PCODE COMDelegate::GetStubForILStub(EEImplMethodDesc* pDelegateMD, MethodDesc** ppStubMD, DWORD dwStubFlags)
{
    CONTRACT(PCODE)
    {
        STANDARD_VM_CHECK;

        PRECONDITION(CheckPointer(pDelegateMD));
        POSTCONDITION(RETVAL != NULL);
    }
    CONTRACT_END;

    ValidateDelegatePInvoke(pDelegateMD);

    dwStubFlags |= NDIRECTSTUB_FL_DELEGATE;

    RETURN NDirect::GetStubForILStub(pDelegateMD, ppStubMD, dwStubFlags);
}

#endif // CROSSGEN_COMPILE


// static
MethodDesc* COMDelegate::GetILStubMethodDesc(EEImplMethodDesc* pDelegateMD, DWORD dwStubFlags)
{
    STANDARD_VM_CONTRACT;

    MethodTable *pMT = pDelegateMD->GetMethodTable();

#ifdef FEATURE_COMINTEROP
    if (pMT->IsWinRTDelegate())
    {
        dwStubFlags |= NDIRECTSTUB_FL_COM | NDIRECTSTUB_FL_WINRT | NDIRECTSTUB_FL_WINRTDELEGATE;
    }
    else
#endif // FEATURE_COMINTEROP
    {
        dwStubFlags |= NDIRECTSTUB_FL_DELEGATE;
    }

    PInvokeStaticSigInfo sigInfo(pDelegateMD);
    return NDirect::CreateCLRToNativeILStub(&sigInfo, dwStubFlags, pDelegateMD);
}


#ifndef CROSSGEN_COMPILE

FCIMPL2(FC_BOOL_RET, COMDelegate::CompareUnmanagedFunctionPtrs, Object *refDelegate1UNSAFE, Object *refDelegate2UNSAFE)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(refDelegate1UNSAFE != NULL);
        PRECONDITION(refDelegate2UNSAFE != NULL);
    }
    CONTRACTL_END;

    DELEGATEREF refD1 = (DELEGATEREF) ObjectToOBJECTREF(refDelegate1UNSAFE);
    DELEGATEREF refD2 = (DELEGATEREF) ObjectToOBJECTREF(refDelegate2UNSAFE);
    BOOL ret = FALSE;
   
    // Make sure this is an unmanaged function pointer wrapped in a delegate.
    CONSISTENCY_CHECK(DELEGATE_MARKER_UNMANAGEDFPTR == refD1->GetInvocationCount());
    CONSISTENCY_CHECK(DELEGATE_MARKER_UNMANAGEDFPTR == refD2->GetInvocationCount());

    ret = (refD1->GetMethodPtr() == refD2->GetMethodPtr() &&
           refD1->GetMethodPtrAux() == refD2->GetMethodPtrAux());
    
    FC_RETURN_BOOL(ret);
}
FCIMPLEND


void COMDelegate::RemoveEntryFromFPtrHash(UPTR key)
{
    WRAPPER_NO_CONTRACT;
    
    // Remove this entry from the lookup hash.
    CrstHolder ch(&COMDelegate::s_DelegateToFPtrHashCrst);
    COMDelegate::s_pDelegateToFPtrHash->DeleteValue(key, NULL);
}

FCIMPL2(PCODE, COMDelegate::GetCallStub, Object* refThisUNSAFE, PCODE method)
{
    FCALL_CONTRACT;

    PCODE target = NULL;

    DELEGATEREF refThis = (DELEGATEREF)ObjectToOBJECTREF(refThisUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_RET_1(refThis);
    MethodDesc *pMeth = MethodTable::GetMethodDescForSlotAddress((PCODE)method);
    _ASSERTE(pMeth);
    _ASSERTE(!pMeth->IsStatic() && pMeth->IsVirtual());
    target = GetVirtualCallStub(pMeth, TypeHandle(pMeth->GetMethodTable()));
    refThis->SetInvocationCount((INT_PTR)(void*)pMeth);
    HELPER_METHOD_FRAME_END();
    return target;
}
FCIMPLEND

FCIMPL3(PCODE, COMDelegate::AdjustTarget, Object* refThisUNSAFE, Object* targetUNSAFE, PCODE method)
{
    FCALL_CONTRACT;

    if (targetUNSAFE == NULL)
        FCThrow(kArgumentNullException);
    
    OBJECTREF refThis = ObjectToOBJECTREF(refThisUNSAFE);
    OBJECTREF target  = ObjectToOBJECTREF(targetUNSAFE);

    HELPER_METHOD_FRAME_BEGIN_RET_2(refThis, target);

    _ASSERTE(refThis);
    _ASSERTE(method);

    MethodTable *pMT = target->GetMethodTable();

    MethodDesc *pMeth = Entry2MethodDesc(method, pMT);
    _ASSERTE(pMeth);
    _ASSERTE(!pMeth->IsStatic());

    // close delegates
    MethodTable* pMTTarg = target->GetMethodTable();
    MethodTable* pMTMeth = pMeth->GetMethodTable();
    
    MethodDesc *pCorrectedMethod = pMeth;

    // Use the Unboxing stub for value class methods, since the value
    // class is constructed using the boxed instance.
    if (pCorrectedMethod->GetMethodTable()->IsValueType() && !pCorrectedMethod->IsUnboxingStub())
    {
        // those should have been ruled out at jit time (code:COMDelegate::GetDelegateCtor)
        _ASSERTE((pMTMeth != g_pValueTypeClass) && (pMTMeth != g_pObjectClass));
        pCorrectedMethod->CheckRestore();
        pCorrectedMethod = pMTTarg->GetBoxedEntryPointMD(pCorrectedMethod);
        _ASSERTE(pCorrectedMethod != NULL);
    }
        
    if (pMeth != pCorrectedMethod)
    {
        method = pCorrectedMethod->GetMultiCallableAddrOfCode();
    }
    HELPER_METHOD_FRAME_END();

    return method;
}
FCIMPLEND

#if defined(_MSC_VER) && !defined(FEATURE_PAL)
// VC++ Compiler intrinsic.
extern "C" void * _ReturnAddress(void);
#endif // _MSC_VER && !FEATURE_PAL

// This is the single constructor for all Delegates.  The compiler
//  doesn't provide an implementation of the Delegate constructor.  We
//  provide that implementation through an ECall call to this method.
FCIMPL3(void, COMDelegate::DelegateConstruct, Object* refThisUNSAFE, Object* targetUNSAFE, PCODE method)
{
    FCALL_CONTRACT;
    // If you modify this logic, please update DacDbiInterfaceImpl::GetDelegateType, DacDbiInterfaceImpl::GetDelegateType,
    // DacDbiInterfaceImpl::GetDelegateFunctionData, and DacDbiInterfaceImpl::GetDelegateTargetObject.


    struct _gc
    {
        DELEGATEREF refThis;
        OBJECTREF target;
    } gc;

    gc.refThis = (DELEGATEREF) ObjectToOBJECTREF(refThisUNSAFE);
    gc.target  = (OBJECTREF) targetUNSAFE;

    HELPER_METHOD_FRAME_BEGIN_PROTECT(gc);

    // via reflection you can pass in just about any value for the method.
    // we can do some basic verification up front to prevent EE exceptions.
    if (method == NULL)
        COMPlusThrowArgumentNull(W("method"));

    _ASSERTE(gc.refThis);
    _ASSERTE(method);

    //  programmers could feed garbage data to DelegateConstruct().
    // It's difficult to validate a method code pointer, but at least we'll
    // try to catch the easy garbage.
    _ASSERTE(isMemoryReadable(method, 1));
    
    MethodTable *pMTTarg = NULL;

    if (gc.target != NULL)
    {
        pMTTarg = gc.target->GetMethodTable();
    }

    MethodDesc *pMethOrig = Entry2MethodDesc(method, pMTTarg);
    MethodDesc *pMeth = pMethOrig;

    MethodTable* pDelMT = gc.refThis->GetMethodTable();

    LOG((LF_STUBS, LL_INFO1000, "In DelegateConstruct: for delegate type %s binding to method %s::%s%s, static = %d\n", 
         pDelMT->GetDebugClassName(),
         pMeth->m_pszDebugClassName, pMeth->m_pszDebugMethodName, pMeth->m_pszDebugMethodSignature, pMeth->IsStatic()));

    _ASSERTE(pMeth);

#ifdef _DEBUG
    // Assert that everything is OK...This is not some bogus
    //  address...Very unlikely that the code below would work
    //  for a random address in memory....
    MethodTable* p = pMeth->GetMethodTable();
    _ASSERTE(p);
    _ASSERTE(p->ValidateWithPossibleAV());
#endif // _DEBUG

    if (Nullable::IsNullableType(pMeth->GetMethodTable()))
        COMPlusThrow(kNotSupportedException); 

    DelegateEEClass *pDelCls = (DelegateEEClass*)pDelMT->GetClass();    
    MethodDesc *pDelegateInvoke = COMDelegate::FindDelegateInvokeMethod(pDelMT);

    MetaSig invokeSig(pDelegateInvoke);
    MetaSig methodSig(pMeth);
    UINT invokeArgCount = invokeSig.NumFixedArgs();
    UINT methodArgCount = methodSig.NumFixedArgs();
    BOOL isStatic = pMeth->IsStatic();
    if (!isStatic) 
    {
        methodArgCount++; // count 'this'
    }

    if (NeedsWrapperDelegate(pMeth))
        gc.refThis = CreateSecureDelegate(gc.refThis, NULL, pMeth);

    if (pMeth->GetLoaderAllocator()->IsCollectible())
        gc.refThis->SetMethodBase(pMeth->GetLoaderAllocator()->GetExposedObject());

    // Open delegates.
    if (invokeArgCount == methodArgCount) 
    {
        // set the target
        gc.refThis->SetTarget(gc.refThis);

        // set the shuffle thunk
        Stub *pShuffleThunk = NULL;
        if (!pMeth->IsStatic() && pMeth->HasRetBuffArg() && IsRetBuffPassedAsFirstArg()) 
            pShuffleThunk = pDelCls->m_pInstRetBuffCallStub;
        else
            pShuffleThunk = pDelCls->m_pStaticCallStub;
        if (!pShuffleThunk) 
            pShuffleThunk = SetupShuffleThunk(pDelMT, pMeth);
        
        gc.refThis->SetMethodPtr(pShuffleThunk->GetEntryPoint());

        // set the ptr aux according to what is needed, if virtual need to call make virtual stub dispatch
        if (!pMeth->IsStatic() && pMeth->IsVirtual() && !pMeth->GetMethodTable()->IsValueType())
        {
            PCODE pTargetCall = GetVirtualCallStub(pMeth, TypeHandle(pMeth->GetMethodTable()));
            gc.refThis->SetMethodPtrAux(pTargetCall);
            gc.refThis->SetInvocationCount((INT_PTR)(void *)pMeth);
        }
        else
        {
            gc.refThis->SetMethodPtrAux(method);
        }
    }
    else 
    {
        MethodTable* pMTMeth = pMeth->GetMethodTable();

        if (!pMeth->IsStatic())
        {
            if (pMTTarg)
            {
                g_IBCLogger.LogMethodTableAccess(pMTTarg);

                // Use the Unboxing stub for value class methods, since the value
                // class is constructed using the boxed instance.
                //
                // <NICE> We could get the JIT to recognise all delegate creation sequences and
                // ensure the thing is always an BoxedEntryPointStub anyway </NICE>

                if (pMTMeth->IsValueType() && !pMeth->IsUnboxingStub())
                {
                    // If these are Object/ValueType.ToString().. etc,
                    // don't need an unboxing Stub.

                    if ((pMTMeth != g_pValueTypeClass) 
                        && (pMTMeth != g_pObjectClass))
                    {
                        pMeth->CheckRestore();
                        pMeth = pMTTarg->GetBoxedEntryPointMD(pMeth);
                        _ASSERTE(pMeth != NULL);
                    }
                }
                // Only update the code address if we've decided to go to a different target...
                // <NICE> We should make sure the code address that the JIT provided to us is always the right one anyway,
                // so we don't have to do all this mucking about. </NICE>
                if (pMeth != pMethOrig)
                {
                    method = pMeth->GetMultiCallableAddrOfCode();
                }
            }

            if (gc.target == NULL)
            {
                COMPlusThrow(kArgumentException, W("Arg_DlgtNullInst"));
            }
        }
#ifdef HAS_THISPTR_RETBUF_PRECODE
        else if (pMeth->HasRetBuffArg() && IsRetBuffPassedAsFirstArg())
            method = pMeth->GetLoaderAllocator()->GetFuncPtrStubs()->GetFuncPtrStub(pMeth, PRECODE_THISPTR_RETBUF);
#endif // HAS_THISPTR_RETBUF_PRECODE

        gc.refThis->SetTarget(gc.target);
        gc.refThis->SetMethodPtr((PCODE)(void *)method);
    }
    HELPER_METHOD_FRAME_END();
}
FCIMPLEND

MethodDesc *COMDelegate::GetMethodDesc(OBJECTREF orDelegate)
{        
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    // If you modify this logic, please update DacDbiInterfaceImpl::GetDelegateType, DacDbiInterfaceImpl::GetDelegateType,
    // DacDbiInterfaceImpl::GetDelegateFunctionData, and DacDbiInterfaceImpl::GetDelegateTargetObject.

    MethodDesc *pMethodHandle = NULL;

    DELEGATEREF thisDel = (DELEGATEREF) orDelegate;
    DELEGATEREF innerDel = NULL;

    INT_PTR count = thisDel->GetInvocationCount();
    if (count != 0)
    {
        // this is one of the following:
        // - multicast - _invocationList is Array && _invocationCount != 0
        // - unamanaged ftn ptr - _invocationList == NULL && _invocationCount == -1
        // - secure delegate - _invocationList is Delegate && _invocationCount != NULL
        // - virtual delegate - _invocationList == null && _invocationCount == (target MethodDesc)
        //                    or _invocationList points to a LoaderAllocator/DynamicResolver (inner open virtual delegate of a Secure Delegate)
        // in the secure delegate case we want to unwrap and return the method desc of the inner delegate
        // in the other cases we return the method desc for the invoke
        innerDel = (DELEGATEREF) thisDel->GetInvocationList();
        bool fOpenVirtualDelegate = false;

        if (innerDel != NULL) 
        {
            MethodTable *pMT = innerDel->GetMethodTable();
            if (pMT->IsDelegate()) 
                return GetMethodDesc(innerDel);
            if (!pMT->IsArray())
            {
                // must be a virtual one
                fOpenVirtualDelegate = true;
            }
        }
        else
        {
            if (count != DELEGATE_MARKER_UNMANAGEDFPTR) 
            {
                // must be a virtual one
                fOpenVirtualDelegate = true;
            }
        }

        if (fOpenVirtualDelegate)
            pMethodHandle = (MethodDesc*)thisDel->GetInvocationCount();
        else
            pMethodHandle = FindDelegateInvokeMethod(thisDel->GetMethodTable());
    }
    else
    {
        // Next, check for an open delegate
        PCODE code = thisDel->GetMethodPtrAux();

        if (code != NULL)
        {
            // Note that MethodTable::GetMethodDescForSlotAddress is significantly faster than Entry2MethodDesc
            pMethodHandle = MethodTable::GetMethodDescForSlotAddress(code);
        }
        else
        {
            MethodTable * pMT = NULL;

            // Must be a normal delegate
            code = thisDel->GetMethodPtr();

            OBJECTREF orThis = thisDel->GetTarget();
            if (orThis!=NULL)
            {
                pMT = orThis->GetMethodTable();
            }

            pMethodHandle = Entry2MethodDesc(code, pMT);
        }
    }

    _ASSERTE(pMethodHandle);
    return pMethodHandle;
}
    
OBJECTREF COMDelegate::GetTargetObject(OBJECTREF obj)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    OBJECTREF targetObject = NULL;

    DELEGATEREF thisDel = (DELEGATEREF) obj; 
    OBJECTREF innerDel = NULL;

    if (thisDel->GetInvocationCount() != 0)
    {
        // this is one of the following:
        // - multicast
        // - unmanaged ftn ptr
        // - secure delegate
        // - virtual delegate - _invocationList == null && _invocationCount == (target MethodDesc)
        //                    or _invocationList points to a LoaderAllocator/DynamicResolver (inner open virtual delegate of a Secure Delegate)
        // in the secure delegate case we want to unwrap and return the object of the inner delegate
        innerDel = (DELEGATEREF) thisDel->GetInvocationList();
        if (innerDel != NULL) 
        {
            MethodTable *pMT = innerDel->GetMethodTable();
            if (pMT->IsDelegate()) 
            {
                targetObject = GetTargetObject(innerDel);
            }
        }
    }
    
    if (targetObject == NULL)
        targetObject = thisDel->GetTarget();

    return targetObject;
}

BOOL COMDelegate::IsTrueMulticastDelegate(OBJECTREF delegate)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    BOOL isMulticast = FALSE;

    size_t invocationCount = ((DELEGATEREF)delegate)->GetInvocationCount();
    if (invocationCount)
    {
        OBJECTREF invocationList = ((DELEGATEREF)delegate)->GetInvocationList();
        if (invocationList != NULL)
        {
            MethodTable *pMT = invocationList->GetMethodTable();
            isMulticast = pMT->IsArray();
        }
    }

    return isMulticast;
}

PCODE COMDelegate::TheDelegateInvokeStub()
{                           
    CONTRACT(PCODE)
    {
        STANDARD_VM_CHECK;
        POSTCONDITION(RETVAL != NULL);
    }
    CONTRACT_END;

#if defined(_TARGET_X86_) && !defined(FEATURE_STUBS_AS_IL)
    static PCODE s_pInvokeStub;

    if (s_pInvokeStub == NULL)
    {
        CPUSTUBLINKER sl;
        sl.EmitDelegateInvoke();
        // Process-wide singleton stub that never unloads
        Stub *pCandidate = sl.Link(SystemDomain::GetGlobalLoaderAllocator()->GetStubHeap(), NEWSTUB_FL_MULTICAST);

        if (InterlockedCompareExchangeT<PCODE>(&s_pInvokeStub, pCandidate->GetEntryPoint(), NULL) != NULL)
        {
            // if we are here someone managed to set the stub before us so we release the current
            pCandidate->DecRef();
        }
    }

    RETURN s_pInvokeStub;
#else
    RETURN GetEEFuncEntryPoint(SinglecastDelegateInvokeStub);
#endif // _TARGET_X86_ && !FEATURE_STUBS_AS_IL
}

// Get the cpu stub for a delegate invoke.
PCODE COMDelegate::GetInvokeMethodStub(EEImplMethodDesc* pMD)
{
    CONTRACT(PCODE)
    {
        STANDARD_VM_CHECK;
        POSTCONDITION(RETVAL != NULL);

        INJECT_FAULT(COMPlusThrowOM());
    }
    CONTRACT_END;

    PCODE               ret = NULL;
    MethodTable *       pDelMT = pMD->GetMethodTable();
    DelegateEEClass*    pClass = (DelegateEEClass*) pDelMT->GetClass();

    if (pMD == pClass->GetInvokeMethod())
    {
        // Validate the invoke method, which at the moment just means checking the calling convention

        if (*pMD->GetSig() != (IMAGE_CEE_CS_CALLCONV_HASTHIS | IMAGE_CEE_CS_CALLCONV_DEFAULT))
            COMPlusThrow(kInvalidProgramException);

        ret = COMDelegate::TheDelegateInvokeStub();
    }
    else
    {

        // Since we do not support asynchronous delegates in CoreCLR, we much ensure that it was indeed a async delegate call
        // and not an invalid-delegate-layout condition. 
        // 
        // If the call was indeed for async delegate invocation, we will just throw an exception.
        if ((pMD == pClass->GetBeginInvokeMethod()) || (pMD == pClass->GetEndInvokeMethod()))
        {
            COMPlusThrow(kPlatformNotSupportedException);
        }


        _ASSERTE(!"Bad Delegate layout");
        COMPlusThrow(kInvalidProgramException);
    }

    RETURN ret;
}

FCIMPL1(Object*, COMDelegate::InternalAlloc, ReflectClassBaseObject * pTargetUNSAFE)
{
    FCALL_CONTRACT;

    REFLECTCLASSBASEREF refTarget = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTargetUNSAFE);
    OBJECTREF refRetVal = NULL;
    TypeHandle targetTH = refTarget->GetType();
    HELPER_METHOD_FRAME_BEGIN_RET_1(refTarget);

    _ASSERTE(targetTH.GetMethodTable() != NULL && targetTH.GetMethodTable()->IsDelegate());
    
    refRetVal = targetTH.GetMethodTable()->Allocate();

    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(refRetVal);
}
FCIMPLEND

FCIMPL1(Object*, COMDelegate::InternalAllocLike, Object* pThis)
{
    FCALL_CONTRACT;

    OBJECTREF refRetVal = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_NOPOLL();

    _ASSERTE(pThis->GetMethodTable() != NULL && pThis->GetMethodTable()->IsDelegate());
    
    refRetVal = pThis->GetMethodTable()->AllocateNoChecks();

    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(refRetVal);
}
FCIMPLEND

FCIMPL2(FC_BOOL_RET, COMDelegate::InternalEqualTypes, Object* pThis, Object *pThat)
{
    FCALL_CONTRACT;

    MethodTable *pThisMT = pThis->GetMethodTable();
    MethodTable *pThatMT = pThat->GetMethodTable();

    _ASSERTE(pThisMT != NULL && pThisMT->IsDelegate());
    _ASSERTE(pThatMT != NULL);
    
    BOOL bResult = (pThisMT == pThatMT);

    if (!bResult)
    {
        HELPER_METHOD_FRAME_BEGIN_RET_0();
        bResult = pThisMT->IsEquivalentTo(pThatMT);
        HELPER_METHOD_FRAME_END();
    }

    FC_RETURN_BOOL(bResult);
}
FCIMPLEND

#endif // CROSSGEN_COMPILE

BOOL COMDelegate::NeedsWrapperDelegate(MethodDesc* pTargetMD)
{
    LIMITED_METHOD_CONTRACT;

#ifdef _TARGET_ARM_
    // For arm VSD expects r4 to contain the indirection cell. However r4 is a non-volatile register
    // and its value must be preserved. So we need to erect a frame and store indirection cell in r4 before calling
    // virtual stub dispatch. Erecting frame is already done by secure delegates so the secureDelegate infrastructure
    //  can easliy be used for our purpose.
    // set needsSecureDelegate flag in order to erect a frame. (Secure Delegate stub also loads the right value in r4)
    if (!pTargetMD->IsStatic() && pTargetMD->IsVirtual() && !pTargetMD->GetMethodTable()->IsValueType())
        return TRUE;
#endif

     return FALSE;
}


#ifndef CROSSGEN_COMPILE

// to create a secure delegate wrapper we need:
// - the delegate to forward to         -> _invocationList
// - the creator assembly               -> _methodAuxPtr
// - the delegate invoke MethodDesc     -> _count
// the 2 fields used for invocation will contain:
// - the delegate itself                -> _pORField
// - the secure stub                    -> _pFPField
DELEGATEREF COMDelegate::CreateSecureDelegate(DELEGATEREF delegate, MethodDesc* pCreatorMethod, MethodDesc* pTargetMD)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    MethodTable *pDelegateType = delegate->GetMethodTable();
    MethodDesc *pMD = ((DelegateEEClass*)(pDelegateType->GetClass()))->GetInvokeMethod();
    // allocate the object
    struct _gc {
        DELEGATEREF refSecDel;
        DELEGATEREF innerDel;
    } gc;
    gc.refSecDel = delegate; 
    gc.innerDel = NULL;

    GCPROTECT_BEGIN(gc);

    // set the proper fields
    //

    // Object reference field... 
    gc.refSecDel->SetTarget(gc.refSecDel);       

    // save the secure invoke stub.  GetSecureInvoke() can trigger GC.
    PCODE tmp = GetSecureInvoke(pMD);
    gc.refSecDel->SetMethodPtr(tmp);       
    // save the assembly
    gc.refSecDel->SetMethodPtrAux((PCODE)(void *)pCreatorMethod);
    // save the delegate MethodDesc for the frame
    gc.refSecDel->SetInvocationCount((INT_PTR)pMD);
    
    // save the delegate to forward to
    gc.innerDel = (DELEGATEREF) pDelegateType->Allocate();
    gc.refSecDel->SetInvocationList(gc.innerDel); 

    if (pCreatorMethod != NULL)
    {
        // If the pCreatorMethod is a collectible method, then stash a reference to the 
        // LoaderAllocator/DynamicResolver of the collectible assembly/method in the invocationList 
        // of the inner delegate
        // (The invocationList of the inner delegate is the only field garaunteed to be unused for
        //  other purposes at this time.)
        if (pCreatorMethod->IsLCGMethod())
        {
            OBJECTREF refCollectible = pCreatorMethod->AsDynamicMethodDesc()->GetLCGMethodResolver()->GetManagedResolver();
            gc.innerDel->SetInvocationList(refCollectible);
        }
        else if (pCreatorMethod->GetLoaderAllocator()->IsCollectible())
        {
            OBJECTREF refCollectible = pCreatorMethod->GetLoaderAllocator()->GetExposedObject();
            gc.innerDel->SetInvocationList(refCollectible);
        }
    }

    GCPROTECT_END();

    return gc.innerDel;
}

// InternalGetMethodInfo
// This method will get the MethodInfo for a delegate
FCIMPL1(ReflectMethodObject *, COMDelegate::FindMethodHandle, Object* refThisIn)
{
    FCALL_CONTRACT;

    MethodDesc* pMD = NULL;
    REFLECTMETHODREF pRet = NULL;
    OBJECTREF refThis = ObjectToOBJECTREF(refThisIn);

    HELPER_METHOD_FRAME_BEGIN_RET_1(refThis);
    
    pMD = GetMethodDesc(refThis);
    pRet = pMD->GetStubMethodInfo();
    HELPER_METHOD_FRAME_END();

    return (ReflectMethodObject*)OBJECTREFToObject(pRet);
}
FCIMPLEND

FCIMPL2(FC_BOOL_RET, COMDelegate::InternalEqualMethodHandles, Object *refLeftIn, Object *refRightIn)
{
    FCALL_CONTRACT;

    OBJECTREF refLeft = ObjectToOBJECTREF(refLeftIn);
    OBJECTREF refRight = ObjectToOBJECTREF(refRightIn);
    BOOL fRet = FALSE;

    HELPER_METHOD_FRAME_BEGIN_RET_2(refLeft, refRight);
    
    MethodDesc* pMDLeft = GetMethodDesc(refLeft);
    MethodDesc* pMDRight = GetMethodDesc(refRight);
    fRet = pMDLeft == pMDRight;
    
    HELPER_METHOD_FRAME_END();

    FC_RETURN_BOOL(fRet);
}
FCIMPLEND

FCIMPL1(MethodDesc*, COMDelegate::GetInvokeMethod, Object* refThisIn)
{
    FCALL_CONTRACT;

    OBJECTREF refThis = ObjectToOBJECTREF(refThisIn);
    MethodTable * pDelMT = refThis->GetMethodTable();

    MethodDesc* pMD = ((DelegateEEClass*)(pDelMT->GetClass()))->GetInvokeMethod();
    _ASSERTE(pMD);
    return pMD;
}
FCIMPLEND

#ifdef FEATURE_MULTICASTSTUB_AS_IL
FCIMPL1(PCODE, COMDelegate::GetMulticastInvoke, Object* refThisIn)
{
    FCALL_CONTRACT;

    OBJECTREF refThis = ObjectToOBJECTREF(refThisIn);
    MethodTable *pDelegateMT = refThis->GetMethodTable();

    DelegateEEClass *delegateEEClass = ((DelegateEEClass*)(pDelegateMT->GetClass()));
    Stub *pStub = delegateEEClass->m_pMultiCastInvokeStub;
    if (pStub == NULL)
    {
        MethodDesc* pMD = delegateEEClass->GetInvokeMethod();
    
        HELPER_METHOD_FRAME_BEGIN_RET_0();

        GCX_PREEMP();

        MetaSig sig(pMD);

        BOOL fReturnVal = !sig.IsReturnTypeVoid();
        
        SigTypeContext emptyContext;
        ILStubLinker sl(pMD->GetModule(), pMD->GetSignature(), &emptyContext, pMD, TRUE, TRUE, FALSE);

        ILCodeStream *pCode = sl.NewCodeStream(ILStubLinker::kDispatch);

        DWORD dwInvocationCountNum = pCode->NewLocal(ELEMENT_TYPE_I4);
        DWORD dwLoopCounterNum = pCode->NewLocal(ELEMENT_TYPE_I4);

        DWORD dwReturnValNum = -1;
        if(fReturnVal)
            dwReturnValNum = pCode->NewLocal(sig.GetRetTypeHandleNT());

        ILCodeLabel *nextDelegate = pCode->NewCodeLabel();
        ILCodeLabel *endOfMethod = pCode->NewCodeLabel();

        // Get count of delegates
        pCode->EmitLoadThis();
        pCode->EmitLDFLD(pCode->GetToken(MscorlibBinder::GetField(FIELD__MULTICAST_DELEGATE__INVOCATION_COUNT)));
        pCode->EmitSTLOC(dwInvocationCountNum);

        // initialize counter
        pCode->EmitLDC(0);
        pCode->EmitSTLOC(dwLoopCounterNum);

        //Label_nextDelegate:
        pCode->EmitLabel(nextDelegate);

#ifdef DEBUGGING_SUPPORTED
        pCode->EmitLoadThis();
        pCode->EmitLDLOC(dwLoopCounterNum);
        pCode->EmitCALL(METHOD__STUBHELPERS__MULTICAST_DEBUGGER_TRACE_HELPER, 2, 0);
#endif // DEBUGGING_SUPPORTED

        // compare LoopCounter with InvocationCount. If equal then branch to Label_endOfMethod
        pCode->EmitLDLOC(dwLoopCounterNum);
        pCode->EmitLDLOC(dwInvocationCountNum);
        pCode->EmitBEQ(endOfMethod);

        // Load next delegate from array using LoopCounter as index
        pCode->EmitLoadThis();
        pCode->EmitLDFLD(pCode->GetToken(MscorlibBinder::GetField(FIELD__MULTICAST_DELEGATE__INVOCATION_LIST)));
        pCode->EmitLDLOC(dwLoopCounterNum);
        pCode->EmitLDELEM_REF();

        // Load the arguments
        UINT paramCount = 0;
        while(paramCount < sig.NumFixedArgs())
            pCode->EmitLDARG(paramCount++);

        // call the delegate
        pCode->EmitCALL(pCode->GetToken(pMD), sig.NumFixedArgs(), fReturnVal);

        // Save return value.
        if(fReturnVal)
            pCode->EmitSTLOC(dwReturnValNum);

        // increment counter
        pCode->EmitLDLOC(dwLoopCounterNum);
        pCode->EmitLDC(1);
        pCode->EmitADD();
        pCode->EmitSTLOC(dwLoopCounterNum);

        // branch to next delegate
        pCode->EmitBR(nextDelegate);

        //Label_endOfMethod
        pCode->EmitLabel(endOfMethod);

        // load the return value. return value from the last delegate call is returned
        if(fReturnVal)
            pCode->EmitLDLOC(dwReturnValNum);

        // return
        pCode->EmitRET();

        PCCOR_SIGNATURE pSig;
        DWORD cbSig;

        pMD->GetSig(&pSig,&cbSig);

        MethodDesc* pStubMD = ILStubCache::CreateAndLinkNewILStubMethodDesc(pMD->GetLoaderAllocator(),
                                                               pMD->GetMethodTable(),
                                                               ILSTUB_MULTICASTDELEGATE_INVOKE,
                                                               pMD->GetModule(),
                                                               pSig, cbSig,
                                                               NULL,
                                                               &sl);
    
        pStub = Stub::NewStub(JitILStub(pStubMD));

        g_IBCLogger.LogEEClassCOWTableAccess(pDelegateMT);

        InterlockedCompareExchangeT<PTR_Stub>(EnsureWritablePages(&delegateEEClass->m_pMultiCastInvokeStub), pStub, NULL);

        HELPER_METHOD_FRAME_END();
    }

    return pStub->GetEntryPoint();
}
FCIMPLEND

#else // FEATURE_MULTICASTSTUB_AS_IL

FCIMPL1(PCODE, COMDelegate::GetMulticastInvoke, Object* refThisIn)
{
    FCALL_CONTRACT;

    OBJECTREF refThis = ObjectToOBJECTREF(refThisIn);
    MethodTable *pDelegateMT = refThis->GetMethodTable();

    DelegateEEClass *delegateEEClass = ((DelegateEEClass*)(pDelegateMT->GetClass()));
    Stub *pStub = delegateEEClass->m_pMultiCastInvokeStub;
    if (pStub == NULL)
    {
        MethodDesc* pMD = delegateEEClass->GetInvokeMethod();
    
        HELPER_METHOD_FRAME_BEGIN_RET_0();

        GCX_PREEMP();

        MetaSig sig(pMD);

        UINT_PTR hash = CPUSTUBLINKER::HashMulticastInvoke(&sig);

        pStub = m_pMulticastStubCache->GetStub(hash);
        if (!pStub)
        {
            CPUSTUBLINKER sl;

            LOG((LF_CORDB,LL_INFO10000, "COMD::GIMS making a multicast delegate\n"));

            sl.EmitMulticastInvoke(hash);

            // The cache is process-wide, based on signature.  It never unloads
            Stub *pCandidate = sl.Link(SystemDomain::GetGlobalLoaderAllocator()->GetStubHeap(), NEWSTUB_FL_MULTICAST);

            Stub *pWinner = m_pMulticastStubCache->AttemptToSetStub(hash,pCandidate);
            pCandidate->DecRef();
            if (!pWinner)
                COMPlusThrowOM();

            LOG((LF_CORDB,LL_INFO10000, "Putting a MC stub at 0x%x (code:0x%x)\n",
                pWinner, (BYTE*)pWinner+sizeof(Stub)));

            pStub = pWinner;
        }

        g_IBCLogger.LogEEClassCOWTableAccess(pDelegateMT);

        // we don't need to do an InterlockedCompareExchange here - the m_pMulticastStubCache->AttemptToSetStub
        // will make sure all threads racing here will get the same stub, so they'll all store the same value
        EnsureWritablePages(&delegateEEClass->m_pMultiCastInvokeStub);
        delegateEEClass->m_pMultiCastInvokeStub = pStub;

        HELPER_METHOD_FRAME_END();
    }

    return pStub->GetEntryPoint();
}
FCIMPLEND
#endif // FEATURE_MULTICASTSTUB_AS_IL

#ifdef FEATURE_STUBS_AS_IL
PCODE COMDelegate::GetSecureInvoke(MethodDesc* pMD)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    MethodTable *       pDelegateMT = pMD->GetMethodTable();
    DelegateEEClass*    delegateEEClass = (DelegateEEClass*) pDelegateMT->GetClass();
    Stub *pStub = delegateEEClass->m_pSecureDelegateInvokeStub;

    if (pStub == NULL)
    {

        GCX_PREEMP();

        MetaSig sig(pMD);

        BOOL fReturnVal = !sig.IsReturnTypeVoid();

        SigTypeContext emptyContext;
        ILStubLinker sl(pMD->GetModule(), pMD->GetSignature(), &emptyContext, pMD, TRUE, TRUE, FALSE);

        ILCodeStream *pCode = sl.NewCodeStream(ILStubLinker::kDispatch);

        // Load the "real" delegate
        pCode->EmitLoadThis();
        pCode->EmitLDFLD(pCode->GetToken(MscorlibBinder::GetField(FIELD__MULTICAST_DELEGATE__INVOCATION_LIST)));

        // Load the arguments
        UINT paramCount = 0;
        while(paramCount < sig.NumFixedArgs())
            pCode->EmitLDARG(paramCount++);

        // Call the delegate
        pCode->EmitCALL(pCode->GetToken(pMD), sig.NumFixedArgs(), fReturnVal);

        // Return
        pCode->EmitRET();

        PCCOR_SIGNATURE pSig;
        DWORD cbSig;

        pMD->GetSig(&pSig,&cbSig);

        MethodDesc* pStubMD =
            ILStubCache::CreateAndLinkNewILStubMethodDesc(pMD->GetLoaderAllocator(),
                                                          pMD->GetMethodTable(),
                                                          ILSTUB_SECUREDELEGATE_INVOKE,
                                                          pMD->GetModule(),
                                                          pSig, cbSig,
                                                          NULL,
                                                          &sl);

        pStub = Stub::NewStub(JitILStub(pStubMD));

        g_IBCLogger.LogEEClassCOWTableAccess(pDelegateMT);

        InterlockedCompareExchangeT<PTR_Stub>(EnsureWritablePages(&delegateEEClass->m_pSecureDelegateInvokeStub), pStub, NULL);

    }
    return pStub->GetEntryPoint();
}
#else // FEATURE_STUBS_AS_IL
PCODE COMDelegate::GetSecureInvoke(MethodDesc* pMD)
{
    CONTRACT (PCODE)
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        POSTCONDITION(RETVAL != NULL);
    }
    CONTRACT_END;

    MethodTable *       pDelegateMT = pMD->GetMethodTable();
    DelegateEEClass*    delegateEEClass = (DelegateEEClass*) pDelegateMT->GetClass();

    Stub *pStub = delegateEEClass->m_pSecureDelegateInvokeStub;

    if (pStub == NULL)
    {
        GCX_PREEMP();

        MetaSig sig(pMD);

        UINT_PTR hash = CPUSTUBLINKER::HashMulticastInvoke(&sig);

        pStub = m_pSecureDelegateStubCache->GetStub(hash);
        if (!pStub)
        {
            CPUSTUBLINKER sl;

            LOG((LF_CORDB,LL_INFO10000, "COMD::GIMS making a multicast delegate\n"));
            sl.EmitSecureDelegateInvoke(hash);

            // The cache is process-wide, based on signature.  It never unloads
            Stub *pCandidate = sl.Link(SystemDomain::GetGlobalLoaderAllocator()->GetStubHeap(), NEWSTUB_FL_MULTICAST);

            Stub *pWinner = m_pSecureDelegateStubCache->AttemptToSetStub(hash, pCandidate);
            pCandidate->DecRef();
            if (!pWinner)
                COMPlusThrowOM();

            LOG((LF_CORDB,LL_INFO10000, "Putting a MC stub at 0x%x (code:0x%x)\n",
                pWinner, (BYTE*)pWinner+sizeof(Stub)));

            pStub = pWinner;
        }

        g_IBCLogger.LogEEClassCOWTableAccess(pDelegateMT);
        EnsureWritablePages(&delegateEEClass->m_pSecureDelegateInvokeStub);
        delegateEEClass->m_pSecureDelegateInvokeStub = pStub;
    }
    RETURN (pStub->GetEntryPoint());
}
#endif // FEATURE_STUBS_AS_IL

#endif // CROSSGEN_COMPILE


static BOOL IsLocationAssignable(TypeHandle fromHandle, TypeHandle toHandle, BOOL relaxedMatch, BOOL fromHandleIsBoxed)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;
    // Identical types are obviously compatible.
    if (fromHandle == toHandle)
        return TRUE;

    // Byref parameters can never be allowed relaxed matching since type safety will always be violated in one
    // of the two directions (in or out). Checking one of the types is enough since a byref type is never
    // compatible with a non-byref type.
    if (fromHandle.IsByRef())
        relaxedMatch = FALSE;

    // If we allow relaxed matching then any subtype of toHandle is probably
    // compatible (definitely so if we know fromHandle is coming from a boxed
    // value such as we get from the bound argument in a closed delegate).
    if (relaxedMatch && fromHandle.CanCastTo(toHandle))
    {
        // If the fromHandle isn't boxed then we need to be careful since
        // non-object reference arguments aren't going to be compatible with
        // object reference locations (there's no implicit boxing going to happen
        // for us).
        if (!fromHandleIsBoxed)
        {
            // Check that the "objrefness" of source and destination matches. In
            // reality there are only three objref classes that would have
            // passed the CanCastTo above given a value type source (Object,
            // ValueType and Enum), but why hard code these in when we can be
            // more robust?
            if (fromHandle.IsGenericVariable())
            {
                TypeVarTypeDesc *fromHandleVar = fromHandle.AsGenericVariable();

                // We need to check whether constraints of fromHandle have been loaded, because the
                // CanCastTo operation might have made its decision without enumerating constraints
                // (e.g. when toHandle is System.Object).
                if (!fromHandleVar->ConstraintsLoaded())
                    fromHandleVar->LoadConstraints(CLASS_DEPENDENCIES_LOADED);

                if (toHandle.IsGenericVariable())
                {
                    TypeVarTypeDesc *toHandleVar = toHandle.AsGenericVariable();

                    // Constraints of toHandleVar were not touched by CanCastTo.
                    if (!toHandleVar->ConstraintsLoaded())
                        toHandleVar->LoadConstraints(CLASS_DEPENDENCIES_LOADED);

                    // Both handles are type variables. The following table lists all possible combinations.
                    // 
                    // In brackets are results of IsConstrainedAsObjRef/IsConstrainedAsValueType
                    // 
                    //            To:| [FALSE/FALSE]         | [FALSE/TRUE]          | [TRUE/FALSE]
                    // From:         |                       |                       |
                    // --------------------------------------------------------------------------------------
                    // [FALSE/FALSE] | ERROR                 | NEVER HAPPENS         | ERROR
                    //               | we know nothing       |                       | From may be a VT
                    // --------------------------------------------------------------------------------------
                    // [FALSE/TRUE]  | ERROR                 | OK                    | ERROR
                    //               | To may be an ObjRef   | both are VT           | mismatch
                    // --------------------------------------------------------------------------------------
                    // [TRUE/FALSE]  | OK (C# compat)        | ERROR - mismatch and  | OK
                    //               | (*)                   | no such instantiation | both are ObjRef
                    // --------------------------------------------------------------------------------------

                    if (fromHandleVar->ConstrainedAsObjRef())
                    {
                        // (*) Normally we would need to check whether toHandleVar is also constrained
                        // as ObjRef here and fail if it's not. However, the C# compiler currently
                        // allows the toHandleVar constraint to be omitted and infers it. We have to
                        // follow the same rule to avoid introducing a breaking change. 
                        // 
                        // Example:
                        // class Gen<T, U> where T : class, U
                        // 
                        // For the sake of delegate co(ntra)variance, U is also regarded as being
                        // constrained as ObjRef even though it has no constraints.
                        
                        if (toHandleVar->ConstrainedAsValueType())
                        {
                            // reference type / value type mismatch
                            return FALSE;
                        }
                    }
                    else
                    {
                        if (toHandleVar->ConstrainedAsValueType())
                        {
                            // If toHandleVar is constrained as value type, fromHandle must be as well.
                            _ASSERTE(fromHandleVar->ConstrainedAsValueType());
                        }
                        else
                        {
                            // It was not possible to prove that the variables are both reference types
                            // or both value types.
                            return FALSE;
                        }
                    }
                }
                else
                {
                    // We need toHandle to be an ObjRef and fromHandle to be constrained as ObjRef,
                    // or toHandle to be a value type and fromHandle to be constrained as a value
                    // type (which must be this specific value type actually as value types are sealed).
                    
                    // Constraints of fromHandle must ensure that it will be ObjRef if toHandle is an
                    // ObjRef, and a value type if toHandle is not an ObjRef.
                    if (CorTypeInfo::IsObjRef_NoThrow(toHandle.GetInternalCorElementType()))
                    {
                        if (!fromHandleVar->ConstrainedAsObjRef())
                            return FALSE;
                    }
                    else
                    {
                        if (!fromHandleVar->ConstrainedAsValueType())
                            return FALSE;
                    }
                }
            }
            else
            {
                _ASSERTE(!toHandle.IsGenericVariable());

                // The COR element types have all the information we need.
                if (CorTypeInfo::IsObjRef_NoThrow(fromHandle.GetInternalCorElementType()) !=
                    CorTypeInfo::IsObjRef_NoThrow(toHandle.GetInternalCorElementType()))
                    return FALSE;
            }
        }

        return TRUE;
    }
    else
    {
        // they are not compatible yet enums can go into each other if their underlying element type is the same
        if (toHandle.GetVerifierCorElementType() == fromHandle.GetVerifierCorElementType()
            && (toHandle.IsEnum() || fromHandle.IsEnum()))
            return TRUE;

    }

    return FALSE;
}

MethodDesc* COMDelegate::FindDelegateInvokeMethod(MethodTable *pMT)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    _ASSERTE(pMT->IsDelegate());

    MethodDesc * pMD = ((DelegateEEClass*)pMT->GetClass())->GetInvokeMethod();
    if (pMD == NULL)
        COMPlusThrowNonLocalized(kMissingMethodException, W("Invoke"));
    return pMD;
}

BOOL COMDelegate::IsDelegateInvokeMethod(MethodDesc *pMD)
{
    LIMITED_METHOD_CONTRACT;

    MethodTable *pMT = pMD->GetMethodTable();
    _ASSERTE(pMT->IsDelegate());

    return (pMD == ((DelegateEEClass *)pMT->GetClass())->GetInvokeMethod());
}

BOOL COMDelegate::IsMethodDescCompatible(TypeHandle   thFirstArg,
                                         TypeHandle   thExactMethodType,
                                         MethodDesc  *pTargetMethod,
                                         TypeHandle   thDelegate,
                                         MethodDesc  *pInvokeMethod,
                                         int          flags,
                                         BOOL        *pfIsOpenDelegate)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Handle easy cases first -- if there's a constraint on whether the target method is static or instance we can check that very
    // quickly.
    if (flags & DBF_StaticMethodOnly && !pTargetMethod->IsStatic())
        return FALSE;
    if (flags & DBF_InstanceMethodOnly && pTargetMethod->IsStatic())
        return FALSE;

    // we don't allow you to bind to methods on Nullable<T> because the unboxing stubs don't know how to 
    // handle this case.   
    if (!pTargetMethod->IsStatic() && Nullable::IsNullableType(pTargetMethod->GetMethodTable()))
        return FALSE;

    // Have to be careful with automatically generated array methods (Get, Set, etc.). The TypeHandle here may actually be one
    // of the "special case" MethodTables (such as Object[]) instead of an ArrayTypeDesc and our TypeHandle CanCastTo code can't
    // cope with all the different possible combinations. In general we want to normalize the TypeHandle into an ArrayTypeDesc
    // for these cases.
    if (thExactMethodType.IsArrayType() && !thExactMethodType.IsArray())
    {
        TypeHandle thElement = thExactMethodType.AsMethodTable()->GetApproxArrayElementTypeHandle();
        CorElementType etElement = thExactMethodType.AsMethodTable()->GetInternalCorElementType();
        unsigned uRank = thExactMethodType.AsMethodTable()->GetRank();

        thExactMethodType = ClassLoader::LoadArrayTypeThrowing(thElement,
                                                               etElement,
                                                               uRank,
                                                               ClassLoader::DontLoadTypes);
    }

    // Get signatures for the delegate invoke and target methods.
    MetaSig sigInvoke(pInvokeMethod, thDelegate);
    MetaSig sigTarget(pTargetMethod, thExactMethodType);

    // Check that there is no vararg mismatch.
    if (sigInvoke.IsVarArg() != sigTarget.IsVarArg())
        return FALSE;

    // The relationship between the number of arguments on the delegate invoke and target methods tells us a lot about the type of
    // delegate we'll create (open or closed over the first argument). We're getting the fixed argument counts here, which are all
    // the arguments apart from any implicit 'this' pointers.
    // On the delegate invoke side (the caller) the total number of arguments is the number of fixed args to Invoke plus one if the
    // delegate is closed over an argument (i.e. that argument is provided at delegate creation time).
    // On the target method side (the callee) the total number of arguments is the number of fixed args plus one if the target is an
    // instance method.
    // These two totals should match for any compatible delegate and target method.
    UINT numFixedInvokeArgs = sigInvoke.NumFixedArgs();
    UINT numFixedTargetArgs = sigTarget.NumFixedArgs();
    UINT numTotalTargetArgs = numFixedTargetArgs + (pTargetMethod->IsStatic() ? 0 : 1);

    // Determine whether the match (if it is otherwise compatible) would result in an open or closed delegate or is just completely
    // out of whack.
    BOOL fIsOpenDelegate;
    if (numTotalTargetArgs == numFixedInvokeArgs)
        // All arguments provided by invoke, delegate must be open.
        fIsOpenDelegate = TRUE;
    else if (numTotalTargetArgs == numFixedInvokeArgs + 1)
        // One too few arguments provided by invoke, delegate must be closed.
        fIsOpenDelegate = FALSE;
    else
        // Target method cannot possibly match the invoke method.
        return FALSE;

    // Deal with cases where the caller wants a specific type of delegate.
    if (flags & DBF_OpenDelegateOnly && !fIsOpenDelegate)
        return FALSE;
    if (flags & DBF_ClosedDelegateOnly && fIsOpenDelegate)
        return FALSE;

    // If the target (or first argument) is null, the delegate type would be closed and the caller explicitly doesn't want to allow
    // closing over null then filter that case now.
    if (flags & DBF_NeverCloseOverNull && thFirstArg.IsNull() && !fIsOpenDelegate)
        return FALSE;

    // If, on the other hand, we're looking at an open delegate but the caller has provided a target it's also not a match.
    if (fIsOpenDelegate && !thFirstArg.IsNull())
        return FALSE;

    // **********OLD COMMENT**********
    // We don't allow open delegates over virtual value type methods. That's because we currently have no way to allow the first
    // argument of the invoke method to be specified in such a way that the passed value would be both compatible with the target
    // method and type safe. Virtual methods always have an objref instance (they depend on this for the vtable lookup algorithm) so
    // we can't take a Foo& first argument like other value type methods. We also can't accept System.Object or System.ValueType in
    // the invoke signature since that's not specific enough and would allow type safety violations.
    // Someday we may invent a boxing stub which would take a Foo& passed in box it before dispatch. This is unlikely given that
    // it's a lot of work for an edge case (especially considering that open delegates over value types are always going to be
    // tightly bound to the specific value type). It would also be an odd case where merely calling a delegate would involve an
    // allocation and thus potential failure before you even entered the method.
    // So for now we simply disallow this case.
    // **********OLD COMMENT END**********
    // Actually we allow them now. We will treat them like non-virtual methods.


    // If we get here the basic shape of the signatures match up for either an open or closed delegate. Now we need to verify that
    // those signatures are type compatible. This is complicated somewhat by the matrix of delegate type to target method types
    // (open static vs closed instance etc.). Where we get the first argument type on the invoke side is controlled by open vs
    // closed: closed delegates get the type from the target, open from the first invoke method argument (which is always a fixed
    // arg). Similarly the location of the first argument type on the target method side is based on static vs instance (static from
    // the first fixed arg, instance from the type of the method).

    TypeHandle thFirstInvokeArg;
    TypeHandle thFirstTargetArg;

    // There is one edge case for an open static delegate which takes no arguments. In that case we're nearly done, just compare the
    // return types.
    if (numTotalTargetArgs == 0)
    {
        _ASSERTE(pTargetMethod->IsStatic());
        _ASSERTE(fIsOpenDelegate);

        goto CheckReturnType;
    }
                
    // Invoke side first...
    if (fIsOpenDelegate)
    {
        // No bound arguments, take first type from invoke signature.
        if (sigInvoke.NextArgNormalized() == ELEMENT_TYPE_END)
            return FALSE;
        thFirstInvokeArg = sigInvoke.GetLastTypeHandleThrowing();
    }
    else
        // We have one bound argument and the type of that is what we must compare first.
        thFirstInvokeArg = thFirstArg;

    // And now the first target method argument for comparison...
    if (pTargetMethod->IsStatic())
    {
        // The first argument for a static method is the first fixed arg.
        if (sigTarget.NextArgNormalized() == ELEMENT_TYPE_END)
            return FALSE;
        thFirstTargetArg = sigTarget.GetLastTypeHandleThrowing();

        // Delegates closed over static methods have a further constraint: the first argument of the target must be an object
        // reference type (otherwise the argument shuffling logic could get complicated).
        if (!fIsOpenDelegate)
        {
            if (thFirstTargetArg.IsGenericVariable())
            {
                // If the first argument of the target is a generic variable, it must be constrained to be an object reference.
                TypeVarTypeDesc *varFirstTargetArg = thFirstTargetArg.AsGenericVariable();
                if (!varFirstTargetArg->ConstrainedAsObjRef())
                    return FALSE;
            }
            else
            {
                // Otherwise the code:CorElementType of the argument must be classified as an object reference.
                CorElementType etFirstTargetArg = thFirstTargetArg.GetInternalCorElementType();
                if (!CorTypeInfo::IsObjRef(etFirstTargetArg))
                    return FALSE;
            }
        }
    }
    else
    {
        // The type of the first argument to an instance method is from the method type.
        thFirstTargetArg = thExactMethodType;

        // If the delegate is open and the target method is on a value type or primitive then the first argument of the invoke
        // method must be a reference to that type. So make promote the type we got from the reference to a ref. (We don't need to
        // do this for the closed instance case because there we got the invocation side type from the first arg passed in, i.e.
        // it's had the ref stripped from it implicitly).
        if (fIsOpenDelegate)
        {
            CorElementType etFirstTargetArg = thFirstTargetArg.GetInternalCorElementType();
            if (etFirstTargetArg <= ELEMENT_TYPE_R8 ||
                etFirstTargetArg == ELEMENT_TYPE_VALUETYPE ||
                etFirstTargetArg == ELEMENT_TYPE_I ||
                etFirstTargetArg == ELEMENT_TYPE_U)
                thFirstTargetArg = thFirstTargetArg.MakeByRef();
        }
    }

    // Now we have enough data to compare the first arguments on the invoke and target side. Skip this if we are closed over null
    // (we don't have enough type information for the match but it doesn't matter because the null matches all object reference
    // types, which our first arg must be in this case). We always relax signature matching for the first argument of an instance
    // method, since it's always allowable to call the method on a more derived type. In cases where we're closed over the first
    // argument we know that argument is boxed (because it was passed to us as an object). We provide this information to
    // IsLocationAssignable because it relaxes signature matching for some important cases (e.g. passing a value type to an argument
    // typed as Object).
    if (!thFirstInvokeArg.IsNull())
        if (!IsLocationAssignable(thFirstInvokeArg,
                                  thFirstTargetArg,
                                  !pTargetMethod->IsStatic() || flags & DBF_RelaxedSignature,
                                  !fIsOpenDelegate))
            return FALSE;

        // Loop over the remaining fixed args, the list should be one to one at this point.
    while (TRUE)
    {
        CorElementType etInvokeArg = sigInvoke.NextArgNormalized();
        CorElementType etTargetArg = sigTarget.NextArgNormalized();
        if (etInvokeArg == ELEMENT_TYPE_END || etTargetArg == ELEMENT_TYPE_END)
        {
            // We've reached the end of one signature. We better be at the end of the other or it's not a match.
            if (etInvokeArg != etTargetArg)
                return FALSE;
            break;
        }
        else
        {
            TypeHandle thInvokeArg = sigInvoke.GetLastTypeHandleThrowing();
            TypeHandle thTargetArg = sigTarget.GetLastTypeHandleThrowing();

            if (!IsLocationAssignable(thInvokeArg, thTargetArg, flags & DBF_RelaxedSignature, FALSE))
                return FALSE;
        }
    } 

 CheckReturnType:

    // Almost there, just compare the return types (remember that the assignment is in the other direction here, from callee to
    // caller, so switch the order of the arguments to IsLocationAssignable).
    // If we ever relax this we have to think about how to unbox this arg in the Nullable<T> case also.  
    if (!IsLocationAssignable(sigTarget.GetRetTypeHandleThrowing(),
                              sigInvoke.GetRetTypeHandleThrowing(),
                              flags & DBF_RelaxedSignature,
                              FALSE))
        return FALSE;

    // We must have a match.
    if (pfIsOpenDelegate)
        *pfIsOpenDelegate = fIsOpenDelegate;
    return TRUE;
}

MethodDesc* COMDelegate::GetDelegateCtor(TypeHandle delegateType, MethodDesc *pTargetMethod, DelegateCtorArgs *pCtorData)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    MethodDesc *pRealCtor = NULL;

    MethodTable *pDelMT = delegateType.AsMethodTable();
    DelegateEEClass *pDelCls = (DelegateEEClass*)(pDelMT->GetClass());

    MethodDesc *pDelegateInvoke = COMDelegate::FindDelegateInvokeMethod(pDelMT);

    MetaSig invokeSig(pDelegateInvoke);
    MetaSig methodSig(pTargetMethod);
    UINT invokeArgCount = invokeSig.NumFixedArgs();
    UINT methodArgCount = methodSig.NumFixedArgs();
    BOOL isStatic = pTargetMethod->IsStatic();
    LoaderAllocator *pTargetMethodLoaderAllocator = pTargetMethod->GetLoaderAllocator();
    BOOL isCollectible = pTargetMethodLoaderAllocator->IsCollectible();
    // A method that may be instantiated over a collectible type, and is static will require a delegate
    // that has the _methodBase field filled in with the LoaderAllocator of the collectible assembly
    // associated with the instantiation.
    BOOL fMaybeCollectibleAndStatic = FALSE;
   
    // Do not allow static methods with [NativeCallableAttribute] to be a delegate target.
    // A native callable method is special and allowing it to be delegate target will destabilize the runtime.
    if (pTargetMethod->HasNativeCallableAttribute())
    {
        COMPlusThrow(kNotSupportedException, W("NotSupported_NativeCallableTarget"));
    }

    if (isStatic)
    {
        // When this method is called and the method being considered is shared, we typically
        // are passed a Wrapper method for the explicit canonical instantiation. It would be illegal
        // to actually call that method, but the jit uses it as a proxy for the real instantiated
        // method, so we can't make the methoddesc apis that report that it is the shared methoddesc
        // report that it is. Hence, this collection of checks that will detect if the methoddesc
        // being used is a normal method desc to shared code, or if it is a wrapped methoddesc
        // corresponding to the actually uncallable instantiation over __Canon.
        if (pTargetMethod->GetMethodTable()->IsSharedByGenericInstantiations())
        {
            fMaybeCollectibleAndStatic = TRUE;
        }
        else if (pTargetMethod->IsSharedByGenericMethodInstantiations())
        {
            fMaybeCollectibleAndStatic = TRUE;
        }
        else if (pTargetMethod->HasMethodInstantiation())
        {
            Instantiation instantiation = pTargetMethod->GetMethodInstantiation();
            for (DWORD iParam = 0; iParam < instantiation.GetNumArgs(); iParam++)
            {
                if (instantiation[iParam] == g_pCanonMethodTableClass)
                {
                    fMaybeCollectibleAndStatic = TRUE;
                    break;
                }
            }
        }
    }

    // If this might be collectible and is static, then we will go down the slow path. Implementing
    // yet another fast path would require a methoddesc parameter, but hopefully isn't necessary.
    if (fMaybeCollectibleAndStatic)
        return NULL;

    if (!isStatic) 
        methodArgCount++; // count 'this'
    MethodDesc *pCallerMethod = (MethodDesc*)pCtorData->pMethod;

    if (NeedsWrapperDelegate(pTargetMethod))
    {
        // If we need a wrapper even it is not a secure delegate, go through slow path
        return NULL;
    }
    
    // Force the slow path for nullable so that we can give the user an error in case were the verifier is not run. 
    MethodTable* pMT = pTargetMethod->GetMethodTable();
    if (!pTargetMethod->IsStatic() && Nullable::IsNullableType(pMT))
        return NULL;

#ifdef FEATURE_COMINTEROP
    // We'll always force classic COM types to go down the slow path for security checks.
    if ((pMT->IsComObjectType() && !pMT->IsWinRTObjectType()) ||
        (pMT->IsComImport() && !pMT->IsProjectedFromWinRT()))
    {
        return NULL;
    }
#endif

    // DELEGATE KINDS TABLE
    //
    //                                  _target         _methodPtr              _methodPtrAux       _invocationList     _invocationCount
    //
    // 1- Instance closed               'this' ptr      target method           null                null                0
    // 2- Instance open non-virt        delegate        shuffle thunk           target method       null                0
    // 3- Instance open virtual         delegate        Virtual-stub dispatch   method id           null                0
    // 4- Static closed                 first arg       target method           null                null                0
    // 5- Static closed (special sig)   delegate        specialSig thunk        target method       first arg           0
    // 6- Static opened                 delegate        shuffle thunk           target method       null                0
    // 7- Secure                        delegate        call thunk              MethodDesc (frame)  target delegate     creator assembly 
    //
    // Delegate invoke arg count == target method arg count - 2, 3, 6
    // Delegate invoke arg count == 1 + target method arg count - 1, 4, 5
    //
    // 1, 4     - MulticastDelegate.ctor1 (simply assign _target and _methodPtr)
    // 5        - MulticastDelegate.ctor2 (see table, takes 3 args)
    // 2, 6     - MulticastDelegate.ctor3 (take shuffle thunk)
    // 3        - MulticastDelegate.ctor4 (take shuffle thunk, retrieve MethodDesc) ???
    //
    // 7 - Needs special handling
    //
    // With collectible types, we need to fill the _methodBase field in with a value that represents the LoaderAllocator of the target method
    // if the delegate is not a closed instance delegate.
    //
    // There are two techniques that will work for this. 
    // One is to simply use the slow path. We use this for unusual constructs. It is rather slow. 
    //  We will use this for the secure variants
    //
    // Another is to pass a gchandle to the delegate ctor. This is fastest, but only works if we can predict the gc handle at this time. 
    //  We will use this for the non secure variants
    //
    // If you modify this logic, please update DacDbiInterfaceImpl::GetDelegateType, DacDbiInterfaceImpl::GetDelegateType,
    // DacDbiInterfaceImpl::GetDelegateFunctionData, and DacDbiInterfaceImpl::GetDelegateTargetObject.


    if (invokeArgCount == methodArgCount) 
    {
        // case 2, 3, 6
        //@TODO:NEWVTWORK: Might need changing.
        // The virtual dispatch stub doesn't work on unboxed value type objects which don't have MT pointers.
        // Since open virtual (delegate kind 3) delegates on value type methods require unboxed objects we cannot use the
        // virtual dispatch stub for them. On the other hand, virtual methods on value types don't need
        // to be dispatched because value types cannot be derived. So we treat them like non-virtual methods (delegate kind 2).
        if (!isStatic && pTargetMethod->IsVirtual() && !pTargetMethod->GetMethodTable()->IsValueType()) 
        {
            // case 3
            if (isCollectible)
                pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_COLLECTIBLE_VIRTUAL_DISPATCH);
            else
                pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_VIRTUAL_DISPATCH);
        }
        else
        {
            // case 2, 6
            if (isCollectible)
                pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_COLLECTIBLE_OPENED);
            else
                pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_OPENED);
        }
        Stub *pShuffleThunk = NULL;
        if (!pTargetMethod->IsStatic() && pTargetMethod->HasRetBuffArg() && IsRetBuffPassedAsFirstArg()) 
            pShuffleThunk = pDelCls->m_pInstRetBuffCallStub;
        else
            pShuffleThunk = pDelCls->m_pStaticCallStub;

        if (!pShuffleThunk) 
            pShuffleThunk = SetupShuffleThunk(pDelMT, pTargetMethod);
        pCtorData->pArg3 = (void*)pShuffleThunk->GetEntryPoint();
        if (isCollectible)
        {
            pCtorData->pArg4 = pTargetMethodLoaderAllocator->GetLoaderAllocatorObjectHandle();
        }
    }
    else 
    {
        // case 1, 4, 5
        //TODO: need to differentiate on 5
        _ASSERTE(invokeArgCount + 1 == methodArgCount);

#ifdef HAS_THISPTR_RETBUF_PRECODE
        // Force closed delegates over static methods with return buffer to go via 
        // the slow path to create ThisPtrRetBufPrecode
        if (isStatic && pTargetMethod->HasRetBuffArg() && IsRetBuffPassedAsFirstArg())
            return NULL;
#endif

        // under the conditions below the delegate ctor needs to perform some heavy operation
        // to get the unboxing stub
        BOOL needsRuntimeInfo = !pTargetMethod->IsStatic() && 
                    pTargetMethod->GetMethodTable()->IsValueType() && !pTargetMethod->IsUnboxingStub();

        if (needsRuntimeInfo)
            pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_RT_CLOSED);
        else
        {
            if (!isStatic) 
                pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_CLOSED);
            else
            {
                if (isCollectible)
                {
                    pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_COLLECTIBLE_CLOSED_STATIC);
                    pCtorData->pArg3 = pTargetMethodLoaderAllocator->GetLoaderAllocatorObjectHandle();
                }
                else
                {
                    pRealCtor = MscorlibBinder::GetMethod(METHOD__MULTICAST_DELEGATE__CTOR_CLOSED_STATIC);
                }
            }
        }
    }

    return pRealCtor;
}


/*@GENERICSVER: new (works for generics too)
    Does a static validation of parameters passed into a delegate constructor.


    For "new Delegate(obj.method)" where method is statically typed as "C::m" and
    the static type of obj is D (some subclass of C)...

    Params:
    instHnd : Static type of the instance, from which pFtn is obtained. Ignored if pFtn 
             is static (i.e. D)
    ftnParentHnd: Parent of the MethodDesc, pFtn, used to create the delegate (i.e. type C)
    pFtn  : (possibly shared) MethodDesc of the function pointer used to create the delegate (i.e. C::m)
    pDlgt : The delegate type (i.e. Delegate)
    module: The module scoping methodMemberRef and delegateConstructorMemberRef
    methodMemberRef: the MemberRef, MemberDef or MemberSpec of the target method  (i.e. a mdToken for C::m)
    delegateConstructorMemberRef: the MemberRef, MemberDef or MemberSpec of the delegate constructor (i.e. a mdToken for Delegate::.ctor)

    Validates the following conditions:
    1.  If the function (pFtn) is not static, pInst should be equal to the type where 
        pFtn is defined or pInst should be a parent of pFtn's type.
    2.  The signature of the function should be compatible with the signature
        of the Invoke method of the delegate type.
        The signature is retrieved from module, methodMemberRef and delegateConstructorMemberRef

    NB: Although some of these arguments are redundant, we pass them in to avoid looking up 
        information that should already be available.
        Instead of comparing type handles modulo some context, the method directly compares metadata to avoid 
        loading classes referenced in the method signatures (hence the need for the module and member refs).
        Also, because this method works directly on metadata, without allowing any additional instantiation of the
        free type variables in the signature of the method or delegate constructor, this code
        will *only* verify a constructor application at the typical (ie. formal) instantiation.
*/
/* static */
BOOL COMDelegate::ValidateCtor(TypeHandle instHnd,
                               TypeHandle ftnParentHnd, 
                               MethodDesc *pFtn, 
                               TypeHandle dlgtHnd,
                               BOOL       *pfIsOpenDelegate)

{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;

        PRECONDITION(CheckPointer(pFtn));
        PRECONDITION(!dlgtHnd.IsNull());
        PRECONDITION(!ftnParentHnd.IsNull());

        INJECT_FAULT(COMPlusThrowOM()); // from MetaSig::CompareElementType
    }
    CONTRACTL_END;

    DelegateEEClass *pdlgEEClass = (DelegateEEClass*)dlgtHnd.AsMethodTable()->GetClass();
    PREFIX_ASSUME(pdlgEEClass != NULL);
    MethodDesc *pDlgtInvoke = pdlgEEClass->GetInvokeMethod();
    if (pDlgtInvoke == NULL)
        return FALSE;
    return IsMethodDescCompatible(instHnd, ftnParentHnd, pFtn, dlgtHnd, pDlgtInvoke, DBF_RelaxedSignature, pfIsOpenDelegate);
}

BOOL COMDelegate::IsSecureDelegate(DELEGATEREF dRef)
{
    CONTRACTL
    {
        MODE_ANY;
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;
    DELEGATEREF innerDel = NULL;
    if (dRef->GetInvocationCount() != 0)
    {
        innerDel = (DELEGATEREF) dRef->GetInvocationList();
        if (innerDel != NULL && innerDel->GetMethodTable()->IsDelegate())
        {
            // We have a secure delegate
            return TRUE;
        }
    }        
    return FALSE;
}

#endif // !DACCESS_COMPILE


// Decides if pcls derives from Delegate.
BOOL COMDelegate::IsDelegate(MethodTable *pMT)
{
    WRAPPER_NO_CONTRACT;
    return (pMT == g_pDelegateClass) || (pMT == g_pMulticastDelegateClass) || pMT->IsDelegate();
}


#if !defined(DACCESS_COMPILE) && !defined(CROSSGEN_COMPILE)


// Helper to construct an UnhandledExceptionEventArgs.  This may fail for out-of-memory or
// other reasons.  Currently, we fall back on passing a NULL eventargs to the event sink.
// Another possibility is to have two shared immutable instances (one for isTerminating and
// another for !isTerminating).  These must be immutable because we perform no synchronization
// around delivery of unhandled exceptions.  They occur in a free-threaded manner on various
// threads.
//
// It doesn't add much value to communicate the isTerminating flag under these unusual
// conditions.
static void TryConstructUnhandledExceptionArgs(OBJECTREF *pThrowable,
                                               BOOL       isTerminating,
                                               OBJECTREF *pOutEventArgs)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    _ASSERTE(pThrowable    != NULL && IsProtectedByGCFrame(pThrowable));
    _ASSERTE(pOutEventArgs != NULL && IsProtectedByGCFrame(pOutEventArgs));
    _ASSERTE(*pOutEventArgs == NULL);

    EX_TRY
    {
        MethodTable *pMT = MscorlibBinder::GetClass(CLASS__UNHANDLED_EVENTARGS);
        *pOutEventArgs = AllocateObject(pMT);

        MethodDescCallSite ctor(METHOD__UNHANDLED_EVENTARGS__CTOR, pOutEventArgs);

        ARG_SLOT args[] =
        {
            ObjToArgSlot(*pOutEventArgs),
            ObjToArgSlot(*pThrowable),
            BoolToArgSlot(isTerminating)
        };

        ctor.Call(args);
    }
    EX_CATCH
    {
        *pOutEventArgs = NULL;      // arguably better than half-constructed object

        // It's not even worth asserting, because these aren't our bugs.
    }
    EX_END_CATCH(SwallowAllExceptions)
}


// Helper to dispatch a single unhandled exception notification, swallowing anything
// that goes wrong.
static void InvokeUnhandledSwallowing(OBJECTREF *pDelegate,
                                      OBJECTREF *pDomain,
                                      OBJECTREF *pEventArgs)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    _ASSERTE(pDelegate  != NULL && IsProtectedByGCFrame(pDelegate));
    _ASSERTE(pDomain    != NULL && IsProtectedByGCFrame(pDomain));
    _ASSERTE(pEventArgs == NULL || IsProtectedByGCFrame(pEventArgs));

    EX_TRY
    {
#if defined(FEATURE_CORRUPTING_EXCEPTIONS)
        BOOL fCanMethodHandleException = g_pConfig->LegacyCorruptedStateExceptionsPolicy();
        if (!fCanMethodHandleException)
        {
            // CSE policy has not been overridden - proceed with our checks.
            //
            // Notifications for CSE are only delivered if the delegate target follows CSE rules.
            // So, get the corruption severity of the active exception that has gone unhandled.
            //
            // By Default, assume that the active exception is not corrupting.
            CorruptionSeverity severity = NotCorrupting;
            Thread *pCurThread = GetThread();
            _ASSERTE(pCurThread != NULL);
            ThreadExceptionState *pExState = pCurThread->GetExceptionState();
            if (pExState->IsExceptionInProgress())
            {
                // If an exception is active, it implies we have a tracker for it.
                // Hence, get the corruption severity from the active exception tracker.
                severity = pExState->GetCurrentExceptionTracker()->GetCorruptionSeverity();
                _ASSERTE(severity > NotSet);
            }

            // Notifications are delivered based upon corruption severity of the exception
            fCanMethodHandleException = ExceptionNotifications::CanDelegateBeInvokedForException(pDelegate, severity);
            if (!fCanMethodHandleException)
            {
                LOG((LF_EH, LL_INFO100, "InvokeUnhandledSwallowing: ADUEN Delegate cannot be invoked for corruption severity %d\n",
                    severity));
            }
        }

        if (fCanMethodHandleException)
#endif // defined(FEATURE_CORRUPTING_EXCEPTIONS)
        {
            // We've already exercised the prestub on this delegate's COMDelegate::GetMethodDesc,
            // as part of wiring up a reliable event sink. Deliver the notification.
            ExceptionNotifications::DeliverExceptionNotification(UnhandledExceptionHandler, pDelegate, pDomain, pEventArgs);
        }
    }
    EX_CATCH
    {
        // It's not even worth asserting, because these aren't our bugs.
    }
    EX_END_CATCH(SwallowAllExceptions)
}

// The unhandled exception event is a little easier to distribute, because
// we simply swallow any failures and proceed to the next event sink.
void DistributeUnhandledExceptionReliably(OBJECTREF *pDelegate,
                                          OBJECTREF *pDomain,
                                          OBJECTREF *pThrowable,
                                          BOOL       isTerminating)
{
    CONTRACTL
    {
        NOTHROW;  
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    _ASSERTE(pDelegate  != NULL && IsProtectedByGCFrame(pDelegate));
    _ASSERTE(pDomain    != NULL && IsProtectedByGCFrame(pDomain));
    _ASSERTE(pThrowable != NULL && IsProtectedByGCFrame(pThrowable));

    EX_TRY
    {
        struct _gc
        {
            PTRARRAYREF Array;
            OBJECTREF   InnerDelegate;
            OBJECTREF   EventArgs;
        } gc;
        ZeroMemory(&gc, sizeof(gc));

        GCPROTECT_BEGIN(gc);

        // Try to construct an UnhandledExceptionEventArgs out of pThrowable & isTerminating.
        // If unsuccessful, the best we can do is pass NULL.
        TryConstructUnhandledExceptionArgs(pThrowable, isTerminating, &gc.EventArgs);

        gc.Array = (PTRARRAYREF) ((DELEGATEREF)(*pDelegate))->GetInvocationList();
        if (gc.Array == NULL || !gc.Array->GetMethodTable()->IsArray())
        {
            InvokeUnhandledSwallowing(pDelegate, pDomain, &gc.EventArgs);
        }
        else
        {
            // The _invocationCount could be less than the array size, if we are sharing
            // immutable arrays cleverly.
            INT_PTR invocationCount = ((DELEGATEREF)(*pDelegate))->GetInvocationCount();
            
            _ASSERTE(FitsInU4(invocationCount));
            DWORD cnt = static_cast<DWORD>(invocationCount);

            _ASSERTE(cnt <= gc.Array->GetNumComponents());

            for (DWORD i=0; i<cnt; i++)
            {
                gc.InnerDelegate = gc.Array->m_Array[i];
                InvokeUnhandledSwallowing(&gc.InnerDelegate, pDomain, &gc.EventArgs);
            }
        }
        GCPROTECT_END();
    }
    EX_CATCH
    {
        // It's not even worth asserting, because these aren't our bugs.
    }
    EX_END_CATCH(SwallowAllExceptions)
}

#endif // !DACCESS_COMPILE && !CROSSGEN_COMPILE