-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathruntime.m
3286 lines (2832 loc) · 105 KB
/
runtime.m
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors: Rolf Bjarne Kvinge
*
* Copyright (C) 2014 Xamarin Inc. (www.xamarin.com)
*
*/
#include <pthread.h>
#include <objc/runtime.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include "product.h"
#include "shared.h"
#include "delegates.h"
#include "runtime-internal.h"
#include "xamarin/xamarin.h"
#if !defined (CORECLR_RUNTIME)
#include "xamarin/monovm-bridge.h"
#else
#include "xamarin/coreclr-bridge.h"
#endif
#if defined (DEBUG)
//extern BOOL NSZombieEnabled;
#endif
/*
* These are values that can be configured in xamarin_setup.
*
* The defaults values here have been chosen to minimize
* the configuration required for the simulator (in particular
* the simlauncher binaries).
*/
#if DEBUG
bool xamarin_gc_pump = false;
#endif
#if MONOMAC
// FIXME: implement release mode for monomac.
bool xamarin_debug_mode = true;
bool xamarin_mac_hybrid_aot = false;
bool xamarin_mac_modern = false;
#else
bool xamarin_debug_mode = false;
#endif
bool xamarin_disable_lldb_attach = false;
bool xamarin_disable_omit_fp = false;
#if DEBUG
bool xamarin_init_mono_debug = true;
#else
bool xamarin_init_mono_debug = false;
#endif
int xamarin_log_level = 0;
const char *xamarin_executable_name = NULL;
#if MONOMAC || TARGET_OS_MACCATALYST
NSString * xamarin_custom_bundle_name = @"MonoBundle";
#endif
#if MONOMAC
bool xamarin_is_mkbundle = false;
char *xamarin_entry_assembly_path = NULL;
#endif
#if defined (__i386__)
const char *xamarin_arch_name = "i386";
#elif defined (__x86_64__)
const char *xamarin_arch_name = "x86_64";
#else
const char *xamarin_arch_name = NULL;
#endif
#if TARGET_OS_WATCH
bool xamarin_is_gc_coop = true;
#else
bool xamarin_is_gc_coop = false;
#endif
enum MarshalObjectiveCExceptionMode xamarin_marshal_objectivec_exception_mode = MarshalObjectiveCExceptionModeDefault;
enum MarshalManagedExceptionMode xamarin_marshal_managed_exception_mode = MarshalManagedExceptionModeDefault;
enum XamarinTriState xamarin_log_exceptions = XamarinTriStateNone;
enum XamarinLaunchMode xamarin_launch_mode = XamarinLaunchModeApp;
#if SUPPORTS_DYNAMIC_REGISTRATION
bool xamarin_supports_dynamic_registration = true;
#endif
const char *xamarin_runtime_configuration_name = NULL;
#if DOTNET
enum XamarinNativeLinkMode xamarin_libmono_native_link_mode = XamarinNativeLinkModeStaticObject;
const char **xamarin_runtime_libraries = NULL;
#endif
/* Callbacks */
xamarin_setup_callback xamarin_setup = NULL;
xamarin_register_module_callback xamarin_register_modules = NULL;
xamarin_register_assemblies_callback xamarin_register_assemblies = NULL;
xamarin_extension_main_callback xamarin_extension_main = NULL;
/* Local variable */
static pthread_mutex_t framework_peer_release_lock;
static MonoGHashTable *xamarin_wrapper_hash;
static bool initialize_started = FALSE;
#include "delegates.inc"
/* Keep Trampolines, InitializationFlags and InitializationOptions in sync with Runtime.cs */
struct Trampolines {
void* tramp;
void* stret_tramp;
void* fpret_single_tramp;
void* fpret_double_tramp;
void* release_tramp;
void* retain_tramp;
void* static_tramp;
void* ctor_tramp;
void* x86_double_abi_stret_tramp;
void* static_fpret_single_tramp;
void* static_fpret_double_tramp;
void* static_stret_tramp;
void* x86_double_abi_static_stret_tramp;
void* long_tramp;
void* static_long_tramp;
#if MONOMAC
void* copy_with_zone1;
void* copy_with_zone2;
#endif
void* get_gchandle_tramp;
void* set_gchandle_tramp;
void* get_flags_tramp;
void* set_flags_tramp;
};
enum InitializationFlags : int {
InitializationFlagsIsPartialStaticRegistrar = 0x01,
InitializationFlagsIsManagedStaticRegistrar = 0x02,
/* unused = 0x04,*/
/* unused = 0x08,*/
InitializationFlagsIsSimulator = 0x10,
InitializationFlagsIsCoreCLR = 0x20,
InitializationFlagsIsNativeAOT = 0x40,
};
struct InitializationOptions {
int size; // the size of this structure. This is used for version checking.
enum InitializationFlags flags;
struct Delegates* Delegates;
struct Trampolines* Trampolines;
struct MTRegistrationMap* RegistrationData;
enum MarshalObjectiveCExceptionMode MarshalObjectiveCExceptionMode;
enum MarshalManagedExceptionMode MarshalManagedExceptionMode;
#if MONOMAC
enum XamarinLaunchMode LaunchMode;
const char *EntryAssemblyPath;
#endif
struct AssemblyLocations* AssemblyLocations;
#if DOTNET
// This struct must be kept in sync with the corresponding struct in Runtime.cs, and since we use the same managed code for both MonoVM and CoreCLR,
// we can't restrict the following fields to CORECLR_RUNTIME only, we can only exclude it from legacy Xamarin.
void *xamarin_objc_msgsend;
void *xamarin_objc_msgsend_super;
void *xamarin_objc_msgsend_stret;
void *xamarin_objc_msgsend_super_stret;
void *unhandled_exception_handler;
void *reference_tracking_begin_end_callback;
void *reference_tracking_is_referenced_callback;
void *reference_tracking_tracked_object_entered_finalization;
#endif
};
static struct Trampolines trampolines = {
(void *) &xamarin_trampoline,
(void *) &xamarin_stret_trampoline,
(void *) &xamarin_fpret_single_trampoline,
(void *) &xamarin_fpret_double_trampoline,
(void *) &xamarin_release_trampoline,
(void *) &xamarin_retain_trampoline,
(void *) &xamarin_static_trampoline,
(void *) &xamarin_ctor_trampoline,
#if defined (__i386__)
(void *) &xamarin_x86_double_abi_stret_trampoline,
#else
NULL,
#endif
(void *) &xamarin_static_fpret_single_trampoline,
(void *) &xamarin_static_fpret_double_trampoline,
(void *) &xamarin_static_stret_trampoline,
#if defined (__i386__)
(void *) &xamarin_static_x86_double_abi_stret_trampoline,
#else
NULL,
#endif
(void *) &xamarin_longret_trampoline,
(void *) &xamarin_static_longret_trampoline,
#if MONOMAC
(void *) &xamarin_copyWithZone_trampoline1,
(void *) &xamarin_copyWithZone_trampoline2,
#endif
(void *) &xamarin_get_gchandle_trampoline,
(void *) &xamarin_set_gchandle_trampoline,
(void *) &xamarin_get_flags_trampoline,
(void *) &xamarin_set_flags_trampoline,
};
static struct InitializationOptions options = { 0 };
#if !defined (CORECLR_RUNTIME)
void
xamarin_add_internal_call (const char *name, const void *method)
{
/* COOP: With cooperative GC, icalls will run, like managed methods,
* in GC Unsafe mode, avoiding a thread state transition. In return
* the icalls must guarantee that they won't block, or run indefinitely
* without a safepoint, by manually performing a transition to GC Safe
* mode. With backward-compatible hybrid GC, icalls run in GC Safe
* mode and the Mono API functions take care of thread state
* transitions, so don't need to perform GC thread state transitions
* themselves.
*
*/
if (xamarin_is_gc_coop)
mono_dangerous_add_raw_internal_call (name, method);
else
mono_add_internal_call (name, method);
}
#endif // !CORECLR_RUNTIME
id
xamarin_get_nsobject_handle (MonoObject *obj)
{
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
#if defined (CORECLR_RUNTIME)
id rv = xamarin_get_handle_for_inativeobject (obj);
LOG_CORECLR (stderr, "xamarin_get_nsobject_handle (%p) => %p\n", obj, rv);
return rv;
#else
struct Managed_NSObject *mobj = (struct Managed_NSObject *) obj;
return mobj->handle;
#endif
}
uint8_t
xamarin_get_nsobject_flags (MonoObject *obj)
{
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
#if defined (CORECLR_RUNTIME)
return xamarin_get_flags_for_nsobject (obj->gchandle);
#else
struct Managed_NSObject *mobj = (struct Managed_NSObject *) obj;
return mobj->flags;
#endif
}
void
xamarin_set_nsobject_flags (MonoObject *obj, uint8_t flags)
{
// COOP: Writing managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
#if defined (CORECLR_RUNTIME)
xamarin_set_flags_for_nsobject (obj->gchandle, flags);
#else
struct Managed_NSObject *mobj = (struct Managed_NSObject *) obj;
mobj->flags = flags;
#endif
}
MonoType *
xamarin_get_parameter_type (MonoMethod *managed_method, int index)
{
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
MonoMethodSignature *msig = mono_method_signature (managed_method);
void *iter = NULL;
MonoType *p = NULL;
if (index == -1) {
p = mono_signature_get_return_type (msig);
} else {
for (int i = 0; i < index + 1; i++) {
xamarin_mono_object_release (&p);
p = mono_signature_get_params (msig, &iter);
}
}
xamarin_bridge_free_mono_signature (&msig);
return p;
}
MonoObject *
xamarin_get_nsobject_with_type_for_ptr (id self, bool owns, MonoType* type, GCHandle *exception_gchandle)
{
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
int32_t created;
return xamarin_get_nsobject_with_type_for_ptr_created (self, owns, type, &created, exception_gchandle);
}
MonoObject *
xamarin_get_nsobject_with_type_for_ptr_created (id self, bool owns, MonoType *type, int32_t *created, GCHandle *exception_gchandle)
{
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
MonoObject *mobj = NULL;
GCHandle gchandle = INVALID_GCHANDLE;
*created = false;
if (self == NULL)
return NULL;
gchandle = xamarin_get_gchandle (self);
if (gchandle != INVALID_GCHANDLE) {
mobj = xamarin_gchandle_get_target (gchandle);
MonoClass *klass = mono_class_from_mono_type (type);
bool isinst = mono_object_isinst (mobj, klass) != NULL;
xamarin_mono_object_release (&klass);
if (isinst) {
return mobj;
} else {
xamarin_mono_object_release (&mobj);
}
}
MonoReflectionType *rtype = mono_type_get_object (mono_domain_get (), type);
MonoObject *rv = xamarin_get_nsobject_with_type (self, rtype, created, exception_gchandle);
xamarin_mono_object_release (&rtype);
return rv;
}
MonoObject *
xamarin_get_managed_object_for_ptr_fast (id self, GCHandle *exception_gchandle)
{
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
MonoObject *mobj = NULL;
GCHandle gchandle = INVALID_GCHANDLE;
gchandle = xamarin_get_gchandle (self);
if (gchandle == INVALID_GCHANDLE) {
mobj = xamarin_try_get_or_construct_nsobject (self, exception_gchandle);
} else {
mobj = xamarin_gchandle_get_target (gchandle);
#if DEBUG
if (self != xamarin_get_nsobject_handle (mobj)) {
xamarin_assertion_message ("Internal consistency error, please file a bug (https://github.com/xamarin/xamarin-macios/issues/new). Additional data: found managed object %p=%p (%s) in native object %p (%s).\n",
mobj, xamarin_get_nsobject_handle (mobj), xamarin_class_get_full_name (mono_object_get_class (mobj), exception_gchandle), self, object_getClassName (self));
}
#endif
}
return mobj;
}
// See comments in the following methods to explain the logic here:
// xamarin_marshal_return_value_impl in trampolines.m
// xamarin_release_managed_ref in runtime.m
void xamarin_framework_peer_waypoint ()
{
// COOP: CHECK
MONO_ASSERT_GC_UNSAFE;
MONO_ENTER_GC_SAFE;
pthread_mutex_lock (&framework_peer_release_lock);
pthread_mutex_unlock (&framework_peer_release_lock);
MONO_EXIT_GC_SAFE;
}
// Same as xamarin_framework_peer_waypoint, except the current mode should be GC Safe.
void xamarin_framework_peer_waypoint_safe ()
{
MONO_ASSERT_GC_SAFE_OR_DETACHED;
pthread_mutex_lock (&framework_peer_release_lock);
pthread_mutex_unlock (&framework_peer_release_lock);
}
MonoObject *
xamarin_new_nsobject (id self, MonoClass *klass, GCHandle *exception_gchandle)
{
MonoType *type = mono_class_get_type (klass);
MonoReflectionType *rtype = mono_type_get_object (mono_domain_get (), type);
xamarin_mono_object_release (&type);
GCHandle obj = xamarin_create_nsobject (rtype, self, NSObjectFlagsNativeRef, exception_gchandle);
xamarin_mono_object_release (&rtype);
return xamarin_gchandle_unwrap (obj);
}
// Returns if a MonoClass is nullable.
// Will also return the element type (it the type is nullable, and if out pointer is not NULL).
bool
xamarin_is_class_nullable (MonoClass *cls, MonoClass **element_type, GCHandle *exception_gchandle)
{
#ifdef DYNAMIC_MONO_RUNTIME
// mono_class_is_nullable/mono_class_get_nullable_param are private
// functions, and as such we can't call find them in libmono.dylib. In
// this case we manually call a managed function to do the work for us (we
// don't use the normal delegate mechanism, because how it's currently
// implemented it would inflict size costs on all platforms, not just
// Xamarin.Mac).
if (!mono_class_is_nullable_exists () || !mono_class_get_nullable_param_exists ()) {
static MonoMethod *get_nullable_type = NULL;
if (get_nullable_type == NULL)
get_nullable_type = mono_class_get_method_from_name (xamarin_get_runtime_class (), "GetNullableType", 1);
GCHandle type_handle = xamarin_gchandle_new ((MonoObject *) mono_type_get_object (mono_domain_get (), mono_class_get_type (cls)), false);
void *args [1] { type_handle };
MonoObject *exc = NULL;
MonoObject *nullable_type_handle = mono_runtime_invoke (get_nullable_type, NULL, args, &exc);
xamarin_gchandle_free (type_handle);
if (exc != NULL) {
*exception_gchandle = xamarin_gchandle_new (exc, FALSE);
return false;
}
MonoReflectionType *nullable_type = (MonoReflectionType *) xamarin_gchandle_unwrap (nullable_type_handle);
if (element_type != NULL && nullable_type != NULL) {
MonoType *mono_type = mono_reflection_type_get_type (nullable_type);
*element_type = mono_class_from_mono_type (mono_type);
xamarin_mono_object_release (&mono_type);
}
bool is_nullable = nullable_type != NULL;
xamarin_mono_object_release (&nullable_type);
return is_nullable;
}
#endif
bool rv = mono_class_is_nullable (cls);
if (rv && element_type)
*element_type = mono_class_get_nullable_param (cls);
return rv;
}
MonoClass *
xamarin_get_nullable_type (MonoClass *cls, GCHandle *exception_gchandle)
{
MonoClass *rv = NULL;
xamarin_is_class_nullable (cls, &rv, exception_gchandle);
return rv;
}
// The XamarinExtendedObject protocol is just to avoid a
// compiler warning (no 'xamarinGetGChandle' selector found).
@protocol XamarinExtendedObject
-(GCHandle) xamarinGetGCHandle;
-(bool) xamarinSetGCHandle: (GCHandle) gc_handle flags: (enum XamarinGCHandleFlags) flags;
-(enum XamarinGCHandleFlags) xamarinGetFlags;
-(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags;
@end
static inline GCHandle
get_gchandle_safe (id self, enum XamarinGCHandleFlags *flags)
{
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_SAFE_OR_DETACHED;
id<XamarinExtendedObject> xself = self;
GCHandle rv = [xself xamarinGetGCHandle];
if (flags)
*flags = [xself xamarinGetFlags];
return rv;
}
static inline bool
set_gchandle (id self, GCHandle gc_handle, enum XamarinGCHandleFlags flags)
{
bool rv;
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_UNSAFE;
MONO_ENTER_GC_SAFE;
id<XamarinExtendedObject> xself = self;
rv = [xself xamarinSetGCHandle: gc_handle flags: flags];
MONO_EXIT_GC_SAFE;
return rv;
}
static inline bool
set_gchandle_safe (id self, GCHandle gc_handle, enum XamarinGCHandleFlags flags)
{
bool rv;
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_SAFE_OR_DETACHED;
id<XamarinExtendedObject> xself = self;
rv = [xself xamarinSetGCHandle: gc_handle flags: flags];
return rv;
}
static inline GCHandle
get_gchandle_without_flags (id self)
{
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_UNSAFE;
GCHandle rv;
MONO_ENTER_GC_SAFE;
id<XamarinExtendedObject> xself = self;
rv = (GCHandle) [xself xamarinGetGCHandle];
MONO_EXIT_GC_SAFE;
return rv;
}
static inline GCHandle
get_gchandle_with_flags (id self, enum XamarinGCHandleFlags* flags)
{
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_UNSAFE;
GCHandle rv;
MONO_ENTER_GC_SAFE;
id<XamarinExtendedObject> xself = self;
rv = (GCHandle) [xself xamarinGetGCHandle];
if (flags != NULL)
*flags = [xself xamarinGetFlags];
MONO_EXIT_GC_SAFE;
return rv;
}
static inline enum XamarinGCHandleFlags
get_flags (id self)
{
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_UNSAFE;
enum XamarinGCHandleFlags rv;
MONO_ENTER_GC_SAFE;
id<XamarinExtendedObject> xself = self;
rv = [xself xamarinGetFlags];
MONO_EXIT_GC_SAFE;
return rv;
}
static inline void
set_flags_safe (id self, enum XamarinGCHandleFlags flags)
{
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_SAFE_OR_DETACHED;
id<XamarinExtendedObject> xself = self;
[xself xamarinSetFlags: flags];
}
static inline enum XamarinGCHandleFlags
get_flags_safe (id self)
{
// COOP: we call a selector, and that must only be done in SAFE mode.
MONO_ASSERT_GC_SAFE_OR_DETACHED;
enum XamarinGCHandleFlags rv;
id<XamarinExtendedObject> xself = self;
rv = [xself xamarinGetFlags];
return rv;
}
GCHandle
xamarin_get_gchandle (id self)
{
// COOP: does not access managed memory: any mode
return get_gchandle_without_flags (self);
}
GCHandle
xamarin_get_gchandle_with_flags (id self, enum XamarinGCHandleFlags* flags)
{
// COOP: does not access managed memory: any mode
return get_gchandle_with_flags (self, flags);
}
bool
xamarin_has_managed_ref (id self)
{
// COOP: get_flags requires UNSAFE mode, so this function requires it too.
return (get_flags (self) & XamarinGCHandleFlags_HasManagedRef) == XamarinGCHandleFlags_HasManagedRef;
}
bool
xamarin_has_managed_ref_safe (id self)
{
// COOP: variation of xamarin_has_managed_ref for SAFE mode.
return (get_flags_safe (self) & XamarinGCHandleFlags_HasManagedRef) == XamarinGCHandleFlags_HasManagedRef;
}
MonoException *
xamarin_create_exception (const char *msg)
{
// COOP: calls mono, needs to be in UNSAFE mode.
MONO_ASSERT_GC_UNSAFE;
return xamarin_create_system_exception (msg);
}
MonoMethod *
xamarin_get_reflection_method_method (MonoReflectionMethod *method)
{
return xamarin_bridge_get_mono_method (method);
}
id
xamarin_get_handle (MonoObject *obj, GCHandle *exception_gchandle)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
MonoClass *klass;
id rv = nil;
if (obj == NULL)
return nil;
klass = mono_object_get_class (obj);
if (xamarin_is_class_nsobject (klass)) {
rv = xamarin_get_nsobject_handle (obj);
} else if (xamarin_is_class_inativeobject (klass)) {
rv = xamarin_get_handle_for_inativeobject (obj, exception_gchandle);
} else {
char *msg = xamarin_strdup_printf ("Unable to marshal from %s.%s to an Objective-C object. "
"The managed class must either inherit from NSObject or implement INativeObject.",
mono_class_get_namespace (klass), mono_class_get_name (klass));
GCHandle ex_handle = xamarin_create_runtime_exception (8039, msg, exception_gchandle);
xamarin_free (msg);
if (*exception_gchandle == INVALID_GCHANDLE)
*exception_gchandle = ex_handle;
}
xamarin_mono_object_release (&klass);
return rv;
}
#if DEBUG
static void
verify_cast (MonoClass *to, MonoObject *obj, Class from_class, SEL sel, MonoMethod *method, GCHandle *exception_gchandle)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
if (!to)
return;
if (mono_object_isinst (obj, to) == NULL) {
MonoClass *from = mono_object_get_class (obj);
char *method_full_name = mono_method_full_name (method, TRUE);
char *from_name = xamarin_class_get_full_name (from, exception_gchandle);
char *to_name = xamarin_class_get_full_name (to, exception_gchandle);
char *msg = xamarin_strdup_printf ("Unable to cast object of type '%s' (Objective-C type: '%s') to type '%s'.\n"
"Additional information:\n"
"\tSelector: %s\n"
"\tMethod: %s\n", from_name, class_getName(from_class), to_name, sel_getName (sel), method_full_name);
MonoException *mono_ex = xamarin_create_system_invalid_cast_exception (msg);
mono_free (from_name);
mono_free (to_name);
xamarin_free (msg);
mono_free (method_full_name);
xamarin_mono_object_release (&from);
*exception_gchandle = xamarin_gchandle_new ((MonoObject *) mono_ex, FALSE);
}
}
#endif
void
xamarin_check_for_gced_object (MonoObject *obj, SEL sel, id self, MonoMethod *method, GCHandle *exception_gchandle)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
if (obj != NULL) {
#if DEBUG
MonoClass *declaring_type = mono_method_get_class (method);
verify_cast (declaring_type, obj, [self class], sel, method, exception_gchandle);
xamarin_mono_object_release (&declaring_type);
#endif
return;
}
#if DOTNET
const char *m = "Failed to marshal the Objective-C object %p (type: %s). "
"Could not find an existing managed instance for this object, "
"nor was it possible to create a new managed instance "
"(because the type '%s' does not have a constructor that takes one NativeHandle argument).\n"
"Additional information:\n"
"\tSelector: %s\n"
"\tMethod: %s\n";
#else
const char *m = "Failed to marshal the Objective-C object %p (type: %s). "
"Could not find an existing managed instance for this object, "
"nor was it possible to create a new managed instance "
"(because the type '%s' does not have a constructor that takes one IntPtr argument).\n"
"Additional information:\n"
"\tSelector: %s\n"
"\tMethod: %s\n";
#endif
char *method_full_name = mono_method_full_name (method, TRUE);
char *type_name = xamarin_lookup_managed_type_name ([self class], exception_gchandle);
if (*exception_gchandle == INVALID_GCHANDLE) {
char *msg = xamarin_strdup_printf (m, self, object_getClassName (self), type_name, sel_getName (sel), method_full_name);
GCHandle ex_handle = xamarin_create_runtime_exception (8027, msg, exception_gchandle);
xamarin_free (msg);
if (*exception_gchandle == INVALID_GCHANDLE)
*exception_gchandle = ex_handle;
}
mono_free (type_name);
mono_free (method_full_name);
}
#if DEBUG
//
// We can't do the type-checks below correctly until we support multiple managed peers for each
// native objects. The problem is that Objective-C can fake multiple inheritance by
// overriding isKindOfClass: A test case can be seen in bug #23421: A parameter in a
// callback from Objective-C is typed as 'NSUrlSessionDownloadTask'. Objective-C may give us an instance
// of an internal class __NSCFBackgroundDownloadTask, which doesn't inherit from NSUrlSessionDownloadTask
// (it inherits from NSUrlSessionTask, which is a superclass of NSUrlSessionDownloadTask). In Objective-C
// the __NSCFBackgroundDownloadTask class gets away with this because it overrides isKindOfClass: to return
// YES for NSUrlSessionDownloadTask. We can't rely on isKindOfClass: when creating a managed peer for
// the native object, because we may already have an instance of the "correct" type (if we
// had support for multiple managed peers, we could just create a new instance of the expected type).
//
void
xamarin_verify_parameter (MonoObject *obj, SEL sel, id self, id arg, unsigned long index, MonoClass *expected, MonoMethod *method)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
// if (arg == NULL)
// return;
//
// if (obj != NULL) {
// verify_cast (expected, obj, [arg class], sel, method);
// return;
// }
//
// const char *m = "Failed to marshal the Objective-C object 0x%x (type: %s). "
// "Could not find an existing managed instance for this object, "
// "nor was it possible to create a new managed instance "
// "(because the type '%s' does not have a constructor that takes one IntPtr argument).\n"
// "Additional information:\n"
// "\tSelector: %s\n"
// "\tMethod: %s\n"
// "\tParameter: %i\n";
//
// char *method_full_name = mono_method_full_name (method, TRUE);
// char *type_name = xamarin_lookup_managed_type_name ([arg class]);
// char *msg = xamarin_strdup_printf (m, arg, object_getClassName (arg), type_name, sel, method_full_name, index);
// MonoException *mex = xamarin_create_exception (msg);
// xamarin_free (msg);
// mono_free (method_full_name);
// mono_free (type_name);
// mono_raise_exception (mex);
}
void
xamarin_check_objc_type (id obj, Class expected_class, SEL sel, id self, int index, MonoMethod *method)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
// if ([obj isKindOfClass:expected_class])
// return;
//
// const char *m = "Failed to marshal the Objective-C object 0x%x (type: %s), expected an object of type %s.\n"
// "Additional information:\n"
// "\tSelector: %s\n"
// "\tMethod: %s\n"
// "\tParameter: %i\n";
//
// char *method_full_name = mono_method_full_name (method, TRUE);
// char *msg = xamarin_strdup_printf (m, obj, object_getClassName (obj), class_getName (expected_class), sel, method_full_name, index);
// MonoException *mono_ex = xamarin_create_system_invalid_cast_exception (msg);
// xamarin_free (msg);
// mono_free (method_full_name);
// mono_raise_exception (mono_ex);
}
#endif
char *
xamarin_class_get_full_name (MonoClass *klass, GCHandle *exception_gchandle)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
MonoType *type = mono_class_get_type (klass);
char * rv = xamarin_type_get_full_name (type, exception_gchandle);
xamarin_mono_object_release (&type);
return rv;
}
char *
xamarin_type_get_full_name (MonoType *type, GCHandle *exception_gchandle)
{
// COOP: Reads managed memory, needs to be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;
MonoReflectionType *rtype = mono_type_get_object (mono_domain_get (), type);
char *rv = xamarin_reflection_type_get_full_name (rtype, exception_gchandle);
xamarin_mono_object_release (&rtype);
return rv;
}
/*
* ToggleRef support
*/
// #define DEBUG_TOGGLEREF 1
MonoToggleRefStatus
xamarin_gc_toggleref_callback (uint8_t flags, id handle, xamarin_get_handle_func get_handle, MonoObject *info)
{
// COOP: this is a callback called by the GC, so I assume the mode here doesn't matter
MonoToggleRefStatus res;
bool disposed = (flags & NSObjectFlagsDisposed) == NSObjectFlagsDisposed;
bool has_managed_ref = (flags & NSObjectFlagsHasManagedRef) == NSObjectFlagsHasManagedRef;
if (disposed || !has_managed_ref) {
res = MONO_TOGGLE_REF_DROP; /* Already disposed, we don't need the managed object around */
} else {
if (handle == NULL)
handle = get_handle (info);
if (handle == NULL) { /* This shouldn't really happen */
res = MONO_TOGGLE_REF_DROP;
} else {
if ([handle retainCount] == 1)
res = MONO_TOGGLE_REF_WEAK;
else
res = MONO_TOGGLE_REF_STRONG;
}
}
#ifdef DEBUG_TOGGLEREF
const char *rv;
if (res == MONO_TOGGLE_REF_DROP) {
rv = "DROP";
} else if (res == MONO_TOGGLE_REF_STRONG) {
rv = "STRONG";
} else if (res == MONO_TOGGLE_REF_WEAK) {
rv = "WEAK";
} else {
rv = "UNKNOWN";
}
const char *cn = NULL;
if (handle == NULL)
handle = get_handle (info);
cn = object_getClassName (handle);
PRINT ("\tinspecting %p handle:%p %s flags: %i RC %d -> %s\n", object, handle, cn, (int) flags, (int) (handle ? [handle retainCount] : 0), rv);
#endif
return res;
}
void
xamarin_gc_event (MonoGCEvent event)
{
// COOP: this is a callback called by the GC, I believe the mode here doesn't matter.
switch (event) {
case MONO_GC_EVENT_PRE_STOP_WORLD:
pthread_mutex_lock (&framework_peer_release_lock);
break;
case MONO_GC_EVENT_POST_START_WORLD:
pthread_mutex_unlock (&framework_peer_release_lock);
break;
default: // silences a compiler warning.
break;
}
}
#if !defined (CORECLR_RUNTIME)
struct _MonoProfiler {
int dummy;
};
static void
xamarin_install_mono_profiler ()
{
static _MonoProfiler profiler = { 0 };
// This must be done before any other mono_profiler_install_* functions are called
// (currently xamarin_enable_new_refcount and xamarin_install_nsautoreleasepool_hooks).
mono_profiler_install (&profiler, NULL);
}
#endif
bool
xamarin_file_exists (const char *path)
{
// COOP: no managed access: any mode
struct stat buffer;
return stat (path, &buffer) == 0;
}
static MonoAssembly *
xamarin_open_assembly_or_assert (const char *name)
{
MonoImageOpenStatus status = MONO_IMAGE_OK;
MonoAssembly *assembly = mono_assembly_open (name, &status);
if (assembly == NULL)
xamarin_assertion_message ("Failed to open the assembly '%s' from the app: %i (errno: %i). This is usually fixed by cleaning and rebuilding your project; if that doesn't work, please file a bug report: https://github.com/xamarin/xamarin-macios/issues/new", name, (int) status, errno);
return assembly;
}
// Returns a retained MonoObject. Caller must release.
MonoAssembly *
xamarin_open_assembly (const char *name)
{
// COOP: this is a function executed only at startup, I believe the mode here doesn't matter.
char path [1024];
bool exists = false;
#if MONOMAC
if (xamarin_get_is_mkbundle ())
return xamarin_open_assembly_or_assert (name);
#endif
exists = xamarin_locate_assembly_resource (name, NULL, name, path, sizeof (path));
#if MONOMAC && DYLIB
if (!exists) {
// Check if we already have the assembly in memory
xamarin_get_assembly_name_without_extension (name, path, sizeof (path));
MonoAssemblyName *aname = mono_assembly_name_new (path);
MonoAssembly *assembly = mono_assembly_loaded (aname);
mono_assembly_name_free (aname);
if (assembly)
return assembly;
xamarin_assertion_message ("Could not find the assembly '%s' in the app nor as an already loaded assembly. This is usually fixed by cleaning and rebuilding your project; if that doesn't work, please file a bug report: https://github.com/xamarin/xamarin-macios/issues/new", name);
}
#endif
if (!exists)
xamarin_assertion_message ("Could not find the assembly '%s' in the app. This is usually fixed by cleaning and rebuilding your project; if that doesn't work, please file a bug report: https://github.com/xamarin/xamarin-macios/issues/new", name);
return xamarin_open_assembly_or_assert (path);
}
bool
xamarin_register_monoassembly (MonoAssembly *assembly, GCHandle *exception_gchandle)
{
// COOP: this is a function executed only at startup, I believe the mode here doesn't matter.
#if SUPPORTS_DYNAMIC_REGISTRATION
if (!xamarin_supports_dynamic_registration) {
#endif
#if defined (CORECLR_RUNTIME)
if (xamarin_log_level > 0) {
MonoReflectionAssembly *rassembly = mono_assembly_get_object (mono_domain_get (), assembly);
GCHandle assembly_gchandle = xamarin_gchandle_new ((MonoObject *) rassembly, false);
xamarin_mono_object_release (&rassembly);
char *assembly_name = xamarin_bridge_get_assembly_name (assembly_gchandle);
xamarin_gchandle_free (assembly_gchandle);
LOG (PRODUCT ": Skipping assembly registration for %s since it's not needed (dynamic registration is not supported)", assembly_name);
mono_free (assembly_name);
}
#else
LOG (PRODUCT ": Skipping assembly registration for %s since it's not needed (dynamic registration is not supported)", mono_assembly_name_get_name (mono_assembly_get_name (assembly)));
#endif
return true;
#if SUPPORTS_DYNAMIC_REGISTRATION
}
#endif
#if SUPPORTS_DYNAMIC_REGISTRATION
MonoReflectionAssembly *rassembly = mono_assembly_get_object (mono_domain_get (), assembly);
xamarin_register_assembly (rassembly, exception_gchandle);
xamarin_mono_object_release (&rassembly);
return *exception_gchandle == INVALID_GCHANDLE;
#endif // SUPPORTS_DYNAMIC_REGISTRATION
}
// Returns a retained MonoObject. Caller must release.