/
object.c
3986 lines (3467 loc) · 110 KB
/
object.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
/*
* object.c: Object creation for the Mono runtime
*
* Author:
* Miguel de Icaza (miguel@ximian.com)
* Paolo Molaro (lupus@ximian.com)
*
* (C) 2001-2004 Ximian, Inc.
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/loader.h>
#include <mono/metadata/object.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/domain-internals.h>
#include "mono/metadata/metadata-internals.h"
#include "mono/metadata/class-internals.h"
#include <mono/metadata/assembly.h>
#include <mono/metadata/threadpool.h>
#include <mono/metadata/marshal.h>
#include "mono/metadata/debug-helpers.h"
#include "mono/metadata/marshal.h"
#include <mono/metadata/threads.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/environment.h>
#include "mono/metadata/profiler-private.h"
#include "mono/metadata/security-manager.h"
#include <mono/os/gc_wrapper.h>
#include <mono/utils/strenc.h>
#ifdef HAVE_BOEHM_GC
#define NEED_TO_ZERO_PTRFREE 1
#define ALLOC_PTRFREE(obj,vt,size) do { (obj) = GC_MALLOC_ATOMIC ((size)); (obj)->vtable = (vt); (obj)->synchronisation = NULL;} while (0)
#define ALLOC_OBJECT(obj,vt,size) do { (obj) = GC_MALLOC ((size)); (obj)->vtable = (vt);} while (0)
#ifdef HAVE_GC_GCJ_MALLOC
#define CREATION_SPEEDUP 1
#define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
#define ALLOC_TYPED(dest,size,type) do { (dest) = GC_GCJ_MALLOC ((size),(type)); } while (0)
#define MAKE_STRING_DESCRIPTOR(bitmap,sz) GC_make_descriptor((GC_bitmap)(bitmap),(sz))
#define MAKE_DESCRIPTOR(bitmap,sz,objsize) GC_make_descriptor((GC_bitmap)(bitmap),(sz))
#else
#define GC_NO_DESCRIPTOR (NULL)
#define ALLOC_TYPED(dest,size,type) do { (dest) = GC_MALLOC ((size)); *(gpointer*)dest = (type);} while (0)
#define MAKE_STRING_DESCRIPTOR(bitmap,sz) NULL
#define MAKE_DESCRIPTOR(bitmap,sz,objsize) NULL
#endif
#else
#ifdef HAVE_SGEN_GC
#define GC_NO_DESCRIPTOR (NULL)
#define ALLOC_PTRFREE(obj,vt,size) do { (obj) = mono_gc_alloc_obj (vt, size);} while (0)
#define ALLOC_OBJECT(obj,vt,size) do { (obj) = mono_gc_alloc_obj (vt, size);} while (0)
#define ALLOC_TYPED(dest,size,type) do { (dest) = mono_gc_alloc_obj (type, size);} while (0)
#define MAKE_STRING_DESCRIPTOR(bitmap,sz) mono_gc_make_descr_for_string ()
#define MAKE_DESCRIPTOR(bitmap,sz,objsize) mono_gc_make_descr_for_object ((bitmap), (sz), (objsize))
#else
#define NEED_TO_ZERO_PTRFREE 1
#define GC_NO_DESCRIPTOR (NULL)
#define ALLOC_PTRFREE(obj,vt,size) do { (obj) = malloc ((size)); (obj)->vtable = (vt); (obj)->synchronisation = NULL;} while (0)
#define ALLOC_OBJECT(obj,vt,size) do { (obj) = calloc (1, (size)); (obj)->vtable = (vt);} while (0)
#define ALLOC_TYPED(dest,size,type) do { (dest) = calloc (1, (size)); *(gpointer*)dest = (type);} while (0)
#define MAKE_STRING_DESCRIPTOR(bitmap,sz) NULL
#define MAKE_DESCRIPTOR(bitmap,sz,objsize) NULL
#endif
#endif
static MonoObject* mono_object_new_ptrfree (MonoVTable *vtable);
static MonoObject* mono_object_new_ptrfree_box (MonoVTable *vtable);
static void
get_default_field_value (MonoDomain* domain, MonoClassField *field, void *value);
static MonoString*
mono_ldstr_metdata_sig (MonoDomain *domain, const char* sig);
#define ldstr_lock() EnterCriticalSection (&ldstr_section)
#define ldstr_unlock() LeaveCriticalSection (&ldstr_section)
static CRITICAL_SECTION ldstr_section;
void
mono_runtime_object_init (MonoObject *this)
{
MonoMethod *method = NULL;
MonoClass *klass = this->vtable->klass;
method = mono_class_get_method_from_name (klass, ".ctor", 0);
g_assert (method);
if (method->klass->valuetype)
this = mono_object_unbox (this);
mono_runtime_invoke (method, this, NULL, NULL);
}
/* The pseudo algorithm for type initialization from the spec
Note it doesn't say anything about domains - only threads.
2. If the type is initialized you are done.
2.1. If the type is not yet initialized, try to take an
initialization lock.
2.2. If successful, record this thread as responsible for
initializing the type and proceed to step 2.3.
2.2.1. If not, see whether this thread or any thread
waiting for this thread to complete already holds the lock.
2.2.2. If so, return since blocking would create a deadlock. This thread
will now see an incompletely initialized state for the type,
but no deadlock will arise.
2.2.3 If not, block until the type is initialized then return.
2.3 Initialize the parent type and then all interfaces implemented
by this type.
2.4 Execute the type initialization code for this type.
2.5 Mark the type as initialized, release the initialization lock,
awaken any threads waiting for this type to be initialized,
and return.
*/
typedef struct
{
guint32 initializing_tid;
guint32 waiting_count;
gboolean done;
CRITICAL_SECTION initialization_section;
} TypeInitializationLock;
/* for locking access to type_initialization_hash and blocked_thread_hash */
#define mono_type_initialization_lock() EnterCriticalSection (&type_initialization_section)
#define mono_type_initialization_unlock() LeaveCriticalSection (&type_initialization_section)
static CRITICAL_SECTION type_initialization_section;
/* from vtable to lock */
static GHashTable *type_initialization_hash;
/* from thread id to thread id being waited on */
static GHashTable *blocked_thread_hash;
/* Main thread */
static MonoThread *main_thread;
/**
* mono_thread_set_main:
* @thread: thread to set as the main thread
*
* This function can be used to instruct the runtime to treat @thread
* as the main thread, ie, the thread that would normally execute the Main()
* method. This basically means that at the end of @thread, the runtime will
* wait for the existing foreground threads to quit and other such details.
*/
void
mono_thread_set_main (MonoThread *thread)
{
main_thread = thread;
}
MonoThread*
mono_thread_get_main (void)
{
return main_thread;
}
void
mono_type_initialization_init (void)
{
InitializeCriticalSection (&type_initialization_section);
type_initialization_hash = g_hash_table_new (NULL, NULL);
blocked_thread_hash = g_hash_table_new (NULL, NULL);
InitializeCriticalSection (&ldstr_section);
}
/*
* mono_runtime_class_init:
* @vtable: vtable that needs to be initialized
*
* This routine calls the class constructor for @vtable.
*/
void
mono_runtime_class_init (MonoVTable *vtable)
{
MonoException *exc;
MonoException *exc_to_throw;
MonoMethod *method = NULL;
MonoClass *klass;
gchar *full_name;
MONO_ARCH_SAVE_REGS;
if (vtable->initialized)
return;
exc = NULL;
klass = vtable->klass;
method = mono_class_get_cctor (klass);
if (method) {
MonoDomain *domain = vtable->domain;
TypeInitializationLock *lock;
guint32 tid = GetCurrentThreadId();
int do_initialization = 0;
MonoDomain *last_domain = NULL;
mono_type_initialization_lock ();
/* double check... */
if (vtable->initialized) {
mono_type_initialization_unlock ();
return;
}
lock = g_hash_table_lookup (type_initialization_hash, vtable);
if (lock == NULL) {
/* This thread will get to do the initialization */
if (mono_domain_get () != domain) {
/* Transfer into the target domain */
last_domain = mono_domain_get ();
if (!mono_domain_set (domain, FALSE)) {
vtable->initialized = 1;
mono_type_initialization_unlock ();
mono_raise_exception (mono_get_exception_appdomain_unloaded ());
}
}
lock = g_malloc (sizeof(TypeInitializationLock));
InitializeCriticalSection (&lock->initialization_section);
lock->initializing_tid = tid;
lock->waiting_count = 1;
lock->done = FALSE;
/* grab the vtable lock while this thread still owns type_initialization_section */
EnterCriticalSection (&lock->initialization_section);
g_hash_table_insert (type_initialization_hash, vtable, lock);
do_initialization = 1;
} else {
gpointer blocked;
TypeInitializationLock *pending_lock;
if (lock->initializing_tid == tid || lock->done) {
mono_type_initialization_unlock ();
return;
}
/* see if the thread doing the initialization is already blocked on this thread */
blocked = GUINT_TO_POINTER (lock->initializing_tid);
while ((pending_lock = (TypeInitializationLock*) g_hash_table_lookup (blocked_thread_hash, blocked))) {
if (pending_lock->initializing_tid == tid) {
if (!pending_lock->done) {
mono_type_initialization_unlock ();
return;
} else {
/* the thread doing the initialization is blocked on this thread,
but on a lock that has already been freed. It just hasn't got
time to awake */
break;
}
}
blocked = GUINT_TO_POINTER (pending_lock->initializing_tid);
}
++lock->waiting_count;
/* record the fact that we are waiting on the initializing thread */
g_hash_table_insert (blocked_thread_hash, GUINT_TO_POINTER (tid), lock);
}
mono_type_initialization_unlock ();
if (do_initialization) {
mono_runtime_invoke (method, NULL, NULL, (MonoObject **) &exc);
if (last_domain)
mono_domain_set (last_domain, TRUE);
lock->done = TRUE;
LeaveCriticalSection (&lock->initialization_section);
} else {
/* this just blocks until the initializing thread is done */
EnterCriticalSection (&lock->initialization_section);
LeaveCriticalSection (&lock->initialization_section);
}
mono_type_initialization_lock ();
if (lock->initializing_tid != tid)
g_hash_table_remove (blocked_thread_hash, GUINT_TO_POINTER (tid));
--lock->waiting_count;
if (lock->waiting_count == 0) {
DeleteCriticalSection (&lock->initialization_section);
g_hash_table_remove (type_initialization_hash, vtable);
g_free (lock);
}
vtable->initialized = 1;
/* FIXME: if the cctor fails, the type must be marked as unusable */
mono_type_initialization_unlock ();
} else {
vtable->initialized = 1;
return;
}
if (exc == NULL ||
(klass->image == mono_defaults.corlib &&
!strcmp (klass->name_space, "System") &&
!strcmp (klass->name, "TypeInitializationException")))
return; /* No static constructor found or avoid infinite loop */
if (klass->name_space && *klass->name_space)
full_name = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
else
full_name = g_strdup (klass->name);
exc_to_throw = mono_get_exception_type_initialization (full_name, exc);
g_free (full_name);
mono_raise_exception (exc_to_throw);
}
static
gboolean release_type_locks (gpointer key, gpointer value, gpointer user)
{
TypeInitializationLock *lock = (TypeInitializationLock*) value;
if (lock->initializing_tid == GPOINTER_TO_UINT (user) && !lock->done) {
lock->done = TRUE;
LeaveCriticalSection (&lock->initialization_section);
--lock->waiting_count;
if (lock->waiting_count == 0) {
DeleteCriticalSection (&lock->initialization_section);
g_free (lock);
return TRUE;
}
}
return FALSE;
}
void
mono_release_type_locks (MonoThread *thread)
{
mono_type_initialization_lock ();
g_hash_table_foreach_remove (type_initialization_hash, release_type_locks, (gpointer)(gsize)(thread->tid));
mono_type_initialization_unlock ();
}
static gpointer
default_trampoline (MonoMethod *method)
{
return method;
}
static gpointer
default_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
{
g_error ("remoting not installed");
return NULL;
}
static gpointer
default_delegate_trampoline (MonoMethod *method, gpointer addr)
{
g_assert_not_reached ();
return NULL;
}
static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
static MonoRemotingTrampoline arch_create_remoting_trampoline = default_remoting_trampoline;
static MonoDelegateTrampoline arch_create_delegate_trampoline = default_delegate_trampoline;
void
mono_install_trampoline (MonoTrampoline func)
{
arch_create_jit_trampoline = func? func: default_trampoline;
}
void
mono_install_remoting_trampoline (MonoRemotingTrampoline func)
{
arch_create_remoting_trampoline = func? func: default_remoting_trampoline;
}
void
mono_install_delegate_trampoline (MonoDelegateTrampoline func)
{
arch_create_delegate_trampoline = func? func: default_delegate_trampoline;
}
static MonoCompileFunc default_mono_compile_method = NULL;
/**
* mono_install_compile_method:
* @func: function to install
*
* This is a VM internal routine
*/
void
mono_install_compile_method (MonoCompileFunc func)
{
default_mono_compile_method = func;
}
/**
* mono_compile_method:
* @method: The method to compile.
*
* This JIT-compiles the method, and returns the pointer to the native code
* produced.
*/
gpointer
mono_compile_method (MonoMethod *method)
{
if (!default_mono_compile_method) {
g_error ("compile method called on uninitialized runtime");
return NULL;
}
return default_mono_compile_method (method);
}
static MonoFreeMethodFunc default_mono_free_method = NULL;
/**
* mono_install_free_method:
* @func: pointer to the MonoFreeMethodFunc used to release a method
*
* This is an internal VM routine, it is used for the engines to
* register a handler to release the resources associated with a method.
*
* Methods are freed when no more references to the delegate that holds
* them are left.
*/
void
mono_install_free_method (MonoFreeMethodFunc func)
{
default_mono_free_method = func;
}
/**
* mono_runtime_free_method:
* @domain; domain where the method is hosted
* @method: method to release
*
* This routine is invoked to free the resources associated with
* a method that has been JIT compiled. This is used to discard
* methods that were used only temporarily (for example, used in marshalling)
*
*/
void
mono_runtime_free_method (MonoDomain *domain, MonoMethod *method)
{
if (default_mono_free_method != NULL)
default_mono_free_method (domain, method);
mono_free_method (method);
}
static MonoInitVTableFunc init_vtable_func = NULL;
/**
* mono_install_init_vtable:
* @func: pointer to the function to be installed
*
* Register a function which will be called by the runtime to initialize the
* method pointers inside a vtable. The JIT can use this function to load the
* vtable from the AOT file for example.
*/
void
mono_install_init_vtable (MonoInitVTableFunc func)
{
init_vtable_func = func;
}
/*
* The vtables in the root appdomain are assumed to be reachable by other
* roots, and we don't use typed allocation in the other domains.
*/
/* The sync block is no longer a GC pointer */
#define GC_HEADER_BITMAP (0)
#define BITMAP_EL_SIZE (sizeof (gsize) * 8)
static gsize*
compute_class_bitmap (MonoClass *class, gsize *bitmap, int size, int offset, int *max_set)
{
MonoClassField *field;
MonoClass *p;
guint32 pos;
int max_size = class->instance_size / sizeof (gpointer);
if (max_size > size) {
bitmap = g_malloc0 (sizeof (gsize) * ((max_size) + 1));
}
for (p = class; p != NULL; p = p->parent) {
gpointer iter = NULL;
while ((field = mono_class_get_fields (p, &iter))) {
MonoType *type;
if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
continue;
/* FIXME: should not happen, flag as type load error */
if (field->type->byref)
break;
pos = field->offset / sizeof (gpointer);
pos += offset;
type = mono_type_get_underlying_type (field->type);
switch (type->type) {
/* FIXME: _I and _U and _PTR should be removed eventually */
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_STRING:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_CLASS:
case MONO_TYPE_OBJECT:
case MONO_TYPE_ARRAY:
g_assert ((field->offset % sizeof(gpointer)) == 0);
bitmap [pos / BITMAP_EL_SIZE] |= ((gsize)1) << (pos % BITMAP_EL_SIZE);
*max_set = MAX (*max_set, pos);
break;
case MONO_TYPE_VALUETYPE: {
MonoClass *fclass = mono_class_from_mono_type (field->type);
if (fclass->has_references) {
/* remove the object header */
compute_class_bitmap (fclass, bitmap, size, pos - (sizeof (MonoObject) / sizeof (gpointer)), max_set);
}
break;
}
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I8:
case MONO_TYPE_U8:
case MONO_TYPE_R4:
case MONO_TYPE_R8:
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_CHAR:
break;
default:
g_assert_not_reached ();
break;
}
}
}
return bitmap;
}
static void
mono_class_compute_gc_descriptor (MonoClass *class)
{
int max_set = 0;
gsize *bitmap;
gsize default_bitmap [4] = {0};
static gboolean gcj_inited = FALSE;
if (!gcj_inited) {
mono_loader_lock ();
mono_register_jit_icall (mono_object_new_ptrfree, "mono_object_new_ptrfree", mono_create_icall_signature ("object ptr"), FALSE);
mono_register_jit_icall (mono_object_new_ptrfree_box, "mono_object_new_ptrfree_box", mono_create_icall_signature ("object ptr"), FALSE);
mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", mono_create_icall_signature ("object ptr"), FALSE);
#ifdef HAVE_GC_GCJ_MALLOC
GC_init_gcj_malloc (5, NULL);
#ifdef GC_REDIRECT_TO_LOCAL
mono_register_jit_icall (GC_local_gcj_malloc, "GC_local_gcj_malloc", mono_create_icall_signature ("object int ptr"), FALSE);
mono_register_jit_icall (GC_local_gcj_fast_malloc, "GC_local_gcj_fast_malloc", mono_create_icall_signature ("object int ptr"), FALSE);
#endif
mono_register_jit_icall (GC_gcj_malloc, "GC_gcj_malloc", mono_create_icall_signature ("object int ptr"), FALSE);
mono_register_jit_icall (GC_gcj_fast_malloc, "GC_gcj_fast_malloc", mono_create_icall_signature ("object int ptr"), FALSE);
#endif
gcj_inited = TRUE;
mono_loader_unlock ();
}
if (!class->inited)
mono_class_init (class);
if (class->gc_descr_inited)
return;
class->gc_descr_inited = TRUE;
class->gc_descr = GC_NO_DESCRIPTOR;
if (class->generic_class || class->generic_container) {
/* bug #75479 */
return;
}
bitmap = default_bitmap;
if (class == mono_defaults.string_class) {
class->gc_descr = (gpointer)MAKE_STRING_DESCRIPTOR (bitmap, 2);
} else if (class->rank) {
mono_class_compute_gc_descriptor (class->element_class);
#ifdef HAVE_SGEN_GC
/* libgc has no usable support for arrays... */
if (!class->element_class->valuetype) {
gsize abm = 1;
class->gc_descr = mono_gc_make_descr_for_array (TRUE, &abm, 1, sizeof (gpointer));
/*printf ("new array descriptor: 0x%x for %s.%s\n", class->gc_descr,
class->name_space, class->name);*/
} else {
/* remove the object header */
bitmap = compute_class_bitmap (class->element_class, default_bitmap, sizeof (default_bitmap) * 8, - (sizeof (MonoObject) / sizeof (gpointer)), &max_set);
class->gc_descr = mono_gc_make_descr_for_array (TRUE, bitmap, mono_array_element_size (class) / sizeof (gpointer), mono_array_element_size (class));
/*printf ("new vt array descriptor: 0x%x for %s.%s\n", class->gc_descr,
class->name_space, class->name);*/
if (bitmap != default_bitmap)
g_free (bitmap);
}
#endif
} else {
/*static int count = 0;
if (count++ > 58)
return;*/
bitmap = compute_class_bitmap (class, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set);
#ifdef HAVE_BOEHM_GC
/* It seems there are issues when the bitmap doesn't fit: play it safe */
if (max_set >= 30) {
/*g_print ("disabling typed alloc (%d) for %s.%s\n", max_set, class->name_space, class->name);*/
if (bitmap != default_bitmap)
g_free (bitmap);
return;
}
#endif
class->gc_descr = (gpointer)MAKE_DESCRIPTOR (bitmap, max_set + 1, class->instance_size);
/*printf ("new descriptor: %p 0x%x for %s.%s\n", class->gc_descr, bitmap [0], class->name_space, class->name);*/
if (bitmap != default_bitmap)
g_free (bitmap);
}
}
/**
* field_is_special_static:
* @fklass: The MonoClass to look up.
* @field: The MonoClassField describing the field.
*
* Returns: SPECIAL_STATIC_THREAD if the field is thread static, SPECIAL_STATIC_CONTEXT if it is context static,
* SPECIAL_STATIC_NONE otherwise.
*/
static gint32
field_is_special_static (MonoClass *fklass, MonoClassField *field)
{
MonoCustomAttrInfo *ainfo;
int i;
ainfo = mono_custom_attrs_from_field (fklass, field);
if (!ainfo)
return FALSE;
for (i = 0; i < ainfo->num_attrs; ++i) {
MonoClass *klass = ainfo->attrs [i].ctor->klass;
if (klass->image == mono_defaults.corlib) {
if (strcmp (klass->name, "ThreadStaticAttribute") == 0) {
mono_custom_attrs_free (ainfo);
return SPECIAL_STATIC_THREAD;
}
else if (strcmp (klass->name, "ContextStaticAttribute") == 0) {
mono_custom_attrs_free (ainfo);
return SPECIAL_STATIC_CONTEXT;
}
}
}
mono_custom_attrs_free (ainfo);
return SPECIAL_STATIC_NONE;
}
static MonoVTable *mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *class);
/**
* mono_class_vtable:
* @domain: the application domain
* @class: the class to initialize
*
* VTables are domain specific because we create domain specific code, and
* they contain the domain specific static class data.
*/
MonoVTable *
mono_class_vtable (MonoDomain *domain, MonoClass *class)
{
MonoClassRuntimeInfo *runtime_info;
g_assert (class);
/* this check can be inlined in jitted code, too */
runtime_info = class->runtime_info;
if (runtime_info && runtime_info->max_domain >= domain->domain_id && runtime_info->domain_vtables [domain->domain_id])
return runtime_info->domain_vtables [domain->domain_id];
return mono_class_create_runtime_vtable (domain, class);
}
static MonoVTable *
mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *class)
{
MonoVTable *vt;
MonoClassRuntimeInfo *runtime_info, *old_info;
MonoClassField *field;
char *t;
int i;
gboolean inited = FALSE;
guint32 vtable_size;
guint32 cindex;
guint32 constant_cols [MONO_CONSTANT_SIZE];
gpointer iter;
mono_domain_lock (domain);
runtime_info = class->runtime_info;
if (runtime_info && runtime_info->max_domain >= domain->domain_id && runtime_info->domain_vtables [domain->domain_id]) {
mono_domain_unlock (domain);
return runtime_info->domain_vtables [domain->domain_id];
}
if (!class->inited)
mono_class_init (class);
mono_class_setup_vtable (class);
mono_stats.used_class_count++;
mono_stats.class_vtable_size += sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
vtable_size = sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
vt = mono_mempool_alloc0 (domain->mp, vtable_size);
vt->klass = class;
vt->rank = class->rank;
vt->domain = domain;
mono_class_compute_gc_descriptor (class);
/*
* We can't use typed allocation in the non-root domains, since the
* collector needs the GC descriptor stored in the vtable even after
* the mempool containing the vtable is destroyed when the domain is
* unloaded. An alternative might be to allocate vtables in the GC
* heap, but this does not seem to work (it leads to crashes inside
* libgc). If that approach is tried, two gc descriptors need to be
* allocated for each class: one for the root domain, and one for all
* other domains. The second descriptor should contain a bit for the
* vtable field in MonoObject, since we can no longer assume the
* vtable is reachable by other roots after the appdomain is unloaded.
*/
#ifdef HAVE_BOEHM_GC
if (domain != mono_get_root_domain ())
vt->gc_descr = GC_NO_DESCRIPTOR;
else
#endif
vt->gc_descr = class->gc_descr;
if (class->class_size) {
if (class->has_static_refs)
vt->data = mono_gc_alloc_fixed (class->class_size, NULL);
else
vt->data = mono_mempool_alloc0 (domain->mp, class->class_size);
mono_g_hash_table_insert (domain->static_data_hash, class, vt->data);
mono_stats.class_static_data_size += class->class_size;
}
cindex = -1;
iter = NULL;
while ((field = mono_class_get_fields (class, &iter))) {
if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
continue;
if (mono_field_is_deleted (field))
continue;
if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)) {
gint32 special_static = class->no_special_static_fields ? SPECIAL_STATIC_NONE : field_is_special_static (class, field);
if (special_static != SPECIAL_STATIC_NONE) {
guint32 size, offset;
int align;
size = mono_type_size (field->type, &align);
offset = mono_alloc_special_static_data (special_static, size, align);
if (!domain->special_static_fields)
domain->special_static_fields = g_hash_table_new (NULL, NULL);
g_hash_table_insert (domain->special_static_fields, field, GUINT_TO_POINTER (offset));
continue;
}
}
if ((field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)) {
MonoClass *fklass = mono_class_from_mono_type (field->type);
g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT));
t = (char*)vt->data + field->offset;
if (fklass->valuetype) {
memcpy (t, field->data, mono_class_value_size (fklass, NULL));
} else {
/* it's a pointer type: add check */
g_assert ((fklass->byval_arg.type == MONO_TYPE_PTR) || (fklass->byval_arg.type == MONO_TYPE_FNPTR));
*t = *(char *)field->data;
}
continue;
}
if (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
continue;
/* later do this only on demand if needed */
if (!field->data) {
cindex = mono_metadata_get_constant_index (class->image, mono_class_get_field_token (field), cindex + 1);
g_assert (cindex);
g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA));
mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
field->def_type = constant_cols [MONO_CONSTANT_TYPE];
field->data = (gpointer)mono_metadata_blob_heap (class->image, constant_cols [MONO_CONSTANT_VALUE]);
}
}
vt->max_interface_id = class->max_interface_id;
vt->interface_offsets = mono_mempool_alloc0 (domain->mp,
sizeof (gpointer) * (class->max_interface_id + 1));
/* initialize interface offsets */
for (i = 0; i <= class->max_interface_id; ++i) {
int slot = class->interface_offsets [i];
if (slot >= 0)
vt->interface_offsets [i] = &(vt->vtable [slot]);
}
/*
* arch_create_jit_trampoline () can recursively call this function again
* because it compiles icall methods right away.
*/
/* FIXME: class_vtable_hash is basically obsolete now: remove as soon
* as we change the code in appdomain.c to invalidate vtables by
* looking at the possible MonoClasses created for the domain.
* Or we can reuse static_data_hash, by using vtable as a key
* and always inserting into that hash.
*/
g_hash_table_insert (domain->class_vtable_hash, class, vt);
/* class->runtime_info is protected by the loader lock, both when
* it it enlarged and when it is stored info.
*/
mono_loader_lock ();
old_info = class->runtime_info;
if (old_info && old_info->max_domain >= domain->domain_id) {
/* someone already created a large enough runtime info */
old_info->domain_vtables [domain->domain_id] = vt;
} else {
int new_size = domain->domain_id;
if (old_info)
new_size = MAX (new_size, old_info->max_domain);
new_size++;
/* make the new size a power of two */
i = 2;
while (new_size > i)
i <<= 1;
new_size = i;
/* this is a bounded memory retention issue: may want to
* handle it differently when we'll have a rcu-like system.
*/
runtime_info = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassRuntimeInfo) + new_size * sizeof (gpointer));
runtime_info->max_domain = new_size - 1;
/* copy the stuff from the older info */
if (old_info) {
memcpy (runtime_info->domain_vtables, old_info->domain_vtables, (old_info->max_domain + 1) * sizeof (gpointer));
}
runtime_info->domain_vtables [domain->domain_id] = vt;
/* keep this last (add membarrier) */
class->runtime_info = runtime_info;
}
mono_loader_unlock ();
/* initialize vtable */
if (init_vtable_func)
inited = init_vtable_func (vt);
if (!inited) {
mono_class_setup_vtable (class);
for (i = 0; i < class->vtable_size; ++i) {
MonoMethod *cm;
if ((cm = class->vtable [i])) {
if (mono_method_signature (cm)->generic_param_count)
vt->vtable [i] = cm;
else
vt->vtable [i] = arch_create_jit_trampoline (cm);
}
}
}
mono_domain_unlock (domain);
/* Initialization is now complete, we can throw if the InheritanceDemand aren't satisfied */
if (mono_is_security_manager_active () && (class->exception_type == MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND)) {
MonoException *exc = mono_class_get_exception_for_failure (class);
g_assert (exc);
mono_raise_exception (exc);
}
/* make sure the the parent is initialized */
if (class->parent)
mono_class_vtable (domain, class->parent);
vt->type = mono_type_get_object (domain, &class->byval_arg);
if (class->contextbound)
vt->remote = 1;
else
vt->remote = 0;
return vt;
}
/**
* mono_class_proxy_vtable:
* @domain: the application domain
* @remove_class: the remote class
*
* Creates a vtable for transparent proxies. It is basically
* a copy of the real vtable of the class wrapped in @remote_class,
* but all function pointers invoke the remoting functions, and
* vtable->klass points to the transparent proxy class, and not to @class.
*/
static MonoVTable *
mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRemotingTarget target_type)
{
MonoVTable *vt, *pvt;
int i, j, vtsize, max_interface_id, extra_interface_vtsize = 0;
MonoClass *k;
GSList *extra_interfaces = NULL;
MonoClass *class = remote_class->proxy_class;
vt = mono_class_vtable (domain, class);
max_interface_id = vt->max_interface_id;
/* Calculate vtable space for extra interfaces */
for (j = 0; j < remote_class->interface_count; j++) {
MonoClass* iclass = remote_class->interfaces[j];
GPtrArray *ifaces;
int method_count;
if (iclass->interface_id <= class->max_interface_id && class->interface_offsets[iclass->interface_id] != -1)
continue; /* interface implemented by the class */
if (g_slist_find (extra_interfaces, iclass))
continue;
extra_interfaces = g_slist_prepend (extra_interfaces, iclass);
method_count = mono_class_num_methods (iclass);
ifaces = mono_class_get_implemented_interfaces (iclass);
if (ifaces) {
for (i = 0; i < ifaces->len; ++i) {
MonoClass *ic = g_ptr_array_index (ifaces, i);
if (ic->interface_id <= class->max_interface_id && class->interface_offsets[ic->interface_id] != -1)
continue; /* interface implemented by the class */
if (g_slist_find (extra_interfaces, ic))
continue;
extra_interfaces = g_slist_prepend (extra_interfaces, ic);
method_count += mono_class_num_methods (ic);
}
g_ptr_array_free (ifaces, TRUE);
}
extra_interface_vtsize += method_count * sizeof (gpointer);
if (iclass->max_interface_id > max_interface_id) max_interface_id = iclass->max_interface_id;
}
vtsize = sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
mono_stats.class_vtable_size += vtsize + extra_interface_vtsize;
pvt = mono_mempool_alloc (domain->mp, vtsize + extra_interface_vtsize);
memcpy (pvt, vt, vtsize);
pvt->klass = mono_defaults.transparent_proxy_class;
/* we need to keep the GC descriptor for a transparent proxy or we confuse the precise GC */
pvt->gc_descr = mono_defaults.transparent_proxy_class->gc_descr;
/* initialize vtable */
mono_class_setup_vtable (class);
for (i = 0; i < class->vtable_size; ++i) {
MonoMethod *cm;
if ((cm = class->vtable [i]))
pvt->vtable [i] = arch_create_remoting_trampoline (cm, target_type);
}
if (class->flags & TYPE_ATTRIBUTE_ABSTRACT) {
/* create trampolines for abstract methods */
for (k = class; k; k = k->parent) {
MonoMethod* m;
gpointer iter = NULL;
while ((m = mono_class_get_methods (k, &iter)))
if (!pvt->vtable [m->slot])
pvt->vtable [m->slot] = arch_create_remoting_trampoline (m, target_type);
}
}
pvt->max_interface_id = max_interface_id;
pvt->interface_offsets = mono_mempool_alloc0 (domain->mp,
sizeof (gpointer) * (max_interface_id + 1));
/* initialize interface offsets */
for (i = 0; i <= class->max_interface_id; ++i) {
int slot = class->interface_offsets [i];
if (slot >= 0)
pvt->interface_offsets [i] = &(pvt->vtable [slot]);
}
if (extra_interfaces) {
int slot = class->vtable_size;
MonoClass* interf;
gpointer iter;
MonoMethod* cm;
GSList *list_item;