/
object.c
8460 lines (7272 loc) · 242 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
/**
* \file
* Object creation for the Mono runtime
*
* Author:
* Miguel de Icaza (miguel@ximian.com)
* Paolo Molaro (lupus@ximian.com)
*
* Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
* Copyright 2004-2011 Novell, Inc (http://www.novell.com)
* Copyright 2001 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>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <stdlib.h>
#include <stdio.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-internals.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/exception-internals.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/marshal.h>
#include "mono/metadata/debug-helpers.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/metadata/verify-internals.h>
#include <mono/metadata/reflection-internals.h>
#include <mono/metadata/w32event.h>
#include <mono/utils/strenc.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/mono-memory-model.h>
#include <mono/utils/checked-build.h>
#include <mono/utils/mono-threads.h>
#include <mono/utils/mono-threads-coop.h>
#include "cominterop.h"
#include <mono/utils/w32api.h>
static void
get_default_field_value (MonoDomain* domain, MonoClassField *field, void *value, MonoError *error);
static MonoString*
mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig, MonoError *error);
static void
free_main_args (void);
static char *
mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, gboolean ignore_error, MonoError *error);
static void
array_full_copy_unchecked_size (MonoArray *src, MonoArray *dest, MonoClass *klass, uintptr_t size);
static MonoMethod*
class_get_virtual_method (MonoClass *klass, MonoMethod *method, gboolean is_proxy, MonoError *error);
/* Class lazy loading functions */
static GENERATE_GET_CLASS_WITH_CACHE (pointer, "System.Reflection", "Pointer")
static GENERATE_GET_CLASS_WITH_CACHE (remoting_services, "System.Runtime.Remoting", "RemotingServices")
static GENERATE_GET_CLASS_WITH_CACHE (unhandled_exception_event_args, "System", "UnhandledExceptionEventArgs")
static GENERATE_GET_CLASS_WITH_CACHE (sta_thread_attribute, "System", "STAThreadAttribute")
static GENERATE_GET_CLASS_WITH_CACHE (activation_services, "System.Runtime.Remoting.Activation", "ActivationServices")
#define ldstr_lock() mono_os_mutex_lock (&ldstr_section)
#define ldstr_unlock() mono_os_mutex_unlock (&ldstr_section)
static mono_mutex_t ldstr_section;
/**
* mono_runtime_object_init:
* \param this_obj the object to initialize
* This function calls the zero-argument constructor (which must
* exist) for the given object.
*/
void
mono_runtime_object_init (MonoObject *this_obj)
{
MonoError error;
mono_runtime_object_init_checked (this_obj, &error);
mono_error_assert_ok (&error);
}
/**
* mono_runtime_object_init_checked:
* \param this_obj the object to initialize
* \param error set on error.
* This function calls the zero-argument constructor (which must
* exist) for the given object and returns TRUE on success, or FALSE
* on error and sets \p error.
*/
gboolean
mono_runtime_object_init_checked (MonoObject *this_obj, MonoError *error)
{
MONO_REQ_GC_UNSAFE_MODE;
MonoMethod *method = NULL;
MonoClass *klass = this_obj->vtable->klass;
error_init (error);
method = mono_class_get_method_from_name (klass, ".ctor", 0);
if (!method)
g_error ("Could not lookup zero argument constructor for class %s", mono_type_get_full_name (klass));
if (method->klass->valuetype)
this_obj = (MonoObject *)mono_object_unbox (this_obj);
mono_runtime_invoke_checked (method, this_obj, NULL, error);
return is_ok (error);
}
/* 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
{
MonoNativeThreadId initializing_tid;
guint32 waiting_count;
gboolean done;
MonoCoopMutex mutex;
/* condvar used to wait for 'done' becoming TRUE */
MonoCoopCond cond;
} TypeInitializationLock;
/* for locking access to type_initialization_hash and blocked_thread_hash */
static MonoCoopMutex type_initialization_section;
static inline void
mono_type_initialization_lock (void)
{
/* The critical sections protected by this lock in mono_runtime_class_init_full () can block */
mono_coop_mutex_lock (&type_initialization_section);
}
static inline void
mono_type_initialization_unlock (void)
{
mono_coop_mutex_unlock (&type_initialization_section);
}
static void
mono_type_init_lock (TypeInitializationLock *lock)
{
MONO_REQ_GC_NEUTRAL_MODE;
mono_coop_mutex_lock (&lock->mutex);
}
static void
mono_type_init_unlock (TypeInitializationLock *lock)
{
mono_coop_mutex_unlock (&lock->mutex);
}
/* 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;
/* Functions supplied by the runtime */
static MonoRuntimeCallbacks callbacks;
/**
* mono_thread_set_main:
* \param thread thread to set as the main thread
* This function can be used to instruct the runtime to treat \p thread
* as the main thread, ie, the thread that would normally execute the \c Main
* method. This basically means that at the end of \p thread, the runtime will
* wait for the existing foreground threads to quit and other such details.
*/
void
mono_thread_set_main (MonoThread *thread)
{
MONO_REQ_GC_UNSAFE_MODE;
static gboolean registered = FALSE;
if (!registered) {
MONO_GC_REGISTER_ROOT_SINGLE (main_thread, MONO_ROOT_SOURCE_THREADING, "main thread object");
registered = TRUE;
}
main_thread = thread;
}
/**
* mono_thread_get_main:
*/
MonoThread*
mono_thread_get_main (void)
{
MONO_REQ_GC_UNSAFE_MODE;
return main_thread;
}
void
mono_type_initialization_init (void)
{
mono_coop_mutex_init_recursive (&type_initialization_section);
type_initialization_hash = g_hash_table_new (NULL, NULL);
blocked_thread_hash = g_hash_table_new (NULL, NULL);
mono_os_mutex_init_recursive (&ldstr_section);
}
void
mono_type_initialization_cleanup (void)
{
#if 0
/* This is causing race conditions with
* mono_release_type_locks
*/
mono_coop_mutex_destroy (&type_initialization_section);
g_hash_table_destroy (type_initialization_hash);
type_initialization_hash = NULL;
#endif
mono_os_mutex_destroy (&ldstr_section);
g_hash_table_destroy (blocked_thread_hash);
blocked_thread_hash = NULL;
free_main_args ();
}
/**
* get_type_init_exception_for_vtable:
*
* Return the stored type initialization exception for VTABLE.
*/
static MonoException*
get_type_init_exception_for_vtable (MonoVTable *vtable)
{
MONO_REQ_GC_UNSAFE_MODE;
MonoError error;
MonoDomain *domain = vtable->domain;
MonoClass *klass = vtable->klass;
MonoException *ex;
gchar *full_name;
if (!vtable->init_failed)
g_error ("Trying to get the init exception for a non-failed vtable of class %s", mono_type_get_full_name (klass));
/*
* If the initializing thread was rudely aborted, the exception is not stored
* in the hash.
*/
ex = NULL;
mono_domain_lock (domain);
if (domain->type_init_exception_hash)
ex = (MonoException *)mono_g_hash_table_lookup (domain->type_init_exception_hash, klass);
mono_domain_unlock (domain);
if (!ex) {
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);
ex = mono_get_exception_type_initialization_checked (full_name, NULL, &error);
g_free (full_name);
return_val_if_nok (&error, NULL);
}
return ex;
}
/**
* mono_runtime_class_init:
* \param vtable vtable that needs to be initialized
* This routine calls the class constructor for \p vtable.
*/
void
mono_runtime_class_init (MonoVTable *vtable)
{
MONO_REQ_GC_UNSAFE_MODE;
MonoError error;
mono_runtime_class_init_full (vtable, &error);
mono_error_assert_ok (&error);
}
/*
* Returns TRUE if the lock was freed.
* LOCKING: Caller should hold type_initialization_lock.
*/
static gboolean
unref_type_lock (TypeInitializationLock *lock)
{
--lock->waiting_count;
if (lock->waiting_count == 0) {
mono_coop_mutex_destroy (&lock->mutex);
mono_coop_cond_destroy (&lock->cond);
g_free (lock);
return TRUE;
} else {
return FALSE;
}
}
/**
* mono_runtime_class_init_full:
* \param vtable that neeeds to be initialized
* \param error set on error
* \returns TRUE if class constructor \c .cctor has been initialized successfully, or FALSE otherwise and sets \p error.
*/
gboolean
mono_runtime_class_init_full (MonoVTable *vtable, MonoError *error)
{
MONO_REQ_GC_UNSAFE_MODE;
MonoMethod *method = NULL;
MonoClass *klass;
gchar *full_name;
MonoDomain *domain = vtable->domain;
TypeInitializationLock *lock;
MonoNativeThreadId tid;
int do_initialization = 0;
MonoDomain *last_domain = NULL;
gboolean pending_tae = FALSE;
error_init (error);
if (vtable->initialized)
return TRUE;
klass = vtable->klass;
if (!klass->image->checked_module_cctor) {
mono_image_check_for_module_cctor (klass->image);
if (klass->image->has_module_cctor) {
MonoClass *module_klass;
MonoVTable *module_vtable;
module_klass = mono_class_get_checked (klass->image, MONO_TOKEN_TYPE_DEF | 1, error);
if (!module_klass) {
return FALSE;
}
module_vtable = mono_class_vtable_full (vtable->domain, module_klass, error);
if (!module_vtable)
return FALSE;
if (!mono_runtime_class_init_full (module_vtable, error))
return FALSE;
}
}
method = mono_class_get_cctor (klass);
if (!method) {
vtable->initialized = 1;
return TRUE;
}
tid = mono_native_thread_id_get ();
/*
* Due some preprocessing inside a global lock. If we are the first thread
* trying to initialize this class, create a separate lock+cond var, and
* acquire it before leaving the global lock. The other threads will wait
* on this cond var.
*/
mono_type_initialization_lock ();
/* double check... */
if (vtable->initialized) {
mono_type_initialization_unlock ();
return TRUE;
}
if (vtable->init_failed) {
mono_type_initialization_unlock ();
/* The type initialization already failed once, rethrow the same exception */
mono_error_set_exception_instance (error, get_type_init_exception_for_vtable (vtable));
return FALSE;
}
lock = (TypeInitializationLock *)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_error_set_exception_instance (error, mono_get_exception_appdomain_unloaded ());
return FALSE;
}
}
lock = (TypeInitializationLock *)g_malloc0 (sizeof (TypeInitializationLock));
mono_coop_mutex_init_recursive (&lock->mutex);
mono_coop_cond_init (&lock->cond);
lock->initializing_tid = tid;
lock->waiting_count = 1;
lock->done = FALSE;
g_hash_table_insert (type_initialization_hash, vtable, lock);
do_initialization = 1;
} else {
gpointer blocked;
TypeInitializationLock *pending_lock;
if (mono_native_thread_id_equals (lock->initializing_tid, tid)) {
mono_type_initialization_unlock ();
return TRUE;
}
/* see if the thread doing the initialization is already blocked on this thread */
gboolean is_blocked = TRUE;
blocked = GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (lock->initializing_tid));
while ((pending_lock = (TypeInitializationLock*) g_hash_table_lookup (blocked_thread_hash, blocked))) {
if (mono_native_thread_id_equals (pending_lock->initializing_tid, tid)) {
if (!pending_lock->done) {
mono_type_initialization_unlock ();
return TRUE;
} 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 */
is_blocked = FALSE;
break;
}
}
blocked = GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (pending_lock->initializing_tid));
}
++lock->waiting_count;
/* record the fact that we are waiting on the initializing thread */
if (is_blocked)
g_hash_table_insert (blocked_thread_hash, GUINT_TO_POINTER (tid), lock);
}
mono_type_initialization_unlock ();
if (do_initialization) {
MonoException *exc = NULL;
/* We are holding the per-vtable lock, do the actual initialization */
mono_threads_begin_abort_protected_block ();
mono_runtime_try_invoke (method, NULL, NULL, (MonoObject**) &exc, error);
mono_threads_end_abort_protected_block ();
//exception extracted, error will be set to the right value later
if (exc == NULL && !mono_error_ok (error))//invoking failed but exc was not set
exc = mono_error_convert_to_exception (error);
else
mono_error_cleanup (error);
error_init (error);
/* If the initialization failed, mark the class as unusable. */
/* Avoid infinite loops */
if (!(!exc ||
(klass->image == mono_defaults.corlib &&
!strcmp (klass->name_space, "System") &&
!strcmp (klass->name, "TypeInitializationException")))) {
vtable->init_failed = 1;
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);
MonoException *exc_to_throw = mono_get_exception_type_initialization_checked (full_name, exc, error);
g_free (full_name);
mono_error_assert_ok (error); //We can't recover from this, no way to fail a type we can't alloc a failure.
/*
* Store the exception object so it could be thrown on subsequent
* accesses.
*/
mono_domain_lock (domain);
if (!domain->type_init_exception_hash)
domain->type_init_exception_hash = mono_g_hash_table_new_type (mono_aligned_addr_hash, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "type initialization exceptions table");
mono_g_hash_table_insert (domain->type_init_exception_hash, klass, exc_to_throw);
mono_domain_unlock (domain);
}
if (last_domain)
mono_domain_set (last_domain, TRUE);
/* Signal to the other threads that we are done */
mono_type_init_lock (lock);
lock->done = TRUE;
mono_coop_cond_broadcast (&lock->cond);
mono_type_init_unlock (lock);
/*
* This can happen if the cctor self-aborts. We need to reactivate tae
* (next interruption checkpoint will throw it) and make sure we won't
* throw tie for the type.
*/
if (exc && mono_object_class (exc) == mono_defaults.threadabortexception_class) {
pending_tae = TRUE;
mono_thread_resume_interruption (FALSE);
}
} else {
/* this just blocks until the initializing thread is done */
mono_type_init_lock (lock);
while (!lock->done)
mono_coop_cond_wait (&lock->cond, &lock->mutex);
mono_type_init_unlock (lock);
}
/* Do cleanup and setting vtable->initialized inside the global lock again */
mono_type_initialization_lock ();
if (!do_initialization)
g_hash_table_remove (blocked_thread_hash, GUINT_TO_POINTER (tid));
gboolean deleted = unref_type_lock (lock);
if (deleted)
g_hash_table_remove (type_initialization_hash, vtable);
/* Have to set this here since we check it inside the global lock */
if (do_initialization && !vtable->init_failed)
vtable->initialized = 1;
mono_type_initialization_unlock ();
/* If vtable init fails because of TAE, we don't throw TIE, only the TAE */
if (vtable->init_failed && !pending_tae) {
/* Either we were the initializing thread or we waited for the initialization */
mono_error_set_exception_instance (error, get_type_init_exception_for_vtable (vtable));
return FALSE;
}
return TRUE;
}
static
gboolean release_type_locks (gpointer key, gpointer value, gpointer user)
{
MONO_REQ_GC_NEUTRAL_MODE;
MonoVTable *vtable = (MonoVTable*)key;
TypeInitializationLock *lock = (TypeInitializationLock*) value;
if (mono_native_thread_id_equals (lock->initializing_tid, MONO_UINT_TO_NATIVE_THREAD_ID (GPOINTER_TO_UINT (user))) && !lock->done) {
lock->done = TRUE;
/*
* Have to set this since it cannot be set by the normal code in
* mono_runtime_class_init (). In this case, the exception object is not stored,
* and get_type_init_exception_for_class () needs to be aware of this.
*/
mono_type_init_lock (lock);
vtable->init_failed = 1;
mono_coop_cond_broadcast (&lock->cond);
mono_type_init_unlock (lock);
gboolean deleted = unref_type_lock (lock);
if (deleted)
return TRUE;
}
return FALSE;
}
void
mono_release_type_locks (MonoInternalThread *thread)
{
MONO_REQ_GC_UNSAFE_MODE;
mono_type_initialization_lock ();
g_hash_table_foreach_remove (type_initialization_hash, release_type_locks, GUINT_TO_POINTER (thread->tid));
mono_type_initialization_unlock ();
}
#ifndef DISABLE_REMOTING
static gpointer
create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target, MonoError *error)
{
if (!callbacks.create_remoting_trampoline)
g_error ("remoting not installed");
return callbacks.create_remoting_trampoline (domain, method, target, error);
}
#endif
static MonoImtTrampolineBuilder imt_trampoline_builder;
static gboolean always_build_imt_trampolines;
#if (MONO_IMT_SIZE > 32)
#error "MONO_IMT_SIZE cannot be larger than 32"
#endif
void
mono_install_callbacks (MonoRuntimeCallbacks *cbs)
{
memcpy (&callbacks, cbs, sizeof (*cbs));
}
MonoRuntimeCallbacks*
mono_get_runtime_callbacks (void)
{
return &callbacks;
}
void
mono_install_imt_trampoline_builder (MonoImtTrampolineBuilder func)
{
imt_trampoline_builder = func;
}
void
mono_set_always_build_imt_trampolines (gboolean value)
{
always_build_imt_trampolines = value;
}
/**
* mono_compile_method:
* \param 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)
{
MonoError error;
gpointer result = mono_compile_method_checked (method, &error);
mono_error_cleanup (&error);
return result;
}
/**
* mono_compile_method_checked:
* \param method The method to compile.
* \param error set on error.
* This JIT-compiles the method, and returns the pointer to the native code
* produced. On failure returns NULL and sets \p error.
*/
gpointer
mono_compile_method_checked (MonoMethod *method, MonoError *error)
{
gpointer res;
MONO_REQ_GC_NEUTRAL_MODE
error_init (error);
g_assert (callbacks.compile_method);
res = callbacks.compile_method (method, error);
return res;
}
gpointer
mono_runtime_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper, MonoError *error)
{
gpointer res;
MONO_REQ_GC_NEUTRAL_MODE;
error_init (error);
res = callbacks.create_jump_trampoline (domain, method, add_sync_wrapper, error);
return res;
}
gpointer
mono_runtime_create_delegate_trampoline (MonoClass *klass)
{
MONO_REQ_GC_NEUTRAL_MODE
g_assert (callbacks.create_delegate_trampoline);
return callbacks.create_delegate_trampoline (mono_domain_get (), klass);
}
/**
* mono_runtime_free_method:
* \param domain domain where the method is hosted
* \param 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)
{
MONO_REQ_GC_NEUTRAL_MODE
if (callbacks.free_method)
callbacks.free_method (domain, method);
mono_method_clear_object (domain, method);
mono_free_method (method);
}
/*
* 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 *klass, gsize *bitmap, int size, int offset, int *max_set, gboolean static_fields)
{
MONO_REQ_GC_NEUTRAL_MODE;
MonoClassField *field;
MonoClass *p;
guint32 pos;
int max_size;
if (static_fields)
max_size = mono_class_data_size (klass) / sizeof (gpointer);
else
max_size = klass->instance_size / sizeof (gpointer);
if (max_size > size) {
g_assert (offset <= 0);
bitmap = (gsize *)g_malloc0 ((max_size + BITMAP_EL_SIZE - 1) / BITMAP_EL_SIZE * sizeof (gsize));
size = max_size;
}
#ifdef HAVE_SGEN_GC
/*An Ephemeron cannot be marked by sgen*/
if (!static_fields && klass->image == mono_defaults.corlib && !strcmp ("Ephemeron", klass->name)) {
*max_set = 0;
memset (bitmap, 0, size / 8);
return bitmap;
}
#endif
for (p = klass; p != NULL; p = p->parent) {
gpointer iter = NULL;
while ((field = mono_class_get_fields (p, &iter))) {
MonoType *type;
if (static_fields) {
if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
continue;
if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
continue;
} else {
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;
if (static_fields && field->offset == -1)
/* special static */
continue;
pos = field->offset / sizeof (gpointer);
pos += offset;
type = mono_type_get_underlying_type (field->type);
switch (type->type) {
case MONO_TYPE_U:
case MONO_TYPE_I:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
break;
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);
g_assert (pos < size || pos <= max_size);
bitmap [pos / BITMAP_EL_SIZE] |= ((gsize)1) << (pos % BITMAP_EL_SIZE);
*max_set = MAX (*max_set, pos);
break;
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (type)) {
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;
} else {
/* fall through */
}
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, FALSE);
}
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_error ("compute_class_bitmap: Invalid type %x for field %s:%s\n", type->type, mono_type_get_full_name (field->parent), field->name);
break;
}
}
if (static_fields)
break;
}
return bitmap;
}
/**
* mono_class_compute_bitmap:
*
* Mono internal function to compute a bitmap of reference fields in a class.
*/
gsize*
mono_class_compute_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int *max_set, gboolean static_fields)
{
MONO_REQ_GC_NEUTRAL_MODE;
return compute_class_bitmap (klass, bitmap, size, offset, max_set, static_fields);
}
#if 0
/*
* similar to the above, but sets the bits in the bitmap for any non-ref field
* and ignores static fields
*/
static gsize*
compute_class_non_ref_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset)
{
MonoClassField *field;
MonoClass *p;
guint32 pos, pos2;
int max_size;
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) {
#if SIZEOF_VOID_P == 8
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
#endif
case MONO_TYPE_I8:
case MONO_TYPE_U8:
case MONO_TYPE_R8:
if ((((field->offset + 7) / sizeof (gpointer)) + offset) != pos) {
pos2 = ((field->offset + 7) / sizeof (gpointer)) + offset;
bitmap [pos2 / BITMAP_EL_SIZE] |= ((gsize)1) << (pos2 % BITMAP_EL_SIZE);
}
/* fall through */
#if SIZEOF_VOID_P == 4
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
#endif
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_R4:
if ((((field->offset + 3) / sizeof (gpointer)) + offset) != pos) {
pos2 = ((field->offset + 3) / sizeof (gpointer)) + offset;
bitmap [pos2 / BITMAP_EL_SIZE] |= ((gsize)1) << (pos2 % BITMAP_EL_SIZE);
}
/* fall through */
case MONO_TYPE_CHAR:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
if ((((field->offset + 1) / sizeof (gpointer)) + offset) != pos) {
pos2 = ((field->offset + 1) / sizeof (gpointer)) + offset;
bitmap [pos2 / BITMAP_EL_SIZE] |= ((gsize)1) << (pos2 % BITMAP_EL_SIZE);
}
/* fall through */
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
bitmap [pos / BITMAP_EL_SIZE] |= ((gsize)1) << (pos % BITMAP_EL_SIZE);
break;
case MONO_TYPE_STRING:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_CLASS:
case MONO_TYPE_OBJECT:
case MONO_TYPE_ARRAY:
break;
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (type)) {
break;
} else {
/* fall through */
}
case MONO_TYPE_VALUETYPE: {
MonoClass *fclass = mono_class_from_mono_type (field->type);
/* remove the object header */
compute_class_non_ref_bitmap (fclass, bitmap, size, pos - (sizeof (MonoObject) / sizeof (gpointer)));
break;
}
default:
g_assert_not_reached ();
break;
}
}
}
return bitmap;
}
/**
* mono_class_insecure_overlapping:
* check if a class with explicit layout has references and non-references
* fields overlapping.
*
* Returns: TRUE if it is insecure to load the type.
*/
gboolean
mono_class_insecure_overlapping (MonoClass *klass)
{
int max_set = 0;
gsize *bitmap;
gsize default_bitmap [4] = {0};
gsize *nrbitmap;
gsize default_nrbitmap [4] = {0};
int i, insecure = FALSE;
return FALSE;
bitmap = compute_class_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, FALSE);
nrbitmap = compute_class_non_ref_bitmap (klass, default_nrbitmap, sizeof (default_nrbitmap) * 8, 0);
for (i = 0; i <= max_set; i += sizeof (bitmap [0]) * 8) {
int idx = i % (sizeof (bitmap [0]) * 8);
if (bitmap [idx] & nrbitmap [idx]) {
insecure = TRUE;
break;
}
}
if (bitmap != default_bitmap)
g_free (bitmap);
if (nrbitmap != default_nrbitmap)
g_free (nrbitmap);
if (insecure) {
g_print ("class %s.%s in assembly %s has overlapping references\n", klass->name_space, klass->name, klass->image->name);
return FALSE;
}
return insecure;
}
#endif
MonoString*
ves_icall_string_alloc (int length)
{
MonoError error;
MonoString *str = mono_string_new_size_checked (mono_domain_get (), length, &error);
mono_error_set_pending_exception (&error);