/
mini-generic-sharing.c
4258 lines (3626 loc) · 128 KB
/
mini-generic-sharing.c
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
/**
* \file
* Support functions for generic sharing.
*
* Author:
* Mark Probst (mark.probst@gmail.com)
*
* Copyright 2007-2011 Novell, Inc (http://www.novell.com)
* Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include <config.h>
#include <mono/metadata/class.h>
#include <mono/metadata/method-builder.h>
#include <mono/metadata/method-builder-ilgen.h>
#include <mono/metadata/method-builder-ilgen-internals.h>
#include <mono/metadata/reflection-internals.h>
#include <mono/metadata/abi-details.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/atomic.h>
#include <mono/utils/unlocked.h>
#include "mini.h"
#include "aot-runtime.h"
#include "mini-runtime.h"
#define ALLOW_PARTIAL_SHARING TRUE
//#define ALLOW_PARTIAL_SHARING FALSE
#if 0
#define DEBUG(...) __VA_ARGS__
#else
#define DEBUG(...)
#endif
static void
mono_class_unregister_image_generic_subclasses (MonoImage *image, gpointer user_data);
/* Counters */
static gint32 rgctx_template_num_allocated;
static gint32 rgctx_template_bytes_allocated;
static gint32 rgctx_oti_num_allocated;
static gint32 rgctx_oti_bytes_allocated;
static gint32 rgctx_oti_num_markers;
static gint32 rgctx_oti_num_data;
static gint32 rgctx_max_slot_number;
static gint32 rgctx_num_allocated;
static gint32 rgctx_num_arrays_allocated;
static gint32 rgctx_bytes_allocated;
static gint32 mrgctx_num_arrays_allocated;
static gint32 mrgctx_bytes_allocated;
static gint32 gsharedvt_num_trampolines;
#define gshared_lock() mono_os_mutex_lock (&gshared_mutex)
#define gshared_unlock() mono_os_mutex_unlock (&gshared_mutex)
static mono_mutex_t gshared_mutex;
static gboolean partial_supported = FALSE;
static inline gboolean
partial_sharing_supported (void)
{
if (!ALLOW_PARTIAL_SHARING)
return FALSE;
/* Enable this when AOT compiling or running in full-aot mode */
if (mono_aot_only)
return TRUE;
if (partial_supported)
return TRUE;
return FALSE;
}
static int
type_check_context_used (MonoType *type, gboolean recursive)
{
switch (mono_type_get_type (type)) {
case MONO_TYPE_VAR:
return MONO_GENERIC_CONTEXT_USED_CLASS;
case MONO_TYPE_MVAR:
return MONO_GENERIC_CONTEXT_USED_METHOD;
case MONO_TYPE_SZARRAY:
return mono_class_check_context_used (mono_type_get_class (type));
case MONO_TYPE_ARRAY:
return mono_class_check_context_used (mono_type_get_array_type (type)->eklass);
case MONO_TYPE_CLASS:
if (recursive)
return mono_class_check_context_used (mono_type_get_class (type));
else
return 0;
case MONO_TYPE_GENERICINST:
if (recursive) {
MonoGenericClass *gclass = type->data.generic_class;
g_assert (mono_class_is_gtd (gclass->container_class));
return mono_generic_context_check_used (&gclass->context);
} else {
return 0;
}
default:
return 0;
}
}
static int
inst_check_context_used (MonoGenericInst *inst)
{
int context_used = 0;
int i;
if (!inst)
return 0;
for (i = 0; i < inst->type_argc; ++i)
context_used |= type_check_context_used (inst->type_argv [i], TRUE);
return context_used;
}
/*
* mono_generic_context_check_used:
* @context: a generic context
*
* Checks whether the context uses a type variable. Returns an int
* with the bit MONO_GENERIC_CONTEXT_USED_CLASS set to reflect whether
* the context's class instantiation uses type variables.
*/
int
mono_generic_context_check_used (MonoGenericContext *context)
{
int context_used = 0;
context_used |= inst_check_context_used (context->class_inst);
context_used |= inst_check_context_used (context->method_inst);
return context_used;
}
/*
* mono_class_check_context_used:
* @class: a class
*
* Checks whether the class's generic context uses a type variable.
* Returns an int with the bit MONO_GENERIC_CONTEXT_USED_CLASS set to
* reflect whether the context's class instantiation uses type
* variables.
*/
int
mono_class_check_context_used (MonoClass *klass)
{
int context_used = 0;
context_used |= type_check_context_used (m_class_get_this_arg (klass), FALSE);
context_used |= type_check_context_used (m_class_get_byval_arg (klass), FALSE);
if (mono_class_is_ginst (klass))
context_used |= mono_generic_context_check_used (&mono_class_get_generic_class (klass)->context);
else if (mono_class_is_gtd (klass))
context_used |= mono_generic_context_check_used (&mono_class_get_generic_container (klass)->context);
return context_used;
}
/*
* LOCKING: loader lock
*/
static MonoRuntimeGenericContextInfoTemplate*
get_info_templates (MonoRuntimeGenericContextTemplate *template_, int type_argc)
{
g_assert (type_argc >= 0);
if (type_argc == 0)
return template_->infos;
return (MonoRuntimeGenericContextInfoTemplate *)g_slist_nth_data (template_->method_templates, type_argc - 1);
}
/*
* LOCKING: loader lock
*/
static void
set_info_templates (MonoImage *image, MonoRuntimeGenericContextTemplate *template_, int type_argc,
MonoRuntimeGenericContextInfoTemplate *oti)
{
g_assert (type_argc >= 0);
if (type_argc == 0)
template_->infos = oti;
else {
int length = g_slist_length (template_->method_templates);
GSList *list;
/* FIXME: quadratic! */
while (length < type_argc) {
template_->method_templates = mono_g_slist_append_image (image, template_->method_templates, NULL);
length++;
}
list = g_slist_nth (template_->method_templates, type_argc - 1);
g_assert (list);
list->data = oti;
}
}
/*
* LOCKING: loader lock
*/
static int
template_get_max_argc (MonoRuntimeGenericContextTemplate *template_)
{
return g_slist_length (template_->method_templates);
}
/*
* LOCKING: loader lock
*/
static MonoRuntimeGenericContextInfoTemplate*
rgctx_template_get_other_slot (MonoRuntimeGenericContextTemplate *template_, int type_argc, int slot)
{
int i;
MonoRuntimeGenericContextInfoTemplate *oti;
g_assert (slot >= 0);
for (oti = get_info_templates (template_, type_argc), i = 0; i < slot; oti = oti->next, ++i) {
if (!oti)
return NULL;
}
return oti;
}
/*
* LOCKING: loader lock
*/
static int
rgctx_template_num_infos (MonoRuntimeGenericContextTemplate *template_, int type_argc)
{
MonoRuntimeGenericContextInfoTemplate *oti;
int i;
for (i = 0, oti = get_info_templates (template_, type_argc); oti; ++i, oti = oti->next)
;
return i;
}
/* Maps from uninstantiated generic classes to GList's of
* uninstantiated generic classes whose parent is the key class or an
* instance of the key class.
*
* LOCKING: loader lock
*/
static GHashTable *generic_subclass_hash;
/*
* LOCKING: templates lock
*/
static void
class_set_rgctx_template (MonoClass *klass, MonoRuntimeGenericContextTemplate *rgctx_template)
{
if (!m_class_get_image (klass)->rgctx_template_hash)
m_class_get_image (klass)->rgctx_template_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
g_hash_table_insert (m_class_get_image (klass)->rgctx_template_hash, klass, rgctx_template);
}
/*
* LOCKING: loader lock
*/
static MonoRuntimeGenericContextTemplate*
class_lookup_rgctx_template (MonoClass *klass)
{
MonoRuntimeGenericContextTemplate *template_;
if (!m_class_get_image (klass)->rgctx_template_hash)
return NULL;
template_ = (MonoRuntimeGenericContextTemplate *)g_hash_table_lookup (m_class_get_image (klass)->rgctx_template_hash, klass);
return template_;
}
/*
* LOCKING: loader lock
*/
static void
register_generic_subclass (MonoClass *klass)
{
MonoClass *parent = m_class_get_parent (klass);
MonoClass *subclass;
MonoRuntimeGenericContextTemplate *rgctx_template = class_lookup_rgctx_template (klass);
g_assert (rgctx_template);
if (mono_class_is_ginst (parent))
parent = mono_class_get_generic_class (parent)->container_class;
if (!generic_subclass_hash)
generic_subclass_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
subclass = (MonoClass *)g_hash_table_lookup (generic_subclass_hash, parent);
rgctx_template->next_subclass = subclass;
g_hash_table_insert (generic_subclass_hash, parent, klass);
}
static void
move_subclasses_not_in_image_foreach_func (MonoClass *klass, MonoClass *subclass, MonoImage *image)
{
MonoClass *new_list;
if (m_class_get_image (klass) == image) {
/* The parent class itself is in the image, so all the
subclasses must be in the image, too. If not,
we're removing an image containing a class which
still has a subclass in another image. */
while (subclass) {
g_assert (m_class_get_image (subclass) == image);
subclass = class_lookup_rgctx_template (subclass)->next_subclass;
}
return;
}
new_list = NULL;
while (subclass) {
MonoRuntimeGenericContextTemplate *subclass_template = class_lookup_rgctx_template (subclass);
MonoClass *next = subclass_template->next_subclass;
if (m_class_get_image (subclass) != image) {
subclass_template->next_subclass = new_list;
new_list = subclass;
}
subclass = next;
}
if (new_list)
g_hash_table_insert (generic_subclass_hash, klass, new_list);
}
/*
* mono_class_unregister_image_generic_subclasses:
* @image: an image
*
* Removes all classes of the image from the generic subclass hash.
* Must be called when an image is unloaded.
*/
static void
mono_class_unregister_image_generic_subclasses (MonoImage *image, gpointer user_data)
{
GHashTable *old_hash;
//g_print ("unregistering image %s\n", image->name);
if (!generic_subclass_hash)
return;
mono_loader_lock ();
old_hash = generic_subclass_hash;
generic_subclass_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
g_hash_table_foreach (old_hash, (GHFunc)move_subclasses_not_in_image_foreach_func, image);
mono_loader_unlock ();
g_hash_table_destroy (old_hash);
}
static MonoRuntimeGenericContextTemplate*
alloc_template (MonoClass *klass)
{
gint32 size = sizeof (MonoRuntimeGenericContextTemplate);
mono_atomic_inc_i32 (&rgctx_template_num_allocated);
mono_atomic_fetch_add_i32 (&rgctx_template_bytes_allocated, size);
return (MonoRuntimeGenericContextTemplate *)mono_image_alloc0 (m_class_get_image (klass), size);
}
/* LOCKING: Takes the loader lock */
static MonoRuntimeGenericContextInfoTemplate*
alloc_oti (MonoImage *image)
{
gint32 size = sizeof (MonoRuntimeGenericContextInfoTemplate);
mono_atomic_inc_i32 (&rgctx_oti_num_allocated);
mono_atomic_fetch_add_i32 (&rgctx_oti_bytes_allocated, size);
return (MonoRuntimeGenericContextInfoTemplate *)mono_image_alloc0 (image, size);
}
#define MONO_RGCTX_SLOT_USED_MARKER ((gpointer)mono_get_object_type ())
/*
* Return true if this info type has the notion of identify.
*
* Some info types expect that each insert results in a new slot been assigned.
*/
static int
info_has_identity (MonoRgctxInfoType info_type)
{
return info_type != MONO_RGCTX_INFO_CAST_CACHE;
}
/*
* LOCKING: loader lock
*/
#if defined(HOST_ANDROID) && defined(TARGET_ARM)
/* work around for HW bug on Nexus9 when running on armv7 */
#ifdef __clang__
static __attribute__ ((optnone)) void
#else
/* gcc */
static __attribute__ ((optimize("O0"))) void
#endif
#else
static void
#endif
rgctx_template_set_slot (MonoImage *image, MonoRuntimeGenericContextTemplate *template_, int type_argc,
int slot, gpointer data, MonoRgctxInfoType info_type)
{
int i;
MonoRuntimeGenericContextInfoTemplate *list = get_info_templates (template_, type_argc);
MonoRuntimeGenericContextInfoTemplate **oti = &list;
g_assert (slot >= 0);
g_assert (data);
i = 0;
while (i <= slot) {
if (i > 0)
oti = &(*oti)->next;
if (!*oti)
*oti = alloc_oti (image);
++i;
}
g_assert (!(*oti)->data);
(*oti)->data = data;
(*oti)->info_type = info_type;
set_info_templates (image, template_, type_argc, list);
/* interlocked by loader lock (by definition) */
if (data == MONO_RGCTX_SLOT_USED_MARKER)
UnlockedIncrement (&rgctx_oti_num_markers);
else
UnlockedIncrement (&rgctx_oti_num_data);
}
/*
* mono_method_get_declaring_generic_method:
* @method: an inflated method
*
* Returns an inflated method's declaring method.
*/
MonoMethod*
mono_method_get_declaring_generic_method (MonoMethod *method)
{
MonoMethodInflated *inflated;
g_assert (method->is_inflated);
inflated = (MonoMethodInflated*)method;
return inflated->declaring;
}
/*
* mono_class_get_method_generic:
* @klass: a class
* @method: a method
* @error: set on error
*
* Given a class and a generic method, which has to be of an
* instantiation of the same class that klass is an instantiation of,
* returns the corresponding method in klass. Example:
*
* klass is Gen<string>
* method is Gen<object>.work<int>
*
* returns: Gen<string>.work<int>
*
* On error sets @error and returns NULL.
*/
MonoMethod*
mono_class_get_method_generic (MonoClass *klass, MonoMethod *method, MonoError *error)
{
MonoMethod *declaring, *m;
int i;
if (method->is_inflated)
declaring = mono_method_get_declaring_generic_method (method);
else
declaring = method;
m = NULL;
if (mono_class_is_ginst (klass)) {
m = mono_class_get_inflated_method (klass, declaring, error);
return_val_if_nok (error, NULL);
}
if (!m) {
mono_class_setup_methods (klass);
if (mono_class_has_failure (klass))
return NULL;
int mcount = mono_class_get_method_count (klass);
MonoMethod **klass_methods = m_class_get_methods (klass);
for (i = 0; i < mcount; ++i) {
m = klass_methods [i];
if (m == declaring)
break;
if (m->is_inflated && mono_method_get_declaring_generic_method (m) == declaring)
break;
}
if (i >= mcount)
return NULL;
}
if (method != declaring) {
MonoGenericContext context;
context.class_inst = NULL;
context.method_inst = mono_method_get_context (method)->method_inst;
m = mono_class_inflate_generic_method_checked (m, &context, error);
return_val_if_nok (error, NULL);
}
return m;
}
static gpointer
inflate_info (MonoRuntimeGenericContextInfoTemplate *oti, MonoGenericContext *context, MonoClass *klass, gboolean temporary)
{
gpointer data = oti->data;
MonoRgctxInfoType info_type = oti->info_type;
ERROR_DECL (error);
g_assert (data);
if (data == MONO_RGCTX_SLOT_USED_MARKER)
return MONO_RGCTX_SLOT_USED_MARKER;
switch (info_type)
{
case MONO_RGCTX_INFO_STATIC_DATA:
case MONO_RGCTX_INFO_KLASS:
case MONO_RGCTX_INFO_ELEMENT_KLASS:
case MONO_RGCTX_INFO_VTABLE:
case MONO_RGCTX_INFO_TYPE:
case MONO_RGCTX_INFO_REFLECTION_TYPE:
case MONO_RGCTX_INFO_CAST_CACHE:
case MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE:
case MONO_RGCTX_INFO_VALUE_SIZE:
case MONO_RGCTX_INFO_CLASS_BOX_TYPE:
case MONO_RGCTX_INFO_CLASS_IS_REF_OR_CONTAINS_REFS:
case MONO_RGCTX_INFO_MEMCPY:
case MONO_RGCTX_INFO_BZERO:
case MONO_RGCTX_INFO_LOCAL_OFFSET:
case MONO_RGCTX_INFO_NULLABLE_CLASS_BOX:
case MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX: {
gpointer result = mono_class_inflate_generic_type_with_mempool (temporary ? NULL : m_class_get_image (klass),
(MonoType *)data, context, error);
mono_error_assert_msg_ok (error, "Could not inflate generic type"); /* FIXME proper error handling */
return result;
}
case MONO_RGCTX_INFO_METHOD:
case MONO_RGCTX_INFO_GENERIC_METHOD_CODE:
case MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER:
case MONO_RGCTX_INFO_METHOD_RGCTX:
case MONO_RGCTX_INFO_METHOD_CONTEXT:
case MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK:
case MONO_RGCTX_INFO_METHOD_DELEGATE_CODE: {
MonoMethod *method = (MonoMethod *)data;
MonoMethod *inflated_method;
MonoType *inflated_type = mono_class_inflate_generic_type_checked (m_class_get_byval_arg (method->klass), context, error);
mono_error_assert_ok (error); /* FIXME don't swallow the error */
MonoClass *inflated_class = mono_class_from_mono_type (inflated_type);
mono_metadata_free_type (inflated_type);
mono_class_init (inflated_class);
g_assert (!method->wrapper_type);
if (m_class_get_byval_arg (inflated_class)->type == MONO_TYPE_ARRAY ||
m_class_get_byval_arg (inflated_class)->type == MONO_TYPE_SZARRAY) {
inflated_method = mono_method_search_in_array_class (inflated_class,
method->name, method->signature);
} else {
ERROR_DECL (error);
inflated_method = mono_class_inflate_generic_method_checked (method, context, error);
g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
}
mono_class_init (inflated_method->klass);
g_assert (inflated_method->klass == inflated_class);
return inflated_method;
}
case MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO: {
MonoGSharedVtMethodInfo *oinfo = (MonoGSharedVtMethodInfo *)data;
MonoGSharedVtMethodInfo *res;
MonoDomain *domain = mono_domain_get ();
int i;
res = (MonoGSharedVtMethodInfo *)mono_domain_alloc0 (domain, sizeof (MonoGSharedVtMethodInfo));
/*
res->nlocals = info->nlocals;
res->locals_types = g_new0 (MonoType*, info->nlocals);
for (i = 0; i < info->nlocals; ++i)
res->locals_types [i] = mono_class_inflate_generic_type (info->locals_types [i], context);
*/
res->num_entries = oinfo->num_entries;
res->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_domain_alloc0 (domain, sizeof (MonoRuntimeGenericContextInfoTemplate) * oinfo->num_entries);
for (i = 0; i < oinfo->num_entries; ++i) {
MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
MonoRuntimeGenericContextInfoTemplate *template_ = &res->entries [i];
memcpy (template_, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
template_->data = inflate_info (template_, context, klass, FALSE);
}
return res;
}
case MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE:
case MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT: {
MonoJumpInfoGSharedVtCall *info = (MonoJumpInfoGSharedVtCall *)data;
MonoMethod *method = info->method;
MonoMethod *inflated_method;
MonoType *inflated_type = mono_class_inflate_generic_type_checked (m_class_get_byval_arg (method->klass), context, error);
mono_error_assert_ok (error); /* FIXME don't swallow the error */
WrapperInfo *winfo = NULL;
MonoClass *inflated_class = mono_class_from_mono_type (inflated_type);
MonoJumpInfoGSharedVtCall *res;
MonoDomain *domain = mono_domain_get ();
res = (MonoJumpInfoGSharedVtCall *)mono_domain_alloc0 (domain, sizeof (MonoJumpInfoGSharedVtCall));
/* Keep the original signature */
res->sig = info->sig;
mono_metadata_free_type (inflated_type);
mono_class_init (inflated_class);
if (method->wrapper_type) {
winfo = mono_marshal_get_wrapper_info (method);
g_assert (winfo);
g_assert (winfo->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER);
method = winfo->d.synchronized_inner.method;
}
if (m_class_get_byval_arg (inflated_class)->type == MONO_TYPE_ARRAY ||
m_class_get_byval_arg (inflated_class)->type == MONO_TYPE_SZARRAY) {
inflated_method = mono_method_search_in_array_class (inflated_class,
method->name, method->signature);
} else {
ERROR_DECL (error);
inflated_method = mono_class_inflate_generic_method_checked (method, context, error);
g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
}
mono_class_init (inflated_method->klass);
g_assert (inflated_method->klass == inflated_class);
if (winfo) {
g_assert (winfo->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER);
inflated_method = mono_marshal_get_synchronized_inner_wrapper (inflated_method);
}
res->method = inflated_method;
return res;
}
case MONO_RGCTX_INFO_CLASS_FIELD:
case MONO_RGCTX_INFO_FIELD_OFFSET: {
ERROR_DECL (error);
MonoClassField *field = (MonoClassField *)data;
MonoType *inflated_type = mono_class_inflate_generic_type_checked (m_class_get_byval_arg (field->parent), context, error);
mono_error_assert_ok (error); /* FIXME don't swallow the error */
MonoClass *inflated_class = mono_class_from_mono_type (inflated_type);
int i = field - m_class_get_fields (field->parent);
gpointer dummy = NULL;
mono_metadata_free_type (inflated_type);
mono_class_get_fields_internal (inflated_class, &dummy);
g_assert (m_class_get_fields (inflated_class));
return &m_class_get_fields (inflated_class) [i];
}
case MONO_RGCTX_INFO_SIG_GSHAREDVT_IN_TRAMPOLINE_CALLI:
case MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI: {
MonoMethodSignature *sig = (MonoMethodSignature *)data;
MonoMethodSignature *isig;
ERROR_DECL (error);
isig = mono_inflate_generic_signature (sig, context, error);
g_assert (mono_error_ok (error));
return isig;
}
case MONO_RGCTX_INFO_VIRT_METHOD_CODE:
case MONO_RGCTX_INFO_VIRT_METHOD_BOX_TYPE: {
MonoJumpInfoVirtMethod *info = (MonoJumpInfoVirtMethod *)data;
MonoJumpInfoVirtMethod *res;
MonoType *t;
MonoDomain *domain = mono_domain_get ();
ERROR_DECL (error);
// FIXME: Temporary
res = (MonoJumpInfoVirtMethod *)mono_domain_alloc0 (domain, sizeof (MonoJumpInfoVirtMethod));
t = mono_class_inflate_generic_type_checked (m_class_get_byval_arg (info->klass), context, error);
mono_error_assert_ok (error); /* FIXME don't swallow the error */
res->klass = mono_class_from_mono_type (t);
mono_metadata_free_type (t);
res->method = mono_class_inflate_generic_method_checked (info->method, context, error);
g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
return res;
}
case MONO_RGCTX_INFO_DELEGATE_TRAMP_INFO: {
ERROR_DECL (error);
MonoDelegateClassMethodPair *dele_info = (MonoDelegateClassMethodPair*)data;
MonoDomain *domain = mono_domain_get ();
MonoType *t = mono_class_inflate_generic_type_checked (m_class_get_byval_arg (dele_info->klass), context, error);
mono_error_assert_msg_ok (error, "Could not inflate generic type"); /* FIXME proper error handling */
MonoClass *klass = mono_class_from_mono_type (t);
mono_metadata_free_type (t);
MonoMethod *method = mono_class_inflate_generic_method_checked (dele_info->method, context, error);
mono_error_assert_msg_ok (error, "Could not inflate generic method"); /* FIXME proper error handling */
// FIXME: Temporary
MonoDelegateClassMethodPair *res = (MonoDelegateClassMethodPair *)mono_domain_alloc0 (domain, sizeof (MonoDelegateClassMethodPair));
res->is_virtual = dele_info->is_virtual;
res->method = method;
res->klass = klass;
return res;
}
default:
g_assert_not_reached ();
}
/* Not reached, quiet compiler */
return NULL;
}
static void
free_inflated_info (MonoRgctxInfoType info_type, gpointer info)
{
if (!info)
return;
switch (info_type) {
case MONO_RGCTX_INFO_STATIC_DATA:
case MONO_RGCTX_INFO_KLASS:
case MONO_RGCTX_INFO_ELEMENT_KLASS:
case MONO_RGCTX_INFO_VTABLE:
case MONO_RGCTX_INFO_TYPE:
case MONO_RGCTX_INFO_REFLECTION_TYPE:
case MONO_RGCTX_INFO_CAST_CACHE:
mono_metadata_free_type ((MonoType *)info);
break;
default:
break;
}
}
static MonoRuntimeGenericContextInfoTemplate
class_get_rgctx_template_oti (MonoClass *klass, int type_argc, guint32 slot, gboolean temporary, gboolean shared, gboolean *do_free);
static MonoClass*
class_uninstantiated (MonoClass *klass)
{
if (mono_class_is_ginst (klass))
return mono_class_get_generic_class (klass)->container_class;
return klass;
}
/*
* get_shared_class:
*
* Return the class used to store information when using generic sharing.
*/
static MonoClass*
get_shared_class (MonoClass *klass)
{
return class_uninstantiated (klass);
}
/*
* mono_class_get_runtime_generic_context_template:
* @class: a class
*
* Looks up or constructs, if necessary, the runtime generic context template for class.
* The template is the same for all instantiations of a class.
*/
static MonoRuntimeGenericContextTemplate*
mono_class_get_runtime_generic_context_template (MonoClass *klass)
{
MonoRuntimeGenericContextTemplate *parent_template, *template_;
guint32 i;
klass = get_shared_class (klass);
mono_loader_lock ();
template_ = class_lookup_rgctx_template (klass);
mono_loader_unlock ();
if (template_)
return template_;
//g_assert (get_shared_class (class) == class);
template_ = alloc_template (klass);
mono_loader_lock ();
if (m_class_get_parent (klass)) {
guint32 num_entries;
int max_argc, type_argc;
parent_template = mono_class_get_runtime_generic_context_template (m_class_get_parent (klass));
max_argc = template_get_max_argc (parent_template);
for (type_argc = 0; type_argc <= max_argc; ++type_argc) {
num_entries = rgctx_template_num_infos (parent_template, type_argc);
/* FIXME: quadratic! */
for (i = 0; i < num_entries; ++i) {
MonoRuntimeGenericContextInfoTemplate oti;
oti = class_get_rgctx_template_oti (m_class_get_parent (klass), type_argc, i, FALSE, FALSE, NULL);
if (oti.data && oti.data != MONO_RGCTX_SLOT_USED_MARKER) {
rgctx_template_set_slot (m_class_get_image (klass), template_, type_argc, i,
oti.data, oti.info_type);
}
}
}
}
if (class_lookup_rgctx_template (klass)) {
/* some other thread already set the template */
template_ = class_lookup_rgctx_template (klass);
} else {
class_set_rgctx_template (klass, template_);
if (m_class_get_parent (klass))
register_generic_subclass (klass);
}
mono_loader_unlock ();
return template_;
}
/*
* class_get_rgctx_template_oti:
*
* Return the info template of CLASS numbered TYPE_ARGC/SLOT.
* temporary signifies whether the inflated info (oti.data) will be
* used temporarily, in which case it might be heap-allocated, or
* permanently, in which case it will be mempool-allocated. If
* temporary is set then *do_free will return whether the returned
* data must be freed.
*
* LOCKING: loader lock
*/
static MonoRuntimeGenericContextInfoTemplate
class_get_rgctx_template_oti (MonoClass *klass, int type_argc, guint32 slot, gboolean temporary, gboolean shared, gboolean *do_free)
{
g_assert ((temporary && do_free) || (!temporary && !do_free));
DEBUG (printf ("get slot: %s %d\n", mono_type_full_name (m_class_get_byval_arg (class)), slot));
if (mono_class_is_ginst (klass) && !shared) {
MonoRuntimeGenericContextInfoTemplate oti;
gboolean tmp_do_free;
oti = class_get_rgctx_template_oti (mono_class_get_generic_class (klass)->container_class,
type_argc, slot, TRUE, FALSE, &tmp_do_free);
if (oti.data) {
gpointer info = oti.data;
oti.data = inflate_info (&oti, &mono_class_get_generic_class (klass)->context, klass, temporary);
if (tmp_do_free)
free_inflated_info (oti.info_type, info);
}
if (temporary)
*do_free = TRUE;
return oti;
} else {
MonoRuntimeGenericContextTemplate *template_;
MonoRuntimeGenericContextInfoTemplate *oti;
template_ = mono_class_get_runtime_generic_context_template (klass);
oti = rgctx_template_get_other_slot (template_, type_argc, slot);
g_assert (oti);
if (temporary)
*do_free = FALSE;
return *oti;
}
}
static MonoMethod*
get_method_nofail (MonoClass *klass, const char *method_name, int num_params, int flags)
{
MonoMethod *method;
ERROR_DECL (error);
method = mono_class_get_method_from_name_checked (klass, method_name, num_params, flags, error);
mono_error_assert_ok (error);
g_assertf (method, "Could not lookup method %s in %s", method_name, m_class_get_name (klass));
return method;
}
static gpointer
class_type_info (MonoDomain *domain, MonoClass *klass, MonoRgctxInfoType info_type, MonoError *error)
{
error_init (error);
switch (info_type) {
case MONO_RGCTX_INFO_STATIC_DATA: {
MonoVTable *vtable = mono_class_vtable_checked (domain, klass, error);
return_val_if_nok (error, NULL);
return mono_vtable_get_static_field_data (vtable);
}
case MONO_RGCTX_INFO_KLASS:
return klass;
case MONO_RGCTX_INFO_ELEMENT_KLASS:
return m_class_get_element_class (klass);
case MONO_RGCTX_INFO_VTABLE: {
MonoVTable *vtable = mono_class_vtable_checked (domain, klass, error);
return_val_if_nok (error, NULL);
return vtable;
}
case MONO_RGCTX_INFO_CAST_CACHE: {
/*First slot is the cache itself, the second the vtable.*/
gpointer **cache_data = (gpointer **)mono_domain_alloc0 (domain, sizeof (gpointer) * 2);
cache_data [1] = (gpointer *)klass;
return cache_data;
}
case MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE:
return GUINT_TO_POINTER (mono_class_array_element_size (klass));
case MONO_RGCTX_INFO_VALUE_SIZE:
if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (klass)))
return GUINT_TO_POINTER (sizeof (gpointer));
else
return GUINT_TO_POINTER (mono_class_value_size (klass, NULL));
case MONO_RGCTX_INFO_CLASS_BOX_TYPE:
if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (klass)))
return GUINT_TO_POINTER (MONO_GSHAREDVT_BOX_TYPE_REF);
else if (mono_class_is_nullable (klass))
return GUINT_TO_POINTER (MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
else
return GUINT_TO_POINTER (MONO_GSHAREDVT_BOX_TYPE_VTYPE);
case MONO_RGCTX_INFO_CLASS_IS_REF_OR_CONTAINS_REFS:
mono_class_init (klass);
/* Can't return 0 */
if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (klass)) || m_class_has_references (klass))
return GUINT_TO_POINTER (2);
else
return GUINT_TO_POINTER (1);
case MONO_RGCTX_INFO_MEMCPY:
case MONO_RGCTX_INFO_BZERO: {
static MonoMethod *memcpy_method [17];
static MonoMethod *bzero_method [17];
MonoJitDomainInfo *domain_info;
int size;
guint32 align;
domain_info = domain_jit_info (domain);
if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (klass))) {
size = sizeof (gpointer);
align = sizeof (gpointer);
} else {
size = mono_class_value_size (klass, &align);
}
if (size != 1 && size != 2 && size != 4 && size != 8)
size = 0;
if (align < size)
size = 0;
if (info_type == MONO_RGCTX_INFO_MEMCPY) {
if (!memcpy_method [size]) {
MonoMethod *m;
char name [32];
if (size == 0)