5 * Dietmar Maurer (dietmar@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
8 * Marek Safar (marek.safar@gmail.com)
9 * Aleksey Kliger (aleksey@xamarin.com)
11 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
12 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
13 * Copyright 2011-2015 Xamarin Inc (http://www.xamarin.com).
14 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
19 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
20 /* Needed for _ecvt_s */
21 #define MINGW_HAS_SECURE_API 1
32 #ifdef HAVE_SYS_TIME_H
38 #if defined (HAVE_WCHAR_H)
42 #include "mono/metadata/icall-internals.h"
43 #include "mono/utils/mono-membar.h"
44 #include <mono/metadata/object.h>
45 #include <mono/metadata/threads.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/metadata/threadpool.h>
48 #include <mono/metadata/threadpool-io.h>
49 #include <mono/metadata/monitor.h>
50 #include <mono/metadata/reflection.h>
51 #include <mono/metadata/image-internals.h>
52 #include <mono/metadata/assembly.h>
53 #include <mono/metadata/assembly-internals.h>
54 #include <mono/metadata/tabledefs.h>
55 #include <mono/metadata/exception.h>
56 #include <mono/metadata/exception-internals.h>
57 #include <mono/metadata/w32file.h>
58 #include <mono/metadata/console-io.h>
59 #include <mono/metadata/mono-route.h>
60 #include <mono/metadata/w32socket.h>
61 #include <mono/metadata/mono-endian.h>
62 #include <mono/metadata/tokentype.h>
63 #include <mono/metadata/metadata-internals.h>
64 #include <mono/metadata/class-internals.h>
65 #include <mono/metadata/class-init.h>
66 #include <mono/metadata/reflection-internals.h>
67 #include <mono/metadata/marshal.h>
68 #include <mono/metadata/gc-internals.h>
69 #include <mono/metadata/mono-gc.h>
70 #include <mono/metadata/rand.h>
71 #include <mono/metadata/appdomain-icalls.h>
72 #include <mono/metadata/string-icalls.h>
73 #include <mono/metadata/debug-helpers.h>
74 #include <mono/metadata/w32process.h>
75 #include <mono/metadata/environment.h>
76 #include <mono/metadata/profiler-private.h>
77 #include <mono/metadata/locales.h>
78 #include <mono/metadata/filewatcher.h>
79 #include <mono/metadata/security.h>
80 #include <mono/metadata/mono-config.h>
81 #include <mono/metadata/cil-coff.h>
82 #include <mono/metadata/number-formatter.h>
83 #include <mono/metadata/security-manager.h>
84 #include <mono/metadata/security-core-clr.h>
85 #include <mono/metadata/mono-perfcounters.h>
86 #include <mono/metadata/mono-debug.h>
87 #include <mono/metadata/mono-ptr-array.h>
88 #include <mono/metadata/verify-internals.h>
89 #include <mono/metadata/runtime.h>
90 #include <mono/metadata/file-mmap.h>
91 #include <mono/metadata/seq-points-data.h>
92 #include <mono/metadata/icall-table.h>
93 #include <mono/metadata/handle.h>
94 #include <mono/metadata/w32mutex.h>
95 #include <mono/metadata/w32semaphore.h>
96 #include <mono/metadata/w32event.h>
97 #include <mono/metadata/abi-details.h>
98 #include <mono/metadata/loader-internals.h>
99 #include <mono/utils/monobitset.h>
100 #include <mono/utils/mono-time.h>
101 #include <mono/utils/mono-proclib.h>
102 #include <mono/utils/mono-string.h>
103 #include <mono/utils/mono-error-internals.h>
104 #include <mono/utils/mono-mmap.h>
105 #include <mono/utils/mono-io-portability.h>
106 #include <mono/utils/mono-digest.h>
107 #include <mono/utils/bsearch.h>
108 #include <mono/utils/mono-os-mutex.h>
109 #include <mono/utils/mono-threads.h>
110 #include <mono/metadata/w32error.h>
111 #include <mono/utils/w32api.h>
112 #include <mono/utils/mono-merp.h>
113 #include <mono/utils/mono-state.h>
114 #include <mono/utils/mono-logger-internals.h>
115 #include <mono/utils/mono-math.h>
116 #if !defined(HOST_WIN32) && defined(HAVE_SYS_UTSNAME_H)
117 #include <sys/utsname.h>
119 #include "icall-decl.h"
120 #include "mono/utils/mono-threads-coop.h"
121 #include "mono/metadata/icall-signatures.h"
123 //#define MONO_DEBUG_ICALLARRAY
125 #ifdef MONO_DEBUG_ICALLARRAY
127 static char debug_icallarray
; // 0:uninitialized 1:true 2:false
130 icallarray_print_enabled (void)
132 if (!debug_icallarray
)
133 debug_icallarray
= MONO_TRACE_IS_TRACED (G_LOG_LEVEL_DEBUG
, MONO_TRACE_ICALLARRAY
) ? 1 : 2;
134 return debug_icallarray
== 1;
138 icallarray_print (const char *format
, ...)
140 if (!icallarray_print_enabled ())
143 va_start (args
, format
);
144 g_printv (format
, args
);
149 #define icallarray_print_enabled() (FALSE)
150 #define icallarray_print(...) /* nothing */
153 /* Lazy class loading functions */
154 static GENERATE_GET_CLASS_WITH_CACHE (module
, "System.Reflection", "Module")
157 array_set_value_impl (MonoArrayHandle arr
, MonoObjectHandle value
, guint32 pos
, gboolean strict
, MonoError
*error
);
159 static MonoArrayHandle
160 type_array_from_modifiers (MonoImage
*image
, MonoType
*type
, int optional
, MonoError
*error
);
162 static inline MonoBoolean
163 is_generic_parameter (MonoType
*type
)
165 return !type
->byref
&& (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
);
170 mono_icall_make_platform_path (gchar
*path
)
175 static inline const gchar
*
176 mono_icall_get_file_path_prefix (const gchar
*path
)
180 #endif /* HOST_WIN32 */
185 MonoJitICallInfos mono_jit_icall_info
;
188 mono_get_jit_icall_info (void)
190 return &mono_jit_icall_info
;
195 MonoJitICallInfos mono_jit_icall_info
;
200 ves_icall_System_Array_GetValueImpl (MonoArrayHandle array
, guint32 pos
, MonoError
*error
)
202 MonoClass
* const array_class
= mono_handle_class (array
);
203 MonoClass
* const element_class
= m_class_get_element_class (array_class
);
205 #ifdef ENABLE_NETCORE
206 if (m_class_is_native_pointer (element_class
)) {
207 mono_error_set_not_supported (error
, NULL
);
212 if (m_class_is_valuetype (element_class
)) {
213 gsize element_size
= mono_array_element_size (array_class
);
214 gpointer element_address
= mono_array_addr_with_size_fast (MONO_HANDLE_RAW (array
), element_size
, (gsize
)pos
);
215 return mono_value_box_handle (MONO_HANDLE_DOMAIN (array
), element_class
, element_address
, error
);
217 MonoObjectHandle result
= mono_new_null ();
218 mono_handle_array_getref (result
, array
, pos
);
223 ves_icall_System_Array_GetValue (MonoArrayHandle arr
, MonoArrayHandle indices
, MonoError
*error
)
225 MONO_CHECK_ARG_NULL_HANDLE (indices
, NULL_HANDLE
);
227 MonoClass
* const indices_class
= mono_handle_class (indices
);
228 MonoClass
* const array_class
= mono_handle_class (arr
);
230 g_assert (m_class_get_rank (indices_class
) == 1);
232 if (MONO_HANDLE_GETVAL (indices
, bounds
) || MONO_HANDLE_GETVAL (indices
, max_length
) != m_class_get_rank (array_class
)) {
233 mono_error_set_argument (error
, NULL
, NULL
);
239 if (!MONO_HANDLE_GETVAL (arr
, bounds
)) {
240 MONO_HANDLE_ARRAY_GETVAL (index
, indices
, gint32
, 0);
241 if (index
< 0 || index
>= MONO_HANDLE_GETVAL (arr
, max_length
)) {
242 mono_error_set_index_out_of_range (error
);
246 return ves_icall_System_Array_GetValueImpl (arr
, index
, error
);
249 for (gint32 i
= 0; i
< m_class_get_rank (array_class
); i
++) {
250 MONO_HANDLE_ARRAY_GETVAL (index
, indices
, gint32
, i
);
251 if ((index
< MONO_HANDLE_GETVAL (arr
, bounds
[i
].lower_bound
)) ||
252 (index
>= (mono_array_lower_bound_t
)MONO_HANDLE_GETVAL (arr
, bounds
[i
].length
) + MONO_HANDLE_GETVAL (arr
, bounds
[i
].lower_bound
))) {
253 mono_error_set_index_out_of_range (error
);
258 MONO_HANDLE_ARRAY_GETVAL (index
, indices
, gint32
, 0);
259 gint32 pos
= index
- MONO_HANDLE_GETVAL (arr
, bounds
[0].lower_bound
);
260 for (gint32 i
= 1; i
< m_class_get_rank (array_class
); i
++) {
261 MONO_HANDLE_ARRAY_GETVAL (index
, indices
, gint32
, i
);
262 pos
= pos
* MONO_HANDLE_GETVAL (arr
, bounds
[i
].length
) + index
-
263 MONO_HANDLE_GETVAL (arr
, bounds
[i
].lower_bound
);
266 return ves_icall_System_Array_GetValueImpl (arr
, pos
, error
);
270 ves_icall_System_Array_SetValueImpl (MonoArrayHandle arr
, MonoObjectHandle value
, guint32 pos
, MonoError
*error
)
272 array_set_value_impl (arr
, value
, pos
, FALSE
, error
);
276 set_invalid_cast (MonoError
*error
, MonoClass
*src_class
, MonoClass
*dst_class
)
278 mono_get_runtime_callbacks ()->set_cast_details (src_class
, dst_class
);
279 mono_error_set_invalid_cast (error
);
283 array_set_value_impl (MonoArrayHandle arr_handle
, MonoObjectHandle value_handle
, guint32 pos
, gboolean strict
, MonoError
*error
)
285 MonoClass
*ac
, *vc
, *ec
;
287 gpointer
*ea
= NULL
, *va
= NULL
;
292 gboolean castOk
= FALSE
;
293 gboolean et_isenum
= FALSE
;
294 gboolean vt_isenum
= FALSE
;
298 if (!MONO_HANDLE_IS_NULL (value_handle
))
299 vc
= mono_handle_class (value_handle
);
303 ac
= mono_handle_class (arr_handle
);
304 ec
= m_class_get_element_class (ac
);
305 esize
= mono_array_element_size (ac
);
307 if (mono_class_is_nullable (ec
)) {
308 #ifdef ENABLE_NETCORE
309 if (vc
&& m_class_is_primitive (vc
) && vc
!= m_class_get_nullable_elem_class (ec
)) {
310 // T -> Nullable<T> T must be exact
311 set_invalid_cast (error
, vc
, ec
);
315 MONO_ENTER_NO_SAFEPOINTS
;
316 ea
= (gpointer
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle
), esize
, pos
);
317 if (!MONO_HANDLE_IS_NULL (value_handle
))
318 va
= (gpointer
*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle
));
319 mono_nullable_init_unboxed ((guint8
*)ea
, va
, ec
);
320 MONO_EXIT_NO_SAFEPOINTS
;
324 if (MONO_HANDLE_IS_NULL (value_handle
)) {
325 MONO_ENTER_NO_SAFEPOINTS
;
326 ea
= (gpointer
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle
), esize
, pos
);
327 mono_gc_bzero_atomic (ea
, esize
);
328 MONO_EXIT_NO_SAFEPOINTS
;
332 #ifdef ENABLE_NETCORE
333 #define WIDENING_MSG NULL
334 #define WIDENING_ARG NULL
336 #define WIDENING_MSG "not a widening conversion"
337 #define WIDENING_ARG "value"
340 #define NO_WIDENING_CONVERSION G_STMT_START{ \
341 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
345 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{ \
346 if (esize < vsize + (extra)) { \
347 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
352 #define INVALID_CAST G_STMT_START{ \
353 mono_get_runtime_callbacks ()->set_cast_details (vc, ec); \
354 mono_error_set_invalid_cast (error); \
359 et
= m_class_get_byval_arg (ec
)->type
;
361 vt
= m_class_get_byval_arg (vc
)->type
;
363 /* Check element (destination) type. */
365 case MONO_TYPE_STRING
:
367 case MONO_TYPE_STRING
:
373 case MONO_TYPE_BOOLEAN
:
375 case MONO_TYPE_BOOLEAN
:
388 NO_WIDENING_CONVERSION
;
400 castOk
= mono_object_handle_isinst_mbyref_raw (value_handle
, ec
, error
);
404 if (!m_class_is_valuetype (ec
)) {
408 MONO_HANDLE_ARRAY_SETREF (arr_handle
, pos
, value_handle
);
413 MONO_ENTER_NO_SAFEPOINTS
;
414 ea
= (gpointer
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle
), esize
, pos
);
415 va
= (gpointer
*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle
));
416 if (m_class_has_references (ec
))
417 mono_value_copy_internal (ea
, va
, ec
);
419 mono_gc_memmove_atomic (ea
, va
, esize
);
420 MONO_EXIT_NO_SAFEPOINTS
;
425 if (!m_class_is_valuetype (vc
))
431 vsize
= mono_class_value_size (vc
, NULL
);
433 et_isenum
= et
== MONO_TYPE_VALUETYPE
&& m_class_is_enumtype (m_class_get_byval_arg (ec
)->data
.klass
);
434 vt_isenum
= vt
== MONO_TYPE_VALUETYPE
&& m_class_is_enumtype (m_class_get_byval_arg (vc
)->data
.klass
);
437 if (strict
&& et_isenum
&& !vt_isenum
) {
444 et
= mono_class_enum_basetype_internal (m_class_get_byval_arg (ec
)->data
.klass
)->type
;
447 vt
= mono_class_enum_basetype_internal (m_class_get_byval_arg (vc
)->data
.klass
)->type
;
449 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
455 case MONO_TYPE_CHAR: \
456 CHECK_WIDENING_CONVERSION(0); \
457 *(etype *) ea = (etype) u64; \
459 /* You can't assign a signed value to an unsigned array. */ \
464 /* You can't assign a floating point number to an integer array. */ \
467 NO_WIDENING_CONVERSION; \
475 #define ASSIGN_SIGNED(etype) G_STMT_START{\
481 CHECK_WIDENING_CONVERSION(0); \
482 *(etype *) ea = (etype) i64; \
484 /* You can assign an unsigned value to a signed array if the array's */ \
485 /* element size is larger than the value size. */ \
490 case MONO_TYPE_CHAR: \
491 CHECK_WIDENING_CONVERSION(1); \
492 *(etype *) ea = (etype) u64; \
494 /* You can't assign a floating point number to an integer array. */ \
497 NO_WIDENING_CONVERSION; \
505 #define ASSIGN_REAL(etype) G_STMT_START{\
509 CHECK_WIDENING_CONVERSION(0); \
510 *(etype *) ea = (etype) r64; \
512 /* All integer values fit into a floating point array, so we don't */ \
513 /* need to CHECK_WIDENING_CONVERSION here. */ \
518 *(etype *) ea = (etype) i64; \
524 case MONO_TYPE_CHAR: \
525 *(etype *) ea = (etype) u64; \
533 MONO_ENTER_NO_SAFEPOINTS
;
534 g_assert (!MONO_HANDLE_IS_NULL (value_handle
));
535 g_assert (m_class_is_valuetype (vc
));
536 va
= (gpointer
*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle
));
537 ea
= (gpointer
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle
), esize
, pos
);
541 u64
= *(guint8
*) va
;
544 u64
= *(guint16
*) va
;
547 u64
= *(guint32
*) va
;
550 u64
= *(guint64
*) va
;
556 i64
= *(gint16
*) va
;
559 i64
= *(gint32
*) va
;
562 i64
= *(gint64
*) va
;
565 r64
= *(gfloat
*) va
;
568 r64
= *(gdouble
*) va
;
571 u64
= *(guint16
*) va
;
573 case MONO_TYPE_BOOLEAN
:
574 /* Boolean is only compatible with itself. */
587 NO_WIDENING_CONVERSION
;
594 /* If we can't do a direct copy, let's try a widening conversion. */
599 ASSIGN_UNSIGNED (guint16
);
602 ASSIGN_UNSIGNED (guint8
);
605 ASSIGN_UNSIGNED (guint16
);
608 ASSIGN_UNSIGNED (guint32
);
611 ASSIGN_UNSIGNED (guint64
);
614 ASSIGN_SIGNED (gint8
);
617 ASSIGN_SIGNED (gint16
);
620 ASSIGN_SIGNED (gint32
);
623 ASSIGN_SIGNED (gint64
);
626 ASSIGN_REAL (gfloat
);
629 ASSIGN_REAL (gdouble
);
636 MONO_EXIT_NO_SAFEPOINTS
;
639 #undef NO_WIDENING_CONVERSION
640 #undef CHECK_WIDENING_CONVERSION
641 #undef ASSIGN_UNSIGNED
650 ves_icall_System_Array_SetValue (MonoArrayHandle arr
, MonoObjectHandle value
,
651 MonoArrayHandle idxs
, MonoError
*error
)
653 icallarray_print ("%s\n", __func__
);
662 if (MONO_HANDLE_IS_NULL (idxs
)) {
663 #ifdef ENABLE_NETCORE
664 mono_error_set_argument_null (error
, "indices", "");
666 mono_error_set_argument_null (error
, "idxs", "");
671 ic
= mono_handle_class (idxs
);
672 ac
= mono_handle_class (arr
);
674 g_assert (m_class_get_rank (ic
) == 1);
675 if (mono_handle_array_has_bounds (idxs
) || MONO_HANDLE_GETVAL (idxs
, max_length
) != m_class_get_rank (ac
)) {
676 #ifdef ENABLE_NETCORE
677 mono_error_set_argument (error
, NULL
, "");
679 mono_error_set_argument (error
, "idxs", "");
684 if (!mono_handle_array_has_bounds (arr
)) {
685 MONO_HANDLE_ARRAY_GETVAL (idx
, idxs
, gint32
, 0);
686 if (idx
< 0 || idx
>= MONO_HANDLE_GETVAL (arr
, max_length
)) {
687 mono_error_set_exception_instance (error
, mono_get_exception_index_out_of_range ());
691 array_set_value_impl (arr
, value
, idx
, TRUE
, error
);
695 gint32 ac_rank
= m_class_get_rank (ac
);
696 for (i
= 0; i
< ac_rank
; i
++) {
697 mono_handle_array_get_bounds_dim (arr
, i
, &dim
);
698 MONO_HANDLE_ARRAY_GETVAL (idx
, idxs
, gint32
, i
);
699 if ((idx
< dim
.lower_bound
) ||
700 (idx
>= (mono_array_lower_bound_t
)dim
.length
+ dim
.lower_bound
)) {
701 mono_error_set_exception_instance (error
, mono_get_exception_index_out_of_range ());
706 MONO_HANDLE_ARRAY_GETVAL (idx
, idxs
, gint32
, 0);
707 mono_handle_array_get_bounds_dim (arr
, 0, &dim
);
708 pos
= idx
- dim
.lower_bound
;
709 for (i
= 1; i
< ac_rank
; i
++) {
710 mono_handle_array_get_bounds_dim (arr
, i
, &dim
);
711 MONO_HANDLE_ARRAY_GETVAL (idx
, idxs
, gint32
, i
);
712 pos
= pos
* dim
.length
+ idx
- dim
.lower_bound
;
715 array_set_value_impl (arr
, value
, pos
, TRUE
, error
);
719 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionTypeHandle type
, MonoArrayHandle lengths
, MonoArrayHandle bounds
, MonoError
*error
)
721 // FIXME? fixed could be used for lengths, bounds.
723 icallarray_print ("%s type:%p length:%p bounds:%p\n", __func__
, type
, lengths
, bounds
);
725 MONO_CHECK_ARG_NULL_HANDLE (type
, NULL_HANDLE_ARRAY
);
726 MONO_CHECK_ARG_NULL_HANDLE (lengths
, NULL_HANDLE_ARRAY
);
728 MONO_CHECK_ARG (lengths
, mono_array_handle_length (lengths
) > 0, NULL_HANDLE_ARRAY
);
729 if (!MONO_HANDLE_IS_NULL (bounds
))
730 MONO_CHECK_ARG (bounds
, mono_array_handle_length (lengths
) == mono_array_handle_length (bounds
), NULL_HANDLE_ARRAY
);
732 for (uintptr_t i
= 0; i
< mono_array_handle_length (lengths
); ++i
) {
734 MONO_HANDLE_ARRAY_GETVAL (length
, lengths
, gint32
, i
);
736 mono_error_set_argument_out_of_range (error
, NULL
, "MonoArgumentException:NULL");
737 return NULL_HANDLE_ARRAY
;
741 MonoClass
*klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type
, type
));
742 if (!mono_class_init_checked (klass
, error
))
743 return NULL_HANDLE_ARRAY
;
745 if (m_class_get_byval_arg (m_class_get_element_class (klass
))->type
== MONO_TYPE_VOID
) {
746 mono_error_set_not_supported (error
, "Arrays of System.Void are not supported.");
747 return NULL_HANDLE_ARRAY
;
750 /* vectors are not the same as one dimensional arrays with non-zero bounds */
751 gboolean bounded
= FALSE
;
752 if (!MONO_HANDLE_IS_NULL (bounds
) && mono_array_handle_length (bounds
) == 1) {
754 MONO_HANDLE_ARRAY_GETVAL (bound0
, bounds
, gint32
, 0);
755 bounded
= bound0
!= 0;
758 MonoClass
* const aklass
= mono_class_create_bounded_array (klass
, mono_array_handle_length (lengths
), bounded
);
759 uintptr_t const aklass_rank
= m_class_get_rank (aklass
);
760 uintptr_t * const sizes
= g_newa (uintptr_t, aklass_rank
);
761 intptr_t * const lower_bounds
= g_newa (intptr_t, aklass_rank
);
763 // Copy lengths and lower_bounds from gint32 to [u]intptr_t.
765 for (uintptr_t i
= 0; i
< aklass_rank
; ++i
) {
766 MONO_HANDLE_ARRAY_GETVAL (sizes
[i
], lengths
, gint32
, i
);
767 if (!MONO_HANDLE_IS_NULL (bounds
))
768 MONO_HANDLE_ARRAY_GETVAL (lower_bounds
[i
], bounds
, gint32
, i
);
770 lower_bounds
[i
] = 0;
773 return mono_array_new_full_handle (MONO_HANDLE_DOMAIN (type
), aklass
, sizes
, lower_bounds
, error
);
777 ves_icall_System_Array_GetRank (MonoObjectHandle arr
, MonoError
*error
)
779 gint32
const result
= m_class_get_rank (mono_handle_class (arr
));
781 icallarray_print ("%s arr:%p res:%d\n", __func__
, MONO_HANDLE_RAW (arr
), result
);
786 static mono_array_size_t
787 mono_array_get_length (MonoArrayHandle arr
, gint32 dimension
, MonoError
*error
)
789 if (dimension
< 0 || dimension
>= m_class_get_rank (mono_handle_class (arr
))) {
790 mono_error_set_index_out_of_range (error
);
794 return MONO_HANDLE_GETVAL (arr
, bounds
) ? MONO_HANDLE_GETVAL (arr
, bounds
[dimension
].length
)
795 : MONO_HANDLE_GETVAL (arr
, max_length
);
799 ves_icall_System_Array_GetLength (MonoArrayHandle arr
, gint32 dimension
, MonoError
*error
)
801 icallarray_print ("%s arr:%p dimension:%d\n", __func__
, MONO_HANDLE_RAW (arr
), (int)dimension
);
803 mono_array_size_t
const length
= mono_array_get_length (arr
, dimension
, error
);
804 if (length
> G_MAXINT32
) {
805 mono_error_set_overflow (error
);
808 return (gint32
)length
;
812 ves_icall_System_Array_GetLongLength (MonoArrayHandle arr
, gint32 dimension
, MonoError
*error
)
814 icallarray_print ("%s arr:%p dimension:%d\n", __func__
, MONO_HANDLE_RAW (arr
), (int)dimension
);
816 return (gint64
)mono_array_get_length (arr
, dimension
, error
);
820 ves_icall_System_Array_GetLowerBound (MonoArrayHandle arr
, gint32 dimension
, MonoError
*error
)
822 icallarray_print ("%s arr:%p dimension:%d\n", __func__
, MONO_HANDLE_RAW (arr
), (int)dimension
);
824 if (dimension
< 0 || dimension
>= m_class_get_rank (mono_handle_class (arr
))) {
825 mono_error_set_index_out_of_range (error
);
829 return MONO_HANDLE_GETVAL (arr
, bounds
) ? MONO_HANDLE_GETVAL (arr
, bounds
[dimension
].lower_bound
)
834 ves_icall_System_Array_ClearInternal (MonoArrayHandle arr
, int idx
, int length
, MonoError
*error
)
836 icallarray_print ("%s arr:%p idx:%d len:%d\n", __func__
, MONO_HANDLE_RAW (arr
), (int)idx
, (int)length
);
838 int sz
= mono_array_element_size (mono_handle_class (arr
));
839 mono_gc_bzero_atomic (mono_array_addr_with_size_fast (MONO_HANDLE_RAW (arr
), sz
, idx
), length
* sz
);
843 ves_icall_System_Array_FastCopy (MonoArrayHandle source
, int source_idx
, MonoArrayHandle dest
, int dest_idx
, int length
, MonoError
*error
)
845 MonoVTable
* const src_vtable
= MONO_HANDLE_GETVAL (source
, obj
.vtable
);
846 MonoVTable
* const dest_vtable
= MONO_HANDLE_GETVAL (dest
, obj
.vtable
);
848 if (src_vtable
->rank
!= dest_vtable
->rank
)
851 MonoArrayBounds
*source_bounds
= MONO_HANDLE_GETVAL (source
, bounds
);
852 MonoArrayBounds
*dest_bounds
= MONO_HANDLE_GETVAL (dest
, bounds
);
854 for (int i
= 0; i
< src_vtable
->rank
; i
++) {
855 if ((source_bounds
&& source_bounds
[i
].lower_bound
> 0) ||
856 (dest_bounds
&& dest_bounds
[i
].lower_bound
> 0))
860 /* there's no integer overflow since mono_array_length_internal returns an unsigned integer */
861 if ((dest_idx
+ length
> mono_array_handle_length (dest
)) ||
862 (source_idx
+ length
> mono_array_handle_length (source
)))
865 MonoClass
* const src_class
= m_class_get_element_class (src_vtable
->klass
);
866 MonoClass
* const dest_class
= m_class_get_element_class (dest_vtable
->klass
);
869 * Handle common cases.
872 /* Case1: object[] -> valuetype[] (ArrayList::ToArray)
873 We fallback to managed here since we need to typecheck each boxed valuetype before storing them in the dest array.
875 if (src_class
== mono_defaults
.object_class
&& m_class_is_valuetype (dest_class
))
878 /* Check if we're copying a char[] <==> (u)short[] */
879 if (src_class
!= dest_class
) {
880 if (m_class_is_valuetype (dest_class
) || m_class_is_enumtype (dest_class
) ||
881 m_class_is_valuetype (src_class
) || m_class_is_valuetype (src_class
))
884 /* It's only safe to copy between arrays if we can ensure the source will always have a subtype of the destination. We bail otherwise. */
885 if (!mono_class_is_subclass_of_internal (src_class
, dest_class
, FALSE
))
888 if (m_class_is_native_pointer (src_class
) || m_class_is_native_pointer (dest_class
))
892 if (m_class_is_valuetype (dest_class
)) {
893 gsize
const element_size
= mono_array_element_size (MONO_HANDLE_GETVAL (source
, obj
.vtable
->klass
));
895 MONO_ENTER_NO_SAFEPOINTS
; // gchandle would also work here, is slow, breaks profiler tests.
897 gconstpointer
const source_addr
=
898 mono_array_addr_with_size_fast (MONO_HANDLE_RAW (source
), element_size
, source_idx
);
899 if (m_class_has_references (dest_class
)) {
900 mono_value_copy_array_handle (dest
, dest_idx
, source_addr
, length
);
902 gpointer
const dest_addr
=
903 mono_array_addr_with_size_fast (MONO_HANDLE_RAW (dest
), element_size
, dest_idx
);
904 mono_gc_memmove_atomic (dest_addr
, source_addr
, element_size
* length
);
907 MONO_EXIT_NO_SAFEPOINTS
;
909 mono_array_handle_memcpy_refs (dest
, dest_idx
, source
, source_idx
, length
);
916 ves_icall_System_Array_GetGenericValueImpl (MonoArray
*arr
, guint32 pos
, gpointer value
)
919 // Generic ref/out parameters are not supported by HANDLES(), so NOHANDLES().
921 icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__
, arr
, pos
, value
);
923 MONO_REQ_GC_UNSAFE_MODE
; // because of gpointer value
925 MonoClass
* const ac
= mono_object_class (arr
);
926 gsize
const esize
= mono_array_element_size (ac
);
927 gconstpointer
* const ea
= (gconstpointer
*)((char*)arr
->vector
+ (pos
* esize
));
929 mono_gc_memmove_atomic (value
, ea
, esize
);
933 ves_icall_System_Array_SetGenericValueImpl (MonoArray
*arr
, guint32 pos
, gpointer value
)
936 // Generic ref/out parameters are not supported by HANDLES(), so NOHANDLES().
938 icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__
, arr
, pos
, value
);
940 MONO_REQ_GC_UNSAFE_MODE
; // because of gpointer value
943 MonoClass
* const ac
= mono_object_class (arr
);
944 MonoClass
* const ec
= m_class_get_element_class (ac
);
946 gsize
const esize
= mono_array_element_size (ac
);
947 gpointer
* const ea
= (gpointer
*)((char*)arr
->vector
+ (pos
* esize
));
949 if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (ec
))) {
950 g_assert (esize
== sizeof (gpointer
));
951 mono_gc_wbarrier_generic_store_internal (ea
, *(MonoObject
**)value
);
953 g_assert (m_class_is_inited (ec
));
954 g_assert (esize
== mono_class_value_size (ec
, NULL
));
955 if (m_class_has_references (ec
))
956 mono_gc_wbarrier_value_copy_internal (ea
, value
, 1, ec
);
958 mono_gc_memmove_atomic (ea
, value
, esize
);
964 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8
*destination
, guint8
*source
, size_t byte_count
)
966 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8
*destination
, guint8
*source
, guint byte_count
)
969 mono_gc_memmove_atomic (destination
, source
, byte_count
);
974 ves_icall_System_Runtime_RuntimeImports_RhBulkMoveWithWriteBarrier (guint8
*destination
, guint8
*source
, size_t byte_count
)
976 mono_gc_wbarrier_range_copy (destination
, source
, byte_count
);
980 ves_icall_System_Runtime_RuntimeImports_Memmove_wbarrier (guint8
*destination
, guint8
*source
, guint len
, MonoType
*type
)
982 if (MONO_TYPE_IS_REFERENCE (type
))
983 mono_gc_wbarrier_arrayref_copy_internal (destination
, source
, len
);
985 mono_gc_wbarrier_value_copy_internal (destination
, source
, len
, mono_class_from_mono_type_internal (type
));
991 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8
*p
, size_t byte_length
)
993 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8
*p
, guint byte_length
)
996 memset (p
, 0, byte_length
);
1000 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArrayHandle array
, MonoClassField
*field_handle
, MonoError
*error
)
1002 MonoClass
*klass
= mono_handle_class (array
);
1003 guint32 size
= mono_array_element_size (klass
);
1004 MonoType
*type
= mono_type_get_underlying_type (m_class_get_byval_arg (m_class_get_element_class (klass
)));
1006 const char *field_data
;
1008 if (MONO_TYPE_IS_REFERENCE (type
) || type
->type
== MONO_TYPE_VALUETYPE
) {
1009 mono_error_set_argument (error
, "array", "Cannot initialize array of non-primitive type");
1013 MonoType
*field_type
= mono_field_get_type_checked (field_handle
, error
);
1017 if (!(field_type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
)) {
1018 mono_error_set_argument_format (error
, "field_handle", "Field '%s' doesn't have an RVA", mono_field_get_name (field_handle
));
1022 size
*= MONO_HANDLE_GETVAL(array
, max_length
);
1023 field_data
= mono_field_get_data (field_handle
);
1025 if (size
> mono_type_size (field_handle
->type
, &align
)) {
1026 mono_error_set_argument (error
, "field_handle", "Field not large enough to fill array");
1030 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
1032 guint ## n *data = (guint ## n *) mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0); \
1033 guint ## n *src = (guint ## n *) field_data; \
1035 nEnt = (size / sizeof(guint ## n)); \
1037 for (i = 0; i < nEnt; i++) { \
1038 data[i] = read ## n (&src[i]); \
1042 /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
1044 switch (type
->type
) {
1045 case MONO_TYPE_CHAR
:
1061 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array
), char, 0), field_data
, size
);
1065 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array
), char, 0), field_data
, size
);
1070 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
1072 return offsetof (MonoString
, chars
);
1076 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObjectHandle obj
, MonoError
*error
)
1078 if (MONO_HANDLE_IS_NULL (obj
) || !m_class_is_valuetype (mono_handle_class (obj
)))
1081 return mono_object_clone_handle (obj
, error
);
1085 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType
*handle
, MonoError
*error
)
1090 MONO_CHECK_ARG_NULL (handle
,);
1092 klass
= mono_class_from_mono_type_internal (handle
);
1093 MONO_CHECK_ARG (handle
, klass
,);
1095 if (mono_class_is_gtd (klass
))
1098 vtable
= mono_class_vtable_checked (mono_domain_get (), klass
, error
);
1099 return_if_nok (error
);
1101 /* This will call the type constructor */
1102 mono_runtime_class_init_full (vtable
, error
);
1106 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (MonoImage
*image
, MonoError
*error
)
1108 mono_image_check_for_module_cctor (image
);
1109 if (!image
->has_module_cctor
)
1112 MonoClass
*module_klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
| 1, error
);
1113 return_if_nok (error
);
1115 MonoVTable
* vtable
= mono_class_vtable_checked (mono_domain_get (), module_klass
, error
);
1116 return_if_nok (error
);
1118 mono_runtime_class_init_full (vtable
, error
);
1122 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
1124 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
1125 // It does not work on win32
1126 #elif defined(TARGET_ANDROID) || defined(__linux__)
1133 MonoInternalThread
*thread
;
1135 mono_thread_info_get_stack_bounds (&stack_addr
, &stack_size
);
1136 /* if we have no info we are optimistic and assume there is enough room */
1140 thread
= mono_thread_internal_current ();
1141 // .net seems to check that at least 50% of stack is available
1142 min_size
= thread
->stack_size
/ 2;
1144 // TODO: It's not always set
1148 current
= (guint8
*)&stack_addr
;
1149 if (current
> stack_addr
) {
1150 if ((current
- stack_addr
) < min_size
)
1153 if (current
- (stack_addr
- stack_size
) < min_size
)
1160 #ifdef ENABLE_NETCORE
1162 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetUninitializedObjectInternal (MonoType
*handle
, MonoError
*error
)
1168 klass
= mono_class_from_mono_type_internal (handle
);
1169 if (m_class_is_string (klass
)) {
1170 mono_error_set_argument (error
, NULL
, NULL
);
1174 if (m_class_is_abstract (klass
) || m_class_is_interface (klass
) || m_class_is_gtd (klass
)) {
1175 mono_error_set_member_access (error
, NULL
, NULL
);
1179 if (m_class_is_byreflike (klass
)) {
1180 mono_error_set_not_supported (error
, NULL
, NULL
);
1184 if (m_class_is_nullable (klass
))
1185 return mono_object_new_handle (mono_domain_get (), m_class_get_nullable_elem_class (klass
), error
);
1187 return mono_object_new_handle (mono_domain_get (), klass
, error
);
1191 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_PrepareMethod (MonoMethod
*method
, gpointer inst_types
, int n_inst_types
, MonoError
*error
)
1193 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) {
1194 mono_error_set_argument (error
, NULL
, NULL
);
1198 MonoGenericContainer
*container
= NULL
;
1199 if (method
->is_generic
)
1200 container
= mono_method_get_generic_container (method
);
1201 else if (m_class_is_gtd (method
->klass
))
1202 container
= mono_class_get_generic_container (method
->klass
);
1204 int nparams
= container
->type_argc
+ (container
->parent
? container
->parent
->type_argc
: 0);
1205 if (nparams
!= n_inst_types
) {
1206 mono_error_set_argument (error
, NULL
, NULL
);
1216 ves_icall_System_Object_MemberwiseClone (MonoObjectHandle this_obj
, MonoError
*error
)
1218 return mono_object_clone_handle (this_obj
, error
);
1222 ves_icall_System_ValueType_InternalGetHashCode (MonoObject
*this_obj
, MonoArray
**fields
)
1226 MonoObject
**values
= NULL
;
1229 gint32 result
= (int)(gsize
)mono_defaults
.int32_class
;
1230 MonoClassField
* field
;
1233 klass
= mono_object_class (this_obj
);
1235 if (mono_class_num_fields (klass
) == 0)
1239 * Compute the starting value of the hashcode for fields of primitive
1240 * types, and return the remaining fields in an array to the managed side.
1241 * This way, we can avoid costly reflection operations in managed code.
1244 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
1245 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1247 if (mono_field_is_deleted (field
))
1249 /* FIXME: Add more types */
1250 switch (field
->type
->type
) {
1252 result
^= *(gint32
*)((guint8
*)this_obj
+ field
->offset
);
1255 result
^= mono_aligned_addr_hash (*(gpointer
*)((guint8
*)this_obj
+ field
->offset
));
1257 case MONO_TYPE_STRING
: {
1259 s
= *(MonoString
**)((guint8
*)this_obj
+ field
->offset
);
1261 result
^= mono_string_hash_internal (s
);
1266 values
= g_newa (MonoObject
*, mono_class_num_fields (klass
));
1267 o
= mono_field_get_value_object_checked (mono_object_domain (this_obj
), field
, this_obj
, error
);
1268 if (!is_ok (error
)) {
1269 mono_error_set_pending_exception (error
);
1272 values
[count
++] = o
;
1278 MonoArray
*fields_arr
= mono_array_new_checked (mono_domain_get (), mono_defaults
.object_class
, count
, error
);
1279 if (mono_error_set_pending_exception (error
))
1281 mono_gc_wbarrier_generic_store_internal (fields
, (MonoObject
*) fields_arr
);
1282 for (i
= 0; i
< count
; ++i
)
1283 mono_array_setref_internal (*fields
, i
, values
[i
]);
1291 ves_icall_System_ValueType_Equals (MonoObject
*this_obj
, MonoObject
*that
, MonoArray
**fields
)
1295 MonoObject
**values
= NULL
;
1297 MonoClassField
* field
;
1303 MONO_CHECK_ARG_NULL (that
, FALSE
);
1305 if (this_obj
->vtable
!= that
->vtable
)
1308 klass
= mono_object_class (this_obj
);
1310 if (m_class_is_enumtype (klass
) && mono_class_enum_basetype_internal (klass
) && mono_class_enum_basetype_internal (klass
)->type
== MONO_TYPE_I4
)
1311 return *(gint32
*)mono_object_get_data (this_obj
) == *(gint32
*)mono_object_get_data (that
);
1314 * Do the comparison for fields of primitive type and return a result if
1315 * possible. Otherwise, return the remaining fields in an array to the
1316 * managed side. This way, we can avoid costly reflection operations in
1320 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
1321 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1323 if (mono_field_is_deleted (field
))
1325 guint8
*this_field
= (guint8
*) this_obj
+ field
->offset
;
1326 guint8
*that_field
= (guint8
*) that
+ field
->offset
;
1328 #define UNALIGNED_COMPARE(type) \
1331 memcpy (&left, this_field, sizeof (type)); \
1332 memcpy (&right, that_field, sizeof (type)); \
1333 if (left != right) \
1337 /* FIXME: Add more types */
1338 switch (field
->type
->type
) {
1341 case MONO_TYPE_BOOLEAN
:
1342 if (*this_field
!= *that_field
)
1347 case MONO_TYPE_CHAR
:
1348 #ifdef NO_UNALIGNED_ACCESS
1349 if (G_UNLIKELY ((intptr_t) this_field
& 1 || (intptr_t) that_field
& 1))
1350 UNALIGNED_COMPARE (gint16
);
1353 if (*(gint16
*) this_field
!= *(gint16
*) that_field
)
1358 #ifdef NO_UNALIGNED_ACCESS
1359 if (G_UNLIKELY ((intptr_t) this_field
& 3 || (intptr_t) that_field
& 3))
1360 UNALIGNED_COMPARE (gint32
);
1363 if (*(gint32
*) this_field
!= *(gint32
*) that_field
)
1368 #ifdef NO_UNALIGNED_ACCESS
1369 if (G_UNLIKELY ((intptr_t) this_field
& 7 || (intptr_t) that_field
& 7))
1370 UNALIGNED_COMPARE (gint64
);
1373 if (*(gint64
*) this_field
!= *(gint64
*) that_field
)
1377 case MONO_TYPE_R4
: {
1379 #ifdef NO_UNALIGNED_ACCESS
1380 memcpy (&d1
, this_field
, sizeof (float));
1381 memcpy (&d2
, that_field
, sizeof (float));
1383 d1
= *(float *) this_field
;
1384 d2
= *(float *) that_field
;
1386 if (d1
!= d2
&& !(mono_isnan (d1
) && mono_isnan (d2
)))
1390 case MONO_TYPE_R8
: {
1392 #ifdef NO_UNALIGNED_ACCESS
1393 memcpy (&d1
, this_field
, sizeof (double));
1394 memcpy (&d2
, that_field
, sizeof (double));
1396 d1
= *(double *) this_field
;
1397 d2
= *(double *) that_field
;
1399 if (d1
!= d2
&& !(mono_isnan (d1
) && mono_isnan (d2
)))
1404 #ifdef NO_UNALIGNED_ACCESS
1405 if (G_UNLIKELY ((intptr_t) this_field
& 7 || (intptr_t) that_field
& 7))
1406 UNALIGNED_COMPARE (gpointer
);
1409 if (*(gpointer
*) this_field
!= *(gpointer
*) that_field
)
1412 case MONO_TYPE_STRING
: {
1413 MonoString
*s1
, *s2
;
1414 guint32 s1len
, s2len
;
1415 s1
= *(MonoString
**)((guint8
*)this_obj
+ field
->offset
);
1416 s2
= *(MonoString
**)((guint8
*)that
+ field
->offset
);
1419 if ((s1
== NULL
) || (s2
== NULL
))
1421 s1len
= mono_string_length_internal (s1
);
1422 s2len
= mono_string_length_internal (s2
);
1426 if (memcmp (mono_string_chars_internal (s1
), mono_string_chars_internal (s2
), s1len
* sizeof (gunichar2
)) != 0)
1432 values
= g_newa (MonoObject
*, mono_class_num_fields (klass
) * 2);
1433 o
= mono_field_get_value_object_checked (mono_object_domain (this_obj
), field
, this_obj
, error
);
1434 if (!is_ok (error
)) {
1435 mono_error_set_pending_exception (error
);
1438 values
[count
++] = o
;
1439 o
= mono_field_get_value_object_checked (mono_object_domain (this_obj
), field
, that
, error
);
1440 if (!is_ok (error
)) {
1441 mono_error_set_pending_exception (error
);
1444 values
[count
++] = o
;
1447 #undef UNALIGNED_COMPARE
1449 if (m_class_is_enumtype (klass
))
1450 /* enums only have one non-static field */
1456 MonoArray
*fields_arr
= mono_array_new_checked (mono_domain_get (), mono_defaults
.object_class
, count
, error
);
1457 if (mono_error_set_pending_exception (error
))
1459 mono_gc_wbarrier_generic_store_internal (fields
, (MonoObject
*) fields_arr
);
1460 for (i
= 0; i
< count
; ++i
)
1461 mono_array_setref_fast (*fields
, i
, values
[i
]);
1468 MonoReflectionTypeHandle
1469 ves_icall_System_Object_GetType (MonoObjectHandle obj
, MonoError
*error
)
1471 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (obj
);
1472 MonoClass
*klass
= mono_handle_class (obj
);
1473 #ifndef DISABLE_REMOTING
1474 if (mono_class_is_transparent_proxy (klass
)) {
1475 MonoTransparentProxyHandle proxy_obj
= MONO_HANDLE_CAST (MonoTransparentProxy
, obj
);
1476 MonoRemoteClass
*remote_class
= MONO_HANDLE_GETVAL (proxy_obj
, remote_class
);
1477 /* If it's a transparent proxy for an interface, return the
1478 * interface type, not the unhelpful proxy_class class (which
1479 * is just MarshalByRefObject). */
1480 MonoType
*proxy_type
=
1481 mono_remote_class_is_interface_proxy (remote_class
) ?
1482 m_class_get_byval_arg (remote_class
->interfaces
[0]) :
1483 m_class_get_byval_arg (remote_class
->proxy_class
);
1484 return mono_type_get_object_handle (domain
, proxy_type
, error
);
1487 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
1491 get_executing (MonoMethod
*m
, gint32 no
, gint32 ilo
, gboolean managed
, gpointer data
)
1493 MonoMethod
**dest
= (MonoMethod
**)data
;
1495 /* skip unmanaged frames */
1500 if (!strcmp (m_class_get_name_space (m
->klass
), "System.Reflection"))
1509 in_corlib_name_space (MonoClass
*klass
, const char *name_space
)
1511 return m_class_get_image (klass
) == mono_defaults
.corlib
&&
1512 !strcmp (m_class_get_name_space (klass
), name_space
);
1516 get_caller_no_reflection (MonoMethod
*m
, gint32 no
, gint32 ilo
, gboolean managed
, gpointer data
)
1518 MonoMethod
**dest
= (MonoMethod
**)data
;
1520 /* skip unmanaged frames */
1524 if (m
->wrapper_type
!= MONO_WRAPPER_NONE
)
1532 if (in_corlib_name_space (m
->klass
, "System.Reflection"))
1543 get_caller_no_system_or_reflection (MonoMethod
*m
, gint32 no
, gint32 ilo
, gboolean managed
, gpointer data
)
1545 MonoMethod
**dest
= (MonoMethod
**)data
;
1547 /* skip unmanaged frames */
1551 if (m
->wrapper_type
!= MONO_WRAPPER_NONE
)
1559 if (in_corlib_name_space (m
->klass
, "System.Reflection") || in_corlib_name_space (m
->klass
, "System"))
1570 * mono_runtime_get_caller_no_system_or_reflection:
1572 * Walk the stack of the current thread and find the first managed method that
1573 * is not in the mscorlib System or System.Reflection namespace. This skips
1574 * unmanaged callers and wrapper methods.
1576 * \returns a pointer to the \c MonoMethod or NULL if we walked past all the
1580 mono_runtime_get_caller_no_system_or_reflection (void)
1582 MonoMethod
*dest
= NULL
;
1583 mono_stack_walk_no_il (get_caller_no_system_or_reflection
, &dest
);
1588 * mono_runtime_get_caller_from_stack_mark:
1590 * Walk the stack and return the assembly of the method referenced
1591 * by the stack mark STACK_MARK.
1594 mono_runtime_get_caller_from_stack_mark (MonoStackCrawlMark
*stack_mark
)
1596 // FIXME: Use the stack mark
1597 MonoMethod
*dest
= NULL
;
1598 mono_stack_walk_no_il (get_caller_no_system_or_reflection
, &dest
);
1600 return m_class_get_image (dest
->klass
)->assembly
;
1605 static MonoReflectionTypeHandle
1606 type_from_parsed_name (MonoTypeNameParse
*info
, MonoStackCrawlMark
*stack_mark
, MonoBoolean ignoreCase
, MonoAssembly
**caller_assembly
, MonoError
*error
)
1609 MonoType
*type
= NULL
;
1610 MonoAssembly
*assembly
= NULL
;
1611 gboolean type_resolve
= FALSE
;
1612 MonoImage
*rootimage
= NULL
;
1617 * We must compute the calling assembly as type loading must happen under a metadata context.
1618 * For example. The main assembly is a.exe and Type.GetType is called from dir/b.dll. Without
1619 * the metadata context (basedir currently) set to dir/b.dll we won't be able to load a dir/c.dll.
1621 m
= mono_method_get_last_managed ();
1622 if (m
&& m_class_get_image (m
->klass
) != mono_defaults
.corlib
) {
1623 /* Happens with inlining */
1624 assembly
= m_class_get_image (m
->klass
)->assembly
;
1626 assembly
= mono_runtime_get_caller_from_stack_mark (stack_mark
);
1629 type_resolve
= TRUE
;
1630 rootimage
= assembly
->image
;
1632 g_warning (G_STRLOC
);
1634 *caller_assembly
= assembly
;
1636 if (info
->assembly
.name
)
1637 assembly
= mono_assembly_load (&info
->assembly
, assembly
? assembly
->basedir
: NULL
, NULL
);
1640 /* When loading from the current assembly, AppDomain.TypeResolve will not be called yet */
1641 type
= mono_reflection_get_type_checked (rootimage
, assembly
->image
, info
, ignoreCase
, &type_resolve
, error
);
1642 goto_if_nok (error
, fail
);
1646 // Say we're looking for System.Generic.Dict<int, Local>
1647 // we FAIL the get type above, because S.G.Dict isn't in assembly->image. So we drop down here.
1648 // but then we FAIL AGAIN because now we pass null as the image and the rootimage and everything
1649 // is messed up when we go to construct the Local as the type arg...
1651 // By contrast, if we started with Mine<System.Generic.Dict<int, Local>> we'd go in with assembly->image
1652 // as the root and then even the detour into generics would still not screw us when we went to load Local.
1653 if (!info
->assembly
.name
&& !type
) {
1655 type
= mono_reflection_get_type_checked (rootimage
, NULL
, info
, ignoreCase
, &type_resolve
, error
);
1656 goto_if_nok (error
, fail
);
1658 if (assembly
&& !type
&& type_resolve
) {
1659 type_resolve
= FALSE
; /* This will invoke TypeResolve if not done in the first 'if' */
1660 type
= mono_reflection_get_type_checked (rootimage
, assembly
->image
, info
, ignoreCase
, &type_resolve
, error
);
1661 goto_if_nok (error
, fail
);
1667 return mono_type_get_object_handle (mono_domain_get (), type
, error
);
1669 return MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
1672 MonoReflectionTypeHandle
1673 ves_icall_System_RuntimeTypeHandle_internal_from_name (MonoStringHandle name
,
1674 MonoStackCrawlMark
*stack_mark
,
1675 MonoReflectionAssemblyHandle callerAssembly
,
1676 MonoBoolean throwOnError
,
1677 MonoBoolean ignoreCase
,
1678 MonoBoolean reflectionOnly
,
1681 MonoTypeNameParse info
;
1682 gboolean free_info
= FALSE
;
1683 MonoAssembly
*caller_assembly
;
1684 MonoReflectionTypeHandle type
= MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
1686 /* The callerAssembly argument is unused for now */
1688 char *str
= mono_string_handle_to_utf8 (name
, error
);
1689 goto_if_nok (error
, leave
);
1692 if (!mono_reflection_parse_type_checked (str
, &info
, error
))
1695 /* mono_reflection_parse_type() mangles the string */
1697 MONO_HANDLE_ASSIGN (type
, type_from_parsed_name (&info
, (MonoStackCrawlMark
*)stack_mark
, ignoreCase
, &caller_assembly
, error
));
1699 goto_if_nok (error
, leave
);
1701 if (MONO_HANDLE_IS_NULL (type
)) {
1703 char *tname
= info
.name_space
? g_strdup_printf ("%s.%s", info
.name_space
, info
.name
) : g_strdup (info
.name
);
1705 if (info
.assembly
.name
)
1706 aname
= mono_stringify_assembly_name (&info
.assembly
);
1707 else if (caller_assembly
)
1708 aname
= mono_stringify_assembly_name (mono_assembly_get_name_internal (caller_assembly
));
1710 aname
= g_strdup ("");
1711 mono_error_set_type_load_name (error
, tname
, aname
, "");
1718 mono_reflection_free_type_info (&info
);
1720 if (!is_ok (error
)) {
1721 if (!throwOnError
) {
1722 mono_error_cleanup (error
);
1725 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
1731 MonoReflectionTypeHandle
1732 ves_icall_System_Type_internal_from_handle (MonoType
*handle
, MonoError
*error
)
1734 MonoDomain
*domain
= mono_domain_get ();
1736 return mono_type_get_object_handle (domain
, handle
, error
);
1740 ves_icall_Mono_RuntimeClassHandle_GetTypeFromClass (MonoClass
*klass
, MonoError
*error
)
1742 return m_class_get_byval_arg (klass
);
1746 ves_icall_Mono_RuntimeGPtrArrayHandle_GPtrArrayFree (GPtrArray
*ptr_array
, MonoError
*error
)
1748 g_ptr_array_free (ptr_array
, TRUE
);
1752 ves_icall_Mono_SafeStringMarshal_GFree (void *c_str
, MonoError
*error
)
1758 ves_icall_Mono_SafeStringMarshal_StringToUtf8 (MonoStringHandle s
, MonoError
*error
)
1760 return mono_string_handle_to_utf8 (s
, error
);
1763 /* System.TypeCode */
1782 TYPECODE_STRING
= 18
1786 ves_icall_type_GetTypeCodeInternal (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
1788 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
1792 return TYPECODE_OBJECT
;
1796 case MONO_TYPE_VOID
:
1797 return TYPECODE_OBJECT
;
1798 case MONO_TYPE_BOOLEAN
:
1799 return TYPECODE_BOOLEAN
;
1801 return TYPECODE_BYTE
;
1803 return TYPECODE_SBYTE
;
1805 return TYPECODE_UINT16
;
1807 return TYPECODE_INT16
;
1808 case MONO_TYPE_CHAR
:
1809 return TYPECODE_CHAR
;
1813 return TYPECODE_OBJECT
;
1815 return TYPECODE_UINT32
;
1817 return TYPECODE_INT32
;
1819 return TYPECODE_UINT64
;
1821 return TYPECODE_INT64
;
1823 return TYPECODE_SINGLE
;
1825 return TYPECODE_DOUBLE
;
1826 case MONO_TYPE_VALUETYPE
: {
1827 MonoClass
*klass
= type
->data
.klass
;
1829 if (m_class_is_enumtype (klass
)) {
1830 t
= mono_class_enum_basetype_internal (klass
)->type
;
1832 } else if (mono_is_corlib_image (m_class_get_image (klass
))) {
1833 if (strcmp (m_class_get_name_space (klass
), "System") == 0) {
1834 if (strcmp (m_class_get_name (klass
), "Decimal") == 0)
1835 return TYPECODE_DECIMAL
;
1836 else if (strcmp (m_class_get_name (klass
), "DateTime") == 0)
1837 return TYPECODE_DATETIME
;
1840 return TYPECODE_OBJECT
;
1842 case MONO_TYPE_STRING
:
1843 return TYPECODE_STRING
;
1844 case MONO_TYPE_SZARRAY
:
1845 case MONO_TYPE_ARRAY
:
1846 case MONO_TYPE_OBJECT
:
1848 case MONO_TYPE_MVAR
:
1849 case MONO_TYPE_TYPEDBYREF
:
1850 return TYPECODE_OBJECT
;
1851 case MONO_TYPE_CLASS
:
1853 MonoClass
*klass
= type
->data
.klass
;
1854 if (m_class_get_image (klass
) == mono_defaults
.corlib
&& strcmp (m_class_get_name_space (klass
), "System") == 0) {
1855 if (strcmp (m_class_get_name (klass
), "DBNull") == 0)
1856 return TYPECODE_DBNULL
;
1859 return TYPECODE_OBJECT
;
1860 case MONO_TYPE_GENERICINST
:
1861 return TYPECODE_OBJECT
;
1863 g_error ("type 0x%02x not handled in GetTypeCode()", t
);
1869 mono_type_get_underlying_type_ignore_byref (MonoType
*type
)
1871 if (type
->type
== MONO_TYPE_VALUETYPE
&& m_class_is_enumtype (type
->data
.klass
))
1872 return mono_class_enum_basetype_internal (type
->data
.klass
);
1873 if (type
->type
== MONO_TYPE_GENERICINST
&& m_class_is_enumtype (type
->data
.generic_class
->container_class
))
1874 return mono_class_enum_basetype_internal (type
->data
.generic_class
->container_class
);
1879 ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle ref_type
, MonoReflectionTypeHandle ref_c
, MonoError
*error
)
1881 g_assert (!MONO_HANDLE_IS_NULL (ref_type
));
1883 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
1884 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
1885 MonoType
*ctype
= MONO_HANDLE_GETVAL (ref_c
, type
);
1886 MonoClass
*klassc
= mono_class_from_mono_type_internal (ctype
);
1888 if (type
->byref
^ ctype
->byref
)
1892 MonoType
*t
= mono_type_get_underlying_type_ignore_byref (type
);
1893 MonoType
*ot
= mono_type_get_underlying_type_ignore_byref (ctype
);
1895 klass
= mono_class_from_mono_type_internal (t
);
1896 klassc
= mono_class_from_mono_type_internal (ot
);
1898 if (mono_type_is_primitive (t
)) {
1899 return mono_type_is_primitive (ot
) && m_class_get_instance_size (klass
) == m_class_get_instance_size (klassc
);
1900 } else if (t
->type
== MONO_TYPE_VAR
|| t
->type
== MONO_TYPE_MVAR
) {
1901 return t
->type
== ot
->type
&& t
->data
.generic_param
->num
== ot
->data
.generic_param
->num
;
1902 } else if (t
->type
== MONO_TYPE_PTR
|| t
->type
== MONO_TYPE_FNPTR
) {
1903 return t
->type
== ot
->type
;
1905 if (ot
->type
== MONO_TYPE_VAR
|| ot
->type
== MONO_TYPE_MVAR
)
1908 if (m_class_is_valuetype (klass
))
1909 return klass
== klassc
;
1910 return m_class_is_valuetype (klass
) == m_class_is_valuetype (klassc
);
1915 mono_class_is_assignable_from_checked (klass
, klassc
, &result
, error
);
1916 return (guint32
)result
;
1920 ves_icall_RuntimeTypeHandle_is_subclass_of (MonoType
*childType
, MonoType
*baseType
)
1923 mono_bool result
= FALSE
;
1924 MonoClass
*childClass
;
1925 MonoClass
*baseClass
;
1927 childClass
= mono_class_from_mono_type_internal (childType
);
1928 baseClass
= mono_class_from_mono_type_internal (baseType
);
1930 if (G_UNLIKELY (childType
->byref
)) {
1931 result
= !baseType
->byref
&& baseClass
== mono_defaults
.object_class
;
1935 if (G_UNLIKELY (baseType
->byref
)) {
1940 if (childType
== baseType
) {
1941 /* .NET IsSubclassOf is not reflexive */
1946 if (G_UNLIKELY (is_generic_parameter (childType
))) {
1947 /* slow path: walk the type hierarchy looking at base types
1948 * until we see baseType. If the current type is not a gparam,
1949 * break out of the loop and use is_subclass_of.
1951 MonoClass
*c
= mono_generic_param_get_base_type (childClass
);
1955 if (c
== baseClass
) {
1959 if (!is_generic_parameter (m_class_get_byval_arg (c
))) {
1960 result
= mono_class_is_subclass_of_internal (c
, baseClass
, FALSE
);
1963 c
= mono_generic_param_get_base_type (c
);
1966 result
= mono_class_is_subclass_of_internal (childClass
, baseClass
, FALSE
);
1969 mono_error_set_pending_exception (error
);
1974 ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type
, MonoObjectHandle obj
, MonoError
*error
)
1976 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
1977 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
1978 mono_class_init_checked (klass
, error
);
1979 return_val_if_nok (error
, FALSE
);
1980 MonoObjectHandle inst
= mono_object_handle_isinst (obj
, klass
, error
);
1981 return_val_if_nok (error
, FALSE
);
1982 return !MONO_HANDLE_IS_NULL (inst
);
1986 ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
1988 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
1990 #ifdef ENABLE_NETCORE
1991 if (type
->byref
|| type
->type
== MONO_TYPE_PTR
|| type
->type
== MONO_TYPE_FNPTR
)
1992 return TYPE_ATTRIBUTE_NOT_PUBLIC
;
1995 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
1996 return mono_class_get_flags (klass
);
1999 MonoReflectionMarshalAsAttributeHandle
2000 ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionFieldHandle field_h
, MonoError
*error
)
2002 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (field_h
);
2003 MonoClassField
*field
= MONO_HANDLE_GETVAL (field_h
, field
);
2004 MonoClass
*klass
= field
->parent
;
2006 MonoGenericClass
*gklass
= mono_class_try_get_generic_class (klass
);
2007 if (mono_class_is_gtd (klass
) ||
2008 (gklass
&& gklass
->context
.class_inst
->is_open
))
2009 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute
, NULL_HANDLE
);
2011 MonoType
*ftype
= mono_field_get_type_internal (field
);
2012 if (ftype
&& !(ftype
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL
))
2013 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute
, NULL_HANDLE
);
2015 MonoMarshalType
*info
= mono_marshal_load_type_info (klass
);
2017 for (int i
= 0; i
< info
->num_fields
; ++i
) {
2018 if (info
->fields
[i
].field
== field
) {
2019 if (!info
->fields
[i
].mspec
)
2020 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute
, NULL_HANDLE
);
2022 return mono_reflection_marshal_as_attribute_from_marshal_spec (domain
, klass
, info
->fields
[i
].mspec
, error
);
2027 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute
, NULL_HANDLE
);
2030 MonoReflectionFieldHandle
2031 ves_icall_System_Reflection_FieldInfo_internal_from_handle_type (MonoClassField
*handle
, MonoType
*type
, MonoError
*error
)
2038 klass
= handle
->parent
;
2040 klass
= mono_class_from_mono_type_internal (type
);
2042 gboolean found
= klass
== handle
->parent
|| mono_class_has_parent (klass
, handle
->parent
);
2045 /* The managed code will throw the exception */
2046 return MONO_HANDLE_CAST (MonoReflectionField
, NULL_HANDLE
);
2049 return mono_field_get_object_handle (mono_domain_get (), klass
, handle
, error
);
2052 MonoReflectionEventHandle
2053 ves_icall_System_Reflection_EventInfo_internal_from_handle_type (MonoEvent
*handle
, MonoType
*type
, MonoError
*error
)
2060 klass
= handle
->parent
;
2062 klass
= mono_class_from_mono_type_internal (type
);
2064 gboolean found
= klass
== handle
->parent
|| mono_class_has_parent (klass
, handle
->parent
);
2066 /* Managed code will throw an exception */
2067 return MONO_HANDLE_CAST (MonoReflectionEvent
, NULL_HANDLE
);
2070 return mono_event_get_object_handle (mono_domain_get (), klass
, handle
, error
);
2073 MonoReflectionPropertyHandle
2074 ves_icall_System_Reflection_RuntimePropertyInfo_internal_from_handle_type (MonoProperty
*handle
, MonoType
*type
, MonoError
*error
)
2081 klass
= handle
->parent
;
2083 klass
= mono_class_from_mono_type_internal (type
);
2085 gboolean found
= klass
== handle
->parent
|| mono_class_has_parent (klass
, handle
->parent
);
2087 /* Managed code will throw an exception */
2088 return MONO_HANDLE_CAST (MonoReflectionProperty
, NULL_HANDLE
);
2091 return mono_property_get_object_handle (mono_domain_get (), klass
, handle
, error
);
2095 ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionFieldHandle field_h
, MonoBoolean optional
, MonoError
*error
)
2097 MonoClassField
*field
= MONO_HANDLE_GETVAL (field_h
, field
);
2099 MonoType
*type
= mono_field_get_type_checked (field
, error
);
2100 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
2102 return type_array_from_modifiers (m_class_get_image (field
->parent
), type
, optional
, error
);
2106 ves_icall_get_method_attributes (MonoMethod
*method
)
2108 return method
->flags
;
2112 ves_icall_get_method_info (MonoMethod
*method
, MonoMethodInfo
*info
, MonoError
*error
)
2114 MonoDomain
*domain
= mono_domain_get ();
2116 MonoMethodSignature
* sig
= mono_method_signature_checked (method
, error
);
2117 return_if_nok (error
);
2119 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (method
->klass
), error
);
2120 return_if_nok (error
);
2122 MONO_STRUCT_SETREF_INTERNAL (info
, parent
, MONO_HANDLE_RAW (rt
));
2124 MONO_HANDLE_ASSIGN (rt
, mono_type_get_object_handle (domain
, sig
->ret
, error
));
2125 return_if_nok (error
);
2127 MONO_STRUCT_SETREF_INTERNAL (info
, ret
, MONO_HANDLE_RAW (rt
));
2129 info
->attrs
= method
->flags
;
2130 info
->implattrs
= method
->iflags
;
2132 if (sig
->call_convention
== MONO_CALL_DEFAULT
)
2133 callconv
= sig
->sentinelpos
>= 0 ? 2 : 1;
2135 if (sig
->call_convention
== MONO_CALL_VARARG
|| sig
->sentinelpos
>= 0)
2140 callconv
|= (sig
->hasthis
<< 5) | (sig
->explicit_this
<< 6);
2141 info
->callconv
= callconv
;
2145 ves_icall_System_Reflection_MonoMethodInfo_get_parameter_info (MonoMethod
*method
, MonoReflectionMethodHandle member
, MonoError
*error
)
2147 MonoDomain
*domain
= mono_domain_get ();
2149 MonoReflectionTypeHandle reftype
= MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
2150 MONO_HANDLE_GET (reftype
, member
, reftype
);
2151 MonoClass
*klass
= NULL
;
2152 if (!MONO_HANDLE_IS_NULL (reftype
))
2153 klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (reftype
, type
));
2154 return mono_param_get_objects_internal (domain
, method
, klass
, error
);
2157 MonoReflectionMarshalAsAttributeHandle
2158 ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod
*method
, MonoError
*error
)
2160 MonoDomain
*domain
= mono_domain_get ();
2161 MonoReflectionMarshalAsAttributeHandle res
= MONO_HANDLE_NEW (MonoReflectionMarshalAsAttribute
, NULL
);
2163 MonoMarshalSpec
**mspecs
= g_new (MonoMarshalSpec
*, mono_method_signature_internal (method
)->param_count
+ 1);
2164 mono_method_get_marshal_info (method
, mspecs
);
2167 MONO_HANDLE_ASSIGN (res
, mono_reflection_marshal_as_attribute_from_marshal_spec (domain
, method
->klass
, mspecs
[0], error
));
2168 goto_if_nok (error
, leave
);
2172 for (int i
= mono_method_signature_internal (method
)->param_count
; i
>= 0; i
--)
2174 mono_metadata_free_marshal_spec (mspecs
[i
]);
2181 ves_icall_RuntimeFieldInfo_GetFieldOffset (MonoReflectionFieldHandle field
, MonoError
*error
)
2183 MonoClassField
*class_field
= MONO_HANDLE_GETVAL (field
, field
);
2184 mono_class_setup_fields (class_field
->parent
);
2186 return class_field
->offset
- MONO_ABI_SIZEOF (MonoObject
);
2189 MonoReflectionTypeHandle
2190 ves_icall_RuntimeFieldInfo_GetParentType (MonoReflectionFieldHandle field
, MonoBoolean declaring
, MonoError
*error
)
2192 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (field
);
2196 MonoClassField
*f
= MONO_HANDLE_GETVAL (field
, field
);
2199 parent
= MONO_HANDLE_GETVAL (field
, klass
);
2202 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (parent
), error
);
2206 ves_icall_RuntimeFieldInfo_GetValueInternal (MonoReflectionField
*field
, MonoObject
*obj
)
2209 MonoClass
*fklass
= field
->klass
;
2210 MonoClassField
*cf
= field
->field
;
2211 MonoDomain
*domain
= mono_object_domain (field
);
2213 if (mono_asmctx_get_kind (&m_class_get_image (fklass
)->assembly
->context
) == MONO_ASMCTX_REFONLY
) {
2214 mono_error_set_invalid_operation (error
,
2215 "It is illegal to get the value on a field on a type loaded using the ReflectionOnly methods.");
2216 mono_error_set_pending_exception (error
);
2220 if (mono_security_core_clr_enabled () &&
2221 !mono_security_core_clr_ensure_reflection_access_field (cf
, error
)) {
2222 mono_error_set_pending_exception (error
);
2226 #ifndef DISABLE_REMOTING
2227 if (G_UNLIKELY (obj
!= NULL
&& mono_class_is_transparent_proxy (mono_object_class (obj
)))) {
2228 /* We get here if someone used a
2229 * System.Reflection.FieldInfo:GetValue on a
2230 * ContextBoundObject's or cross-domain MarshalByRefObject's
2231 * transparent proxy. */
2232 MonoObject
*result
= mono_load_remote_field_new_checked (obj
, fklass
, cf
, error
);
2233 mono_error_set_pending_exception (error
);
2238 MonoObject
* result
= mono_field_get_value_object_checked (domain
, cf
, obj
, error
);
2239 mono_error_set_pending_exception (error
);
2244 ves_icall_RuntimeFieldInfo_SetValueInternal (MonoReflectionFieldHandle field
, MonoObjectHandle obj
, MonoObjectHandle value
, MonoError
*error
)
2246 MonoClassField
*cf
= MONO_HANDLE_GETVAL (field
, field
);
2248 MonoClass
*field_klass
= MONO_HANDLE_GETVAL (field
, klass
);
2249 if (mono_asmctx_get_kind (&m_class_get_image (field_klass
)->assembly
->context
) == MONO_ASMCTX_REFONLY
) {
2250 mono_error_set_invalid_operation (error
, "It is illegal to set the value on a field on a type loaded using the ReflectionOnly methods.");
2254 if (mono_security_core_clr_enabled () &&
2255 !mono_security_core_clr_ensure_reflection_access_field (cf
, error
)) {
2259 #ifndef DISABLE_REMOTING
2260 if (G_UNLIKELY (!MONO_HANDLE_IS_NULL (obj
) && mono_class_is_transparent_proxy (mono_handle_class (obj
)))) {
2261 /* We get here if someone used a
2262 * System.Reflection.FieldInfo:SetValue on a
2263 * ContextBoundObject's or cross-domain MarshalByRefObject's
2264 * transparent proxy. */
2265 /* FIXME: use handles for mono_store_remote_field_new_checked */
2266 MonoObject
*v
= MONO_HANDLE_RAW (value
);
2267 MonoObject
*o
= MONO_HANDLE_RAW (obj
);
2268 mono_store_remote_field_new_checked (o
, field_klass
, cf
, v
, error
);
2273 MonoType
*type
= mono_field_get_type_checked (cf
, error
);
2274 return_if_nok (error
);
2276 gboolean isref
= FALSE
;
2277 uint32_t value_gchandle
= 0;
2280 switch (type
->type
) {
2283 case MONO_TYPE_BOOLEAN
:
2286 case MONO_TYPE_CHAR
:
2295 case MONO_TYPE_VALUETYPE
:
2298 if (!MONO_HANDLE_IS_NULL (value
))
2299 v
= (char*)mono_object_handle_pin_unbox (value
, &value_gchandle
);
2301 case MONO_TYPE_STRING
:
2302 case MONO_TYPE_OBJECT
:
2303 case MONO_TYPE_CLASS
:
2304 case MONO_TYPE_ARRAY
:
2305 case MONO_TYPE_SZARRAY
:
2309 case MONO_TYPE_GENERICINST
: {
2310 MonoGenericClass
*gclass
= type
->data
.generic_class
;
2311 g_assert (!gclass
->context
.class_inst
->is_open
);
2313 if (mono_class_is_nullable (mono_class_from_mono_type_internal (type
))) {
2314 MonoClass
*nklass
= mono_class_from_mono_type_internal (type
);
2317 * Convert the boxed vtype into a Nullable structure.
2318 * This is complicated by the fact that Nullables have
2319 * a variable structure.
2321 MonoObjectHandle nullable
= mono_object_new_handle (mono_domain_get (), nklass
, error
);
2322 return_if_nok (error
);
2324 uint32_t nullable_gchandle
= 0;
2325 guint8
*nval
= (guint8
*)mono_object_handle_pin_unbox (nullable
, &nullable_gchandle
);
2326 mono_nullable_init_from_handle (nval
, value
, nklass
);
2329 value_gchandle
= nullable_gchandle
;
2333 isref
= !m_class_is_valuetype (gclass
->container_class
);
2334 if (!isref
&& !MONO_HANDLE_IS_NULL (value
)) {
2335 v
= (char*)mono_object_handle_pin_unbox (value
, &value_gchandle
);
2341 g_error ("type 0x%x not handled in "
2342 "ves_icall_FieldInfo_SetValueInternal", type
->type
);
2347 /* either value is a reference type, or it's a value type and we pinned
2348 * it and v points to the payload. */
2349 g_assert ((isref
&& v
== NULL
&& value_gchandle
== 0) ||
2350 (!isref
&& v
!= NULL
&& value_gchandle
!= 0) ||
2351 (!isref
&& v
== NULL
&& value_gchandle
== 0));
2353 if (type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
2354 MonoVTable
*vtable
= mono_class_vtable_checked (MONO_HANDLE_DOMAIN (field
), cf
->parent
, error
);
2355 goto_if_nok (error
, leave
);
2357 if (!vtable
->initialized
) {
2358 if (!mono_runtime_class_init_full (vtable
, error
))
2362 mono_field_static_set_value_internal (vtable
, cf
, MONO_HANDLE_RAW (value
)); /* FIXME make mono_field_static_set_value work with handles for value */
2364 mono_field_static_set_value_internal (vtable
, cf
, v
);
2368 MONO_HANDLE_SET_FIELD_REF (obj
, cf
, value
);
2370 mono_field_set_value_internal (MONO_HANDLE_RAW (obj
), cf
, v
); /* FIXME: make mono_field_set_value take a handle for obj */
2374 mono_gchandle_free_internal (value_gchandle
);
2377 static MonoObjectHandle
2378 typed_reference_to_object (MonoTypedRef
*tref
, MonoError
*error
)
2380 HANDLE_FUNCTION_ENTER ();
2381 MonoObjectHandle result
;
2382 if (MONO_TYPE_IS_REFERENCE (tref
->type
)) {
2383 MonoObject
** objp
= (MonoObject
**)tref
->value
;
2384 result
= MONO_HANDLE_NEW (MonoObject
, *objp
);
2385 } else if (mono_type_is_pointer (tref
->type
)) {
2386 /* Boxed as UIntPtr */
2387 result
= mono_value_box_handle (mono_domain_get (), mono_get_uintptr_class (), tref
->value
, error
);
2389 result
= mono_value_box_handle (mono_domain_get (), tref
->klass
, tref
->value
, error
);
2391 HANDLE_FUNCTION_RETURN_REF (MonoObject
, result
);
2395 ves_icall_System_RuntimeFieldHandle_GetValueDirect (MonoReflectionFieldHandle field_h
, MonoReflectionTypeHandle field_type_h
, MonoTypedRef
*obj
, MonoReflectionTypeHandle context_type_h
, MonoError
*error
)
2397 MonoClassField
*field
= MONO_HANDLE_GETVAL (field_h
, field
);
2398 MonoClass
*klass
= mono_class_from_mono_type_internal (field
->type
);
2400 if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (field
->parent
))) {
2401 mono_error_set_not_implemented (error
, "");
2402 return MONO_HANDLE_NEW (MonoObject
, NULL
);
2403 } else if (MONO_TYPE_IS_REFERENCE (field
->type
)) {
2404 return MONO_HANDLE_NEW (MonoObject
, *(MonoObject
**)((guint8
*)obj
->value
+ field
->offset
- sizeof (MonoObject
)));
2406 return mono_value_box_handle (mono_domain_get (), klass
, (guint8
*)obj
->value
+ field
->offset
- sizeof (MonoObject
), error
);
2411 ves_icall_System_RuntimeFieldHandle_SetValueDirect (MonoReflectionFieldHandle field_h
, MonoReflectionTypeHandle field_type_h
, MonoTypedRef
*obj
, MonoObjectHandle value_h
, MonoReflectionTypeHandle context_type_h
, MonoError
*error
)
2413 MonoClassField
*f
= MONO_HANDLE_GETVAL (field_h
, field
);
2417 if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (f
->parent
))) {
2418 MonoObjectHandle objHandle
= typed_reference_to_object (obj
, error
);
2419 return_if_nok (error
);
2420 ves_icall_RuntimeFieldInfo_SetValueInternal (field_h
, objHandle
, value_h
, error
);
2421 } else if (MONO_TYPE_IS_REFERENCE (f
->type
)) {
2422 mono_copy_value (f
->type
, (guint8
*)obj
->value
+ f
->offset
- sizeof (MonoObject
), MONO_HANDLE_RAW (value_h
), FALSE
);
2425 g_assert (MONO_HANDLE_RAW (value_h
));
2426 mono_copy_value (f
->type
, (guint8
*)obj
->value
+ f
->offset
- sizeof (MonoObject
), mono_object_handle_pin_unbox (value_h
, &gchandle
), FALSE
);
2427 mono_gchandle_free_internal (gchandle
);
2432 ves_icall_RuntimeFieldInfo_GetRawConstantValue (MonoReflectionField
*rfield
)
2434 MonoObject
*o
= NULL
;
2435 MonoClassField
*field
= rfield
->field
;
2437 MonoDomain
*domain
= mono_object_domain (rfield
);
2439 MonoTypeEnum def_type
;
2440 const char *def_value
;
2444 mono_class_init_internal (field
->parent
);
2446 t
= mono_field_get_type_checked (field
, error
);
2447 if (!is_ok (error
)) {
2448 mono_error_set_pending_exception (error
);
2452 if (!(t
->attrs
& FIELD_ATTRIBUTE_HAS_DEFAULT
)) {
2453 mono_error_set_invalid_operation (error
, NULL
);
2454 mono_error_set_pending_exception (error
);
2458 if (image_is_dynamic (m_class_get_image (field
->parent
))) {
2459 MonoClass
*klass
= field
->parent
;
2460 int fidx
= field
- m_class_get_fields (klass
);
2461 MonoFieldDefaultValue
*def_values
= mono_class_get_field_def_values (klass
);
2463 g_assert (def_values
);
2464 def_type
= def_values
[fidx
].def_type
;
2465 def_value
= def_values
[fidx
].data
;
2467 if (def_type
== MONO_TYPE_END
) {
2468 mono_error_set_invalid_operation (error
, NULL
);
2469 mono_error_set_pending_exception (error
);
2473 def_value
= mono_class_get_field_default_value (field
, &def_type
);
2474 /* FIXME, maybe we should try to raise TLE if field->parent is broken */
2476 mono_error_set_invalid_operation (error
, NULL
);
2477 mono_error_set_pending_exception (error
);
2482 /*FIXME unify this with reflection.c:mono_get_object_from_blob*/
2486 case MONO_TYPE_BOOLEAN
:
2489 case MONO_TYPE_CHAR
:
2497 case MONO_TYPE_R8
: {
2500 /* boxed value type */
2501 t
= g_new0 (MonoType
, 1);
2503 klass
= mono_class_from_mono_type_internal (t
);
2505 o
= mono_object_new_checked (domain
, klass
, error
);
2506 if (!is_ok (error
)) {
2507 mono_error_set_pending_exception (error
);
2510 v
= ((gchar
*) o
) + sizeof (MonoObject
);
2511 mono_get_constant_value_from_blob (domain
, def_type
, def_value
, v
, error
);
2512 if (mono_error_set_pending_exception (error
))
2516 case MONO_TYPE_STRING
:
2517 case MONO_TYPE_CLASS
:
2518 mono_get_constant_value_from_blob (domain
, def_type
, def_value
, &o
, error
);
2519 if (mono_error_set_pending_exception (error
))
2523 g_assert_not_reached ();
2529 MonoReflectionTypeHandle
2530 ves_icall_RuntimeFieldInfo_ResolveType (MonoReflectionFieldHandle ref_field
, MonoError
*error
)
2532 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_field
);
2533 MonoClassField
*field
= MONO_HANDLE_GETVAL (ref_field
, field
);
2534 MonoType
*type
= mono_field_get_type_checked (field
, error
);
2535 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
2536 return mono_type_get_object_handle (domain
, type
, error
);
2540 ves_icall_RuntimePropertyInfo_get_property_info (MonoReflectionPropertyHandle property
, MonoPropertyInfo
*info
, PInfo req_info
, MonoError
*error
)
2542 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (property
);
2543 const MonoProperty
*pproperty
= MONO_HANDLE_GETVAL (property
, property
);
2545 if ((req_info
& PInfo_ReflectedType
) != 0) {
2546 MonoClass
*klass
= MONO_HANDLE_GETVAL (property
, klass
);
2547 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
2548 return_if_nok (error
);
2550 MONO_STRUCT_SETREF_INTERNAL (info
, parent
, MONO_HANDLE_RAW (rt
));
2552 if ((req_info
& PInfo_DeclaringType
) != 0) {
2553 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (pproperty
->parent
), error
);
2554 return_if_nok (error
);
2556 MONO_STRUCT_SETREF_INTERNAL (info
, declaring_type
, MONO_HANDLE_RAW (rt
));
2559 if ((req_info
& PInfo_Name
) != 0) {
2560 MonoStringHandle name
= mono_string_new_handle (domain
, pproperty
->name
, error
);
2561 return_if_nok (error
);
2563 MONO_STRUCT_SETREF_INTERNAL (info
, name
, MONO_HANDLE_RAW (name
));
2566 if ((req_info
& PInfo_Attributes
) != 0)
2567 info
->attrs
= pproperty
->attrs
;
2569 if ((req_info
& PInfo_GetMethod
) != 0) {
2570 MonoClass
*property_klass
= MONO_HANDLE_GETVAL (property
, klass
);
2571 MonoReflectionMethodHandle rm
;
2572 if (pproperty
->get
&&
2573 (((pproperty
->get
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) != METHOD_ATTRIBUTE_PRIVATE
) ||
2574 pproperty
->get
->klass
== property_klass
)) {
2575 rm
= mono_method_get_object_handle (domain
, pproperty
->get
, property_klass
, error
);
2576 return_if_nok (error
);
2578 rm
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
2581 MONO_STRUCT_SETREF_INTERNAL (info
, get
, MONO_HANDLE_RAW (rm
));
2583 if ((req_info
& PInfo_SetMethod
) != 0) {
2584 MonoClass
*property_klass
= MONO_HANDLE_GETVAL (property
, klass
);
2585 MonoReflectionMethodHandle rm
;
2586 if (pproperty
->set
&&
2587 (((pproperty
->set
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) != METHOD_ATTRIBUTE_PRIVATE
) ||
2588 pproperty
->set
->klass
== property_klass
)) {
2589 rm
= mono_method_get_object_handle (domain
, pproperty
->set
, property_klass
, error
);
2590 return_if_nok (error
);
2592 rm
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
2595 MONO_STRUCT_SETREF_INTERNAL (info
, set
, MONO_HANDLE_RAW (rm
));
2598 * There may be other methods defined for properties, though, it seems they are not exposed
2599 * in the reflection API
2604 add_event_other_methods_to_array (MonoDomain
*domain
, MonoMethod
*m
, MonoArrayHandle dest
, int i
, MonoError
*error
)
2606 HANDLE_FUNCTION_ENTER ();
2608 MonoReflectionMethodHandle rm
= mono_method_get_object_handle (domain
, m
, NULL
, error
);
2609 goto_if_nok (error
, leave
);
2610 MONO_HANDLE_ARRAY_SETREF (dest
, i
, rm
);
2612 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
2616 ves_icall_RuntimeEventInfo_get_event_info (MonoReflectionMonoEventHandle ref_event
, MonoEventInfo
*info
, MonoError
*error
)
2618 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_event
);
2620 MonoClass
*klass
= MONO_HANDLE_GETVAL (ref_event
, klass
);
2621 MonoEvent
*event
= MONO_HANDLE_GETVAL (ref_event
, event
);
2623 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
2624 return_if_nok (error
);
2625 MONO_STRUCT_SETREF_INTERNAL (info
, reflected_type
, MONO_HANDLE_RAW (rt
));
2627 rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (event
->parent
), error
);
2628 return_if_nok (error
);
2629 MONO_STRUCT_SETREF_INTERNAL (info
, declaring_type
, MONO_HANDLE_RAW (rt
));
2631 MonoStringHandle ev_name
= mono_string_new_handle (domain
, event
->name
, error
);
2632 return_if_nok (error
);
2633 MONO_STRUCT_SETREF_INTERNAL (info
, name
, MONO_HANDLE_RAW (ev_name
));
2635 info
->attrs
= event
->attrs
;
2637 MonoReflectionMethodHandle rm
;
2639 rm
= mono_method_get_object_handle (domain
, event
->add
, klass
, error
);
2640 return_if_nok (error
);
2642 rm
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
2645 MONO_STRUCT_SETREF_INTERNAL (info
, add_method
, MONO_HANDLE_RAW (rm
));
2647 if (event
->remove
) {
2648 rm
= mono_method_get_object_handle (domain
, event
->remove
, klass
, error
);
2649 return_if_nok (error
);
2651 rm
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
2654 MONO_STRUCT_SETREF_INTERNAL (info
, remove_method
, MONO_HANDLE_RAW (rm
));
2657 rm
= mono_method_get_object_handle (domain
, event
->raise
, klass
, error
);
2658 return_if_nok (error
);
2660 rm
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
2663 MONO_STRUCT_SETREF_INTERNAL (info
, raise_method
, MONO_HANDLE_RAW (rm
));
2665 #ifndef MONO_SMALL_CONFIG
2668 while (event
->other
[n
])
2670 MonoArrayHandle info_arr
= mono_array_new_handle (domain
, mono_defaults
.method_info_class
, n
, error
);
2671 return_if_nok (error
);
2673 MONO_STRUCT_SETREF_INTERNAL (info
, other_methods
, MONO_HANDLE_RAW (info_arr
));
2675 for (i
= 0; i
< n
; i
++)
2676 if (!add_event_other_methods_to_array (domain
, event
->other
[i
], info_arr
, i
, error
))
2683 collect_interfaces (MonoClass
*klass
, GHashTable
*ifaces
, MonoError
*error
)
2688 mono_class_setup_interfaces (klass
, error
);
2689 return_if_nok (error
);
2691 int klass_interface_count
= m_class_get_interface_count (klass
);
2692 MonoClass
**klass_interfaces
= m_class_get_interfaces (klass
);
2693 for (i
= 0; i
< klass_interface_count
; i
++) {
2694 ic
= klass_interfaces
[i
];
2695 g_hash_table_insert (ifaces
, ic
, ic
);
2697 collect_interfaces (ic
, ifaces
, error
);
2698 return_if_nok (error
);
2703 MonoArrayHandle iface_array
;
2704 MonoGenericContext
*context
;
2708 } FillIfaceArrayData
;
2711 fill_iface_array (gpointer key
, gpointer value
, gpointer user_data
)
2713 HANDLE_FUNCTION_ENTER ();
2714 FillIfaceArrayData
*data
= (FillIfaceArrayData
*)user_data
;
2715 MonoClass
*ic
= (MonoClass
*)key
;
2716 MonoType
*ret
= m_class_get_byval_arg (ic
), *inflated
= NULL
;
2717 MonoError
*error
= data
->error
;
2719 goto_if_nok (error
, leave
);
2721 if (data
->context
&& mono_class_is_ginst (ic
) && mono_class_get_generic_class (ic
)->context
.class_inst
->is_open
) {
2722 inflated
= ret
= mono_class_inflate_generic_type_checked (ret
, data
->context
, error
);
2723 goto_if_nok (error
, leave
);
2726 MonoReflectionTypeHandle rt
;
2727 rt
= mono_type_get_object_handle (data
->domain
, ret
, error
);
2728 goto_if_nok (error
, leave
);
2730 MONO_HANDLE_ARRAY_SETREF (data
->iface_array
, data
->next_idx
, rt
);
2734 mono_metadata_free_type (inflated
);
2736 HANDLE_FUNCTION_RETURN ();
2740 get_interfaces_hash (gconstpointer v1
)
2742 MonoClass
*k
= (MonoClass
*)v1
;
2744 return m_class_get_type_token (k
);
2748 ves_icall_RuntimeType_GetInterfaces (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2750 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2751 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2753 GHashTable
*iface_hash
= g_hash_table_new (get_interfaces_hash
, NULL
);
2755 MonoGenericContext
*context
= NULL
;
2756 if (mono_class_is_ginst (klass
) && mono_class_get_generic_class (klass
)->context
.class_inst
->is_open
) {
2757 context
= mono_class_get_context (klass
);
2758 klass
= mono_class_get_generic_class (klass
)->container_class
;
2761 for (MonoClass
*parent
= klass
; parent
; parent
= m_class_get_parent (parent
)) {
2762 mono_class_setup_interfaces (parent
, error
);
2763 goto_if_nok (error
, fail
);
2764 collect_interfaces (parent
, iface_hash
, error
);
2765 goto_if_nok (error
, fail
);
2769 domain
= MONO_HANDLE_DOMAIN (ref_type
);
2772 len
= g_hash_table_size (iface_hash
);
2774 g_hash_table_destroy (iface_hash
);
2775 if (!domain
->empty_types
) {
2776 domain
->empty_types
= mono_array_new_cached (domain
, mono_defaults
.runtimetype_class
, 0, error
);
2777 goto_if_nok (error
, fail
);
2779 return MONO_HANDLE_NEW (MonoArray
, domain
->empty_types
);
2782 FillIfaceArrayData data
;
2783 data
.iface_array
= MONO_HANDLE_NEW (MonoArray
, mono_array_new_cached (domain
, mono_defaults
.runtimetype_class
, len
, error
));
2784 goto_if_nok (error
, fail
);
2785 data
.context
= context
;
2787 data
.domain
= domain
;
2790 g_hash_table_foreach (iface_hash
, fill_iface_array
, &data
);
2792 goto_if_nok (error
, fail
);
2794 g_hash_table_destroy (iface_hash
);
2795 return data
.iface_array
;
2798 g_hash_table_destroy (iface_hash
);
2799 return NULL_HANDLE_ARRAY
;
2803 set_interface_map_data_method_object (MonoDomain
*domain
, MonoMethod
*method
, MonoClass
*iclass
, int ioffset
, MonoClass
*klass
, MonoArrayHandle targets
, MonoArrayHandle methods
, int i
, MonoError
*error
)
2805 HANDLE_FUNCTION_ENTER ();
2807 MonoReflectionMethodHandle member
= mono_method_get_object_handle (domain
, method
, iclass
, error
);
2808 goto_if_nok (error
, leave
);
2810 MONO_HANDLE_ARRAY_SETREF (methods
, i
, member
);
2812 MONO_HANDLE_ASSIGN (member
, mono_method_get_object_handle (domain
, m_class_get_vtable (klass
) [i
+ ioffset
], klass
, error
));
2813 goto_if_nok (error
, leave
);
2815 MONO_HANDLE_ARRAY_SETREF (targets
, i
, member
);
2818 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
2822 ves_icall_RuntimeType_GetInterfaceMapData (MonoReflectionTypeHandle ref_type
, MonoReflectionTypeHandle ref_iface
, MonoArrayHandleOut targets
, MonoArrayHandleOut methods
, MonoError
*error
)
2824 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2825 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2826 MonoType
*iface
= MONO_HANDLE_GETVAL (ref_iface
, type
);
2827 MonoClass
*iclass
= mono_class_from_mono_type_internal (iface
);
2829 mono_class_init_checked (klass
, error
);
2830 return_if_nok (error
);
2831 mono_class_init_checked (iclass
, error
);
2832 return_if_nok (error
);
2834 mono_class_setup_vtable (klass
);
2836 gboolean variance_used
;
2837 int ioffset
= mono_class_interface_offset_with_variance (klass
, iclass
, &variance_used
);
2841 int len
= mono_class_num_methods (iclass
);
2842 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
2843 MonoArrayHandle targets_arr
= mono_array_new_handle (domain
, mono_defaults
.method_info_class
, len
, error
);
2844 return_if_nok (error
);
2845 MONO_HANDLE_ASSIGN (targets
, targets_arr
);
2847 MonoArrayHandle methods_arr
= mono_array_new_handle (domain
, mono_defaults
.method_info_class
, len
, error
);
2848 return_if_nok (error
);
2849 MONO_HANDLE_ASSIGN (methods
, methods_arr
);
2853 gpointer iter
= NULL
;
2854 while ((method
= mono_class_get_methods (iclass
, &iter
))) {
2855 if (!set_interface_map_data_method_object (domain
, method
, iclass
, ioffset
, klass
, targets
, methods
, i
, error
))
2862 ves_icall_RuntimeType_GetPacking (MonoReflectionTypeHandle ref_type
, guint32
*packing
, guint32
*size
, MonoError
*error
)
2864 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2865 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2867 mono_class_init_checked (klass
, error
);
2868 return_if_nok (error
);
2870 if (image_is_dynamic (m_class_get_image (klass
))) {
2871 MonoReflectionTypeBuilderHandle tb
= MONO_HANDLE_CAST (MonoReflectionTypeBuilder
, ref_type
);
2872 *packing
= MONO_HANDLE_GETVAL (tb
, packing_size
);
2873 *size
= MONO_HANDLE_GETVAL (tb
, class_size
);
2875 mono_metadata_packing_from_typedef (m_class_get_image (klass
), m_class_get_type_token (klass
), packing
, size
);
2879 MonoReflectionTypeHandle
2880 ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2882 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
2883 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2885 if (!type
->byref
&& type
->type
== MONO_TYPE_SZARRAY
) {
2886 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (type
->data
.klass
), error
);
2889 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2890 mono_class_init_checked (klass
, error
);
2891 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
2893 // GetElementType should only return a type for:
2894 // Array Pointer PassedByRef
2896 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
2897 else if (m_class_get_element_class (klass
) && MONO_CLASS_IS_ARRAY (klass
))
2898 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (m_class_get_element_class (klass
)), error
);
2899 else if (m_class_get_element_class (klass
) && type
->type
== MONO_TYPE_PTR
)
2900 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (m_class_get_element_class (klass
)), error
);
2902 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
2905 MonoReflectionTypeHandle
2906 ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2908 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
2909 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2912 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
2914 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2915 if (!m_class_get_parent (klass
))
2916 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
2918 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (m_class_get_parent (klass
)), error
);
2922 ves_icall_RuntimeTypeHandle_GetCorElementType (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2924 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2927 return MONO_TYPE_BYREF
;
2929 return (guint32
)type
->type
;
2933 ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2935 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2938 klass
= mono_class_from_mono_type_internal (type
);
2939 mono_class_init_internal (klass
);
2940 return m_class_has_references (klass
);
2944 ves_icall_RuntimeTypeHandle_IsByRefLike (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2946 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2947 /* .NET Core says byref types are not IsByRefLike */
2950 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2951 return m_class_is_byreflike (klass
);
2955 ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2957 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2958 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
2959 mono_class_init_checked (klass
, error
);
2960 return_val_if_nok (error
, FALSE
);
2962 return mono_class_is_com_object (klass
);
2966 ves_icall_reflection_get_token (MonoObjectHandle obj
, MonoError
*error
)
2969 return mono_reflection_get_token_checked (obj
, error
);
2972 MonoReflectionModuleHandle
2973 ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionTypeHandle type
, MonoError
*error
)
2975 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (type
);
2976 MonoType
*t
= MONO_HANDLE_GETVAL (type
, type
);
2977 MonoClass
*klass
= mono_class_from_mono_type_internal (t
);
2978 return mono_module_get_object_handle (domain
, m_class_get_image (klass
), error
);
2981 MonoReflectionAssemblyHandle
2982 ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionTypeHandle type
, MonoError
*error
)
2984 MonoDomain
*domain
= mono_domain_get ();
2985 MonoType
*t
= MONO_HANDLE_GETVAL (type
, type
);
2986 MonoClass
*klass
= mono_class_from_mono_type_internal (t
);
2987 return mono_assembly_get_object_handle (domain
, m_class_get_image (klass
)->assembly
, error
);
2990 MonoReflectionTypeHandle
2991 ves_icall_RuntimeType_get_DeclaringType (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
2993 MonoDomain
*domain
= mono_domain_get ();
2994 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
2998 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
2999 if (type
->type
== MONO_TYPE_VAR
) {
3000 MonoGenericContainer
*param
= mono_type_get_generic_param_owner (type
);
3001 klass
= param
? param
->owner
.klass
: NULL
;
3002 } else if (type
->type
== MONO_TYPE_MVAR
) {
3003 MonoGenericContainer
*param
= mono_type_get_generic_param_owner (type
);
3004 klass
= param
? param
->owner
.method
->klass
: NULL
;
3006 klass
= m_class_get_nested_in (mono_class_from_mono_type_internal (type
));
3010 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
3012 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
3016 ves_icall_RuntimeType_get_Name (MonoReflectionTypeHandle reftype
, MonoError
*error
)
3018 MonoDomain
*domain
= mono_domain_get ();
3019 MonoType
*type
= MONO_HANDLE_RAW(reftype
)->type
;
3020 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
3023 char *n
= g_strdup_printf ("%s&", m_class_get_name (klass
));
3024 MonoStringHandle res
= mono_string_new_handle (domain
, n
, error
);
3030 return mono_string_new_handle (domain
, m_class_get_name (klass
), error
);
3035 ves_icall_RuntimeType_get_Namespace (MonoReflectionTypeHandle type
, MonoError
*error
)
3037 MonoDomain
*domain
= mono_domain_get ();
3038 MonoClass
*klass
= mono_class_from_mono_type_handle (type
);
3040 MonoClass
*klass_nested_in
;
3041 while ((klass_nested_in
= m_class_get_nested_in (klass
)))
3042 klass
= klass_nested_in
;
3044 if (m_class_get_name_space (klass
) [0] == '\0')
3045 return NULL_HANDLE_STRING
;
3047 return mono_string_new_handle (domain
, m_class_get_name_space (klass
), error
);
3051 ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3054 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3056 if (type
->type
!= MONO_TYPE_ARRAY
&& type
->type
!= MONO_TYPE_SZARRAY
) {
3057 mono_error_set_argument (error
, "type", "Type must be an array type");
3061 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
3063 return m_class_get_rank (klass
);
3066 static MonoArrayHandle
3067 create_type_array (MonoDomain
*domain
, MonoBoolean runtimeTypeArray
, int count
, MonoError
*error
)
3069 return mono_array_new_handle (domain
, runtimeTypeArray
? mono_defaults
.runtimetype_class
: mono_defaults
.systemtype_class
, count
, error
);
3073 set_type_object_in_array (MonoDomain
*domain
, MonoType
*type
, MonoArrayHandle dest
, int i
, MonoError
*error
)
3075 HANDLE_FUNCTION_ENTER();
3077 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, type
, error
);
3078 goto_if_nok (error
, leave
);
3080 MONO_HANDLE_ARRAY_SETREF (dest
, i
, rt
);
3083 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
3087 ves_icall_RuntimeType_GetGenericArguments (MonoReflectionTypeHandle ref_type
, MonoBoolean runtimeTypeArray
, MonoError
*error
)
3090 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
3092 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3093 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
3095 MonoArrayHandle res
= MONO_HANDLE_NEW (MonoArray
, NULL
);
3096 if (mono_class_is_gtd (klass
)) {
3097 MonoGenericContainer
*container
= mono_class_get_generic_container (klass
);
3098 MONO_HANDLE_ASSIGN (res
, create_type_array (domain
, runtimeTypeArray
, container
->type_argc
, error
));
3099 goto_if_nok (error
, leave
);
3100 for (int i
= 0; i
< container
->type_argc
; ++i
) {
3101 MonoClass
*pklass
= mono_class_create_generic_parameter (mono_generic_container_get_param (container
, i
));
3103 if (!set_type_object_in_array (domain
, m_class_get_byval_arg (pklass
), res
, i
, error
))
3107 } else if (mono_class_is_ginst (klass
)) {
3108 MonoGenericInst
*inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
3109 MONO_HANDLE_ASSIGN (res
, create_type_array (domain
, runtimeTypeArray
, inst
->type_argc
, error
));
3110 goto_if_nok (error
, leave
);
3111 for (int i
= 0; i
< inst
->type_argc
; ++i
) {
3112 if (!set_type_object_in_array (domain
, inst
->type_argv
[i
], res
, i
, error
))
3122 ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3126 if (!IS_MONOTYPE (MONO_HANDLE_RAW(ref_type
)))
3129 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3133 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
3134 return mono_class_is_gtd (klass
);
3137 MonoReflectionTypeHandle
3138 ves_icall_RuntimeTypeHandle_GetGenericTypeDefinition_impl (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3141 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3143 MonoReflectionTypeHandle ret
= MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
3149 klass
= mono_class_from_mono_type_internal (type
);
3151 if (mono_class_is_gtd (klass
)) {
3152 /* check this one */
3153 MONO_HANDLE_ASSIGN (ret
, ref_type
);
3156 if (mono_class_is_ginst (klass
)) {
3157 MonoClass
*generic_class
= mono_class_get_generic_class (klass
)->container_class
;
3159 guint32 ref_info_handle
= mono_class_get_ref_info_handle (generic_class
);
3161 if (m_class_was_typebuilder (generic_class
) && ref_info_handle
) {
3162 MonoObjectHandle tb
= mono_gchandle_get_target_handle (ref_info_handle
);
3163 g_assert (!MONO_HANDLE_IS_NULL (tb
));
3164 MONO_HANDLE_ASSIGN (ret
, tb
);
3166 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
3167 MONO_HANDLE_ASSIGN (ret
, mono_type_get_object_handle (domain
, m_class_get_byval_arg (generic_class
), error
));
3174 MonoReflectionTypeHandle
3175 ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype
, MonoArrayHandle type_array
, MonoError
*error
)
3178 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (reftype
);
3180 g_assert (IS_MONOTYPE_HANDLE (reftype
));
3181 MonoType
*type
= MONO_HANDLE_GETVAL (reftype
, type
);
3182 mono_class_init_checked (mono_class_from_mono_type_internal (type
), error
);
3183 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
3185 int count
= mono_array_handle_length (type_array
);
3186 MonoType
**types
= g_new0 (MonoType
*, count
);
3188 MonoReflectionTypeHandle t
= MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
3189 for (int i
= 0; i
< count
; i
++) {
3190 MONO_HANDLE_ARRAY_GETREF (t
, type_array
, i
);
3191 types
[i
] = MONO_HANDLE_GETVAL (t
, type
);
3194 MonoType
*geninst
= mono_reflection_bind_generic_parameters (reftype
, count
, types
, error
);
3197 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
3200 MonoClass
*klass
= mono_class_from_mono_type_internal (geninst
);
3202 /*we might inflate to the GTD*/
3203 if (mono_class_is_ginst (klass
) && !mono_verifier_class_is_valid_generic_instantiation (klass
)) {
3204 mono_error_set_argument (error
, "typeArguments", "Invalid generic arguments");
3205 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
3208 return mono_type_get_object_handle (domain
, geninst
, error
);
3212 ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3217 if (!IS_MONOTYPE (MONO_HANDLE_RAW (ref_type
)))
3220 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3224 klass
= mono_class_from_mono_type_internal (type
);
3225 return mono_class_is_ginst (klass
) || mono_class_is_gtd (klass
);
3229 ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3232 if (!IS_MONOTYPE_HANDLE (ref_type
))
3234 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3236 if (is_generic_parameter (type
))
3237 return mono_type_get_generic_param_num (type
);
3241 MonoGenericParamInfo
*
3242 ves_icall_RuntimeTypeHandle_GetGenericParameterInfo (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3245 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3246 return mono_generic_param_info (type
->data
.generic_param
);
3250 ves_icall_RuntimeTypeHandle_IsGenericVariable (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3252 MonoType
*type
= MONO_HANDLE_GETVAL(ref_type
, type
);
3253 return is_generic_parameter (type
);
3256 MonoReflectionMethodHandle
3257 ves_icall_RuntimeType_GetCorrespondingInflatedMethod (MonoReflectionTypeHandle ref_type
,
3258 MonoReflectionMethodHandle generic
,
3262 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
3263 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3264 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
3266 mono_class_init_checked (klass
, error
);
3267 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
));
3269 MonoMethod
*generic_method
= MONO_HANDLE_GETVAL (generic
, method
);
3271 MonoReflectionMethodHandle ret
= MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
3273 gpointer iter
= NULL
;
3274 while ((method
= mono_class_get_methods (klass
, &iter
))) {
3275 if (method
->token
== generic_method
->token
) {
3276 ret
= mono_method_get_object_handle (domain
, method
, klass
, error
);
3277 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
));
3284 MonoReflectionMethodHandle
3285 ves_icall_RuntimeType_get_DeclaringMethod (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
3288 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
3289 MonoReflectionMethodHandle ret
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
3291 if (type
->byref
|| (type
->type
!= MONO_TYPE_MVAR
&& type
->type
!= MONO_TYPE_VAR
)) {
3292 mono_error_set_invalid_operation (error
, "DeclaringMethod can only be used on generic arguments");
3295 if (type
->type
== MONO_TYPE_VAR
)
3299 method
= mono_type_get_generic_param_owner (type
)->owner
.method
;
3303 domain
= MONO_HANDLE_DOMAIN (ref_type
);
3305 MONO_HANDLE_ASSIGN (ret
, mono_method_get_object_handle (domain
, method
, method
->klass
, error
));
3311 ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError
*error
)
3314 mono_error_set_not_implemented (error
, "%s", "System.RuntimeType.IsTypeExportedToWindowsRuntime");
3319 ves_icall_System_RuntimeType_IsWindowsRuntimeObjectType (MonoError
*error
)
3322 mono_error_set_not_implemented (error
, "%s", "System.RuntimeType.IsWindowsRuntimeObjectType");
3327 ves_icall_RuntimeMethodInfo_GetPInvoke (MonoReflectionMethodHandle ref_method
, int* flags
, MonoStringHandleOut entry_point
, MonoStringHandleOut dll_name
, MonoError
*error
)
3329 MonoDomain
*domain
= mono_domain_get ();
3330 MonoMethod
*method
= MONO_HANDLE_GETVAL (ref_method
, method
);
3331 MonoImage
*image
= m_class_get_image (method
->klass
);
3332 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*)method
;
3333 MonoTableInfo
*tables
= image
->tables
;
3334 MonoTableInfo
*im
= &tables
[MONO_TABLE_IMPLMAP
];
3335 MonoTableInfo
*mr
= &tables
[MONO_TABLE_MODULEREF
];
3336 guint32 im_cols
[MONO_IMPLMAP_SIZE
];
3337 guint32 scope_token
;
3338 const char *import
= NULL
;
3339 const char *scope
= NULL
;
3343 if (image_is_dynamic (image
)) {
3344 MonoReflectionMethodAux
*method_aux
=
3345 (MonoReflectionMethodAux
*)g_hash_table_lookup (((MonoDynamicImage
*)image
)->method_aux_hash
, method
);
3347 import
= method_aux
->dllentry
;
3348 scope
= method_aux
->dll
;
3351 if (!import
|| !scope
) {
3352 mono_error_set_argument (error
, "method", "System.Refleciton.Emit method with invalid pinvoke information");
3357 if (piinfo
->implmap_idx
) {
3358 mono_metadata_decode_row (im
, piinfo
->implmap_idx
- 1, im_cols
, MONO_IMPLMAP_SIZE
);
3360 piinfo
->piflags
= im_cols
[MONO_IMPLMAP_FLAGS
];
3361 import
= mono_metadata_string_heap (image
, im_cols
[MONO_IMPLMAP_NAME
]);
3362 scope_token
= mono_metadata_decode_row_col (mr
, im_cols
[MONO_IMPLMAP_SCOPE
] - 1, MONO_MODULEREF_NAME
);
3363 scope
= mono_metadata_string_heap (image
, scope_token
);
3367 *flags
= piinfo
->piflags
;
3368 MONO_HANDLE_ASSIGN (entry_point
, mono_string_new_handle (domain
, import
, error
));
3369 return_if_nok (error
);
3370 MONO_HANDLE_ASSIGN (dll_name
, mono_string_new_handle (domain
, scope
, error
));
3373 MonoReflectionMethodHandle
3374 ves_icall_RuntimeMethodInfo_GetGenericMethodDefinition (MonoReflectionMethodHandle ref_method
, MonoError
*error
)
3377 MonoMethod
*method
= MONO_HANDLE_GETVAL (ref_method
, method
);
3379 if (method
->is_generic
)
3382 if (!method
->is_inflated
)
3383 return MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
3385 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
3387 MonoMethod
*result
= imethod
->declaring
;
3388 /* Not a generic method. */
3389 if (!result
->is_generic
)
3390 return MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
3392 if (image_is_dynamic (m_class_get_image (method
->klass
))) {
3393 MonoDynamicImage
*image
= (MonoDynamicImage
*)m_class_get_image (method
->klass
);
3396 * FIXME: Why is this stuff needed at all ? Why can't the code below work for
3397 * the dynamic case as well ?
3399 mono_image_lock ((MonoImage
*)image
);
3400 MonoReflectionMethodHandle res
= MONO_HANDLE_NEW (MonoReflectionMethod
, (MonoReflectionMethod
*)mono_g_hash_table_lookup (image
->generic_def_objects
, imethod
));
3401 mono_image_unlock ((MonoImage
*)image
);
3403 if (!MONO_HANDLE_IS_NULL (res
))
3407 if (imethod
->context
.class_inst
) {
3408 MonoClass
*klass
= ((MonoMethod
*) imethod
)->klass
;
3409 /*Generic methods gets the context of the GTD.*/
3410 if (mono_class_get_context (klass
)) {
3411 result
= mono_class_inflate_generic_method_full_checked (result
, klass
, mono_class_get_context (klass
), error
);
3412 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
));
3416 return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (ref_method
), result
, NULL
, error
);
3420 ves_icall_RuntimeMethodInfo_get_IsGenericMethod (MonoReflectionMethodHandle ref_method
, MonoError
*erro
)
3422 MonoMethod
*method
= MONO_HANDLE_GETVAL (ref_method
, method
);
3423 return mono_method_signature_internal (method
)->generic_param_count
!= 0;
3427 ves_icall_RuntimeMethodInfo_get_IsGenericMethodDefinition (MonoReflectionMethodHandle ref_method
, MonoError
*Error
)
3429 MonoMethod
*method
= MONO_HANDLE_GETVAL (ref_method
, method
);
3430 return method
->is_generic
;
3434 set_array_generic_argument_handle_inflated (MonoDomain
*domain
, MonoGenericInst
*inst
, int i
, MonoArrayHandle arr
, MonoError
*error
)
3436 HANDLE_FUNCTION_ENTER ();
3438 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, inst
->type_argv
[i
], error
);
3439 goto_if_nok (error
, leave
);
3440 MONO_HANDLE_ARRAY_SETREF (arr
, i
, rt
);
3442 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
3446 set_array_generic_argument_handle_gparam (MonoDomain
*domain
, MonoGenericContainer
*container
, int i
, MonoArrayHandle arr
, MonoError
*error
)
3448 HANDLE_FUNCTION_ENTER ();
3450 MonoGenericParam
*param
= mono_generic_container_get_param (container
, i
);
3451 MonoClass
*pklass
= mono_class_create_generic_parameter (param
);
3452 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (pklass
), error
);
3453 goto_if_nok (error
, leave
);
3454 MONO_HANDLE_ARRAY_SETREF (arr
, i
, rt
);
3456 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
3460 ves_icall_RuntimeMethodInfo_GetGenericArguments (MonoReflectionMethodHandle ref_method
, MonoError
*error
)
3463 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_method
);
3464 MonoMethod
*method
= MONO_HANDLE_GETVAL (ref_method
, method
);
3466 if (method
->is_inflated
) {
3467 MonoGenericInst
*inst
= mono_method_get_context (method
)->method_inst
;
3470 int count
= inst
->type_argc
;
3471 MonoArrayHandle res
= mono_array_new_handle (domain
, mono_defaults
.systemtype_class
, count
, error
);
3472 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
3474 for (int i
= 0; i
< count
; i
++) {
3475 if (!set_array_generic_argument_handle_inflated (domain
, inst
, i
, res
, error
))
3478 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
3483 int count
= mono_method_signature_internal (method
)->generic_param_count
;
3484 MonoArrayHandle res
= mono_array_new_handle (domain
, mono_defaults
.systemtype_class
, count
, error
);
3485 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
3487 MonoGenericContainer
*container
= mono_method_get_generic_container (method
);
3488 for (int i
= 0; i
< count
; i
++) {
3489 if (!set_array_generic_argument_handle_gparam (domain
, container
, i
, res
, error
))
3492 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
3497 ves_icall_InternalInvoke (MonoReflectionMethod
*method
, MonoObject
*this_arg
, MonoArray
*params
, MonoException
**exc
)
3501 * Invoke from reflection is supposed to always be a virtual call (the API
3502 * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
3503 * greater flexibility.
3505 MonoMethod
*m
= method
->method
;
3506 MonoMethodSignature
*sig
= mono_method_signature_internal (m
);
3509 void *obj
= this_arg
;
3513 if (mono_security_core_clr_enabled () &&
3514 !mono_security_core_clr_ensure_reflection_access_method (m
, error
)) {
3515 mono_error_set_pending_exception (error
);
3519 if (!(m
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3520 if (!mono_class_vtable_checked (mono_object_domain (method
), m
->klass
, error
)) {
3521 mono_error_cleanup (error
); /* FIXME does this make sense? */
3522 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_class_get_exception_for_failure (m
->klass
));
3527 if (!mono_object_isinst_checked (this_arg
, m
->klass
, error
)) {
3528 if (!is_ok (error
)) {
3529 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_error_convert_to_exception (error
));
3532 char *this_name
= mono_type_get_full_name (mono_object_class (this_arg
));
3533 char *target_name
= mono_type_get_full_name (m
->klass
);
3534 char *msg
= g_strdup_printf ("Object of type '%s' doesn't match target type '%s'", this_name
, target_name
);
3535 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Reflection", "TargetException", msg
));
3537 g_free (target_name
);
3541 m
= mono_object_get_virtual_method_internal (this_arg
, m
);
3542 /* must pass the pointer to the value for valuetype methods */
3543 if (m_class_is_valuetype (m
->klass
))
3544 obj
= mono_object_unbox_internal (this_arg
);
3545 } else if (strcmp (m
->name
, ".ctor") && !m
->wrapper_type
) {
3546 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Reflection", "TargetException", "Non-static method requires a target."));
3551 if (sig
->ret
->byref
) {
3553 MonoType
* ret_byval
= m_class_get_byval_arg (mono_class_from_mono_type_internal (sig
->ret
));
3554 if (ret_byval
->byref
) {
3555 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "NotSupportedException", "Cannot invoke method returning ByRef to ByRefLike type via reflection"));
3559 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_exception_from_name_msg (mono_defaults
.corlib
, "System", "NotSupportedException", "Cannot invoke method returning ByRef type via reflection"));
3564 pcount
= params
? mono_array_length_internal (params
): 0;
3565 if (pcount
!= sig
->param_count
) {
3566 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_exception_from_name (mono_defaults
.corlib
, "System.Reflection", "TargetParameterCountException"));
3570 if (mono_class_is_abstract (m
->klass
) && !strcmp (m
->name
, ".ctor") && !this_arg
) {
3571 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Reflection", "TargetException", "Cannot invoke constructor of an abstract class."));
3575 image
= m_class_get_image (m
->klass
);
3576 if (mono_asmctx_get_kind (&image
->assembly
->context
) == MONO_ASMCTX_REFONLY
) {
3577 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_get_exception_invalid_operation ("It is illegal to invoke a method on a type loaded using the ReflectionOnly api."));
3581 if (image_is_dynamic (image
) && !((MonoDynamicImage
*)image
)->run
) {
3582 mono_gc_wbarrier_generic_store_internal (exc
, (MonoObject
*) mono_get_exception_not_supported ("Cannot invoke a method in a dynamic assembly without run access."));
3586 if (m_class_get_rank (m
->klass
) && !strcmp (m
->name
, ".ctor")) {
3590 intptr_t *lower_bounds
;
3591 pcount
= mono_array_length_internal (params
);
3592 lengths
= g_newa (uintptr_t, pcount
);
3593 /* Note: the synthetized array .ctors have int32 as argument type */
3594 for (i
= 0; i
< pcount
; ++i
)
3595 lengths
[i
] = *(int32_t*) ((char*)mono_array_get_internal (params
, gpointer
, i
) + sizeof (MonoObject
));
3597 if (m_class_get_rank (m
->klass
) == 1 && sig
->param_count
== 2 && m_class_get_rank (m_class_get_element_class (m
->klass
))) {
3598 /* This is a ctor for jagged arrays. MS creates an array of arrays. */
3599 arr
= mono_array_new_full_checked (mono_object_domain (params
), m
->klass
, lengths
, NULL
, error
);
3600 if (!is_ok (error
)) {
3601 mono_error_set_pending_exception (error
);
3605 for (i
= 0; i
< mono_array_length_internal (arr
); ++i
) {
3606 MonoArray
*subarray
= mono_array_new_full_checked (mono_object_domain (params
), m_class_get_element_class (m
->klass
), &lengths
[1], NULL
, error
);
3607 if (!is_ok (error
)) {
3608 mono_error_set_pending_exception (error
);
3611 mono_array_setref_fast (arr
, i
, subarray
);
3613 return (MonoObject
*)arr
;
3616 if (m_class_get_rank (m
->klass
) == pcount
) {
3617 /* Only lengths provided. */
3618 arr
= mono_array_new_full_checked (mono_object_domain (params
), m
->klass
, lengths
, NULL
, error
);
3619 if (!is_ok (error
)) {
3620 mono_error_set_pending_exception (error
);
3624 return (MonoObject
*)arr
;
3626 g_assert (pcount
== (m_class_get_rank (m
->klass
) * 2));
3627 /* The arguments are lower-bound-length pairs */
3628 lower_bounds
= (intptr_t *)g_alloca (sizeof (intptr_t) * pcount
);
3630 for (i
= 0; i
< pcount
/ 2; ++i
) {
3631 lower_bounds
[i
] = *(int32_t*) ((char*)mono_array_get_internal (params
, gpointer
, (i
* 2)) + sizeof (MonoObject
));
3632 lengths
[i
] = *(int32_t*) ((char*)mono_array_get_internal (params
, gpointer
, (i
* 2) + 1) + sizeof (MonoObject
));
3635 arr
= mono_array_new_full_checked (mono_object_domain (params
), m
->klass
, lengths
, lower_bounds
, error
);
3636 if (!is_ok (error
)) {
3637 mono_error_set_pending_exception (error
);
3641 return (MonoObject
*)arr
;
3644 MonoObject
*result
= mono_runtime_invoke_array_checked (m
, obj
, params
, error
);
3645 mono_error_set_pending_exception (error
);
3649 #ifndef DISABLE_REMOTING
3651 internal_execute_field_getter (MonoDomain
*domain
, MonoObject
*this_arg
, MonoArray
*params
, MonoArray
**outArgs
, MonoError
*error
)
3654 MonoArray
*out_args
;
3655 MonoClass
*k
= mono_object_class (this_arg
);
3659 /* If this is a proxy, then it must be a CBO */
3660 if (mono_class_is_transparent_proxy (k
)) {
3661 MonoTransparentProxy
*tp
= (MonoTransparentProxy
*) this_arg
;
3662 this_arg
= tp
->rp
->unwrapped_server
;
3663 g_assert (this_arg
);
3664 k
= mono_object_class (this_arg
);
3667 name
= mono_array_get_internal (params
, MonoString
*, 1);
3668 str
= mono_string_to_utf8_checked_internal (name
, error
);
3669 return_if_nok (error
);
3672 MonoClassField
* field
= mono_class_get_field_from_name_full (k
, str
, NULL
);
3675 MonoClass
*field_klass
= mono_class_from_mono_type_internal (field
->type
);
3677 if (m_class_is_valuetype (field_klass
)) {
3678 result
= mono_value_box_checked (domain
, field_klass
, (char *)this_arg
+ field
->offset
, error
);
3679 return_if_nok (error
);
3681 result
= (MonoObject
*)*((gpointer
*)((char *)this_arg
+ field
->offset
));
3683 out_args
= mono_array_new_checked (domain
, mono_defaults
.object_class
, 1, error
);
3684 return_if_nok (error
);
3685 mono_gc_wbarrier_generic_store_internal (outArgs
, (MonoObject
*) out_args
);
3686 mono_array_setref_internal (out_args
, 0, result
);
3689 k
= m_class_get_parent (k
);
3693 g_assert_not_reached ();
3697 internal_execute_field_setter (MonoDomain
*domain
, MonoObject
*this_arg
, MonoArray
*params
, MonoArray
**outArgs
, MonoError
*error
)
3700 MonoArray
*out_args
;
3701 MonoClass
*k
= mono_object_class (this_arg
);
3707 /* If this is a proxy, then it must be a CBO */
3708 if (mono_class_is_transparent_proxy (k
)) {
3709 MonoTransparentProxy
*tp
= (MonoTransparentProxy
*) this_arg
;
3710 this_arg
= tp
->rp
->unwrapped_server
;
3711 g_assert (this_arg
);
3712 k
= mono_object_class (this_arg
);
3715 name
= mono_array_get_internal (params
, MonoString
*, 1);
3716 str
= mono_string_to_utf8_checked_internal (name
, error
);
3717 return_if_nok (error
);
3720 MonoClassField
* field
= mono_class_get_field_from_name_full (k
, str
, NULL
);
3723 MonoClass
*field_klass
= mono_class_from_mono_type_internal (field
->type
);
3724 MonoObject
*val
= (MonoObject
*)mono_array_get_internal (params
, gpointer
, 2);
3726 if (m_class_is_valuetype (field_klass
)) {
3727 size
= mono_type_size (field
->type
, &align
);
3728 g_assert (size
== mono_class_value_size (field_klass
, NULL
));
3729 mono_gc_wbarrier_value_copy_internal ((char *)this_arg
+ field
->offset
, (char*)val
+ sizeof (MonoObject
), 1, field_klass
);
3731 mono_gc_wbarrier_set_field_internal (this_arg
, (char*)this_arg
+ field
->offset
, val
);
3734 out_args
= mono_array_new_checked (domain
, mono_defaults
.object_class
, 0, error
);
3735 return_if_nok (error
);
3736 mono_gc_wbarrier_generic_store_internal (outArgs
, (MonoObject
*) out_args
);
3740 k
= m_class_get_parent (k
);
3744 g_assert_not_reached ();
3748 ves_icall_InternalExecute (MonoReflectionMethod
*method
, MonoObject
*this_arg
, MonoArray
*params
, MonoArray
**outArgs
)
3751 MonoDomain
*domain
= mono_object_domain (method
);
3752 MonoMethod
*m
= method
->method
;
3753 MonoMethodSignature
*sig
= mono_method_signature_internal (m
);
3754 MonoArray
*out_args
;
3756 int i
, j
, outarg_count
= 0;
3758 if (m
->klass
== mono_defaults
.object_class
) {
3759 if (!strcmp (m
->name
, "FieldGetter")) {
3760 internal_execute_field_getter (domain
, this_arg
, params
, outArgs
, error
);
3761 mono_error_set_pending_exception (error
);
3763 } else if (!strcmp (m
->name
, "FieldSetter")) {
3764 internal_execute_field_setter (domain
, this_arg
, params
, outArgs
, error
);
3765 mono_error_set_pending_exception (error
);
3770 for (i
= 0; i
< mono_array_length_internal (params
); i
++) {
3771 if (sig
->params
[i
]->byref
)
3775 out_args
= mono_array_new_checked (domain
, mono_defaults
.object_class
, outarg_count
, error
);
3776 if (mono_error_set_pending_exception (error
))
3779 /* handle constructors only for objects already allocated */
3780 if (!strcmp (method
->method
->name
, ".ctor"))
3781 g_assert (this_arg
);
3783 /* This can be called only on MBR objects, so no need to unbox for valuetypes. */
3784 g_assert (!m_class_is_valuetype (method
->method
->klass
));
3785 result
= mono_runtime_invoke_array_checked (method
->method
, this_arg
, params
, error
);
3786 if (mono_error_set_pending_exception (error
))
3789 for (i
= 0, j
= 0; i
< mono_array_length_internal (params
); i
++) {
3790 if (sig
->params
[i
]->byref
) {
3792 arg
= mono_array_get_internal (params
, gpointer
, i
);
3793 mono_array_setref_internal (out_args
, j
, arg
);
3798 mono_gc_wbarrier_generic_store_internal (outArgs
, (MonoObject
*) out_args
);
3805 read_enum_value (const char *mem
, int type
)
3808 case MONO_TYPE_BOOLEAN
:
3810 return *(guint8
*)mem
;
3812 return *(gint8
*)mem
;
3813 case MONO_TYPE_CHAR
:
3815 return read16 (mem
);
3817 return (gint16
) read16 (mem
);
3820 return read32 (mem
);
3822 return (gint32
) read32 (mem
);
3826 return read64 (mem
);
3829 #if SIZEOF_REGISTER == 8
3830 return read64 (mem
);
3832 return read32 (mem
);
3835 g_assert_not_reached ();
3841 write_enum_value (void *mem
, int type
, guint64 value
)
3846 case MONO_TYPE_BOOLEAN
: {
3847 guint8
*p
= (guint8
*)mem
;
3853 case MONO_TYPE_CHAR
: {
3854 guint16
*p
= (guint16
*)mem
;
3860 case MONO_TYPE_R4
: {
3861 guint32
*p
= (guint32
*)mem
;
3867 case MONO_TYPE_R8
: {
3868 guint64
*p
= (guint64
*)mem
;
3874 #if SIZEOF_REGISTER == 8
3875 guint64
*p
= (guint64
*)mem
;
3878 guint32
*p
= (guint32
*)mem
;
3885 g_assert_not_reached ();
3891 ves_icall_System_Enum_ToObject (MonoReflectionTypeHandle enumType
, guint64 value
, MonoError
*error
)
3895 MonoObjectHandle resultHandle
;
3898 domain
= MONO_HANDLE_DOMAIN (enumType
);
3899 enumc
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (enumType
, type
));
3901 mono_class_init_checked (enumc
, error
);
3902 goto_if_nok (error
, return_null
);
3904 etype
= mono_class_enum_basetype_internal (enumc
);
3906 resultHandle
= mono_object_new_handle (domain
, enumc
, error
);
3907 goto_if_nok (error
, return_null
);
3909 write_enum_value (mono_handle_unbox_unsafe (resultHandle
), etype
->type
, value
);
3911 return resultHandle
;
3914 return MONO_HANDLE_NEW (MonoObject
, NULL
);
3918 ves_icall_System_Enum_InternalHasFlag (MonoObjectHandle a
, MonoObjectHandle b
, MonoError
*error
)
3920 int size
= mono_class_value_size (mono_handle_class (a
), NULL
);
3921 guint64 a_val
= 0, b_val
= 0;
3923 memcpy (&a_val
, mono_handle_unbox_unsafe (a
), size
);
3924 memcpy (&b_val
, mono_handle_unbox_unsafe (b
), size
);
3926 return (a_val
& b_val
) == b_val
;
3930 ves_icall_System_Enum_get_value (MonoObjectHandle ehandle
, MonoError
*error
)
3932 MonoObjectHandle resultHandle
;
3936 goto_if (MONO_HANDLE_IS_NULL (ehandle
), return_null
);
3938 g_assert (m_class_is_enumtype (mono_handle_class (ehandle
)));
3940 enumc
= mono_class_from_mono_type_internal (mono_class_enum_basetype_internal (mono_handle_class (ehandle
)));
3942 resultHandle
= mono_object_new_handle (MONO_HANDLE_DOMAIN (ehandle
), enumc
, error
);
3943 goto_if_nok (error
, return_null
);
3944 size
= mono_class_value_size (enumc
, NULL
);
3946 memcpy (mono_handle_unbox_unsafe (resultHandle
), mono_handle_unbox_unsafe (ehandle
), size
);
3948 return resultHandle
;
3950 return MONO_HANDLE_NEW (MonoObject
, NULL
);
3953 MonoReflectionTypeHandle
3954 ves_icall_System_Enum_get_underlying_type (MonoReflectionTypeHandle type
, MonoError
*error
)
3959 klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type
, type
));
3960 mono_class_init_checked (klass
, error
);
3961 goto_if_nok (error
, return_null
);
3963 etype
= mono_class_enum_basetype_internal (klass
);
3965 mono_error_set_argument (error
, "enumType", "Type provided must be an Enum.");
3969 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (type
), etype
, error
);
3972 return MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
3976 ves_icall_System_Enum_InternalGetCorElementType (MonoObjectHandle this_handle
, MonoError
*error
)
3978 MonoClass
*klass
= MONO_HANDLE_GETVAL (this_handle
, vtable
)->klass
;
3980 return (int)m_class_get_byval_arg (m_class_get_element_class (klass
))->type
;
3984 ves_icall_System_Enum_compare_value_to (MonoObjectHandle enumHandle
, MonoObjectHandle otherHandle
, MonoError
*error
)
3986 if (MONO_HANDLE_IS_NULL (otherHandle
))
3989 if (MONO_HANDLE_GETVAL (enumHandle
, vtable
)->klass
!= MONO_HANDLE_GETVAL (otherHandle
, vtable
)->klass
)
3992 gpointer tdata
= mono_handle_unbox_unsafe (enumHandle
);
3993 gpointer odata
= mono_handle_unbox_unsafe (otherHandle
);
3994 MonoType
*basetype
= mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle
, vtable
)->klass
);
3995 g_assert (basetype
);
3997 #define COMPARE_ENUM_VALUES(ENUM_TYPE) do { \
3998 ENUM_TYPE me = *((ENUM_TYPE*)tdata); \
3999 ENUM_TYPE other = *((ENUM_TYPE*)odata); \
4002 return me > other ? 1 : -1; \
4005 switch (basetype
->type
) {
4006 case MONO_TYPE_BOOLEAN
:
4008 COMPARE_ENUM_VALUES (guint8
);
4010 COMPARE_ENUM_VALUES (gint8
);
4011 case MONO_TYPE_CHAR
:
4013 COMPARE_ENUM_VALUES (guint16
);
4015 COMPARE_ENUM_VALUES (gint16
);
4017 COMPARE_ENUM_VALUES (guint32
);
4019 COMPARE_ENUM_VALUES (gint32
);
4021 COMPARE_ENUM_VALUES (gfloat
);
4023 COMPARE_ENUM_VALUES (guint64
);
4025 COMPARE_ENUM_VALUES (gint64
);
4027 COMPARE_ENUM_VALUES (gdouble
);
4029 #if SIZEOF_REGISTER == 8
4030 COMPARE_ENUM_VALUES (guint64
);
4032 COMPARE_ENUM_VALUES (guint32
);
4035 #if SIZEOF_REGISTER == 8
4036 COMPARE_ENUM_VALUES (gint64
);
4038 COMPARE_ENUM_VALUES (gint32
);
4041 #undef COMPARE_ENUM_VALUES
4042 /* indicates that the enum was of an unsupported underlying type */
4047 ves_icall_System_Enum_get_hashcode (MonoObjectHandle enumHandle
, MonoError
*error
)
4049 gpointer data
= mono_handle_unbox_unsafe (enumHandle
);
4050 MonoType
*basetype
= mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle
, vtable
)->klass
);
4051 g_assert (basetype
);
4053 switch (basetype
->type
) {
4054 case MONO_TYPE_I1
: {
4055 gint8 value
= *((gint8
*)data
);
4056 return ((int)value
^ (int)value
<< 8);
4059 return *((guint8
*)data
);
4060 case MONO_TYPE_CHAR
:
4062 return *((guint16
*)data
);
4064 case MONO_TYPE_I2
: {
4065 gint16 value
= *((gint16
*)data
);
4066 return ((int)(guint16
)value
| (((int)value
) << 16));
4070 return *((guint32
*)data
);
4072 return *((gint32
*)data
);
4075 case MONO_TYPE_R8
: {
4076 gint64 value
= *((gint64
*)data
);
4077 return (gint
)(value
& 0xffffffff) ^ (int)(value
>> 32);
4081 #if SIZEOF_REGISTER == 8
4082 gint64 value
= *((gint64
*)data
);
4083 return (gint
)(value
& 0xffffffff) ^ (int)(value
>> 32);
4085 return *((guint32
*)data
);
4089 g_error ("Implement type 0x%02x in get_hashcode", basetype
->type
);
4095 get_enum_field (MonoDomain
*domain
, MonoArrayHandle names
, MonoArrayHandle values
, int base_type
, MonoClassField
*field
, guint
* j
, guint64
*previous_value
, gboolean
*sorted
, MonoError
*error
)
4098 HANDLE_FUNCTION_ENTER();
4099 guint64 field_value
;
4101 MonoTypeEnum def_type
;
4103 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
4105 if (strcmp ("value__", mono_field_get_name (field
)) == 0)
4107 if (mono_field_is_deleted (field
))
4109 MonoStringHandle name
;
4110 name
= mono_string_new_handle (domain
, mono_field_get_name (field
), error
);
4111 goto_if_nok (error
, leave
);
4112 MONO_HANDLE_ARRAY_SETREF (names
, *j
, name
);
4114 p
= mono_class_get_field_default_value (field
, &def_type
);
4115 /* len = */ mono_metadata_decode_blob_size (p
, &p
);
4117 field_value
= read_enum_value (p
, base_type
);
4118 MONO_HANDLE_ARRAY_SETVAL (values
, guint64
, *j
, field_value
);
4120 if (*previous_value
> field_value
)
4123 *previous_value
= field_value
;
4126 HANDLE_FUNCTION_RETURN();
4130 ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionTypeHandle type
, MonoArrayHandleOut values
, MonoArrayHandleOut names
, MonoError
*error
)
4132 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (type
);
4133 MonoClass
*enumc
= mono_class_from_mono_type_internal (MONO_HANDLE_RAW(type
)->type
);
4134 guint j
= 0, nvalues
;
4136 MonoClassField
*field
;
4138 guint64 previous_value
= 0;
4139 gboolean sorted
= TRUE
;
4142 mono_class_init_checked (enumc
, error
);
4143 return_val_if_nok (error
, FALSE
);
4145 if (!m_class_is_enumtype (enumc
)) {
4147 mono_error_set_argument (error
, NULL
, "Type provided must be an Enum.");
4149 mono_error_set_argument (error
, "enumType", "Type provided must be an Enum.");
4154 base_type
= mono_class_enum_basetype_internal (enumc
)->type
;
4156 nvalues
= mono_class_num_fields (enumc
) > 0 ? mono_class_num_fields (enumc
) - 1 : 0;
4157 MONO_HANDLE_ASSIGN(names
, mono_array_new_handle (domain
, mono_defaults
.string_class
, nvalues
, error
));
4158 return_val_if_nok (error
, FALSE
);
4159 MONO_HANDLE_ASSIGN(values
, mono_array_new_handle (domain
, mono_defaults
.uint64_class
, nvalues
, error
));
4160 return_val_if_nok (error
, FALSE
);
4163 while ((field
= mono_class_get_fields_internal (enumc
, &iter
))) {
4164 get_enum_field(domain
, names
, values
, base_type
, field
, &j
, &previous_value
, &sorted
, error
);
4168 return_val_if_nok (error
, FALSE
);
4170 return sorted
|| base_type
== MONO_TYPE_R4
|| base_type
== MONO_TYPE_R8
;
4174 BFLAGS_IgnoreCase
= 1,
4175 BFLAGS_DeclaredOnly
= 2,
4176 BFLAGS_Instance
= 4,
4178 BFLAGS_Public
= 0x10,
4179 BFLAGS_NonPublic
= 0x20,
4180 BFLAGS_FlattenHierarchy
= 0x40,
4181 BFLAGS_InvokeMethod
= 0x100,
4182 BFLAGS_CreateInstance
= 0x200,
4183 BFLAGS_GetField
= 0x400,
4184 BFLAGS_SetField
= 0x800,
4185 BFLAGS_GetProperty
= 0x1000,
4186 BFLAGS_SetProperty
= 0x2000,
4187 BFLAGS_ExactBinding
= 0x10000,
4188 BFLAGS_SuppressChangeType
= 0x20000,
4189 BFLAGS_OptionalParamBinding
= 0x40000
4194 MLISTTYPE_CaseSensitive
= 1,
4195 MLISTTYPE_CaseInsensitive
= 2,
4196 MLISTTYPE_HandleToInfo
= 3
4200 ves_icall_RuntimeType_GetFields_native (MonoReflectionTypeHandle ref_type
, char *utf8_name
, guint32 bflags
, guint32 mlisttype
, MonoError
*error
)
4203 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
4206 return g_ptr_array_new ();
4209 int (*compare_func
) (const char *s1
, const char *s2
) = NULL
;
4210 compare_func
= ((bflags
& BFLAGS_IgnoreCase
) || (mlisttype
== MLISTTYPE_CaseInsensitive
)) ? mono_utf8_strcasecmp
: strcmp
;
4212 MonoClass
*startklass
, *klass
;
4213 klass
= startklass
= mono_class_from_mono_type_internal (type
);
4215 GPtrArray
*ptr_array
= g_ptr_array_sized_new (16);
4218 if (mono_class_has_failure (klass
)) {
4219 mono_error_set_for_class_failure (error
, klass
);
4223 MonoClassField
*field
;
4226 while ((field
= mono_class_get_fields_lazy (klass
, &iter
))) {
4227 guint32 flags
= mono_field_get_flags (field
);
4229 if (mono_field_is_deleted_with_flags (field
, flags
))
4231 if ((flags
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
) == FIELD_ATTRIBUTE_PUBLIC
) {
4232 if (bflags
& BFLAGS_Public
)
4234 } else if ((klass
== startklass
) || (flags
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
) != FIELD_ATTRIBUTE_PRIVATE
) {
4235 if (bflags
& BFLAGS_NonPublic
) {
4242 if (flags
& FIELD_ATTRIBUTE_STATIC
) {
4243 if (bflags
& BFLAGS_Static
)
4244 if ((bflags
& BFLAGS_FlattenHierarchy
) || (klass
== startklass
))
4247 if (bflags
& BFLAGS_Instance
)
4254 if (((mlisttype
!= MLISTTYPE_All
) && (utf8_name
!= NULL
)) && compare_func (mono_field_get_name (field
), utf8_name
))
4257 g_ptr_array_add (ptr_array
, field
);
4259 if (!(bflags
& BFLAGS_DeclaredOnly
) && (klass
= m_class_get_parent (klass
)))
4265 g_ptr_array_free (ptr_array
, TRUE
);
4270 method_nonpublic (MonoMethod
* method
, gboolean start_klass
)
4272 switch (method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) {
4273 case METHOD_ATTRIBUTE_ASSEM
:
4274 return (start_klass
|| mono_defaults
.generic_ilist_class
);
4275 case METHOD_ATTRIBUTE_PRIVATE
:
4277 case METHOD_ATTRIBUTE_PUBLIC
:
4285 mono_class_get_methods_by_name (MonoClass
*klass
, const char *name
, guint32 bflags
, guint32 mlisttype
, gboolean allow_ctors
, MonoError
*error
)
4288 MonoClass
*startklass
;
4292 /*FIXME, use MonoBitSet*/
4293 guint32 method_slots_default
[8];
4294 guint32
*method_slots
= NULL
;
4295 int (*compare_func
) (const char *s1
, const char *s2
) = NULL
;
4297 array
= g_ptr_array_new ();
4301 compare_func
= ((bflags
& BFLAGS_IgnoreCase
) || (mlisttype
== MLISTTYPE_CaseInsensitive
)) ? mono_utf8_strcasecmp
: strcmp
;
4303 /* An optimization for calls made from Delegate:CreateDelegate () */
4304 if (m_class_is_delegate (klass
) && klass
!= mono_defaults
.delegate_class
&& klass
!= mono_defaults
.multicastdelegate_class
&& name
&& !strcmp (name
, "Invoke") && (bflags
== (BFLAGS_Public
| BFLAGS_Static
| BFLAGS_Instance
))) {
4305 method
= mono_get_delegate_invoke_internal (klass
);
4308 g_ptr_array_add (array
, method
);
4312 mono_class_setup_methods (klass
);
4313 mono_class_setup_vtable (klass
);
4314 if (mono_class_has_failure (klass
))
4317 if (is_generic_parameter (m_class_get_byval_arg (klass
)))
4318 nslots
= mono_class_get_vtable_size (m_class_get_parent (klass
));
4320 nslots
= MONO_CLASS_IS_INTERFACE_INTERNAL (klass
) ? mono_class_num_methods (klass
) : mono_class_get_vtable_size (klass
);
4321 if (nslots
>= sizeof (method_slots_default
) * 8) {
4322 method_slots
= g_new0 (guint32
, nslots
/ 32 + 1);
4324 method_slots
= method_slots_default
;
4325 memset (method_slots
, 0, sizeof (method_slots_default
));
4328 mono_class_setup_methods (klass
);
4329 mono_class_setup_vtable (klass
);
4330 if (mono_class_has_failure (klass
))
4334 while ((method
= mono_class_get_methods (klass
, &iter
))) {
4336 if (method
->slot
!= -1) {
4337 g_assert (method
->slot
< nslots
);
4338 if (method_slots
[method
->slot
>> 5] & (1 << (method
->slot
& 0x1f)))
4340 if (!(method
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
))
4341 method_slots
[method
->slot
>> 5] |= 1 << (method
->slot
& 0x1f);
4344 if (!allow_ctors
&& method
->name
[0] == '.' && (strcmp (method
->name
, ".ctor") == 0 || strcmp (method
->name
, ".cctor") == 0))
4346 if ((method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
) {
4347 if (bflags
& BFLAGS_Public
)
4349 } else if ((bflags
& BFLAGS_NonPublic
) && method_nonpublic (method
, (klass
== startklass
))) {
4355 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
4356 if (bflags
& BFLAGS_Static
)
4357 if ((bflags
& BFLAGS_FlattenHierarchy
) || (klass
== startklass
))
4360 if (bflags
& BFLAGS_Instance
)
4367 if ((mlisttype
!= MLISTTYPE_All
) && (name
!= NULL
)) {
4368 if (compare_func (name
, method
->name
))
4373 g_ptr_array_add (array
, method
);
4375 if (!(bflags
& BFLAGS_DeclaredOnly
) && (klass
= m_class_get_parent (klass
)))
4377 if (method_slots
!= method_slots_default
)
4378 g_free (method_slots
);
4383 if (method_slots
!= method_slots_default
)
4384 g_free (method_slots
);
4385 g_ptr_array_free (array
, TRUE
);
4387 g_assert (mono_class_has_failure (klass
));
4388 mono_error_set_for_class_failure (error
, klass
);
4393 ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type
, const char *mname
, guint32 bflags
, guint32 mlisttype
, MonoError
*error
)
4396 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
4398 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
4400 return g_ptr_array_new ();
4403 return mono_class_get_methods_by_name (klass
, mname
, bflags
, mlisttype
, FALSE
, error
);
4407 ves_icall_RuntimeType_GetConstructors_native (MonoReflectionTypeHandle ref_type
, guint32 bflags
, MonoError
*error
)
4410 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
4412 return g_ptr_array_new ();
4415 MonoClass
*startklass
, *klass
;
4416 klass
= startklass
= mono_class_from_mono_type_internal (type
);
4418 mono_class_setup_methods (klass
);
4419 if (mono_class_has_failure (klass
)) {
4420 mono_error_set_for_class_failure (error
, klass
);
4425 GPtrArray
*res_array
= g_ptr_array_sized_new (4); /* FIXME, guestimating */
4428 gpointer iter
= NULL
;
4429 while ((method
= mono_class_get_methods (klass
, &iter
))) {
4431 if (strcmp (method
->name
, ".ctor") && strcmp (method
->name
, ".cctor"))
4433 if ((method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
) {
4434 if (bflags
& BFLAGS_Public
)
4437 if (bflags
& BFLAGS_NonPublic
)
4443 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
4444 if (bflags
& BFLAGS_Static
)
4445 if ((bflags
& BFLAGS_FlattenHierarchy
) || (klass
== startklass
))
4448 if (bflags
& BFLAGS_Instance
)
4454 g_ptr_array_add (res_array
, method
);
4461 property_hash (gconstpointer data
)
4463 MonoProperty
*prop
= (MonoProperty
*)data
;
4465 return g_str_hash (prop
->name
);
4469 property_accessor_override (MonoMethod
*method1
, MonoMethod
*method2
)
4471 if (method1
->slot
!= -1 && method1
->slot
== method2
->slot
)
4474 if (mono_class_get_generic_type_definition (method1
->klass
) == mono_class_get_generic_type_definition (method2
->klass
)) {
4475 if (method1
->is_inflated
)
4476 method1
= ((MonoMethodInflated
*) method1
)->declaring
;
4477 if (method2
->is_inflated
)
4478 method2
= ((MonoMethodInflated
*) method2
)->declaring
;
4481 return mono_metadata_signature_equal (mono_method_signature_internal (method1
), mono_method_signature_internal (method2
));
4485 property_equal (MonoProperty
*prop1
, MonoProperty
*prop2
)
4487 // Properties are hide-by-name-and-signature
4488 if (!g_str_equal (prop1
->name
, prop2
->name
))
4491 /* If we see a property in a generic method, we want to
4492 compare the generic signatures, not the inflated signatures
4493 because we might conflate two properties that were
4497 public T this[T t] { getter { return t; } } // method 1
4498 public U this[U u] { getter { return u; } } // method 2
4501 If we see int Foo<int,int>::Item[int] we need to know if
4502 the indexer came from method 1 or from method 2, and we
4503 shouldn't conflate them. (Bugzilla 36283)
4505 if (prop1
->get
&& prop2
->get
&& !property_accessor_override (prop1
->get
, prop2
->get
))
4508 if (prop1
->set
&& prop2
->set
&& !property_accessor_override (prop1
->set
, prop2
->set
))
4515 property_accessor_nonpublic (MonoMethod
* accessor
, gboolean start_klass
)
4520 return method_nonpublic (accessor
, start_klass
);
4524 ves_icall_RuntimeType_GetPropertiesByName_native (MonoReflectionTypeHandle ref_type
, gchar
*propname
, guint32 bflags
, guint32 mlisttype
, MonoError
*error
)
4527 // Fetch non-public properties as well because they can hide public properties with the same name in base classes
4528 bflags
|= BFLAGS_NonPublic
;
4531 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
4535 return g_ptr_array_new ();
4539 MonoClass
*startklass
, *klass
;
4540 klass
= startklass
= mono_class_from_mono_type_internal (type
);
4542 int (*compare_func
) (const char *s1
, const char *s2
) = (mlisttype
== MLISTTYPE_CaseInsensitive
) ? mono_utf8_strcasecmp
: strcmp
;
4544 GPtrArray
*res_array
= g_ptr_array_sized_new (8); /*This the average for ASP.NET types*/
4546 GHashTable
*properties
= g_hash_table_new (property_hash
, (GEqualFunc
)property_equal
);
4549 mono_class_setup_methods (klass
);
4550 mono_class_setup_vtable (klass
);
4551 if (mono_class_has_failure (klass
)) {
4552 mono_error_set_for_class_failure (error
, klass
);
4559 while ((prop
= mono_class_get_properties (klass
, &iter
))) {
4561 MonoMethod
*method
= prop
->get
;
4566 flags
= method
->flags
;
4568 if ((prop
->get
&& ((prop
->get
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
)) ||
4569 (prop
->set
&& ((prop
->set
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
))) {
4570 if (bflags
& BFLAGS_Public
)
4572 } else if (bflags
& BFLAGS_NonPublic
) {
4573 if (property_accessor_nonpublic(prop
->get
, startklass
== klass
) ||
4574 property_accessor_nonpublic(prop
->set
, startklass
== klass
)) {
4582 if (flags
& METHOD_ATTRIBUTE_STATIC
) {
4583 if (bflags
& BFLAGS_Static
)
4584 if ((bflags
& BFLAGS_FlattenHierarchy
) || (klass
== startklass
))
4587 if (bflags
& BFLAGS_Instance
)
4595 if ((mlisttype
!= MLISTTYPE_All
) && (propname
!= NULL
) && compare_func (propname
, prop
->name
))
4598 if (g_hash_table_lookup (properties
, prop
))
4601 g_ptr_array_add (res_array
, prop
);
4603 g_hash_table_insert (properties
, prop
, prop
);
4605 if (!(bflags
& BFLAGS_DeclaredOnly
) && (klass
= m_class_get_parent (klass
))) {
4607 // BFLAGS_NonPublic should be excluded for base classes
4608 bflags
&= ~BFLAGS_NonPublic
;
4613 g_hash_table_destroy (properties
);
4620 g_hash_table_destroy (properties
);
4621 g_ptr_array_free (res_array
, TRUE
);
4627 event_hash (gconstpointer data
)
4629 MonoEvent
*event
= (MonoEvent
*)data
;
4631 return g_str_hash (event
->name
);
4635 event_equal (MonoEvent
*event1
, MonoEvent
*event2
)
4637 // Events are hide-by-name
4638 return g_str_equal (event1
->name
, event2
->name
);
4642 ves_icall_RuntimeType_GetEvents_native (MonoReflectionTypeHandle ref_type
, char *utf8_name
, guint32 mlisttype
, MonoError
*error
)
4645 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
4648 return g_ptr_array_new ();
4651 int (*compare_func
) (const char *s1
, const char *s2
) = (mlisttype
== MLISTTYPE_CaseInsensitive
) ? mono_utf8_strcasecmp
: strcmp
;
4653 GPtrArray
*res_array
= g_ptr_array_sized_new (4);
4655 MonoClass
*startklass
, *klass
;
4656 klass
= startklass
= mono_class_from_mono_type_internal (type
);
4658 GHashTable
*events
= g_hash_table_new (event_hash
, (GEqualFunc
)event_equal
);
4660 mono_class_setup_methods (klass
);
4661 mono_class_setup_vtable (klass
);
4662 if (mono_class_has_failure (klass
)) {
4663 mono_error_set_for_class_failure (error
, klass
);
4670 while ((event
= mono_class_get_events (klass
, &iter
))) {
4672 // Remove inherited privates and inherited
4673 // without add/remove/raise methods
4674 if (klass
!= startklass
)
4676 MonoMethod
*method
= event
->add
;
4678 method
= event
->remove
;
4680 method
= event
->raise
;
4683 if ((method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PRIVATE
)
4687 if ((mlisttype
!= MLISTTYPE_All
) && (utf8_name
!= NULL
) && compare_func (event
->name
, utf8_name
))
4690 if (g_hash_table_lookup (events
, event
))
4693 g_ptr_array_add (res_array
, event
);
4695 g_hash_table_insert (events
, event
, event
);
4697 if ((klass
= m_class_get_parent (klass
)))
4700 g_hash_table_destroy (events
);
4706 g_hash_table_destroy (events
);
4708 g_ptr_array_free (res_array
, TRUE
);
4714 ves_icall_RuntimeType_GetNestedTypes_native (MonoReflectionTypeHandle ref_type
, char *str
, guint32 bflags
, guint32 mlisttype
, MonoError
*error
)
4717 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
4720 return g_ptr_array_new ();
4723 int (*compare_func
) (const char *s1
, const char *s2
) = ((bflags
& BFLAGS_IgnoreCase
) || (mlisttype
== MLISTTYPE_CaseInsensitive
)) ? mono_utf8_strcasecmp
: strcmp
;
4725 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
4728 * If a nested type is generic, return its generic type definition.
4729 * Note that this means that the return value is essentially the set
4730 * of nested types of the generic type definition of @klass.
4732 * A note in MSDN claims that a generic type definition can have
4733 * nested types that aren't generic. In any case, the container of that
4734 * nested type would be the generic type definition.
4736 if (mono_class_is_ginst (klass
))
4737 klass
= mono_class_get_generic_class (klass
)->container_class
;
4739 GPtrArray
*res_array
= g_ptr_array_new ();
4742 gpointer iter
= NULL
;
4743 while ((nested
= mono_class_get_nested_types (klass
, &iter
))) {
4745 if ((mono_class_get_flags (nested
) & TYPE_ATTRIBUTE_VISIBILITY_MASK
) == TYPE_ATTRIBUTE_NESTED_PUBLIC
) {
4746 if (bflags
& BFLAGS_Public
)
4749 if (bflags
& BFLAGS_NonPublic
)
4755 if ((mlisttype
!= MLISTTYPE_All
) && (str
!= NULL
) && compare_func (m_class_get_name (nested
), str
))
4758 g_ptr_array_add (res_array
, m_class_get_byval_arg (nested
));
4765 get_type_from_module_builder_module (MonoArrayHandle modules
, int i
, MonoTypeNameParse
*info
, MonoBoolean ignoreCase
, gboolean
*type_resolve
, MonoError
*error
)
4767 HANDLE_FUNCTION_ENTER ();
4769 MonoType
*type
= NULL
;
4770 MonoReflectionModuleBuilderHandle mb
= MONO_HANDLE_NEW (MonoReflectionModuleBuilder
, NULL
);
4771 MONO_HANDLE_ARRAY_GETREF (mb
, modules
, i
);
4772 MonoDynamicImage
*dynamic_image
= MONO_HANDLE_GETVAL (mb
, dynamic_image
);
4773 type
= mono_reflection_get_type_checked (&dynamic_image
->image
, &dynamic_image
->image
, info
, ignoreCase
, type_resolve
, error
);
4774 HANDLE_FUNCTION_RETURN_VAL (type
);
4778 get_type_from_module_builder_loaded_modules (MonoArrayHandle loaded_modules
, int i
, MonoTypeNameParse
*info
, MonoBoolean ignoreCase
, gboolean
*type_resolve
, MonoError
*error
)
4780 HANDLE_FUNCTION_ENTER ();
4782 MonoType
*type
= NULL
;
4783 MonoReflectionModuleHandle mod
= MONO_HANDLE_NEW (MonoReflectionModule
, NULL
);
4784 MONO_HANDLE_ARRAY_GETREF (mod
, loaded_modules
, i
);
4785 MonoImage
*image
= MONO_HANDLE_GETVAL (mod
, image
);
4786 type
= mono_reflection_get_type_checked (image
, image
, info
, ignoreCase
, type_resolve
, error
);
4787 HANDLE_FUNCTION_RETURN_VAL (type
);
4790 MonoReflectionTypeHandle
4791 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHandle assembly_h
, MonoReflectionModuleHandle module
, MonoStringHandle name
, MonoBoolean throwOnError
, MonoBoolean ignoreCase
, MonoError
*error
)
4794 ERROR_DECL (parse_error
);
4796 MonoTypeNameParse info
;
4797 gboolean type_resolve
;
4799 /* On MS.NET, this does not fire a TypeResolve event */
4800 type_resolve
= TRUE
;
4801 char *str
= mono_string_handle_to_utf8 (name
, error
);
4802 goto_if_nok (error
, fail
);
4804 /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
4805 if (!mono_reflection_parse_type_checked (str
, &info
, parse_error
)) {
4807 mono_reflection_free_type_info (&info
);
4808 mono_error_cleanup (parse_error
);
4810 mono_error_set_argument (error
, "typeName", "failed to parse the type");
4813 /*g_print ("failed parse\n");*/
4814 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
4817 if (info
.assembly
.name
) {
4819 mono_reflection_free_type_info (&info
);
4821 /* 1.0 and 2.0 throw different exceptions */
4822 if (mono_defaults
.generic_ilist_class
)
4823 mono_error_set_argument (error
, NULL
, "Type names passed to Assembly.GetType() must not specify an assembly.");
4825 mono_error_set_type_load_name (error
, g_strdup (""), g_strdup (""), "Type names passed to Assembly.GetType() must not specify an assembly.");
4828 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
4833 if (!MONO_HANDLE_IS_NULL (module
)) {
4834 MonoImage
*image
= MONO_HANDLE_GETVAL (module
, image
);
4836 type
= mono_reflection_get_type_checked (image
, image
, &info
, ignoreCase
, &type_resolve
, error
);
4837 if (!is_ok (error
)) {
4839 mono_reflection_free_type_info (&info
);
4845 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
4846 if (assembly_is_dynamic (assembly
)) {
4847 /* Enumerate all modules */
4848 MonoReflectionAssemblyBuilderHandle abuilder
= MONO_HANDLE_NEW (MonoReflectionAssemblyBuilder
, NULL
);
4849 MONO_HANDLE_ASSIGN (abuilder
, assembly_h
);
4852 MonoArrayHandle modules
= MONO_HANDLE_NEW (MonoArray
, NULL
);
4853 MONO_HANDLE_GET (modules
, abuilder
, modules
);
4854 if (!MONO_HANDLE_IS_NULL (modules
)) {
4855 int n
= mono_array_handle_length (modules
);
4856 for (i
= 0; i
< n
; ++i
) {
4857 type
= get_type_from_module_builder_module (modules
, i
, &info
, ignoreCase
, &type_resolve
, error
);
4858 if (!is_ok (error
)) {
4860 mono_reflection_free_type_info (&info
);
4868 MonoArrayHandle loaded_modules
= MONO_HANDLE_NEW (MonoArray
, NULL
);
4869 MONO_HANDLE_GET (loaded_modules
, abuilder
, loaded_modules
);
4870 if (!type
&& !MONO_HANDLE_IS_NULL (loaded_modules
)) {
4871 int n
= mono_array_handle_length (loaded_modules
);
4872 for (i
= 0; i
< n
; ++i
) {
4873 type
= get_type_from_module_builder_loaded_modules (loaded_modules
, i
, &info
, ignoreCase
, &type_resolve
, error
);
4875 if (!is_ok (error
)) {
4877 mono_reflection_free_type_info (&info
);
4886 type
= mono_reflection_get_type_checked (assembly
->image
, assembly
->image
, &info
, ignoreCase
, &type_resolve
, error
);
4887 if (!is_ok (error
)) {
4889 mono_reflection_free_type_info (&info
);
4895 mono_reflection_free_type_info (&info
);
4899 ERROR_DECL (inner_error
);
4900 char *type_name
= mono_string_handle_to_utf8 (name
, inner_error
);
4901 mono_error_assert_ok (inner_error
);
4902 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
4903 char *assmname
= mono_stringify_assembly_name (&assembly
->aname
);
4904 mono_error_set_type_load_name (error
, type_name
, assmname
, "%s", "");
4908 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
4911 if (type
->type
== MONO_TYPE_CLASS
) {
4912 MonoClass
*klass
= mono_type_get_class (type
);
4914 /* need to report exceptions ? */
4915 if (throwOnError
&& mono_class_has_failure (klass
)) {
4916 /* report SecurityException (or others) that occured when loading the assembly */
4917 mono_error_set_for_class_failure (error
, klass
);
4922 /* g_print ("got it\n"); */
4923 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (assembly_h
), type
, error
);
4925 g_assert (!is_ok (error
));
4926 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
4930 replace_shadow_path (MonoDomain
*domain
, gchar
*dirname
, gchar
**filename
)
4933 gchar
*shadow_ini_file
;
4936 /* Check for shadow-copied assembly */
4937 if (mono_is_shadow_copy_enabled (domain
, dirname
)) {
4938 shadow_ini_file
= g_build_filename (dirname
, "__AssemblyInfo__.ini", NULL
);
4940 if (!g_file_get_contents (shadow_ini_file
, &content
, &len
, NULL
) ||
4941 !g_file_test (content
, G_FILE_TEST_IS_REGULAR
)) {
4945 g_free (shadow_ini_file
);
4946 if (content
!= NULL
) {
4948 *filename
= content
;
4956 ves_icall_System_Reflection_RuntimeAssembly_get_code_base (MonoReflectionAssemblyHandle assembly
, MonoBoolean escaped
, MonoError
*error
)
4959 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly
);
4960 MonoAssembly
*mass
= MONO_HANDLE_GETVAL (assembly
, assembly
);
4964 if (g_path_is_absolute (mass
->image
->name
)) {
4965 absolute
= g_strdup (mass
->image
->name
);
4966 dirname
= g_path_get_dirname (absolute
);
4968 absolute
= g_build_filename (mass
->basedir
, mass
->image
->name
, NULL
);
4969 dirname
= g_strdup (mass
->basedir
);
4972 replace_shadow_path (domain
, dirname
, &absolute
);
4975 mono_icall_make_platform_path (absolute
);
4979 uri
= g_filename_to_uri (absolute
, NULL
, NULL
);
4981 const gchar
*prepend
= mono_icall_get_file_path_prefix (absolute
);
4982 uri
= g_strconcat (prepend
, absolute
, NULL
);
4987 MonoStringHandle res
;
4989 res
= mono_string_new_handle (domain
, uri
, error
);
4992 res
= MONO_HANDLE_NEW (MonoString
, NULL
);
4998 ves_icall_System_Reflection_RuntimeAssembly_get_global_assembly_cache (MonoReflectionAssemblyHandle assembly
, MonoError
*error
)
5001 MonoAssembly
*mass
= MONO_HANDLE_GETVAL (assembly
,assembly
);
5003 return mass
->in_gac
;
5006 MonoReflectionAssemblyHandle
5007 ves_icall_System_Reflection_Assembly_load_with_partial_name (MonoStringHandle mname
, MonoObjectHandle evidence
, MonoError
*error
)
5010 MonoImageOpenStatus status
;
5011 MonoReflectionAssemblyHandle result
= MONO_HANDLE_CAST (MonoReflectionAssembly
, NULL_HANDLE
);
5013 name
= mono_string_handle_to_utf8 (mname
, error
);
5014 goto_if_nok (error
, leave
);
5016 res
= mono_assembly_load_with_partial_name_internal (name
, &status
);
5022 result
= mono_assembly_get_object_handle (mono_domain_get (), res
, error
);
5028 ves_icall_System_Reflection_RuntimeAssembly_get_location (MonoReflectionAssemblyHandle refassembly
, MonoError
*error
)
5030 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (refassembly
);
5031 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (refassembly
, assembly
);
5032 return mono_string_new_handle (domain
, mono_image_get_filename (assembly
->image
), error
);
5036 ves_icall_System_Reflection_RuntimeAssembly_get_ReflectionOnly (MonoReflectionAssemblyHandle assembly_h
, MonoError
*error
)
5039 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5040 return mono_asmctx_get_kind (&assembly
->context
) == MONO_ASMCTX_REFONLY
;
5044 ves_icall_System_Reflection_RuntimeAssembly_InternalImageRuntimeVersion (MonoReflectionAssemblyHandle refassembly
, MonoError
*error
)
5046 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (refassembly
);
5047 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (refassembly
, assembly
);
5049 return mono_string_new_handle (domain
, assembly
->image
->version
, error
);
5052 MonoReflectionMethodHandle
5053 ves_icall_System_Reflection_RuntimeAssembly_get_EntryPoint (MonoReflectionAssemblyHandle assembly_h
, MonoError
*error
)
5056 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly_h
);
5057 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5060 MonoReflectionMethodHandle res
= MONO_HANDLE_NEW (MonoReflectionMethod
, NULL
);
5061 guint32 token
= mono_image_get_entry_point (assembly
->image
);
5065 method
= mono_get_method_checked (assembly
->image
, token
, NULL
, NULL
, error
);
5066 goto_if_nok (error
, leave
);
5068 MONO_HANDLE_ASSIGN (res
, mono_method_get_object_handle (domain
, method
, NULL
, error
));
5073 MonoReflectionModuleHandle
5074 ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssemblyHandle assembly
, MonoError
*error
)
5077 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly
);
5078 MonoAssembly
*a
= MONO_HANDLE_GETVAL (assembly
, assembly
);
5079 return mono_module_get_object_handle (domain
, a
->image
, error
);
5083 add_manifest_resource_name_to_array (MonoDomain
*domain
, MonoImage
*image
, MonoTableInfo
*table
, int i
, MonoArrayHandle dest
, MonoError
*error
)
5085 HANDLE_FUNCTION_ENTER ();
5087 const char *val
= mono_metadata_string_heap (image
, mono_metadata_decode_row_col (table
, i
, MONO_MANIFEST_NAME
));
5088 MonoStringHandle str
= mono_string_new_handle (domain
, val
, error
);
5089 goto_if_nok (error
, leave
);
5090 MONO_HANDLE_ARRAY_SETREF (dest
, i
, str
);
5092 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
5096 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceNames (MonoReflectionAssemblyHandle assembly_h
, MonoError
*error
)
5099 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly_h
);
5100 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5101 MonoTableInfo
*table
= &assembly
->image
->tables
[MONO_TABLE_MANIFESTRESOURCE
];
5102 MonoArrayHandle result
= mono_array_new_handle (domain
, mono_defaults
.string_class
, table
->rows
, error
);
5103 goto_if_nok (error
, fail
);
5106 for (i
= 0; i
< table
->rows
; ++i
) {
5107 if (!add_manifest_resource_name_to_array (domain
, assembly
->image
, table
, i
, result
, error
))
5112 return NULL_HANDLE_ARRAY
;
5116 ves_icall_System_Reflection_RuntimeAssembly_GetAotIdInternal (MonoArrayHandle guid_h
, MonoError
*error
)
5118 g_assert (mono_array_handle_length (guid_h
) == 16);
5120 guint8
*aotid
= mono_runtime_get_aotid_arr ();
5124 MONO_ENTER_NO_SAFEPOINTS
;
5125 guint8
*data
= (guint8
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h
), 1, 0);
5126 memcpy (data
, aotid
, 16);
5127 MONO_EXIT_NO_SAFEPOINTS
;
5132 static MonoAssemblyName
*
5133 create_referenced_assembly_name (MonoDomain
*domain
, MonoImage
*image
, MonoTableInfo
*t
, int i
, MonoError
*error
)
5136 MonoAssemblyName
*aname
= g_new0 (MonoAssemblyName
, 1);
5138 mono_assembly_get_assemblyref_checked (image
, i
, aname
, error
);
5139 return_val_if_nok (error
, NULL
);
5140 aname
->hash_alg
= ASSEMBLY_HASH_SHA1
/* SHA1 (default) */;
5141 /* name and culture are pointers into the image tables, but we need
5142 * real malloc'd strings (so that we can g_free() them later from
5143 * Mono.RuntimeMarshal.FreeAssemblyName) */
5144 aname
->name
= g_strdup (aname
->name
);
5145 aname
->culture
= g_strdup (aname
->culture
);
5146 /* Don't need the hash value in managed */
5147 aname
->hash_value
= NULL
;
5148 aname
->hash_len
= 0;
5149 g_assert (aname
->public_key
== NULL
);
5151 /* note: this function doesn't return the codebase on purpose (i.e. it can
5152 be used under partial trust as path information isn't present). */
5157 ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflectionAssemblyHandle assembly
, MonoError
*error
)
5160 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly
);
5161 MonoAssembly
*ass
= MONO_HANDLE_GETVAL(assembly
, assembly
);
5162 MonoImage
*image
= ass
->image
;
5164 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLYREF
];
5165 int count
= t
->rows
;
5167 GPtrArray
*result
= g_ptr_array_sized_new (count
);
5169 for (int i
= 0; i
< count
; i
++) {
5170 MonoAssemblyName
*aname
= create_referenced_assembly_name (domain
, image
, t
, i
, error
);
5173 g_ptr_array_add (result
, aname
);
5178 /* move this in some file in mono/util/ */
5180 g_concat_dir_and_file (const char *dir
, const char *file
)
5182 g_return_val_if_fail (dir
!= NULL
, NULL
);
5183 g_return_val_if_fail (file
!= NULL
, NULL
);
5186 * If the directory name doesn't have a / on the end, we need
5187 * to add one so we get a proper path to the file
5189 if (dir
[strlen(dir
) - 1] != G_DIR_SEPARATOR
)
5190 return g_strconcat (dir
, G_DIR_SEPARATOR_S
, file
, NULL
);
5192 return g_strconcat (dir
, file
, NULL
);
5196 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInternal (MonoReflectionAssemblyHandle assembly_h
, MonoStringHandle name
, gint32
*size
, MonoReflectionModuleHandleOut ref_module
, MonoError
*error
)
5199 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly_h
);
5200 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5201 MonoTableInfo
*table
= &assembly
->image
->tables
[MONO_TABLE_MANIFESTRESOURCE
];
5203 guint32 cols
[MONO_MANIFEST_SIZE
];
5204 guint32 impl
, file_idx
;
5208 char *n
= mono_string_handle_to_utf8 (name
, error
);
5209 return_val_if_nok (error
, NULL
);
5211 for (i
= 0; i
< table
->rows
; ++i
) {
5212 mono_metadata_decode_row (table
, i
, cols
, MONO_MANIFEST_SIZE
);
5213 val
= mono_metadata_string_heap (assembly
->image
, cols
[MONO_MANIFEST_NAME
]);
5214 if (strcmp (val
, n
) == 0)
5218 if (i
== table
->rows
)
5221 impl
= cols
[MONO_MANIFEST_IMPLEMENTATION
];
5224 * this code should only be called after obtaining the
5225 * ResourceInfo and handling the other cases.
5227 g_assert ((impl
& MONO_IMPLEMENTATION_MASK
) == MONO_IMPLEMENTATION_FILE
);
5228 file_idx
= impl
>> MONO_IMPLEMENTATION_BITS
;
5230 module
= mono_image_load_file_for_image_checked (assembly
->image
, file_idx
, error
);
5231 if (!is_ok (error
) || !module
)
5235 module
= assembly
->image
;
5238 MonoReflectionModuleHandle rm
= mono_module_get_object_handle (domain
, module
, error
);
5239 return_val_if_nok (error
, NULL
);
5240 MONO_HANDLE_ASSIGN (ref_module
, rm
);
5242 return (void*)mono_image_get_resource (module
, cols
[MONO_MANIFEST_OFFSET
], (guint32
*)size
);
5246 get_manifest_resource_info_internal (MonoReflectionAssemblyHandle assembly_h
, MonoStringHandle name
, MonoManifestResourceInfoHandle info
, MonoError
*error
)
5248 HANDLE_FUNCTION_ENTER ();
5249 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly_h
);
5250 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5251 MonoTableInfo
*table
= &assembly
->image
->tables
[MONO_TABLE_MANIFESTRESOURCE
];
5253 guint32 cols
[MONO_MANIFEST_SIZE
];
5254 guint32 file_cols
[MONO_FILE_SIZE
];
5258 gboolean result
= FALSE
;
5260 n
= mono_string_handle_to_utf8 (name
, error
);
5261 goto_if_nok (error
, leave
);
5263 for (i
= 0; i
< table
->rows
; ++i
) {
5264 mono_metadata_decode_row (table
, i
, cols
, MONO_MANIFEST_SIZE
);
5265 val
= mono_metadata_string_heap (assembly
->image
, cols
[MONO_MANIFEST_NAME
]);
5266 if (strcmp (val
, n
) == 0)
5270 if (i
== table
->rows
)
5273 if (!cols
[MONO_MANIFEST_IMPLEMENTATION
]) {
5274 MONO_HANDLE_SETVAL (info
, location
, guint32
, RESOURCE_LOCATION_EMBEDDED
| RESOURCE_LOCATION_IN_MANIFEST
);
5277 switch (cols
[MONO_MANIFEST_IMPLEMENTATION
] & MONO_IMPLEMENTATION_MASK
) {
5278 case MONO_IMPLEMENTATION_FILE
:
5279 i
= cols
[MONO_MANIFEST_IMPLEMENTATION
] >> MONO_IMPLEMENTATION_BITS
;
5280 table
= &assembly
->image
->tables
[MONO_TABLE_FILE
];
5281 mono_metadata_decode_row (table
, i
- 1, file_cols
, MONO_FILE_SIZE
);
5282 val
= mono_metadata_string_heap (assembly
->image
, file_cols
[MONO_FILE_NAME
]);
5283 MONO_HANDLE_SET (info
, filename
, mono_string_new_handle (domain
, val
, error
));
5284 if (file_cols
[MONO_FILE_FLAGS
] & FILE_CONTAINS_NO_METADATA
)
5285 MONO_HANDLE_SETVAL (info
, location
, guint32
, 0);
5287 MONO_HANDLE_SETVAL (info
, location
, guint32
, RESOURCE_LOCATION_EMBEDDED
);
5290 case MONO_IMPLEMENTATION_ASSEMBLYREF
:
5291 i
= cols
[MONO_MANIFEST_IMPLEMENTATION
] >> MONO_IMPLEMENTATION_BITS
;
5292 mono_assembly_load_reference (assembly
->image
, i
- 1);
5293 if (assembly
->image
->references
[i
- 1] == REFERENCE_MISSING
) {
5294 mono_error_set_file_not_found (error
, NULL
, "Assembly %d referenced from assembly %s not found ", i
- 1, assembly
->image
->name
);
5297 MonoReflectionAssemblyHandle assm_obj
;
5298 assm_obj
= mono_assembly_get_object_handle (mono_domain_get (), assembly
->image
->references
[i
- 1], error
);
5299 goto_if_nok (error
, leave
);
5300 MONO_HANDLE_SET (info
, assembly
, assm_obj
);
5302 /* Obtain info recursively */
5303 get_manifest_resource_info_internal (assm_obj
, name
, info
, error
);
5304 goto_if_nok (error
, leave
);
5306 location
= MONO_HANDLE_GETVAL (info
, location
);
5307 location
|= RESOURCE_LOCATION_ANOTHER_ASSEMBLY
;
5308 MONO_HANDLE_SETVAL (info
, location
, guint32
, location
);
5311 case MONO_IMPLEMENTATION_EXP_TYPE
:
5312 g_assert_not_reached ();
5319 HANDLE_FUNCTION_RETURN_VAL (result
);
5323 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInfoInternal (MonoReflectionAssemblyHandle assembly_h
, MonoStringHandle name
, MonoManifestResourceInfoHandle info_h
, MonoError
*error
)
5326 return get_manifest_resource_info_internal (assembly_h
, name
, info_h
, error
);
5330 add_filename_to_files_array (MonoDomain
*domain
, MonoAssembly
* assembly
, MonoTableInfo
*table
, int i
, MonoArrayHandle dest
, int dest_idx
, MonoError
*error
)
5332 HANDLE_FUNCTION_ENTER();
5334 const char *val
= mono_metadata_string_heap (assembly
->image
, mono_metadata_decode_row_col (table
, i
, MONO_FILE_NAME
));
5335 char *n
= g_concat_dir_and_file (assembly
->basedir
, val
);
5336 MonoStringHandle str
= mono_string_new_handle (domain
, n
, error
);
5338 goto_if_nok (error
, leave
);
5339 MONO_HANDLE_ARRAY_SETREF (dest
, dest_idx
, str
);
5341 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
5345 ves_icall_System_Reflection_RuntimeAssembly_GetFilesInternal (MonoReflectionAssemblyHandle assembly_h
, MonoStringHandle name
, MonoBoolean resource_modules
, MonoError
*error
)
5348 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly_h
);
5349 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5350 MonoTableInfo
*table
= &assembly
->image
->tables
[MONO_TABLE_FILE
];
5353 /* check hash if needed */
5354 if (!MONO_HANDLE_IS_NULL(name
)) {
5355 char *n
= mono_string_handle_to_utf8 (name
, error
);
5356 goto_if_nok (error
, fail
);
5358 for (i
= 0; i
< table
->rows
; ++i
) {
5359 const char *val
= mono_metadata_string_heap (assembly
->image
, mono_metadata_decode_row_col (table
, i
, MONO_FILE_NAME
));
5360 if (strcmp (val
, n
) == 0) {
5362 n
= g_concat_dir_and_file (assembly
->basedir
, val
);
5363 MonoStringHandle fn
= mono_string_new_handle (domain
, n
, error
);
5365 goto_if_nok (error
, fail
);
5366 return MONO_HANDLE_CAST (MonoObject
, fn
);
5374 for (i
= 0; i
< table
->rows
; ++i
) {
5375 if (resource_modules
|| !(mono_metadata_decode_row_col (table
, i
, MONO_FILE_FLAGS
) & FILE_CONTAINS_NO_METADATA
))
5379 MonoArrayHandle result
;
5380 result
= mono_array_new_handle (domain
, mono_defaults
.string_class
, count
, error
);
5381 goto_if_nok (error
, fail
);
5384 for (i
= 0; i
< table
->rows
; ++i
) {
5385 if (resource_modules
|| !(mono_metadata_decode_row_col (table
, i
, MONO_FILE_FLAGS
) & FILE_CONTAINS_NO_METADATA
)) {
5386 if (!add_filename_to_files_array (domain
, assembly
, table
, i
, result
, count
, error
))
5391 return MONO_HANDLE_CAST (MonoObject
, result
);
5397 add_module_to_modules_array (MonoDomain
*domain
, MonoArrayHandle dest
, int *dest_idx
, MonoImage
* module
, MonoError
*error
)
5399 HANDLE_FUNCTION_ENTER ();
5402 MonoReflectionModuleHandle rm
= mono_module_get_object_handle (domain
, module
, error
);
5403 goto_if_nok (error
, leave
);
5405 MONO_HANDLE_ARRAY_SETREF (dest
, *dest_idx
, rm
);
5410 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
5414 add_file_to_modules_array (MonoDomain
*domain
, MonoArrayHandle dest
, int dest_idx
, MonoImage
*image
, MonoTableInfo
*table
, int table_idx
, MonoError
*error
)
5416 HANDLE_FUNCTION_ENTER ();
5419 guint32 cols
[MONO_FILE_SIZE
];
5420 mono_metadata_decode_row (table
, table_idx
, cols
, MONO_FILE_SIZE
);
5421 if (cols
[MONO_FILE_FLAGS
] & FILE_CONTAINS_NO_METADATA
) {
5422 MonoReflectionModuleHandle rm
= mono_module_file_get_object_handle (domain
, image
, table_idx
, error
);
5423 goto_if_nok (error
, leave
);
5424 MONO_HANDLE_ARRAY_SETREF (dest
, dest_idx
, rm
);
5426 MonoImage
*m
= mono_image_load_file_for_image_checked (image
, table_idx
+ 1, error
);
5427 goto_if_nok (error
, leave
);
5429 const char *filename
= mono_metadata_string_heap (image
, cols
[MONO_FILE_NAME
]);
5430 mono_error_set_file_not_found (error
, filename
, "%s", "");
5433 MonoReflectionModuleHandle rm
= mono_module_get_object_handle (domain
, m
, error
);
5434 goto_if_nok (error
, leave
);
5435 MONO_HANDLE_ARRAY_SETREF (dest
, dest_idx
, rm
);
5439 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
5443 ves_icall_System_Reflection_RuntimeAssembly_GetModulesInternal (MonoReflectionAssemblyHandle assembly_h
, MonoError
*error
)
5446 MonoDomain
*domain
= mono_domain_get();
5447 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5449 int i
, j
, file_count
= 0;
5450 MonoImage
**modules
;
5451 guint32 module_count
, real_module_count
;
5452 MonoTableInfo
*table
;
5453 MonoImage
*image
= assembly
->image
;
5455 g_assert (image
!= NULL
);
5456 g_assert (!assembly_is_dynamic (assembly
));
5458 table
= &image
->tables
[MONO_TABLE_FILE
];
5459 file_count
= table
->rows
;
5461 modules
= image
->modules
;
5462 module_count
= image
->module_count
;
5464 real_module_count
= 0;
5465 for (i
= 0; i
< module_count
; ++i
)
5467 real_module_count
++;
5469 klass
= mono_class_get_module_class ();
5470 MonoArrayHandle res
= mono_array_new_handle (domain
, klass
, 1 + real_module_count
+ file_count
, error
);
5471 goto_if_nok (error
, fail
);
5473 MonoReflectionModuleHandle image_obj
;
5474 image_obj
= mono_module_get_object_handle (domain
, image
, error
);
5475 goto_if_nok (error
, fail
);
5477 MONO_HANDLE_ARRAY_SETREF (res
, 0, image_obj
);
5480 for (i
= 0; i
< module_count
; ++i
)
5481 if (!add_module_to_modules_array (domain
, res
, &j
, modules
[i
], error
))
5484 for (i
= 0; i
< file_count
; ++i
, ++j
) {
5485 if (!add_file_to_modules_array (domain
, res
, j
, image
, table
, i
, error
))
5491 return NULL_HANDLE_ARRAY
;
5494 MonoReflectionMethodHandle
5495 ves_icall_GetCurrentMethod (MonoError
*error
)
5499 MonoMethod
*m
= mono_method_get_last_managed ();
5502 mono_error_set_not_supported (error
, "Stack walks are not supported on this platform.");
5503 return MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
5506 while (m
->is_inflated
)
5507 m
= ((MonoMethodInflated
*)m
)->declaring
;
5509 return mono_method_get_object_handle (mono_domain_get (), m
, NULL
, error
);
5513 mono_method_get_equivalent_method (MonoMethod
*method
, MonoClass
*klass
)
5516 if (method
->is_inflated
&& ((MonoMethodInflated
*)method
)->context
.method_inst
) {
5519 MonoMethodInflated
*inflated
= (MonoMethodInflated
*)method
;
5520 //method is inflated, we should inflate it on the other class
5521 MonoGenericContext ctx
;
5522 ctx
.method_inst
= inflated
->context
.method_inst
;
5523 ctx
.class_inst
= inflated
->context
.class_inst
;
5524 if (mono_class_is_ginst (klass
))
5525 ctx
.class_inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
5526 else if (mono_class_is_gtd (klass
))
5527 ctx
.class_inst
= mono_class_get_generic_container (klass
)->context
.class_inst
;
5528 result
= mono_class_inflate_generic_method_full_checked (inflated
->declaring
, klass
, &ctx
, error
);
5529 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
5533 mono_class_setup_methods (method
->klass
);
5534 if (mono_class_has_failure (method
->klass
))
5536 int mcount
= mono_class_get_method_count (method
->klass
);
5537 MonoMethod
**method_klass_methods
= m_class_get_methods (method
->klass
);
5538 for (i
= 0; i
< mcount
; ++i
) {
5539 if (method_klass_methods
[i
] == method
) {
5544 mono_class_setup_methods (klass
);
5545 if (mono_class_has_failure (klass
))
5547 g_assert (offset
>= 0 && offset
< mono_class_get_method_count (klass
));
5548 return m_class_get_methods (klass
) [offset
];
5551 MonoReflectionMethodHandle
5552 ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodFromHandleInternalType_native (MonoMethod
*method
, MonoType
*type
, MonoBoolean generic_check
, MonoError
*error
)
5556 if (type
&& generic_check
) {
5557 klass
= mono_class_from_mono_type_internal (type
);
5558 if (mono_class_get_generic_type_definition (method
->klass
) != mono_class_get_generic_type_definition (klass
))
5559 return MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
5561 if (method
->klass
!= klass
) {
5562 method
= mono_method_get_equivalent_method (method
, klass
);
5564 return MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
5567 klass
= mono_class_from_mono_type_internal (type
);
5569 klass
= method
->klass
;
5570 return mono_method_get_object_handle (mono_domain_get (), method
, klass
, error
);
5573 MonoReflectionMethodBodyHandle
5574 ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodBodyInternal (MonoMethod
*method
, MonoError
*error
)
5577 return mono_method_body_get_object_handle (mono_domain_get (), method
, error
);
5581 MonoReflectionAssemblyHandle
5582 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoStackCrawlMark
*stack_mark
, MonoError
*error
)
5584 MonoAssembly
*assembly
;
5585 assembly
= mono_runtime_get_caller_from_stack_mark (stack_mark
);
5586 g_assert (assembly
);
5587 return mono_assembly_get_object_handle (mono_domain_get (), assembly
, error
);
5590 MonoReflectionAssemblyHandle
5591 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError
*error
)
5595 MonoMethod
*dest
= NULL
;
5596 mono_stack_walk_no_il (get_executing
, &dest
);
5598 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest
->klass
)->assembly
, error
);
5602 MonoReflectionAssemblyHandle
5603 ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError
*error
)
5607 MonoDomain
* domain
= mono_domain_get ();
5609 if (!domain
->entry_assembly
)
5610 return MONO_HANDLE_CAST (MonoReflectionAssembly
, NULL_HANDLE
);
5612 return mono_assembly_get_object_handle (domain
, domain
->entry_assembly
, error
);
5615 MonoReflectionAssemblyHandle
5616 ves_icall_System_Reflection_Assembly_GetCallingAssembly (MonoError
*error
)
5623 mono_stack_walk_no_il (get_executing
, &dest
);
5625 mono_stack_walk_no_il (get_caller_no_reflection
, &dest
);
5629 mono_error_set_not_supported (error
, "Stack walks are not supported on this platform.");
5630 return MONO_HANDLE_CAST (MonoReflectionAssembly
, NULL_HANDLE
);
5632 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest
->klass
)->assembly
, error
);
5636 ves_icall_System_RuntimeType_getFullName (MonoReflectionTypeHandle object
, MonoBoolean full_name
,
5637 MonoBoolean assembly_qualified
, MonoError
*error
)
5639 MonoDomain
*domain
= mono_object_domain (MONO_HANDLE_RAW (object
));
5640 MonoType
*type
= MONO_HANDLE_RAW (object
)->type
;
5641 MonoTypeNameFormat format
;
5642 MonoStringHandle res
;
5646 format
= assembly_qualified
?
5647 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
:
5648 MONO_TYPE_NAME_FORMAT_FULL_NAME
;
5650 format
= MONO_TYPE_NAME_FORMAT_REFLECTION
;
5652 name
= mono_type_get_name_full (type
, format
);
5654 return NULL_HANDLE_STRING
;
5656 if (full_name
&& (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
)) {
5658 return NULL_HANDLE_STRING
;
5661 res
= mono_string_new_handle (domain
, name
, error
);
5668 ves_icall_RuntimeType_get_core_clr_security_level (MonoReflectionTypeHandle rfield
, MonoError
*error
)
5671 MonoType
*type
= MONO_HANDLE_GETVAL (rfield
, type
);
5672 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
5674 mono_class_init_checked (klass
, error
);
5675 return_val_if_nok (error
, -1);
5676 return mono_security_core_clr_class_level (klass
);
5680 ves_icall_RuntimeFieldInfo_get_core_clr_security_level (MonoReflectionFieldHandle rfield
, MonoError
*error
)
5682 MonoClassField
*field
= MONO_HANDLE_GETVAL (rfield
, field
);
5683 return mono_security_core_clr_field_level (field
, TRUE
);
5687 ves_icall_RuntimeMethodInfo_get_core_clr_security_level (MonoReflectionMethodHandle rfield
, MonoError
*error
)
5689 MonoMethod
*method
= MONO_HANDLE_GETVAL (rfield
, method
);
5690 return mono_security_core_clr_method_level (method
, TRUE
);
5694 ves_icall_System_Reflection_RuntimeAssembly_get_fullname (MonoReflectionAssemblyHandle assembly
, MonoError
*error
)
5697 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly
);
5698 MonoAssembly
*mass
= MONO_HANDLE_GETVAL (assembly
, assembly
);
5701 name
= mono_stringify_assembly_name (&mass
->aname
);
5702 MonoStringHandle res
= mono_string_new_handle (domain
, name
, error
);
5708 ves_icall_System_Reflection_AssemblyName_GetNativeName (MonoAssembly
*mass
)
5710 return &mass
->aname
;
5714 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoStringHandle fname
, MonoAssemblyName
*name
, MonoStringHandleOut normalized_codebase
, MonoError
*error
)
5717 MonoImageOpenStatus status
= MONO_IMAGE_OK
;
5718 char *codebase
= NULL
;
5725 filename
= mono_string_handle_to_utf8 (fname
, error
);
5726 return_if_nok (error
);
5728 dirname
= g_path_get_dirname (filename
);
5729 replace_shadow_path (mono_domain_get (), dirname
, &filename
);
5732 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_ASSEMBLY
, "InternalGetAssemblyName (\"%s\")", filename
);
5734 image
= mono_image_open_full (filename
, &status
, TRUE
);
5737 if (status
== MONO_IMAGE_IMAGE_INVALID
)
5738 mono_error_set_bad_image_by_name (error
, filename
, "Invalid Image");
5740 mono_error_set_file_not_found (error
, filename
, "%s", "");
5745 res
= mono_assembly_fill_assembly_name_full (image
, name
, TRUE
);
5747 mono_image_close (image
);
5749 mono_error_set_argument (error
, "assemblyFile", "The file does not contain a manifest");
5753 if (filename
!= NULL
&& *filename
!= '\0') {
5756 codebase
= g_strdup (filename
);
5758 mono_icall_make_platform_path (codebase
);
5760 const gchar
*prepend
= mono_icall_get_file_path_prefix (codebase
);
5762 result
= g_strconcat (prepend
, codebase
, NULL
);
5766 MONO_HANDLE_ASSIGN (normalized_codebase
, mono_string_new_handle (mono_domain_get (), codebase
, error
));
5769 mono_image_close (image
);
5774 ves_icall_System_Reflection_RuntimeAssembly_LoadPermissions (MonoReflectionAssemblyHandle assembly_h
,
5775 char **minimum
, guint32
*minLength
, char **optional
, guint32
*optLength
, char **refused
, guint32
*refLength
, MonoError
*error
)
5778 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
5779 MonoBoolean result
= FALSE
;
5780 MonoDeclSecurityEntry entry
;
5782 /* SecurityAction.RequestMinimum */
5783 if (mono_declsec_get_assembly_action (assembly
, SECURITY_ACTION_REQMIN
, &entry
)) {
5784 *minimum
= entry
.blob
;
5785 *minLength
= entry
.size
;
5788 /* SecurityAction.RequestOptional */
5789 if (mono_declsec_get_assembly_action (assembly
, SECURITY_ACTION_REQOPT
, &entry
)) {
5790 *optional
= entry
.blob
;
5791 *optLength
= entry
.size
;
5794 /* SecurityAction.RequestRefuse */
5795 if (mono_declsec_get_assembly_action (assembly
, SECURITY_ACTION_REQREFUSE
, &entry
)) {
5796 *refused
= entry
.blob
;
5797 *refLength
= entry
.size
;
5805 mono_module_type_is_visible (MonoTableInfo
*tdef
, MonoImage
*image
, int type
)
5807 guint32 attrs
, visibility
;
5809 attrs
= mono_metadata_decode_row_col (tdef
, type
- 1, MONO_TYPEDEF_FLAGS
);
5810 visibility
= attrs
& TYPE_ATTRIBUTE_VISIBILITY_MASK
;
5811 if (visibility
!= TYPE_ATTRIBUTE_PUBLIC
&& visibility
!= TYPE_ATTRIBUTE_NESTED_PUBLIC
)
5814 } while ((type
= mono_metadata_token_index (mono_metadata_nested_in_typedef (image
, type
))));
5820 image_get_type (MonoDomain
*domain
, MonoImage
*image
, MonoTableInfo
*tdef
, int table_idx
, int count
, MonoArrayHandle res
, MonoArrayHandle exceptions
, MonoBoolean exportedOnly
, MonoError
*error
)
5823 HANDLE_FUNCTION_ENTER ();
5824 ERROR_DECL (klass_error
);
5825 MonoClass
*klass
= mono_class_get_checked (image
, table_idx
| MONO_TOKEN_TYPE_DEF
, klass_error
);
5828 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
5829 return_if_nok (error
);
5831 MONO_HANDLE_ARRAY_SETREF (res
, count
, rt
);
5833 MonoExceptionHandle ex
= mono_error_convert_to_exception_handle (klass_error
);
5834 MONO_HANDLE_ARRAY_SETREF (exceptions
, count
, ex
);
5836 HANDLE_FUNCTION_RETURN ();
5839 static MonoArrayHandle
5840 mono_module_get_types (MonoDomain
*domain
, MonoImage
*image
, MonoArrayHandleOut exceptions
, MonoBoolean exportedOnly
, MonoError
*error
)
5842 MonoTableInfo
*tdef
= &image
->tables
[MONO_TABLE_TYPEDEF
];
5847 /* we start the count from 1 because we skip the special type <Module> */
5850 for (i
= 1; i
< tdef
->rows
; ++i
) {
5851 if (mono_module_type_is_visible (tdef
, image
, i
+ 1))
5855 count
= tdef
->rows
- 1;
5857 MonoArrayHandle res
= mono_array_new_handle (domain
, mono_defaults
.runtimetype_class
, count
, error
);
5858 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
5859 MONO_HANDLE_ASSIGN (exceptions
, mono_array_new_handle (domain
, mono_defaults
.exception_class
, count
, error
));
5860 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
5862 for (i
= 1; i
< tdef
->rows
; ++i
) {
5863 if (!exportedOnly
|| mono_module_type_is_visible (tdef
, image
, i
+1)) {
5864 image_get_type (domain
, image
, tdef
, i
+ 1, count
, res
, exceptions
, exportedOnly
, error
);
5865 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
5874 append_module_types (MonoDomain
*domain
, MonoArrayHandleOut res
, MonoArrayHandleOut exceptions
, MonoImage
*image
, MonoBoolean exportedOnly
, MonoError
*error
)
5876 HANDLE_FUNCTION_ENTER ();
5878 MonoArrayHandle ex2
= MONO_HANDLE_NEW (MonoArray
, NULL
);
5879 MonoArrayHandle res2
= mono_module_get_types (domain
, image
, ex2
, exportedOnly
, error
);
5880 goto_if_nok (error
, leave
);
5882 /* Append the new types to the end of the array */
5883 if (mono_array_handle_length (res2
) > 0) {
5886 len1
= mono_array_handle_length (res
);
5887 len2
= mono_array_handle_length (res2
);
5889 MonoArrayHandle res3
= mono_array_new_handle (domain
, mono_defaults
.runtimetype_class
, len1
+ len2
, error
);
5890 goto_if_nok (error
, leave
);
5892 mono_array_handle_memcpy_refs (res3
, 0, res
, 0, len1
);
5893 mono_array_handle_memcpy_refs (res3
, len1
, res2
, 0, len2
);
5894 MONO_HANDLE_ASSIGN (res
, res3
);
5896 MonoArrayHandle ex3
= mono_array_new_handle (domain
, mono_defaults
.runtimetype_class
, len1
+ len2
, error
);
5897 goto_if_nok (error
, leave
);
5899 mono_array_handle_memcpy_refs (ex3
, 0, exceptions
, 0, len1
);
5900 mono_array_handle_memcpy_refs (ex3
, len1
, ex2
, 0, len2
);
5901 MONO_HANDLE_ASSIGN (exceptions
, ex3
);
5904 HANDLE_FUNCTION_RETURN ();
5908 set_class_failure_in_array (MonoArrayHandle exl
, int i
, MonoClass
*klass
)
5910 HANDLE_FUNCTION_ENTER ();
5911 ERROR_DECL (unboxed_error
);
5912 mono_error_set_for_class_failure (unboxed_error
, klass
);
5914 MonoExceptionHandle exc
= MONO_HANDLE_NEW (MonoException
, mono_error_convert_to_exception (unboxed_error
));
5915 MONO_HANDLE_ARRAY_SETREF (exl
, i
, exc
);
5916 HANDLE_FUNCTION_RETURN ();
5920 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssemblyHandle assembly_handle
, MonoBoolean exportedOnly
, MonoError
*error
)
5922 MonoArrayHandle exceptions
= MONO_HANDLE_NEW(MonoArray
, NULL
);
5925 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (assembly_handle
);
5926 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_handle
, assembly
);
5928 g_assert (!assembly_is_dynamic (assembly
));
5929 MonoImage
*image
= assembly
->image
;
5930 MonoTableInfo
*table
= &image
->tables
[MONO_TABLE_FILE
];
5931 MonoArrayHandle res
= mono_module_get_types (domain
, image
, exceptions
, exportedOnly
, error
);
5932 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
5934 /* Append data from all modules in the assembly */
5935 for (i
= 0; i
< table
->rows
; ++i
) {
5936 if (!(mono_metadata_decode_row_col (table
, i
, MONO_FILE_FLAGS
) & FILE_CONTAINS_NO_METADATA
)) {
5937 MonoImage
*loaded_image
= mono_assembly_load_module_checked (image
->assembly
, i
+ 1, error
);
5938 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
5941 append_module_types (domain
, res
, exceptions
, loaded_image
, exportedOnly
, error
);
5942 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
5947 /* the ReflectionTypeLoadException must have all the types (Types property),
5948 * NULL replacing types which throws an exception. The LoaderException must
5949 * contain all exceptions for NULL items.
5952 int len
= mono_array_handle_length (res
);
5956 MonoReflectionTypeHandle t
= MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
5957 for (i
= 0; i
< len
; i
++) {
5958 MONO_HANDLE_ARRAY_GETREF (t
, res
, i
);
5960 if (!MONO_HANDLE_IS_NULL (t
)) {
5961 MonoClass
*klass
= mono_type_get_class (MONO_HANDLE_GETVAL (t
, type
));
5962 if ((klass
!= NULL
) && mono_class_has_failure (klass
)) {
5963 /* keep the class in the list */
5964 list
= g_list_append (list
, klass
);
5965 /* and replace Type with NULL */
5966 MONO_HANDLE_ARRAY_SETREF (res
, i
, NULL_HANDLE
);
5973 if (list
|| ex_count
) {
5975 int j
, length
= g_list_length (list
) + ex_count
;
5977 MonoArrayHandle exl
= mono_array_new_handle (domain
, mono_defaults
.exception_class
, length
, error
);
5978 if (!is_ok (error
)) {
5980 return NULL_HANDLE_ARRAY
;
5982 /* Types for which mono_class_get_checked () succeeded */
5983 MonoExceptionHandle exc
= MONO_HANDLE_NEW (MonoException
, NULL
);
5984 for (i
= 0, tmp
= list
; tmp
; i
++, tmp
= tmp
->next
) {
5985 set_class_failure_in_array (exl
, i
, (MonoClass
*)tmp
->data
);
5987 /* Types for which it don't */
5988 for (j
= 0; j
< mono_array_handle_length (exceptions
); ++j
) {
5989 MONO_HANDLE_ARRAY_GETREF (exc
, exceptions
, j
);
5990 if (!MONO_HANDLE_IS_NULL (exc
)) {
5991 g_assert (i
< length
);
5992 MONO_HANDLE_ARRAY_SETREF (exl
, i
, exc
);
5999 MONO_HANDLE_ASSIGN (exc
, mono_get_exception_reflection_type_load_checked (res
, exl
, error
));
6000 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
6001 mono_error_set_exception_handle (error
, exc
);
6002 return NULL_HANDLE_ARRAY
;
6010 ves_icall_System_Reflection_RuntimeAssembly_GetExportedTypes (MonoReflectionAssemblyHandle assembly_handle
, MonoError
*error
)
6012 return ves_icall_System_Reflection_Assembly_GetTypes (assembly_handle
, TRUE
, error
);
6016 ves_icall_System_Reflection_RuntimeAssembly_GetForwardedTypes (MonoReflectionAssemblyHandle assembly_h
, MonoError
*error
)
6018 MonoAssembly
*assembly
= MONO_HANDLE_GETVAL (assembly_h
, assembly
);
6019 MonoImage
*image
= assembly
->image
;
6020 guint32 cols
[MONO_EXP_TYPE_SIZE
];
6023 guint32 impl
, assembly_idx
;
6026 g_assert (!assembly_is_dynamic (assembly
));
6027 MonoTableInfo
*table
= &image
->tables
[MONO_TABLE_EXPORTEDTYPE
];
6028 for (int i
= 0; i
< table
->rows
; ++i
) {
6029 if (mono_metadata_decode_row_col (table
, i
, MONO_EXP_TYPE_FLAGS
) & TYPE_ATTRIBUTE_FORWARDER
)
6032 MonoArrayHandle res
= mono_array_new_handle (mono_domain_get (), mono_defaults
.runtimetype_class
, count
, error
);
6033 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
6035 for (int i
= 0; i
< table
->rows
; ++i
) {
6036 mono_metadata_decode_row (table
, i
, cols
, MONO_EXP_TYPE_SIZE
);
6037 if (!(cols
[MONO_EXP_TYPE_FLAGS
] & TYPE_ATTRIBUTE_FORWARDER
))
6039 impl
= cols
[MONO_EXP_TYPE_IMPLEMENTATION
];
6040 name
= mono_metadata_string_heap (image
, cols
[MONO_EXP_TYPE_NAME
]);
6041 nspace
= mono_metadata_string_heap (image
, cols
[MONO_EXP_TYPE_NAMESPACE
]);
6043 g_assert ((impl
& MONO_IMPLEMENTATION_MASK
) == MONO_IMPLEMENTATION_ASSEMBLYREF
);
6045 assembly_idx
= impl
>> MONO_IMPLEMENTATION_BITS
;
6047 mono_assembly_load_reference (image
, assembly_idx
- 1);
6048 g_assert (image
->references
[assembly_idx
- 1]);
6049 if (image
->references
[assembly_idx
- 1] == (gpointer
)-1)
6051 MonoClass
*klass
= mono_class_from_name_checked (image
->references
[assembly_idx
- 1]->image
, nspace
, name
, error
);
6054 MonoReflectionTypeHandle rt
= mono_type_get_object_handle (mono_domain_get (), m_class_get_byval_arg (klass
), error
);
6057 MONO_HANDLE_ARRAY_SETREF (res
, aindex
, rt
);
6060 if (aindex
< count
) {
6062 mono_error_set_type_load_name (error
, g_strdup (""), g_strdup (""), "");
6063 return MONO_HANDLE_NEW (MonoArray
, NULL
);
6070 ves_icall_Mono_RuntimeMarshal_FreeAssemblyName (MonoAssemblyName
*aname
, MonoBoolean free_struct
, MonoError
*error
)
6072 mono_assembly_name_free (aname
);
6078 ves_icall_Mono_Runtime_DisableMicrosoftTelemetry (MonoError
*error
)
6080 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6081 mono_merp_disable ();
6083 // Icall has platform check in managed too.
6084 g_assert_not_reached ();
6089 ves_icall_Mono_Runtime_AnnotateMicrosoftTelemetry (const char *key
, const char *value
, MonoError
*error
)
6091 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6092 if (!mono_merp_enabled ())
6093 g_error ("Cannot add attributes to telemetry without enabling subsystem");
6094 mono_merp_add_annotation (key
, value
);
6096 // Icall has platform check in managed too.
6097 g_assert_not_reached ();
6102 ves_icall_Mono_Runtime_EnableMicrosoftTelemetry (const char *appBundleID
, const char *appSignature
, const char *appVersion
, const char *merpGUIPath
, const char *eventType
, const char *appPath
, const char *configDir
, MonoError
*error
)
6104 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6105 mono_merp_enable (appBundleID
, appSignature
, appVersion
, merpGUIPath
, eventType
, appPath
, configDir
);
6107 mono_get_runtime_callbacks ()->install_state_summarizer ();
6109 // Icall has platform check in managed too.
6110 g_assert_not_reached ();
6114 // Number derived from trials on relevant hardware.
6115 // If it seems large, please confirm it's safe to shrink
6117 #define MONO_MAX_SUMMARY_LEN_ICALL 500000
6120 ves_icall_Mono_Runtime_ExceptionToState (MonoExceptionHandle exc_handle
, guint64
*portable_hash_out
, guint64
*unportable_hash_out
, MonoError
*error
)
6122 MonoStringHandle result
;
6124 #ifndef DISABLE_CRASH_REPORTING
6125 if (mono_get_eh_callbacks ()->mono_summarize_exception
) {
6126 // FIXME: Push handles down into mini/mini-exceptions.c
6127 MonoException
*exc
= MONO_HANDLE_RAW (exc_handle
);
6128 MonoThreadSummary out
;
6129 mono_get_eh_callbacks ()->mono_summarize_exception (exc
, &out
);
6131 *portable_hash_out
= (guint64
) out
.hashes
.offset_free_hash
;
6132 *unportable_hash_out
= (guint64
) out
.hashes
.offset_rich_hash
;
6134 MonoStateWriter writer
;
6135 char *scratch
= g_new0 (gchar
, MONO_MAX_SUMMARY_LEN_ICALL
);
6136 mono_state_writer_init (&writer
, scratch
, MONO_MAX_SUMMARY_LEN_ICALL
);
6137 mono_native_state_init (&writer
);
6138 gboolean first_thread_added
= TRUE
;
6139 mono_native_state_add_thread (&writer
, &out
, NULL
, first_thread_added
, TRUE
);
6140 char *output
= mono_native_state_free (&writer
, FALSE
);
6141 result
= mono_string_new_handle (mono_domain_get (), output
, error
);
6148 *portable_hash_out
= 0;
6149 *unportable_hash_out
= 0;
6150 result
= mono_string_new_handle (mono_domain_get (), "", error
);
6155 ves_icall_Mono_Runtime_SendMicrosoftTelemetry (const char *payload
, guint64 portable_hash
, guint64 unportable_hash
, MonoError
*error
)
6157 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6158 if (!mono_merp_enabled ())
6159 g_error ("Cannot send telemetry without registering parameters first");
6161 pid_t crashed_pid
= getpid ();
6163 MonoStackHash hashes
;
6164 memset (&hashes
, 0, sizeof (MonoStackHash
));
6165 hashes
.offset_free_hash
= portable_hash
;
6166 hashes
.offset_rich_hash
= unportable_hash
;
6168 // Tells mono that we want to send the HANG EXC_TYPE.
6169 const char *signal
= "SIGTERM";
6171 gboolean success
= mono_merp_invoke (crashed_pid
, signal
, payload
, &hashes
);
6173 //g_assert_not_reached ();
6174 mono_error_set_generic_error (error
, "System", "Exception", "We were unable to start the Microsoft Error Reporting client.");
6177 // Icall has platform check in managed too.
6178 g_assert_not_reached ();
6183 ves_icall_Mono_Runtime_DumpTelemetry (const char *payload
, guint64 portable_hash
, guint64 unportable_hash
, MonoError
*error
)
6185 #ifndef DISABLE_CRASH_REPORTING
6186 MonoStackHash hashes
;
6187 memset (&hashes
, 0, sizeof (MonoStackHash
));
6188 hashes
.offset_free_hash
= portable_hash
;
6189 hashes
.offset_rich_hash
= unportable_hash
;
6190 mono_crash_dump (payload
, &hashes
);
6197 ves_icall_Mono_Runtime_DumpStateSingle (guint64
*portable_hash
, guint64
*unportable_hash
, MonoError
*error
)
6199 MonoStringHandle result
;
6201 #ifndef DISABLE_CRASH_REPORTING
6202 MonoStackHash hashes
;
6203 memset (&hashes
, 0, sizeof (MonoStackHash
));
6204 MonoContext
*ctx
= NULL
;
6206 MonoThreadSummary this_thread
;
6207 if (!mono_threads_summarize_one (&this_thread
, ctx
))
6208 return mono_string_new_handle (mono_domain_get (), "", error
);
6210 *portable_hash
= (guint64
) this_thread
.hashes
.offset_free_hash
;
6211 *unportable_hash
= (guint64
) this_thread
.hashes
.offset_rich_hash
;
6213 MonoStateWriter writer
;
6214 char *scratch
= g_new0 (gchar
, MONO_MAX_SUMMARY_LEN_ICALL
);
6215 mono_state_writer_init (&writer
, scratch
, MONO_MAX_SUMMARY_LEN_ICALL
);
6216 mono_native_state_init (&writer
);
6217 gboolean first_thread_added
= TRUE
;
6218 mono_native_state_add_thread (&writer
, &this_thread
, NULL
, first_thread_added
, TRUE
);
6219 char *output
= mono_native_state_free (&writer
, FALSE
);
6220 result
= mono_string_new_handle (mono_domain_get (), output
, error
);
6225 *unportable_hash
= 0;
6226 result
= mono_string_new_handle (mono_domain_get (), "", error
);
6234 ves_icall_Mono_Runtime_RegisterReportingForNativeLib (const char *path_suffix
, const char *module_name
)
6236 #ifndef DISABLE_CRASH_REPORTING
6237 if (mono_get_eh_callbacks ()->mono_register_native_library
)
6238 mono_get_eh_callbacks ()->mono_register_native_library (path_suffix
, module_name
);
6243 ves_icall_Mono_Runtime_EnableCrashReportingLog (const char *directory
, MonoError
*error
)
6245 #ifndef DISABLE_CRASH_REPORTING
6246 mono_summarize_set_timeline_dir (directory
);
6251 ves_icall_Mono_Runtime_CheckCrashReportingLog (const char *directory
, MonoBoolean clear
, MonoError
*error
)
6254 #ifndef DISABLE_CRASH_REPORTING
6255 ret
= (int) mono_summarize_timeline_read_level (directory
, clear
!= 0);
6263 ves_icall_Mono_Runtime_DumpStateTotal (guint64
*portable_hash
, guint64
*unportable_hash
, MonoError
*error
)
6265 MonoStringHandle result
;
6267 #ifndef DISABLE_CRASH_REPORTING
6268 char *scratch
= g_new0 (gchar
, MONO_MAX_SUMMARY_LEN_ICALL
);
6271 MonoStackHash hashes
;
6272 memset (&hashes
, 0, sizeof (MonoStackHash
));
6273 MonoContext
*ctx
= NULL
;
6275 mono_get_runtime_callbacks ()->install_state_summarizer ();
6277 mono_summarize_timeline_start ();
6279 gboolean success
= mono_threads_summarize (ctx
, &out
, &hashes
, TRUE
, FALSE
, scratch
, MONO_MAX_SUMMARY_LEN_ICALL
);
6280 mono_summarize_timeline_phase_log (MonoSummaryCleanup
);
6283 return mono_string_new_handle (mono_domain_get (), "", error
);
6285 *portable_hash
= (guint64
) hashes
.offset_free_hash
;
6286 *unportable_hash
= (guint64
) hashes
.offset_rich_hash
;
6287 result
= mono_string_new_handle (mono_domain_get (), out
, error
);
6289 // out is now a pointer into garbage memory
6292 mono_summarize_timeline_phase_log (MonoSummaryDone
);
6295 *unportable_hash
= 0;
6296 result
= mono_string_new_handle (mono_domain_get (), "", error
);
6303 ves_icall_System_Reflection_AssemblyName_ParseAssemblyName (const char *name
, MonoAssemblyName
*aname
, MonoBoolean
*is_version_defined_arg
, MonoBoolean
*is_token_defined_arg
)
6305 gboolean is_version_defined
= FALSE
;
6306 gboolean is_token_defined
= FALSE
;
6307 gboolean result
= FALSE
;
6309 result
= mono_assembly_name_parse_full (name
, aname
, TRUE
, &is_version_defined
, &is_token_defined
);
6311 *is_version_defined_arg
= (MonoBoolean
)is_version_defined
;
6312 *is_token_defined_arg
= (MonoBoolean
)is_token_defined
;
6317 MonoReflectionTypeHandle
6318 ves_icall_System_Reflection_RuntimeModule_GetGlobalType (MonoImage
*image
, MonoError
*error
)
6320 MonoDomain
*domain
= mono_domain_get ();
6325 MonoReflectionTypeHandle ret
= MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
6327 if (image_is_dynamic (image
) && ((MonoDynamicImage
*)image
)->initial_image
)
6328 /* These images do not have a global type */
6331 klass
= mono_class_get_checked (image
, 1 | MONO_TOKEN_TYPE_DEF
, error
);
6332 goto_if_nok (error
, leave
);
6334 ret
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
6340 ves_icall_System_Reflection_RuntimeModule_GetGuidInternal (MonoImage
*image
, MonoArrayHandle guid_h
, MonoError
*error
)
6342 g_assert (mono_array_handle_length (guid_h
) == 16);
6344 if (!image
->metadata_only
) {
6345 g_assert (image
->heap_guid
.data
);
6346 g_assert (image
->heap_guid
.size
>= 16);
6348 MONO_ENTER_NO_SAFEPOINTS
;
6349 guint8
*data
= (guint8
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h
), 1, 0);
6350 memcpy (data
, (guint8
*)image
->heap_guid
.data
, 16);
6351 MONO_EXIT_NO_SAFEPOINTS
;
6353 MONO_ENTER_NO_SAFEPOINTS
;
6354 guint8
*data
= (guint8
*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h
), 1, 0);
6355 memset (data
, 0, 16);
6356 MONO_EXIT_NO_SAFEPOINTS
;
6361 static inline gpointer
6362 mono_icall_module_get_hinstance (MonoImage
*image
)
6364 return (gpointer
) (-1);
6366 #endif /* HOST_WIN32 */
6369 ves_icall_System_Reflection_RuntimeModule_GetHINSTANCE (MonoImage
*image
, MonoError
*error
)
6371 return mono_icall_module_get_hinstance (image
);
6375 ves_icall_System_Reflection_RuntimeModule_GetPEKind (MonoImage
*image
, gint32
*pe_kind
, gint32
*machine
, MonoError
*error
)
6377 if (image_is_dynamic (image
)) {
6378 MonoDynamicImage
*dyn
= (MonoDynamicImage
*)image
;
6379 *pe_kind
= dyn
->pe_kind
;
6380 *machine
= dyn
->machine
;
6383 *pe_kind
= (image
->image_info
->cli_cli_header
.ch_flags
& 0x3);
6384 *machine
= image
->image_info
->cli_header
.coff
.coff_machine
;
6389 ves_icall_System_Reflection_RuntimeModule_GetMDStreamVersion (MonoImage
*image
, MonoError
*error
)
6391 return (image
->md_version_major
<< 16) | (image
->md_version_minor
);
6395 ves_icall_System_Reflection_RuntimeModule_InternalGetTypes (MonoImage
*image
, MonoError
*error
)
6399 MonoDomain
*domain
= mono_domain_get ();
6402 MonoArrayHandle arr
= mono_array_new_handle (domain
, mono_defaults
.runtimetype_class
, 0, error
);
6405 MonoArrayHandle exceptions
= MONO_HANDLE_NEW (MonoArray
, NULL
);
6406 MonoArrayHandle res
= mono_module_get_types (domain
, image
, exceptions
, FALSE
, error
);
6407 return_val_if_nok (error
, MONO_HANDLE_CAST(MonoArray
, NULL_HANDLE
));
6409 int n
= mono_array_handle_length (exceptions
);
6410 MonoExceptionHandle ex
= MONO_HANDLE_NEW (MonoException
, NULL
);
6411 for (int i
= 0; i
< n
; ++i
) {
6412 MONO_HANDLE_ARRAY_GETREF(ex
, exceptions
, i
);
6413 if (!MONO_HANDLE_IS_NULL (ex
)) {
6414 mono_error_set_exception_handle (error
, ex
);
6415 return MONO_HANDLE_CAST(MonoArray
, NULL_HANDLE
);
6423 mono_memberref_is_method (MonoImage
*image
, guint32 token
)
6425 if (!image_is_dynamic (image
)) {
6426 guint32 cols
[MONO_MEMBERREF_SIZE
];
6428 const MonoTableInfo
*table
= &image
->tables
[MONO_TABLE_MEMBERREF
];
6429 int idx
= mono_metadata_token_index (token
) - 1;
6430 if (idx
< 0 || table
->rows
<= idx
) {
6433 mono_metadata_decode_row (table
, idx
, cols
, MONO_MEMBERREF_SIZE
);
6434 sig
= mono_metadata_blob_heap (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
6435 mono_metadata_decode_blob_size (sig
, &sig
);
6436 return (*sig
!= 0x6);
6439 MonoClass
*handle_class
;
6441 if (!mono_lookup_dynamic_token_class (image
, token
, FALSE
, &handle_class
, NULL
, error
)) {
6442 mono_error_cleanup (error
); /* just probing, ignore error */
6446 return mono_defaults
.methodhandle_class
== handle_class
;
6450 static MonoGenericInst
*
6451 get_generic_inst_from_array_handle (MonoArrayHandle type_args
)
6453 int type_argc
= mono_array_handle_length (type_args
);
6454 int size
= MONO_SIZEOF_GENERIC_INST
+ type_argc
* sizeof (MonoType
*);
6456 MonoGenericInst
*ginst
= (MonoGenericInst
*)g_alloca (size
);
6457 memset (ginst
, 0, sizeof (MonoGenericInst
));
6458 ginst
->type_argc
= type_argc
;
6459 for (int i
= 0; i
< type_argc
; i
++) {
6460 MONO_HANDLE_ARRAY_GETVAL (ginst
->type_argv
[i
], type_args
, MonoType
*, i
);
6462 ginst
->is_open
= FALSE
;
6463 for (int i
= 0; i
< type_argc
; i
++) {
6464 if (mono_class_is_open_constructed_type (ginst
->type_argv
[i
])) {
6465 ginst
->is_open
= TRUE
;
6470 return mono_metadata_get_canonical_generic_inst (ginst
);
6474 init_generic_context_from_args_handles (MonoGenericContext
*context
, MonoArrayHandle type_args
, MonoArrayHandle method_args
)
6476 if (!MONO_HANDLE_IS_NULL (type_args
)) {
6477 context
->class_inst
= get_generic_inst_from_array_handle (type_args
);
6479 context
->class_inst
= NULL
;
6481 if (!MONO_HANDLE_IS_NULL (method_args
)) {
6482 context
->method_inst
= get_generic_inst_from_array_handle (method_args
);
6484 context
->method_inst
= NULL
;
6490 module_resolve_type_token (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6492 HANDLE_FUNCTION_ENTER ();
6494 MonoType
*result
= NULL
;
6496 int table
= mono_metadata_token_table (token
);
6497 int index
= mono_metadata_token_index (token
);
6498 MonoGenericContext context
;
6500 *resolve_error
= ResolveTokenError_Other
;
6502 /* Validate token */
6503 if ((table
!= MONO_TABLE_TYPEDEF
) && (table
!= MONO_TABLE_TYPEREF
) &&
6504 (table
!= MONO_TABLE_TYPESPEC
)) {
6505 *resolve_error
= ResolveTokenError_BadTable
;
6509 if (image_is_dynamic (image
)) {
6510 if ((table
== MONO_TABLE_TYPEDEF
) || (table
== MONO_TABLE_TYPEREF
)) {
6511 ERROR_DECL (inner_error
);
6512 klass
= (MonoClass
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, NULL
, inner_error
);
6513 mono_error_cleanup (inner_error
);
6514 result
= klass
? m_class_get_byval_arg (klass
) : NULL
;
6518 init_generic_context_from_args_handles (&context
, type_args
, method_args
);
6519 ERROR_DECL (inner_error
);
6520 klass
= (MonoClass
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, &context
, inner_error
);
6521 mono_error_cleanup (inner_error
);
6522 result
= klass
? m_class_get_byval_arg (klass
) : NULL
;
6526 if ((index
<= 0) || (index
> image
->tables
[table
].rows
)) {
6527 *resolve_error
= ResolveTokenError_OutOfRange
;
6531 init_generic_context_from_args_handles (&context
, type_args
, method_args
);
6532 klass
= mono_class_get_checked (image
, token
, error
);
6534 klass
= mono_class_inflate_generic_class_checked (klass
, &context
, error
);
6535 goto_if_nok (error
, leave
);
6538 result
= m_class_get_byval_arg (klass
);
6540 HANDLE_FUNCTION_RETURN_VAL (result
);
6544 ves_icall_System_Reflection_RuntimeModule_ResolveTypeToken (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6546 return module_resolve_type_token (image
, token
, type_args
, method_args
, resolve_error
, error
);
6550 module_resolve_method_token (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6552 HANDLE_FUNCTION_ENTER ();
6554 MonoMethod
*method
= NULL
;
6555 int table
= mono_metadata_token_table (token
);
6556 int index
= mono_metadata_token_index (token
);
6557 MonoGenericContext context
;
6559 *resolve_error
= ResolveTokenError_Other
;
6561 /* Validate token */
6562 if ((table
!= MONO_TABLE_METHOD
) && (table
!= MONO_TABLE_METHODSPEC
) &&
6563 (table
!= MONO_TABLE_MEMBERREF
)) {
6564 *resolve_error
= ResolveTokenError_BadTable
;
6568 if (image_is_dynamic (image
)) {
6569 if (table
== MONO_TABLE_METHOD
) {
6570 ERROR_DECL (inner_error
);
6571 method
= (MonoMethod
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, NULL
, inner_error
);
6572 mono_error_cleanup (inner_error
);
6576 if ((table
== MONO_TABLE_MEMBERREF
) && !(mono_memberref_is_method (image
, token
))) {
6577 *resolve_error
= ResolveTokenError_BadTable
;
6581 init_generic_context_from_args_handles (&context
, type_args
, method_args
);
6582 ERROR_DECL (inner_error
);
6583 method
= (MonoMethod
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, &context
, inner_error
);
6584 mono_error_cleanup (inner_error
);
6588 if ((index
<= 0) || (index
> image
->tables
[table
].rows
)) {
6589 *resolve_error
= ResolveTokenError_OutOfRange
;
6592 if ((table
== MONO_TABLE_MEMBERREF
) && (!mono_memberref_is_method (image
, token
))) {
6593 *resolve_error
= ResolveTokenError_BadTable
;
6597 init_generic_context_from_args_handles (&context
, type_args
, method_args
);
6598 method
= mono_get_method_checked (image
, token
, NULL
, &context
, error
);
6601 HANDLE_FUNCTION_RETURN_VAL (method
);
6605 ves_icall_System_Reflection_RuntimeModule_ResolveMethodToken (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6607 return module_resolve_method_token (image
, token
, type_args
, method_args
, resolve_error
, error
);
6611 ves_icall_System_Reflection_RuntimeModule_ResolveStringToken (MonoImage
*image
, guint32 token
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6613 int index
= mono_metadata_token_index (token
);
6615 *resolve_error
= ResolveTokenError_Other
;
6617 /* Validate token */
6618 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
6619 *resolve_error
= ResolveTokenError_BadTable
;
6620 return NULL_HANDLE_STRING
;
6623 if (image_is_dynamic (image
)) {
6624 ERROR_DECL (ignore_inner_error
);
6625 // FIXME ignoring error
6626 // FIXME Push MONO_HANDLE_NEW to lower layers.
6627 MonoStringHandle result
= MONO_HANDLE_NEW (MonoString
, (MonoString
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, NULL
, ignore_inner_error
));
6628 mono_error_cleanup (ignore_inner_error
);
6632 if ((index
<= 0) || (index
>= image
->heap_us
.size
)) {
6633 *resolve_error
= ResolveTokenError_OutOfRange
;
6634 return NULL_HANDLE_STRING
;
6637 /* FIXME: What to do if the index points into the middle of a string ? */
6638 return mono_ldstr_handle (mono_domain_get (), image
, index
, error
);
6641 static MonoClassField
*
6642 module_resolve_field_token (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6644 HANDLE_FUNCTION_ENTER ();
6646 int table
= mono_metadata_token_table (token
);
6647 int index
= mono_metadata_token_index (token
);
6648 MonoGenericContext context
;
6649 MonoClassField
*field
= NULL
;
6652 *resolve_error
= ResolveTokenError_Other
;
6654 /* Validate token */
6655 if ((table
!= MONO_TABLE_FIELD
) && (table
!= MONO_TABLE_MEMBERREF
)) {
6656 *resolve_error
= ResolveTokenError_BadTable
;
6660 if (image_is_dynamic (image
)) {
6661 if (table
== MONO_TABLE_FIELD
) {
6662 ERROR_DECL (inner_error
);
6663 field
= (MonoClassField
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, NULL
, inner_error
);
6664 mono_error_cleanup (inner_error
);
6668 if (mono_memberref_is_method (image
, token
)) {
6669 *resolve_error
= ResolveTokenError_BadTable
;
6673 init_generic_context_from_args_handles (&context
, type_args
, method_args
);
6674 ERROR_DECL (inner_error
);
6675 field
= (MonoClassField
*)mono_lookup_dynamic_token_class (image
, token
, FALSE
, NULL
, &context
, inner_error
);
6676 mono_error_cleanup (inner_error
);
6680 if ((index
<= 0) || (index
> image
->tables
[table
].rows
)) {
6681 *resolve_error
= ResolveTokenError_OutOfRange
;
6684 if ((table
== MONO_TABLE_MEMBERREF
) && (mono_memberref_is_method (image
, token
))) {
6685 *resolve_error
= ResolveTokenError_BadTable
;
6689 init_generic_context_from_args_handles (&context
, type_args
, method_args
);
6690 field
= mono_field_from_token_checked (image
, token
, &klass
, &context
, error
);
6693 HANDLE_FUNCTION_RETURN_VAL (field
);
6697 ves_icall_System_Reflection_RuntimeModule_ResolveFieldToken (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6699 return module_resolve_field_token (image
, token
, type_args
, method_args
, resolve_error
, error
);
6703 ves_icall_System_Reflection_RuntimeModule_ResolveMemberToken (MonoImage
*image
, guint32 token
, MonoArrayHandle type_args
, MonoArrayHandle method_args
, MonoResolveTokenError
*error
, MonoError
*merror
)
6705 int table
= mono_metadata_token_table (token
);
6707 error_init (merror
);
6708 *error
= ResolveTokenError_Other
;
6711 case MONO_TABLE_TYPEDEF
:
6712 case MONO_TABLE_TYPEREF
:
6713 case MONO_TABLE_TYPESPEC
: {
6714 MonoType
*t
= module_resolve_type_token (image
, token
, type_args
, method_args
, error
, merror
);
6716 return MONO_HANDLE_CAST (MonoObject
, mono_type_get_object_handle (mono_domain_get (), t
, merror
));
6721 case MONO_TABLE_METHOD
:
6722 case MONO_TABLE_METHODSPEC
: {
6723 MonoMethod
*m
= module_resolve_method_token (image
, token
, type_args
, method_args
, error
, merror
);
6725 return MONO_HANDLE_CAST (MonoObject
, mono_method_get_object_handle (mono_domain_get (), m
, m
->klass
, merror
));
6729 case MONO_TABLE_FIELD
: {
6730 MonoClassField
*f
= module_resolve_field_token (image
, token
, type_args
, method_args
, error
, merror
);
6732 return MONO_HANDLE_CAST (MonoObject
, mono_field_get_object_handle (mono_domain_get (), f
->parent
, f
, merror
));
6737 case MONO_TABLE_MEMBERREF
:
6738 if (mono_memberref_is_method (image
, token
)) {
6739 MonoMethod
*m
= module_resolve_method_token (image
, token
, type_args
, method_args
, error
, merror
);
6741 return MONO_HANDLE_CAST (MonoObject
, mono_method_get_object_handle (mono_domain_get (), m
, m
->klass
, merror
));
6746 MonoClassField
*f
= module_resolve_field_token (image
, token
, type_args
, method_args
, error
, merror
);
6748 return MONO_HANDLE_CAST (MonoObject
, mono_field_get_object_handle (mono_domain_get (), f
->parent
, f
, merror
));
6756 *error
= ResolveTokenError_BadTable
;
6763 ves_icall_System_Reflection_RuntimeModule_ResolveSignature (MonoImage
*image
, guint32 token
, MonoResolveTokenError
*resolve_error
, MonoError
*error
)
6766 int table
= mono_metadata_token_table (token
);
6767 int idx
= mono_metadata_token_index (token
);
6768 MonoTableInfo
*tables
= image
->tables
;
6772 *resolve_error
= ResolveTokenError_OutOfRange
;
6774 /* FIXME: Support other tables ? */
6775 if (table
!= MONO_TABLE_STANDALONESIG
)
6776 return NULL_HANDLE_ARRAY
;
6778 if (image_is_dynamic (image
))
6779 return NULL_HANDLE_ARRAY
;
6781 if ((idx
== 0) || (idx
> tables
[MONO_TABLE_STANDALONESIG
].rows
))
6782 return NULL_HANDLE_ARRAY
;
6784 sig
= mono_metadata_decode_row_col (&tables
[MONO_TABLE_STANDALONESIG
], idx
- 1, 0);
6786 ptr
= mono_metadata_blob_heap (image
, sig
);
6787 len
= mono_metadata_decode_blob_size (ptr
, &ptr
);
6789 MonoArrayHandle res
= mono_array_new_handle (mono_domain_get (), mono_defaults
.byte_class
, len
, error
);
6790 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
6792 gpointer array_base
= MONO_ARRAY_HANDLE_PIN (res
, guint8
, 0, &h
);
6793 memcpy (array_base
, ptr
, len
);
6794 mono_gchandle_free_internal (h
);
6799 check_for_invalid_type (MonoClass
*klass
, MonoError
*error
)
6805 if (m_class_get_byval_arg (klass
)->type
!= MONO_TYPE_TYPEDBYREF
)
6808 name
= mono_type_get_full_name (klass
);
6809 mono_error_set_type_load_name (error
, name
, g_strdup (""), "");
6812 MonoReflectionTypeHandle
6813 ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type
, int rank
, MonoError
*error
)
6816 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
6818 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
6819 check_for_invalid_type (klass
, error
);
6820 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
6823 if (rank
== 0) //single dimension array
6824 aklass
= mono_class_create_array (klass
, 1);
6826 aklass
= mono_class_create_bounded_array (klass
, rank
, TRUE
);
6828 if (mono_class_has_failure (aklass
)) {
6829 mono_error_set_for_class_failure (error
, aklass
);
6830 return MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
);
6833 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
6834 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (aklass
), error
);
6837 MonoReflectionTypeHandle
6838 ves_icall_RuntimeType_make_byref_type (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
6841 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
6843 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
6844 mono_class_init_checked (klass
, error
);
6845 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
6847 check_for_invalid_type (klass
, error
);
6848 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
6850 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
6851 return mono_type_get_object_handle (domain
, m_class_get_this_arg (klass
), error
);
6854 MonoReflectionTypeHandle
6855 ves_icall_RuntimeType_MakePointerType (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
6858 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
6859 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
6860 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
6861 mono_class_init_checked (klass
, error
);
6862 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
6864 check_for_invalid_type (klass
, error
);
6865 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionType
, NULL_HANDLE
));
6867 MonoClass
*pklass
= mono_class_create_ptr (type
);
6869 return mono_type_get_object_handle (domain
, m_class_get_byval_arg (pklass
), error
);
6873 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionTypeHandle ref_type
, MonoObjectHandle target
,
6874 MonoReflectionMethodHandle info
, MonoBoolean throwOnBindFailure
, MonoError
*error
)
6876 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
6877 MonoClass
*delegate_class
= mono_class_from_mono_type_internal (type
);
6878 MonoMethod
*method
= MONO_HANDLE_GETVAL (info
, method
);
6879 MonoMethodSignature
*sig
= mono_method_signature_internal (method
);
6881 mono_class_init_checked (delegate_class
, error
);
6882 return_val_if_nok (error
, NULL_HANDLE
);
6884 if (!(m_class_get_parent (delegate_class
) == mono_defaults
.multicastdelegate_class
)) {
6885 /* FIXME improve this exception message */
6886 mono_error_set_execution_engine (error
, "file %s: line %d (%s): assertion failed: (%s)", __FILE__
, __LINE__
,
6888 "delegate_class->parent == mono_defaults.multicastdelegate_class");
6892 if (mono_security_core_clr_enabled ()) {
6893 ERROR_DECL (security_error
);
6894 if (!mono_security_core_clr_ensure_delegate_creation (method
, security_error
)) {
6895 if (throwOnBindFailure
)
6896 mono_error_move (error
, security_error
);
6898 mono_error_cleanup (security_error
);
6903 if (sig
->generic_param_count
&& method
->wrapper_type
== MONO_WRAPPER_NONE
) {
6904 if (!method
->is_inflated
) {
6905 mono_error_set_argument (error
, "method", " Cannot bind to the target method because its signature differs from that of the delegate type");
6910 MonoObjectHandle delegate
= mono_object_new_handle (MONO_HANDLE_DOMAIN (ref_type
), delegate_class
, error
);
6911 return_val_if_nok (error
, NULL_HANDLE
);
6913 if (!method_is_dynamic (method
) && (!MONO_HANDLE_IS_NULL (target
) && method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
&& method
->klass
!= mono_handle_class (target
))) {
6914 method
= mono_object_handle_get_virtual_method (target
, method
, error
);
6915 return_val_if_nok (error
, NULL_HANDLE
);
6918 mono_delegate_ctor_with_method (delegate
, target
, NULL
, method
, error
);
6919 return_val_if_nok (error
, NULL_HANDLE
);
6923 MonoMulticastDelegateHandle
6924 ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegateHandle delegate
, MonoError
*error
)
6928 MonoClass
*klass
= mono_handle_class (delegate
);
6929 g_assert (mono_class_has_parent (klass
, mono_defaults
.multicastdelegate_class
));
6931 MonoMulticastDelegateHandle ret
= MONO_HANDLE_CAST (MonoMulticastDelegate
, mono_object_new_handle (MONO_HANDLE_DOMAIN (delegate
), klass
, error
));
6932 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoMulticastDelegate
, NULL_HANDLE
));
6934 MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoDelegate
, ret
), invoke_impl
, gpointer
, mono_runtime_create_delegate_trampoline (klass
));
6939 MonoReflectionMethodHandle
6940 ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegateHandle delegate
, MonoError
*error
)
6944 MonoObjectHandle delegate_target
= MONO_HANDLE_NEW_GET (MonoObject
, delegate
, target
);
6945 MonoMethod
*m
= mono_object_handle_get_virtual_method (delegate_target
, MONO_HANDLE_GETVAL (delegate
, method
), error
);
6946 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
));
6947 return mono_method_get_object_handle (mono_domain_get (), m
, m
->klass
, error
);
6952 static inline gint32
6953 mono_array_get_byte_length (MonoArray
*array
)
6959 klass
= array
->obj
.vtable
->klass
;
6961 if (array
->bounds
== NULL
)
6962 length
= array
->max_length
;
6965 int klass_rank
= m_class_get_rank (klass
);
6966 for (i
= 0; i
< klass_rank
; ++ i
)
6967 length
*= array
->bounds
[i
].length
;
6970 switch (m_class_get_byval_arg (m_class_get_element_class (klass
))->type
) {
6973 case MONO_TYPE_BOOLEAN
:
6977 case MONO_TYPE_CHAR
:
6985 return length
* sizeof (gpointer
);
6996 ves_icall_System_Buffer_ByteLengthInternal (MonoArray
*array
)
6998 return mono_array_get_byte_length (array
);
7002 ves_icall_System_Buffer_GetByteInternal (MonoArray
*array
, gint32 idx
)
7004 return mono_array_get_internal (array
, gint8
, idx
);
7008 ves_icall_System_Buffer_SetByteInternal (MonoArray
*array
, gint32 idx
, gint8 value
)
7010 mono_array_set_internal (array
, gint8
, idx
, value
);
7014 ves_icall_System_Buffer_MemcpyInternal (gpointer dest
, gconstpointer src
, gint32 count
)
7016 memcpy (dest
, src
, count
);
7020 ves_icall_System_Buffer_BlockCopyInternal (MonoArray
*src
, gint32 src_offset
, MonoArray
*dest
, gint32 dest_offset
, gint32 count
)
7022 guint8
*src_buf
, *dest_buf
;
7026 mono_error_set_argument (error
, "count", "is negative");
7027 mono_error_set_pending_exception (error
);
7031 g_assert (count
>= 0);
7033 /* This is called directly from the class libraries without going through the managed wrapper */
7034 MONO_CHECK_ARG_NULL (src
, FALSE
);
7035 MONO_CHECK_ARG_NULL (dest
, FALSE
);
7037 /* watch out for integer overflow */
7038 if ((src_offset
> mono_array_get_byte_length (src
) - count
) || (dest_offset
> mono_array_get_byte_length (dest
) - count
))
7041 src_buf
= (guint8
*)src
->vector
+ src_offset
;
7042 dest_buf
= (guint8
*)dest
->vector
+ dest_offset
;
7045 memcpy (dest_buf
, src_buf
, count
);
7047 memmove (dest_buf
, src_buf
, count
); /* Source and dest are the same array */
7052 #ifndef DISABLE_REMOTING
7054 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObjectHandle this_obj
, MonoStringHandle class_name
, MonoError
*error
)
7057 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (this_obj
);
7058 MonoRealProxyHandle rp
= MONO_HANDLE_CAST (MonoRealProxy
, this_obj
);
7060 MonoObjectHandle res
= mono_object_new_handle (domain
, mono_defaults
.transparent_proxy_class
, error
);
7061 return_val_if_nok (error
, NULL_HANDLE
);
7063 MonoTransparentProxyHandle tp
= MONO_HANDLE_CAST (MonoTransparentProxy
, res
);
7065 MONO_HANDLE_SET (tp
, rp
, rp
);
7067 MonoReflectionTypeHandle reftype
= MONO_HANDLE_NEW (MonoReflectionType
, NULL
);
7068 MONO_HANDLE_GET (reftype
, rp
, class_to_proxy
);
7069 MonoType
*type
= MONO_HANDLE_GETVAL (reftype
, type
);
7070 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
7072 // mono_remote_class_vtable cannot handle errors well, so force any loading error to occur early
7073 mono_class_setup_vtable (klass
);
7074 if (mono_class_has_failure (klass
)) {
7075 mono_error_set_for_class_failure (error
, klass
);
7079 MonoObjectHandle remoting_obj
= mono_object_handle_isinst (this_obj
, mono_defaults
.iremotingtypeinfo_class
, error
);
7080 return_val_if_nok (error
, NULL_HANDLE
);
7081 MONO_HANDLE_SETVAL (tp
, custom_type_info
, MonoBoolean
, !MONO_HANDLE_IS_NULL (remoting_obj
));
7083 MonoRemoteClass
*remote_class
= (MonoRemoteClass
*)mono_remote_class (domain
, class_name
, klass
, error
);
7084 return_val_if_nok (error
, NULL_HANDLE
);
7085 MONO_HANDLE_SETVAL (tp
, remote_class
, MonoRemoteClass
*, remote_class
);
7087 MONO_HANDLE_SETVAL (res
, vtable
, MonoVTable
*, (MonoVTable
*)mono_remote_class_vtable (domain
, remote_class
, rp
, error
));
7088 return_val_if_nok (error
, NULL_HANDLE
);
7092 MonoReflectionType
*
7093 ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxy
*tp
)
7096 g_assert (tp
!= NULL
&& mono_object_class (tp
) == mono_defaults
.transparent_proxy_class
);
7097 g_assert (tp
->remote_class
!= NULL
&& tp
->remote_class
->proxy_class
!= NULL
);
7098 MonoReflectionType
*ret
= mono_type_get_object_checked (mono_object_domain (tp
), m_class_get_byval_arg (tp
->remote_class
->proxy_class
), error
);
7099 mono_error_set_pending_exception (error
);
7105 /* System.Environment */
7108 ves_icall_System_Environment_get_UserName (MonoError
*error
)
7111 /* using glib is more portable */
7112 const gchar
*user_name
= g_get_user_name ();
7113 if (user_name
!= NULL
)
7114 return mono_string_new_handle (mono_domain_get (), user_name
, error
);
7116 return NULL_HANDLE_STRING
;
7120 static MonoStringHandle
7121 mono_icall_get_machine_name (MonoError
*error
)
7124 #if !defined(DISABLE_SOCKETS)
7125 MonoStringHandle result
;
7128 #if defined _SC_HOST_NAME_MAX
7129 n
= sysconf (_SC_HOST_NAME_MAX
);
7133 buf
= (char*)g_malloc (n
+ 1);
7135 #if defined(HAVE_GETHOSTNAME)
7136 if (gethostname (buf
, n
) == 0){
7138 // try truncating the string at the first dot
7139 for (i
= 0; i
< n
; i
++) {
7140 if (buf
[i
] == '.') {
7145 result
= mono_string_new_handle (mono_domain_get (), buf
, error
);
7148 result
= MONO_HANDLE_CAST (MonoString
, NULL_HANDLE
);
7154 return mono_string_new_handle (mono_domain_get (), "mono", error
);
7157 #endif /* !HOST_WIN32 */
7160 ves_icall_System_Environment_get_MachineName (MonoError
*error
)
7163 return mono_icall_get_machine_name (error
);
7168 mono_icall_get_platform (void)
7170 #if defined(__MACH__)
7173 // Notice that the value is hidden from user code, and only exposed
7174 // to mscorlib. This is due to Mono's Unix/MacOS code predating the
7175 // define and making assumptions based on Unix/128/4 values before there
7176 // was a MacOS define. Lots of code would assume that not-Unix meant
7177 // Windows, but in this case, it would be OSX.
7185 #endif /* !HOST_WIN32 */
7188 ves_icall_System_Environment_get_Platform (void)
7190 return mono_icall_get_platform ();
7194 static inline MonoStringHandle
7195 mono_icall_get_new_line (MonoError
*error
)
7198 return mono_string_new_handle (mono_domain_get (), "\n", error
);
7200 #endif /* !HOST_WIN32 */
7203 ves_icall_System_Environment_get_NewLine (MonoError
*error
)
7205 return mono_icall_get_new_line (error
);
7209 static inline MonoBoolean
7210 mono_icall_is_64bit_os (void)
7212 #if SIZEOF_VOID_P == 8
7215 #if defined(HAVE_SYS_UTSNAME_H)
7216 struct utsname name
;
7218 if (uname (&name
) >= 0) {
7219 return strcmp (name
.machine
, "x86_64") == 0 || strncmp (name
.machine
, "aarch64", 7) == 0 || strncmp (name
.machine
, "ppc64", 5) == 0 || strncmp (name
.machine
, "riscv64", 7) == 0;
7225 #endif /* !HOST_WIN32 */
7228 ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
7230 return mono_icall_is_64bit_os ();
7234 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar
*utf8_name
, MonoError
*error
)
7238 if (utf8_name
== NULL
)
7239 return NULL_HANDLE_STRING
;
7241 value
= g_getenv (utf8_name
);
7244 return NULL_HANDLE_STRING
;
7246 MonoStringHandle res
= mono_string_new_handle (mono_domain_get (), value
, error
);
7252 * There is no standard way to get at environ.
7255 #ifndef __MINGW32_VERSION
7256 #if defined(__APPLE__)
7257 #if defined (TARGET_OSX)
7258 /* Apple defines this in crt_externs.h but doesn't provide that header for
7259 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
7260 * in fact exist on all implementations (so far)
7263 gchar
***_NSGetEnviron(void);
7265 #define environ (*_NSGetEnviron())
7267 static char *mono_environ
[1] = { NULL
};
7268 #define environ mono_environ
7269 #endif /* defined (TARGET_OSX) */
7280 ves_icall_System_Environment_GetCommandLineArgs (MonoError
*error
)
7283 MonoArrayHandle result
= mono_runtime_get_main_args_handle (error
);
7289 mono_icall_get_environment_variable_names (MonoError
*error
)
7299 for (e
= environ
; *e
!= 0; ++ e
)
7302 domain
= mono_domain_get ();
7303 names
= mono_array_new_checked (domain
, mono_defaults
.string_class
, n
, error
);
7304 return_val_if_nok (error
, NULL
);
7307 for (e
= environ
; *e
!= 0; ++ e
) {
7308 parts
= g_strsplit (*e
, "=", 2);
7310 str
= mono_string_new_checked (domain
, *parts
, error
);
7311 if (!is_ok (error
)) {
7315 mono_array_setref_internal (names
, n
, str
);
7325 #endif /* !HOST_WIN32 */
7328 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
7331 MonoArray
*result
= mono_icall_get_environment_variable_names (error
);
7332 mono_error_set_pending_exception (error
);
7337 ves_icall_System_Environment_InternalSetEnvironmentVariable (const gunichar2
*name
, gint32 name_length
,
7338 const gunichar2
*value
, gint32 value_length
, MonoError
*error
)
7341 if (!value
|| !value_length
|| !value
[0])
7344 SetEnvironmentVariableW (name
, value
);
7346 char *utf8_name
= NULL
;
7347 char *utf8_value
= NULL
;
7349 utf8_name
= mono_utf16_to_utf8 (name
, name_length
, error
); // FIXME: this should be ascii
7350 goto_if_nok (error
, exit
);
7352 if (!value
|| !value_length
|| !value
[0]) {
7353 g_unsetenv (utf8_name
);
7357 utf8_value
= mono_utf16_to_utf8 (value
, value_length
, error
);
7358 goto_if_nok (error
, exit
);
7360 g_setenv (utf8_name
, utf8_value
, TRUE
);
7363 g_free (utf8_value
);
7368 ves_icall_System_Environment_Exit (int result
)
7370 mono_environment_exitcode_set (result
);
7372 if (!mono_runtime_try_shutdown ())
7373 mono_thread_exit ();
7375 /* Suspend all managed threads since the runtime is going away */
7376 mono_thread_suspend_all_other_threads ();
7378 mono_runtime_quit ();
7380 /* we may need to do some cleanup here... */
7385 ves_icall_System_Environment_GetGacPath (MonoError
*error
)
7387 return mono_string_new_handle (mono_domain_get (), mono_assembly_getrootdir (), error
);
7391 static inline MonoStringHandle
7392 mono_icall_get_windows_folder_path (int folder
, MonoError
*error
)
7395 g_warning ("ves_icall_System_Environment_GetWindowsFolderPath should only be called on Windows!");
7396 return mono_string_new_handle (mono_domain_get (), "", error
);
7398 #endif /* !HOST_WIN32 */
7401 ves_icall_System_Environment_GetWindowsFolderPath (int folder
, MonoError
*error
)
7403 return mono_icall_get_windows_folder_path (folder
, error
);
7406 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
7408 mono_icall_get_logical_drives (void)
7411 gunichar2 buf
[256], *ptr
, *dname
;
7413 guint initial_size
= 127, size
= 128;
7416 MonoString
*drivestr
;
7417 MonoDomain
*domain
= mono_domain_get ();
7423 while (size
> initial_size
) {
7424 size
= (guint
) mono_w32file_get_logical_drive (initial_size
, ptr
);
7425 if (size
> initial_size
) {
7428 ptr
= (gunichar2
*)g_malloc0 ((size
+ 1) * sizeof (gunichar2
));
7429 initial_size
= size
;
7443 result
= mono_array_new_checked (domain
, mono_defaults
.string_class
, ndrives
, error
);
7444 if (mono_error_set_pending_exception (error
))
7451 while (*u16
) { u16
++; len
++; }
7452 drivestr
= mono_string_new_utf16_checked (domain
, dname
, len
, error
);
7453 if (mono_error_set_pending_exception (error
))
7456 mono_array_setref_internal (result
, ndrives
++, drivestr
);
7466 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
7469 ves_icall_System_Environment_GetLogicalDrives (void)
7471 return mono_icall_get_logical_drives ();
7475 ves_icall_System_IO_DriveInfo_GetDriveFormat (const gunichar2
*path
, gint32 path_length
, MonoError
*error
)
7477 gunichar2 volume_name
[MAX_PATH
+ 1];
7479 if (mono_w32file_get_file_system_type (path
, volume_name
, MAX_PATH
+ 1) == FALSE
)
7480 return NULL_HANDLE_STRING
;
7481 return mono_string_new_utf16_handle (mono_domain_get (), volume_name
, g_utf16_len (volume_name
), error
);
7485 ves_icall_System_Environment_InternalGetHome (MonoError
*error
)
7487 const gchar
*home_dir
= g_get_home_dir ();
7488 if (home_dir
!= NULL
)
7489 return mono_string_new_handle (mono_domain_get (), home_dir
, error
);
7491 return NULL_HANDLE_STRING
;
7494 static const char * const encodings
[] = {
7496 "ascii", "us_ascii", "us", "ansi_x3.4_1968",
7497 "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
7498 "iso_ir_6", "iso646_us", "iso_646.irv:1991",
7500 "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
7501 "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
7502 "x_unicode_2_0_utf_7",
7504 "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
7505 "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
7507 "utf_16", "UTF_16LE", "ucs_2", "unicode",
7510 "unicodefffe", "utf_16be",
7517 * Returns the internal codepage, if the value of "int_code_page" is
7518 * 1 at entry, and we can not compute a suitable code page number,
7519 * returns the code page as a string
7522 ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32
*int_code_page
, MonoError
*error
)
7528 char *codepage
= NULL
;
7530 int want_name
= *int_code_page
;
7533 *int_code_page
= -1;
7535 g_get_charset (&cset
);
7536 c
= codepage
= g_strdup (cset
);
7537 for (c
= codepage
; *c
; c
++){
7538 if (isascii (*c
) && isalpha (*c
))
7543 /* g_print ("charset: %s\n", cset); */
7545 /* handle some common aliases */
7548 for (i
= 0; p
!= 0; ){
7551 p
= encodings
[++i
];
7554 if (strcmp (p
, codepage
) == 0){
7555 *int_code_page
= code
;
7558 p
= encodings
[++i
];
7561 if (strstr (codepage
, "utf_8") != NULL
)
7562 *int_code_page
|= 0x10000000;
7565 if (want_name
&& *int_code_page
== -1)
7566 return mono_string_new_handle (mono_domain_get (), cset
, error
);
7567 return NULL_HANDLE_STRING
;
7571 ves_icall_System_Environment_get_HasShutdownStarted (void)
7573 return mono_runtime_is_shutting_down () || mono_domain_is_unloading (mono_domain_get ());
7579 ves_icall_System_Environment_BroadcastSettingChange (MonoError
*error
)
7586 ves_icall_System_Environment_get_TickCount (void)
7588 /* this will overflow after ~24 days */
7589 return (gint32
) (mono_msec_boottime () & 0xffffffff);
7594 ves_icall_System_Environment_get_TickCount64 (void)
7596 return mono_msec_boottime ();
7601 ves_icall_System_Runtime_Versioning_VersioningHelper_GetRuntimeId (MonoError
*error
)
7606 #ifndef DISABLE_REMOTING
7608 ves_icall_IsTransparentProxy (MonoObjectHandle proxy
, MonoError
*error
)
7610 if (MONO_HANDLE_IS_NULL (proxy
))
7613 if (mono_class_is_transparent_proxy (mono_handle_class (proxy
)))
7619 MonoReflectionMethodHandle
7620 ves_icall_Remoting_RemotingServices_GetVirtualMethod (
7621 MonoReflectionTypeHandle rtype
, MonoReflectionMethodHandle rmethod
, MonoError
*error
)
7623 MonoReflectionMethodHandle ret
= MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
);
7625 if (MONO_HANDLE_IS_NULL (rtype
)) {
7626 mono_error_set_argument_null (error
, "type", "");
7629 if (MONO_HANDLE_IS_NULL (rmethod
)) {
7630 mono_error_set_argument_null (error
, "method", "");
7634 MonoMethod
*method
= MONO_HANDLE_GETVAL (rmethod
, method
);
7635 MonoType
*type
= MONO_HANDLE_GETVAL (rtype
, type
);
7636 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
7637 mono_class_init_checked (klass
, error
);
7638 return_val_if_nok (error
, ret
);
7640 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass
))
7643 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
7646 if ((method
->flags
& METHOD_ATTRIBUTE_FINAL
) || !(method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) {
7647 if (klass
== method
->klass
|| mono_class_is_subclass_of_internal (klass
, method
->klass
, FALSE
))
7652 mono_class_setup_vtable (klass
);
7653 MonoMethod
**vtable
= m_class_get_vtable (klass
);
7655 MonoMethod
*res
= NULL
;
7656 if (mono_class_is_interface (method
->klass
)) {
7657 gboolean variance_used
= FALSE
;
7658 /*MS fails with variant interfaces but it's the right thing to do anyway.*/
7659 int offs
= mono_class_interface_offset_with_variance (klass
, method
->klass
, &variance_used
);
7661 res
= vtable
[offs
+ method
->slot
];
7663 if (!(klass
== method
->klass
|| mono_class_is_subclass_of_internal (klass
, method
->klass
, FALSE
)))
7666 if (method
->slot
!= -1)
7667 res
= vtable
[method
->slot
];
7673 ret
= mono_method_get_object_handle (mono_domain_get (), res
, NULL
, error
);
7678 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type
, MonoBoolean enable
, MonoError
*error
)
7680 MonoClass
*klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type
, type
));
7681 MonoVTable
*vtable
= mono_class_vtable_checked (mono_domain_get (), klass
, error
);
7682 return_if_nok (error
);
7684 mono_vtable_set_is_remote (vtable
, enable
);
7687 #else /* DISABLE_REMOTING */
7690 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type
, MonoBoolean enable
, MonoError
*error
)
7692 g_assert_not_reached ();
7698 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionTypeHandle type
, MonoError
*error
)
7700 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (type
);
7701 MonoClass
*klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type
, type
));
7702 mono_class_init_checked (klass
, error
);
7703 return_val_if_nok (error
, NULL_HANDLE
);
7705 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass
) || mono_class_is_abstract (klass
)) {
7706 mono_error_set_argument (error
, "type", "Type cannot be instantiated");
7710 if (m_class_get_rank (klass
) >= 1) {
7711 g_assert (m_class_get_rank (klass
) == 1);
7712 return MONO_HANDLE_CAST (MonoObject
, mono_array_new_handle (domain
, m_class_get_element_class (klass
), 0, error
));
7714 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, klass
, error
);
7715 return_val_if_nok (error
, NULL_HANDLE
);
7717 /* Bypass remoting object creation check */
7718 return MONO_HANDLE_NEW (MonoObject
, mono_object_new_alloc_specific_checked (vtable
, error
));
7725 ves_icall_System_IO_get_temp_path (MonoError
*error
)
7727 return mono_string_new_handle (mono_domain_get (), g_get_tmp_dir (), error
);
7732 #if defined(ENABLE_MONODROID) || defined(ENABLE_MONOTOUCH) || defined(TARGET_WASM)
7734 G_EXTERN_C gpointer
CreateZStream (gint32 compress
, MonoBoolean gzip
, gpointer feeder
, gpointer data
);
7735 G_EXTERN_C gint32
CloseZStream (gpointer stream
);
7736 G_EXTERN_C gint32
Flush (gpointer stream
);
7737 G_EXTERN_C gint32
ReadZStream (gpointer stream
, gpointer buffer
, gint32 length
);
7738 G_EXTERN_C gint32
WriteZStream (gpointer stream
, gpointer buffer
, gint32 length
);
7741 ves_icall_System_IO_Compression_DeflateStreamNative_CreateZStream (gint32 compress
, MonoBoolean gzip
, gpointer feeder
, gpointer data
)
7743 #ifdef MONO_CROSS_COMPILE
7746 return CreateZStream (compress
, gzip
, feeder
, data
);
7751 ves_icall_System_IO_Compression_DeflateStreamNative_CloseZStream (gpointer stream
)
7753 #ifdef MONO_CROSS_COMPILE
7756 return CloseZStream (stream
);
7761 ves_icall_System_IO_Compression_DeflateStreamNative_Flush (gpointer stream
)
7763 #ifdef MONO_CROSS_COMPILE
7766 return Flush (stream
);
7771 ves_icall_System_IO_Compression_DeflateStreamNative_ReadZStream (gpointer stream
, gpointer buffer
, gint32 length
)
7773 #ifdef MONO_CROSS_COMPILE
7776 return ReadZStream (stream
, buffer
, length
);
7781 ves_icall_System_IO_Compression_DeflateStreamNative_WriteZStream (gpointer stream
, gpointer buffer
, gint32 length
)
7783 #ifdef MONO_CROSS_COMPILE
7786 return WriteZStream (stream
, buffer
, length
);
7792 #ifndef PLATFORM_NO_DRIVEINFO
7794 ves_icall_System_IO_DriveInfo_GetDiskFreeSpace (const gunichar2
*path_name
, gint32 path_name_length
, guint64
*free_bytes_avail
,
7795 guint64
*total_number_of_bytes
, guint64
*total_number_of_free_bytes
,
7799 g_assert (free_bytes_avail
);
7800 g_assert (total_number_of_bytes
);
7801 g_assert (total_number_of_free_bytes
);
7803 // FIXME check for embedded nuls here or managed
7805 *error
= ERROR_SUCCESS
;
7806 *free_bytes_avail
= (guint64
)-1;
7807 *total_number_of_bytes
= (guint64
)-1;
7808 *total_number_of_free_bytes
= (guint64
)-1;
7810 gboolean result
= mono_w32file_get_disk_free_space (path_name
, free_bytes_avail
, total_number_of_bytes
, total_number_of_free_bytes
);
7812 *error
= mono_w32error_get_last ();
7816 #endif /* PLATFORM_NO_DRIVEINFO */
7819 ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod
*method
, MonoError
*error
)
7821 return mono_compile_method_checked (method
, error
);
7825 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (MonoError
*error
)
7829 const char *mono_cfg_dir
= mono_get_config_dir ();
7831 return mono_string_new_handle (mono_domain_get (), "", error
);
7833 path
= g_build_path (G_DIR_SEPARATOR_S
, mono_cfg_dir
, "mono", mono_get_runtime_info ()->framework_version
, "machine.config", NULL
);
7835 mono_icall_make_platform_path (path
);
7837 MonoStringHandle mcpath
= mono_string_new_handle (mono_domain_get (), path
, error
);
7840 mono_error_assert_ok (error
);
7846 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_app_config (MonoError
*error
)
7848 const gchar
*app_config
;
7850 gchar
*config_file_name
, *config_file_path
;
7851 gsize len
, config_file_path_length
, config_ext_length
;
7854 domain
= mono_domain_get ();
7855 MonoStringHandle file
= MONO_HANDLE_NEW (MonoString
, domain
->setup
->configuration_file
);
7856 if (MONO_HANDLE_IS_NULL (file
) || MONO_HANDLE_GETVAL (file
, length
) == 0)
7857 return MONO_HANDLE_CAST (MonoString
, mono_new_null ());
7859 // Retrieve config file and remove the extension
7860 config_file_name
= mono_string_handle_to_utf8 (file
, error
);
7861 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoString
, NULL_HANDLE
));
7863 config_file_path
= mono_portability_find_file (config_file_name
, TRUE
);
7864 if (!config_file_path
)
7865 config_file_path
= config_file_name
;
7867 config_file_path_length
= strlen (config_file_path
);
7868 config_ext_length
= strlen (".config");
7869 if (config_file_path_length
<= config_ext_length
) {
7870 if (config_file_name
!= config_file_path
)
7871 g_free (config_file_name
);
7872 return MONO_HANDLE_CAST (MonoString
, NULL_HANDLE
);
7875 len
= config_file_path_length
- config_ext_length
;
7876 module
= (gchar
*)g_malloc0 (len
+ 1);
7877 memcpy (module
, config_file_path
, len
);
7878 // Get the config file from the module name
7879 app_config
= mono_config_string_for_assembly_file (module
);
7882 if (config_file_name
!= config_file_path
)
7883 g_free (config_file_name
);
7884 g_free (config_file_path
);
7887 return MONO_HANDLE_CAST (MonoString
, NULL_HANDLE
);
7889 return mono_string_new_handle (mono_domain_get (), app_config
, error
);
7892 static MonoStringHandle
7893 get_bundled_machine_config (MonoError
*error
)
7895 const gchar
*machine_config
;
7897 machine_config
= mono_get_machine_config ();
7899 if (!machine_config
)
7900 return NULL_HANDLE_STRING
;
7902 return mono_string_new_handle (mono_domain_get (), machine_config
, error
);
7906 ves_icall_System_Environment_get_bundled_machine_config (MonoError
*error
)
7908 return get_bundled_machine_config (error
);
7913 ves_icall_System_Configuration_DefaultConfig_get_bundled_machine_config (MonoError
*error
)
7915 return get_bundled_machine_config (error
);
7919 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_machine_config (MonoError
*error
)
7921 return get_bundled_machine_config (error
);
7926 ves_icall_System_Web_Util_ICalls_get_machine_install_dir (MonoError
*error
)
7928 const char *mono_cfg_dir
= mono_get_config_dir ();
7930 return mono_string_new_handle (mono_domain_get (), "", error
);
7932 char *path
= g_path_get_dirname (mono_cfg_dir
);
7934 mono_icall_make_platform_path (path
);
7936 MonoStringHandle ipath
= mono_string_new_handle (mono_domain_get (), path
, error
);
7943 ves_icall_get_resources_ptr (MonoReflectionAssemblyHandle assembly
, gpointer
*result
, gint32
*size
, MonoError
*error
)
7945 MonoPEResourceDataEntry
*entry
;
7948 if (MONO_HANDLE_IS_NULL (assembly
) || !result
|| !size
)
7953 MonoAssembly
*assm
= MONO_HANDLE_GETVAL (assembly
, assembly
);
7954 image
= assm
->image
;
7955 entry
= (MonoPEResourceDataEntry
*)mono_image_lookup_resource (image
, MONO_PE_RESOURCE_ID_ASPNET_STRING
, 0, NULL
);
7959 *result
= mono_image_rva_map (image
, entry
->rde_data_offset
);
7964 *size
= entry
->rde_size
;
7970 ves_icall_System_Diagnostics_Debugger_IsAttached_internal (MonoError
*error
)
7972 return mono_is_debugger_attached ();
7976 ves_icall_System_Diagnostics_Debugger_IsLogging (MonoError
*error
)
7978 return mono_get_runtime_callbacks ()->debug_log_is_enabled
7979 && mono_get_runtime_callbacks ()->debug_log_is_enabled ();
7983 ves_icall_System_Diagnostics_Debugger_Log (int level
, MonoStringHandle category
, MonoStringHandle message
, MonoError
*error
)
7985 if (mono_get_runtime_callbacks ()->debug_log
)
7986 mono_get_runtime_callbacks ()->debug_log (level
, category
, message
);
7991 mono_icall_write_windows_debug_string (const gunichar2
*message
)
7993 g_warning ("WriteWindowsDebugString called and HOST_WIN32 not defined!\n");
7995 #endif /* !HOST_WIN32 */
7998 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (const gunichar2
*message
, MonoError
*error
)
8000 mono_icall_write_windows_debug_string (message
);
8003 /* Only used for value types */
8005 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionTypeHandle ref_type
, MonoError
*error
)
8007 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (ref_type
);
8008 MonoType
*type
= MONO_HANDLE_GETVAL (ref_type
, type
);
8009 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
8011 mono_class_init_checked (klass
, error
);
8012 return_val_if_nok (error
, NULL_HANDLE
);
8014 if (mono_class_is_nullable (klass
))
8015 /* No arguments -> null */
8018 return mono_object_new_handle (domain
, klass
, error
);
8021 MonoReflectionMethodHandle
8022 ves_icall_RuntimeMethodInfo_get_base_method (MonoReflectionMethodHandle m
, MonoBoolean definition
, MonoError
*error
)
8024 MonoMethod
*method
= MONO_HANDLE_GETVAL (m
, method
);
8026 MonoMethod
*base
= mono_method_get_base_method (method
, definition
, error
);
8027 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoReflectionMethod
, NULL_HANDLE
));
8028 if (base
== method
) {
8029 /* we want to short-circuit and return 'm' here. But we should
8030 return the same method object that
8031 mono_method_get_object_handle, below would return. Since
8032 that call takes NULL for the reftype argument, it will take
8033 base->klass as the reflected type for the MonoMethod. So we
8034 need to check that m also has base->klass as the reflected
8036 MonoReflectionTypeHandle orig_reftype
= MONO_HANDLE_NEW_GET (MonoReflectionType
, m
, reftype
);
8037 MonoClass
*orig_klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (orig_reftype
, type
));
8038 if (base
->klass
== orig_klass
)
8041 return mono_method_get_object_handle (mono_domain_get (), base
, NULL
, error
);
8045 ves_icall_RuntimeMethodInfo_get_name (MonoReflectionMethodHandle m
, MonoError
*error
)
8047 MonoMethod
*method
= MONO_HANDLE_GETVAL (m
, method
);
8049 MonoStringHandle s
= mono_string_new_handle (MONO_HANDLE_DOMAIN (m
), method
->name
, error
);
8050 return_val_if_nok (error
, NULL_HANDLE_STRING
);
8051 MONO_HANDLE_SET (m
, name
, s
);
8056 ves_icall_System_ArgIterator_Setup (MonoArgIterator
*iter
, char* argsp
, char* start
)
8058 iter
->sig
= *(MonoMethodSignature
**)argsp
;
8060 g_assert (iter
->sig
->sentinelpos
<= iter
->sig
->param_count
);
8061 g_assert (iter
->sig
->call_convention
== MONO_CALL_VARARG
);
8064 /* FIXME: it's not documented what start is exactly... */
8068 iter
->args
= argsp
+ sizeof (gpointer
);
8070 iter
->num_args
= iter
->sig
->param_count
- iter
->sig
->sentinelpos
;
8072 /* g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos); */
8076 ves_icall_System_ArgIterator_IntGetNextArg (MonoArgIterator
*iter
, MonoTypedRef
*res
)
8078 guint32 i
, arg_size
;
8081 i
= iter
->sig
->sentinelpos
+ iter
->next_arg
;
8083 g_assert (i
< iter
->sig
->param_count
);
8085 res
->type
= iter
->sig
->params
[i
];
8086 res
->klass
= mono_class_from_mono_type_internal (res
->type
);
8087 arg_size
= mono_type_stack_size (res
->type
, &align
);
8088 #if defined(__arm__) || defined(__mips__)
8089 iter
->args
= (guint8
*)(((gsize
)iter
->args
+ (align
) - 1) & ~(align
- 1));
8091 res
->value
= iter
->args
;
8092 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
8093 if (arg_size
<= sizeof (gpointer
)) {
8095 int padding
= arg_size
- mono_type_size (res
->type
, &dummy
);
8096 res
->value
= (guint8
*)res
->value
+ padding
;
8099 iter
->args
= (char*)iter
->args
+ arg_size
;
8102 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res->type->type, arg_size, res->value); */
8106 ves_icall_System_ArgIterator_IntGetNextArgWithType (MonoArgIterator
*iter
, MonoTypedRef
*res
, MonoType
*type
)
8108 guint32 i
, arg_size
;
8111 i
= iter
->sig
->sentinelpos
+ iter
->next_arg
;
8113 g_assert (i
< iter
->sig
->param_count
);
8115 while (i
< iter
->sig
->param_count
) {
8116 if (!mono_metadata_type_equal (type
, iter
->sig
->params
[i
]))
8118 res
->type
= iter
->sig
->params
[i
];
8119 res
->klass
= mono_class_from_mono_type_internal (res
->type
);
8120 /* FIXME: endianess issue... */
8121 arg_size
= mono_type_stack_size (res
->type
, &align
);
8122 #if defined(__arm__) || defined(__mips__)
8123 iter
->args
= (guint8
*)(((gsize
)iter
->args
+ (align
) - 1) & ~(align
- 1));
8125 res
->value
= iter
->args
;
8126 iter
->args
= (char*)iter
->args
+ arg_size
;
8128 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value); */
8131 /* g_print ("arg type 0x%02x not found\n", res.type->type); */
8133 memset (res
, 0, sizeof (MonoTypedRef
));
8137 ves_icall_System_ArgIterator_IntGetNextArgType (MonoArgIterator
*iter
)
8141 i
= iter
->sig
->sentinelpos
+ iter
->next_arg
;
8143 g_assert (i
< iter
->sig
->param_count
);
8145 return iter
->sig
->params
[i
];
8149 ves_icall_System_TypedReference_ToObject (MonoTypedRef
* tref
, MonoError
*error
)
8151 return typed_reference_to_object (tref
, error
);
8155 ves_icall_System_TypedReference_InternalMakeTypedReference (MonoTypedRef
*res
, MonoObjectHandle target
, MonoArrayHandle fields
, MonoReflectionTypeHandle last_field
, MonoError
*error
)
8158 MonoType
*ftype
= NULL
;
8161 memset (res
, 0, sizeof (MonoTypedRef
));
8163 g_assert (mono_array_handle_length (fields
) > 0);
8165 klass
= mono_handle_class (target
);
8168 for (i
= 0; i
< mono_array_handle_length (fields
); ++i
) {
8170 MONO_HANDLE_ARRAY_GETVAL (f
, fields
, MonoClassField
*, i
);
8177 offset
+= f
->offset
- sizeof (MonoObject
);
8178 klass
= mono_class_from_mono_type_internal (f
->type
);
8183 res
->klass
= mono_class_from_mono_type_internal (ftype
);
8184 res
->value
= (guint8
*)MONO_HANDLE_RAW (target
) + offset
;
8188 prelink_method (MonoMethod
*method
, MonoError
*error
)
8191 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
8193 mono_lookup_pinvoke_call_internal (method
, error
);
8194 /* create the wrapper, too? */
8198 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethodHandle method
, MonoError
*error
)
8202 prelink_method (MONO_HANDLE_GETVAL (method
, method
), error
);
8206 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type
, MonoError
*error
)
8209 MonoClass
*klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type
, type
));
8211 gpointer iter
= NULL
;
8213 mono_class_init_checked (klass
, error
);
8214 return_if_nok (error
);
8216 while ((m
= mono_class_get_methods (klass
, &iter
))) {
8217 prelink_method (m
, error
);
8218 return_if_nok (error
);
8223 * used by System.Runtime.InteropServices.RuntimeInformation.(OS|Process)Architecture;
8224 * which use them in different ways for filling in an enum
8227 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetRuntimeArchitecture (MonoError
*error
)
8230 return mono_string_new_handle (mono_domain_get (), mono_config_get_cpu (), error
);
8234 * used by System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform
8237 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetOSName (MonoError
*error
)
8240 return mono_string_new_handle (mono_domain_get (), mono_config_get_os (), error
);
8244 ves_icall_Interop_Sys_DoubleToString(double value
, char *format
, char *buffer
, int bufferLength
)
8246 #if defined(TARGET_ARM)
8247 /* workaround for faulty vcmp.f64 implementation on some 32bit ARM CPUs */
8248 guint64 bits
= *(guint64
*) &value
;
8249 if (bits
== 0x1) { /* 4.9406564584124654E-324 */
8250 g_assert (!strcmp (format
, "%.40e"));
8251 return snprintf (buffer
, bufferLength
, "%s", "4.9406564584124654417656879286822137236506e-324");
8252 } else if (bits
== 0x4) { /* 2E-323 */
8253 g_assert (!strcmp (format
, "%.40e"));
8254 return snprintf (buffer
, bufferLength
, "%s", "1.9762625833649861767062751714728854894602e-323");
8258 return snprintf(buffer
, bufferLength
, format
, value
);
8262 ves_icall_System_Runtime_RuntimeImports_ecvt_s(char *buffer
, size_t sizeInBytes
, double value
, int count
, int* dec
, int* sign
)
8264 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
8265 _ecvt_s(buffer
, sizeInBytes
, value
, count
, dec
, sign
);
8270 /* These parameters are "readonly" in corlib/System/NumberFormatter.cs */
8272 ves_icall_System_NumberFormatter_GetFormatterTables (guint64
const **mantissas
,
8273 gint32
const **exponents
,
8274 gunichar2
const **digitLowerTable
,
8275 gunichar2
const **digitUpperTable
,
8276 gint64
const **tenPowersList
,
8277 gint32
const **decHexDigits
)
8279 *mantissas
= Formatter_MantissaBitsTable
;
8280 *exponents
= Formatter_TensExponentTable
;
8281 *digitLowerTable
= Formatter_DigitLowerTable
;
8282 *digitUpperTable
= Formatter_DigitUpperTable
;
8283 *tenPowersList
= Formatter_TenPowersList
;
8284 *decHexDigits
= Formatter_DecHexDigits
;
8288 add_modifier_to_array (MonoDomain
*domain
, MonoType
*type
, MonoArrayHandle dest
, int dest_idx
, MonoError
*error
)
8290 HANDLE_FUNCTION_ENTER ();
8292 MonoClass
*klass
= mono_class_from_mono_type_internal (type
);
8294 MonoReflectionTypeHandle rt
;
8295 rt
= mono_type_get_object_handle (domain
, m_class_get_byval_arg (klass
), error
);
8296 goto_if_nok (error
, leave
);
8298 MONO_HANDLE_ARRAY_SETREF (dest
, dest_idx
, rt
);
8300 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
8304 * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
8305 * and avoid useless allocations.
8307 static MonoArrayHandle
8308 type_array_from_modifiers (MonoImage
*image
, MonoType
*type
, int optional
, MonoError
*error
)
8311 MonoDomain
*domain
= mono_domain_get ();
8313 int cmod_count
= mono_type_custom_modifier_count (type
);
8314 if (cmod_count
== 0)
8318 for (i
= 0; i
< cmod_count
; ++i
) {
8320 (void) mono_type_get_custom_modifier (type
, i
, &required
, error
);
8321 goto_if_nok (error
, fail
);
8322 if ((optional
&& !required
) || (!optional
&& required
))
8328 MonoArrayHandle res
;
8329 res
= mono_array_new_handle (domain
, mono_defaults
.systemtype_class
, count
, error
);
8330 goto_if_nok (error
, fail
);
8332 for (i
= 0; i
< cmod_count
; ++i
) {
8334 MonoType
*cmod_type
= mono_type_get_custom_modifier (type
, i
, &required
, error
);
8335 goto_if_nok (error
, fail
);
8336 if ((optional
&& !required
) || (!optional
&& required
)) {
8337 if (!add_modifier_to_array (domain
, cmod_type
, res
, count
, error
))
8344 return MONO_HANDLE_NEW (MonoArray
, NULL
);
8348 ves_icall_RuntimeParameterInfo_GetTypeModifiers (MonoReflectionTypeHandle rt
, MonoObjectHandle member
, int pos
, MonoBoolean optional
, MonoError
*error
)
8351 MonoType
*type
= MONO_HANDLE_GETVAL (rt
, type
);
8352 MonoClass
*member_class
= mono_handle_class (member
);
8353 MonoMethod
*method
= NULL
;
8355 MonoMethodSignature
*sig
;
8357 if (mono_class_is_reflection_method_or_constructor (member_class
)) {
8358 method
= MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod
, member
), method
);
8359 } else if (m_class_get_image (member_class
) == mono_defaults
.corlib
&& !strcmp ("RuntimePropertyInfo", m_class_get_name (member_class
))) {
8360 MonoProperty
*prop
= MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionProperty
, member
), property
);
8361 if (!(method
= prop
->get
))
8365 char *type_name
= mono_type_get_full_name (member_class
);
8366 mono_error_set_not_supported (error
, "Custom modifiers on a ParamInfo with member %s are not supported", type_name
);
8368 return NULL_HANDLE_ARRAY
;
8371 image
= m_class_get_image (method
->klass
);
8372 sig
= mono_method_signature_internal (method
);
8376 type
= sig
->params
[pos
];
8378 return type_array_from_modifiers (image
, type
, optional
, error
);
8382 get_property_type (MonoProperty
*prop
)
8384 MonoMethodSignature
*sig
;
8386 sig
= mono_method_signature_internal (prop
->get
);
8388 } else if (prop
->set
) {
8389 sig
= mono_method_signature_internal (prop
->set
);
8390 return sig
->params
[sig
->param_count
- 1];
8396 ves_icall_RuntimePropertyInfo_GetTypeModifiers (MonoReflectionPropertyHandle property
, MonoBoolean optional
, MonoError
*error
)
8399 MonoProperty
*prop
= MONO_HANDLE_GETVAL (property
, property
);
8400 MonoClass
*klass
= MONO_HANDLE_GETVAL (property
, klass
);
8401 MonoType
*type
= get_property_type (prop
);
8402 MonoImage
*image
= m_class_get_image (klass
);
8405 return NULL_HANDLE_ARRAY
;
8406 return type_array_from_modifiers (image
, type
, optional
, error
);
8410 *Construct a MonoType suited to be used to decode a constant blob object.
8412 * @type is the target type which will be constructed
8413 * @blob_type is the blob type, for example, that comes from the constant table
8414 * @real_type is the expected constructed type.
8417 mono_type_from_blob_type (MonoType
*type
, MonoTypeEnum blob_type
, MonoType
*real_type
)
8419 type
->type
= blob_type
;
8420 type
->data
.klass
= NULL
;
8421 if (blob_type
== MONO_TYPE_CLASS
)
8422 type
->data
.klass
= mono_defaults
.object_class
;
8423 else if (real_type
->type
== MONO_TYPE_VALUETYPE
&& m_class_is_enumtype (real_type
->data
.klass
)) {
8424 /* For enums, we need to use the base type */
8425 type
->type
= MONO_TYPE_VALUETYPE
;
8426 type
->data
.klass
= mono_class_from_mono_type_internal (real_type
);
8428 type
->data
.klass
= mono_class_from_mono_type_internal (real_type
);
8432 ves_icall_property_info_get_default_value (MonoReflectionProperty
*property
)
8436 MonoProperty
*prop
= property
->property
;
8437 MonoType
*type
= get_property_type (prop
);
8438 MonoDomain
*domain
= mono_object_domain (property
);
8439 MonoTypeEnum def_type
;
8440 const char *def_value
;
8443 mono_class_init_internal (prop
->parent
);
8445 if (!(prop
->attrs
& PROPERTY_ATTRIBUTE_HAS_DEFAULT
)) {
8446 mono_error_set_invalid_operation (error
, NULL
);
8447 mono_error_set_pending_exception (error
);
8451 def_value
= mono_class_get_property_default_value (prop
, &def_type
);
8453 mono_type_from_blob_type (&blob_type
, def_type
, type
);
8454 o
= mono_get_object_from_blob (domain
, &blob_type
, def_value
, error
);
8456 mono_error_set_pending_exception (error
);
8461 ves_icall_MonoCustomAttrs_IsDefinedInternal (MonoObjectHandle obj
, MonoReflectionTypeHandle attr_type
, MonoError
*error
)
8464 MonoClass
*attr_class
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type
, type
));
8466 mono_class_init_checked (attr_class
, error
);
8467 return_val_if_nok (error
, FALSE
);
8469 MonoCustomAttrInfo
*cinfo
= mono_reflection_get_custom_attrs_info_checked (obj
, error
);
8470 return_val_if_nok (error
, FALSE
);
8474 gboolean found
= mono_custom_attrs_has_attr (cinfo
, attr_class
);
8476 mono_custom_attrs_free (cinfo
);
8481 ves_icall_MonoCustomAttrs_GetCustomAttributesInternal (MonoObjectHandle obj
, MonoReflectionTypeHandle attr_type
, MonoBoolean pseudoattrs
, MonoError
*error
)
8483 MonoClass
*attr_class
;
8484 if (MONO_HANDLE_IS_NULL (attr_type
))
8487 attr_class
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type
, type
));
8490 mono_class_init_checked (attr_class
, error
);
8491 return_val_if_nok (error
, NULL_HANDLE_ARRAY
);
8494 return mono_reflection_get_custom_attrs_by_type_handle (obj
, attr_class
, error
);
8498 ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObjectHandle obj
, MonoError
*error
)
8501 return mono_reflection_get_custom_attrs_data_checked (obj
, error
);
8506 ves_icall_Mono_Runtime_GetDisplayName (MonoError
*error
)
8509 MonoStringHandle display_name
;
8512 info
= mono_get_runtime_callbacks ()->get_runtime_build_info ();
8513 display_name
= mono_string_new_handle (mono_domain_get (), info
, error
);
8515 return display_name
;
8519 static inline gint32
8520 mono_icall_wait_for_input_idle (gpointer handle
, gint32 milliseconds
)
8522 return WAIT_TIMEOUT
;
8524 #endif /* !HOST_WIN32 */
8527 ves_icall_Microsoft_Win32_NativeMethods_WaitForInputIdle (gpointer handle
, gint32 milliseconds
, MonoError
*error
)
8529 return mono_icall_wait_for_input_idle (handle
, milliseconds
);
8533 ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcessId (MonoError
*error
)
8535 return mono_process_current_pid ();
8539 ves_icall_Mono_TlsProviderFactory_IsBtlsSupported (MonoError
*error
)
8551 ves_icall_System_Runtime_InteropServices_Marshal_GetHRForException_WinRT(MonoExceptionHandle ex
, MonoError
*error
)
8553 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.Marshal.GetHRForException_WinRT internal call is not implemented.");
8558 ves_icall_System_Runtime_InteropServices_Marshal_GetNativeActivationFactory(MonoObjectHandle type
, MonoError
*error
)
8560 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.Marshal.GetNativeActivationFactory internal call is not implemented.");
8565 ves_icall_System_Runtime_InteropServices_Marshal_GetRawIUnknownForComObjectNoAddRef(MonoObjectHandle obj
, MonoError
*error
)
8567 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.Marshal.GetRawIUnknownForComObjectNoAddRef internal call is not implemented.");
8572 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo(MonoError
*error
)
8574 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.GetRestrictedErrorInfo internal call is not implemented.");
8579 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoOriginateLanguageException (int ierr
, MonoStringHandle message
, void* languageException
, MonoError
*error
)
8581 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoOriginateLanguageException internal call is not implemented.");
8586 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoReportUnhandledError (MonoObjectHandle oerr
, MonoError
*error
)
8588 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoReportUnhandledError internal call is not implemented.");
8592 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsCreateString(MonoStringHandle sourceString
, int length
, void** hstring
, MonoError
*error
)
8594 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsCreateString internal call is not implemented.");
8599 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsDeleteString(void* hstring
, MonoError
*error
)
8601 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsDeleteString internal call is not implemented.");
8606 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsGetStringRawBuffer(void* hstring
, unsigned* length
, MonoError
*error
)
8608 mono_error_set_not_implemented (error
, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsGetStringRawBuffer internal call is not implemented.");
8617 ves_icall_System_IO_LogcatTextWriter_Log (const char *appname
, gint32 level
, const char *message
)
8619 g_log (appname
, (GLogLevelFlags
)level
, "%s", message
);
8624 static const MonoIcallTableCallbacks
*icall_table
;
8625 static mono_mutex_t icall_mutex
;
8626 static GHashTable
*icall_hash
= NULL
;
8627 static GHashTable
*jit_icall_hash_addr
= NULL
;
8629 typedef struct _MonoIcallHashTableValue
{
8630 gconstpointer method
;
8632 } MonoIcallHashTableValue
;
8635 mono_install_icall_table_callbacks (const MonoIcallTableCallbacks
*cb
)
8637 g_assert (cb
->version
== MONO_ICALL_TABLE_CALLBACKS_VERSION
);
8642 mono_icall_init (void)
8644 #ifndef DISABLE_ICALL_TABLES
8645 mono_icall_table_init ();
8647 icall_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
8648 mono_os_mutex_init (&icall_mutex
);
8652 mono_icall_lock (void)
8654 mono_locks_os_acquire (&icall_mutex
, IcallLock
);
8658 mono_icall_unlock (void)
8660 mono_locks_os_release (&icall_mutex
, IcallLock
);
8664 mono_icall_cleanup (void)
8666 g_hash_table_destroy (icall_hash
);
8667 g_hash_table_destroy (jit_icall_hash_addr
);
8668 mono_os_mutex_destroy (&icall_mutex
);
8672 add_internal_call_with_flags (const char *name
, gconstpointer method
, guint32 flags
)
8674 char *key
= g_strdup (name
);
8675 MonoIcallHashTableValue
*value
= g_new (MonoIcallHashTableValue
, 1);
8677 value
->method
= method
;
8678 value
->flags
= flags
;
8681 g_hash_table_insert (icall_hash
, key
, (gpointer
)value
);
8682 mono_icall_unlock ();
8687 * mono_add_internal_call:
8688 * \param name method specification to surface to the managed world
8689 * \param method pointer to a C method to invoke when the method is called
8691 * This method surfaces the C function pointed by \p method as a method
8692 * that has been surfaced in managed code with the method specified in
8693 * \p name as an internal call.
8695 * Internal calls are surfaced to all app domains loaded and they are
8696 * accessibly by a type with the specified name.
8698 * You must provide a fully qualified type name, that is namespaces
8699 * and type name, followed by a colon and the method name, with an
8700 * optional signature to bind.
8702 * For example, the following are all valid declarations:
8704 * \c MyApp.Services.ScriptService:Accelerate
8706 * \c MyApp.Services.ScriptService:Slowdown(int,bool)
8708 * You use method parameters in cases where there might be more than
8709 * one surface method to managed code. That way you can register different
8710 * internal calls for different method overloads.
8712 * The internal calls are invoked with no marshalling. This means that .NET
8713 * types like \c System.String are exposed as \c MonoString* parameters. This is
8714 * different than the way that strings are surfaced in P/Invoke.
8716 * For more information on how the parameters are marshalled, see the
8717 * <a href="http://www.mono-project.com/docs/advanced/embedding/">Mono Embedding</a>
8720 * See the <a href="mono-api-methods.html#method-desc">Method Description</a>
8721 * reference for more information on the format of method descriptions.
8724 mono_add_internal_call (const char *name
, gconstpointer method
)
8726 mono_add_internal_call_with_flags (name
, method
, FALSE
);
8730 * mono_dangerous_add_raw_internal_call:
8731 * \param name method specification to surface to the managed world
8732 * \param method pointer to a C method to invoke when the method is called
8734 * Similar to \c mono_add_internal_call but with more requirements for correct
8737 * A thread running a dangerous raw internal call will avoid a thread state
8738 * transition on entry and exit, but it must take responsiblity for cooperating
8739 * with the Mono runtime.
8741 * The \p method must NOT:
8743 * Run for an unbounded amount of time without calling the mono runtime.
8744 * Additionally, the method must switch to GC Safe mode to perform all blocking
8745 * operations: performing blocking I/O, taking locks, etc.
8749 mono_dangerous_add_raw_internal_call (const char *name
, gconstpointer method
)
8751 mono_add_internal_call_with_flags (name
, method
, TRUE
);
8755 * mono_add_internal_call_with_flags:
8756 * \param name method specification to surface to the managed world
8757 * \param method pointer to a C method to invoke when the method is called
8758 * \param cooperative if \c TRUE, run icall in GC Unsafe (cooperatively suspended) mode,
8759 * otherwise GC Safe (blocking)
8761 * Like \c mono_add_internal_call, but if \p cooperative is \c TRUE the added
8762 * icall promises that it will use the coopertive API to inform the runtime
8763 * when it is running blocking operations, that it will not run for unbounded
8764 * amounts of time without safepointing, and that it will not hold managed
8765 * object references across suspend safepoints.
8767 * If \p cooperative is \c FALSE, run the icall in GC Safe mode - the icall may
8768 * block. The icall must obey the GC Safe rules, e.g. it must not touch
8769 * unpinned managed memory.
8773 mono_add_internal_call_with_flags (const char *name
, gconstpointer method
, gboolean cooperative
)
8775 add_internal_call_with_flags (name
, method
, cooperative
? MONO_ICALL_FLAGS_COOPERATIVE
: MONO_ICALL_FLAGS_FOREIGN
);
8779 mono_add_internal_call_internal (const char *name
, gconstpointer method
)
8781 mono_add_internal_call_with_flags (name
, method
, TRUE
);
8785 * we should probably export this as an helper (handle nested types).
8786 * Returns the number of chars written in buf.
8789 concat_class_name (char *buf
, int bufsize
, MonoClass
*klass
)
8791 int nspacelen
, cnamelen
;
8792 nspacelen
= strlen (m_class_get_name_space (klass
));
8793 cnamelen
= strlen (m_class_get_name (klass
));
8794 if (nspacelen
+ cnamelen
+ 2 > bufsize
)
8797 memcpy (buf
, m_class_get_name_space (klass
), nspacelen
);
8798 buf
[nspacelen
++] = '.';
8800 memcpy (buf
+ nspacelen
, m_class_get_name (klass
), cnamelen
);
8801 buf
[nspacelen
+ cnamelen
] = 0;
8802 return nspacelen
+ cnamelen
;
8806 no_icall_table (void)
8808 g_assert_not_reached ();
8812 mono_lookup_internal_call_full_with_flags (MonoMethod
*method
, gboolean warn_on_missing
, guint32
*flags
)
8818 int typelen
= 0, mlen
, siglen
;
8821 g_assert (method
!= NULL
);
8823 if (method
->is_inflated
)
8824 method
= ((MonoMethodInflated
*) method
)->declaring
;
8826 if (m_class_get_nested_in (method
->klass
)) {
8827 int pos
= concat_class_name (mname
, sizeof (mname
)-2, m_class_get_nested_in (method
->klass
));
8831 mname
[pos
++] = '/';
8834 typelen
= concat_class_name (mname
+pos
, sizeof (mname
)-pos
-1, method
->klass
);
8840 typelen
= concat_class_name (mname
, sizeof (mname
), method
->klass
);
8845 classname
= g_strdup (mname
);
8847 mname
[typelen
] = ':';
8848 mname
[typelen
+ 1] = ':';
8850 mlen
= strlen (method
->name
);
8851 memcpy (mname
+ typelen
+ 2, method
->name
, mlen
);
8852 sigstart
= mname
+ typelen
+ 2 + mlen
;
8855 tmpsig
= mono_signature_get_desc (mono_method_signature_internal (method
), TRUE
);
8856 siglen
= strlen (tmpsig
);
8857 if (typelen
+ mlen
+ siglen
+ 6 > sizeof (mname
)) {
8862 memcpy (sigstart
+ 1, tmpsig
, siglen
);
8863 sigstart
[siglen
+ 1] = ')';
8864 sigstart
[siglen
+ 2] = 0;
8867 /* mono_marshal_get_native_wrapper () depends on this */
8868 if (method
->klass
== mono_defaults
.string_class
&& !strcmp (method
->name
, ".ctor"))
8869 return (gconstpointer
)ves_icall_System_String_ctor_RedirectToCreateString
;
8873 res
= g_hash_table_lookup (icall_hash
, mname
);
8875 MonoIcallHashTableValue
*value
= (MonoIcallHashTableValue
*)res
;
8877 *flags
= value
->flags
;
8878 res
= value
->method
;
8879 mono_icall_unlock ();
8884 /* try without signature */
8886 res
= g_hash_table_lookup (icall_hash
, mname
);
8888 MonoIcallHashTableValue
*value
= (MonoIcallHashTableValue
*)res
;
8890 *flags
= value
->flags
;
8891 res
= value
->method
;
8892 mono_icall_unlock ();
8898 mono_icall_unlock ();
8900 /* Fail only when the result is actually used */
8901 return (gconstpointer
)no_icall_table
;
8903 gboolean uses_handles
= FALSE
;
8904 g_assert (icall_table
->lookup
);
8905 res
= icall_table
->lookup (method
, classname
, sigstart
- mlen
, sigstart
, &uses_handles
);
8906 if (res
&& flags
&& uses_handles
)
8907 *flags
= *flags
| MONO_ICALL_FLAGS_USES_HANDLES
;
8908 mono_icall_unlock ();
8914 if (warn_on_missing
) {
8915 g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", mname
);
8916 g_print ("\nYour mono runtime and class libraries are out of sync.\n");
8917 g_print ("The out of sync library is: %s\n", m_class_get_image (method
->klass
)->name
);
8918 g_print ("\nWhen you update one from git you need to update, compile and install\nthe other too.\n");
8919 g_print ("Do not report this as a bug unless you're sure you have updated correctly:\nyou probably have a broken mono install.\n");
8920 g_print ("If you see other errors or faults after this message they are probably related\n");
8921 g_print ("and you need to fix your mono install first.\n");
8929 * mono_lookup_internal_call_full:
8930 * \param method the method to look up
8931 * \param uses_handles out argument if method needs handles around managed objects.
8932 * \returns a pointer to the icall code for the given method. If
8933 * \p uses_handles is not NULL, it will be set to TRUE if the method
8934 * needs managed objects wrapped using the infrastructure in handle.h
8936 * If the method is not found, warns and returns NULL.
8939 mono_lookup_internal_call_full (MonoMethod
*method
, gboolean warn_on_missing
, mono_bool
*uses_handles
, mono_bool
*foreign
)
8942 *uses_handles
= FALSE
;
8946 guint32 flags
= MONO_ICALL_FLAGS_NONE
;
8947 gconstpointer addr
= mono_lookup_internal_call_full_with_flags (method
, warn_on_missing
, &flags
);
8949 if (uses_handles
&& (flags
& MONO_ICALL_FLAGS_USES_HANDLES
))
8950 *uses_handles
= TRUE
;
8951 if (foreign
&& (flags
& MONO_ICALL_FLAGS_FOREIGN
))
8957 * mono_lookup_internal_call:
8960 mono_lookup_internal_call (MonoMethod
*method
)
8962 return (gpointer
)mono_lookup_internal_call_full (method
, TRUE
, NULL
, NULL
);
8966 * mono_lookup_icall_symbol:
8968 * Given the icall METHOD, returns its C symbol.
8971 mono_lookup_icall_symbol (MonoMethod
*m
)
8976 g_assert (icall_table
->lookup_icall_symbol
);
8978 func
= (gpointer
)mono_lookup_internal_call_full (m
, FALSE
, NULL
, NULL
);
8981 return icall_table
->lookup_icall_symbol (func
);
8984 #if defined(TARGET_WIN32) && defined(TARGET_X86)
8986 * Under windows, the default pinvoke calling convention is STDCALL but
8989 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION MONO_CALL_C
8991 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION 0
8994 // Storage for these enums is pointer-sized as it gets replaced with MonoType*.
8996 // mono_create_icall_signatures depends on this order. Handle with care.
8997 // It is alphabetical.
8998 typedef enum ICallSigType
{
8999 ICALL_SIG_TYPE_bool
= 0x00,
9000 ICALL_SIG_TYPE_boolean
= ICALL_SIG_TYPE_bool
,
9001 ICALL_SIG_TYPE_double
= 0x01,
9002 ICALL_SIG_TYPE_float
= 0x02,
9003 ICALL_SIG_TYPE_int
= 0x03,
9004 ICALL_SIG_TYPE_int16
= 0x04,
9005 ICALL_SIG_TYPE_int32
= 0x05,
9006 ICALL_SIG_TYPE_int8
= 0x06,
9007 ICALL_SIG_TYPE_long
= 0x07,
9008 ICALL_SIG_TYPE_obj
= 0x08,
9009 ICALL_SIG_TYPE_object
= ICALL_SIG_TYPE_obj
,
9010 ICALL_SIG_TYPE_ptr
= ICALL_SIG_TYPE_int
,
9011 ICALL_SIG_TYPE_ptrref
= 0x09,
9012 ICALL_SIG_TYPE_string
= 0x0A,
9013 ICALL_SIG_TYPE_uint16
= 0x0B,
9014 ICALL_SIG_TYPE_uint32
= 0x0C,
9015 ICALL_SIG_TYPE_uint8
= 0x0D,
9016 ICALL_SIG_TYPE_ulong
= 0x0E,
9017 ICALL_SIG_TYPE_void
= 0x0F,
9020 #define ICALL_SIG_TYPES_1(a) ICALL_SIG_TYPE_ ## a,
9021 #define ICALL_SIG_TYPES_2(a, b) ICALL_SIG_TYPES_1 (a ) ICALL_SIG_TYPES_1 (b)
9022 #define ICALL_SIG_TYPES_3(a, b, c) ICALL_SIG_TYPES_2 (a, b ) ICALL_SIG_TYPES_1 (c)
9023 #define ICALL_SIG_TYPES_4(a, b, c, d) ICALL_SIG_TYPES_3 (a, b, c ) ICALL_SIG_TYPES_1 (d)
9024 #define ICALL_SIG_TYPES_5(a, b, c, d, e) ICALL_SIG_TYPES_4 (a, b, c, d ) ICALL_SIG_TYPES_1 (e)
9025 #define ICALL_SIG_TYPES_6(a, b, c, d, e, f) ICALL_SIG_TYPES_5 (a, b, c, d, e) ICALL_SIG_TYPES_1 (f)
9026 #define ICALL_SIG_TYPES_7(a, b, c, d, e, f, g) ICALL_SIG_TYPES_6 (a, b, c, d, e, f) ICALL_SIG_TYPES_1 (g)
9027 #define ICALL_SIG_TYPES_8(a, b, c, d, e, f, g, h) ICALL_SIG_TYPES_7 (a, b, c, d, e, f, g) ICALL_SIG_TYPES_1 (h)
9029 #define ICALL_SIG_TYPES(n, types) ICALL_SIG_TYPES_ ## n types
9031 // A scheme to make these const would be nice.
9033 #define ICALL_SIG(n, xtypes) \
9035 MonoMethodSignature sig; \
9037 } ICALL_SIG_NAME (n, xtypes);
9039 MonoMethodSignature end
; // terminal zeroed element
9040 } mono_icall_signatures
= {
9042 #define ICALL_SIG(n, types) { { \
9044 n, /* param_count */ \
9045 -1, /* sentinelpos */ \
9046 0, /* generic_param_count */ \
9047 MONO_ICALL_SIGNATURE_CALL_CONVENTION, \
9049 0, /* explicit_this */ \
9051 0, /* is_inflated */ \
9052 0, /* has_type_parameters */ \
9053 }, /* possible gap here, depending on MONO_ZERO_LEN_ARRAY */ \
9054 { ICALL_SIG_TYPES (n, types) } }, /* params and ret */
9059 #define ICALL_SIG(n, types) MonoMethodSignature * const ICALL_SIG_NAME (n, types) = &mono_icall_signatures.ICALL_SIG_NAME (n, types).sig;
9064 mono_create_icall_signatures (void)
9066 // Fixup the mostly statically initialized icall signatures.
9067 // x = m_class_get_byval_arg (x)
9068 // Initialize ret with params [0] and params [i] with params [i + 1].
9069 // ptrref is special
9071 // FIXME This is a bit obscure.
9073 typedef MonoMethodSignature G_MAY_ALIAS MonoMethodSignature_a
;
9074 typedef gsize G_MAY_ALIAS gsize_a
;
9076 MonoType
* const lookup
[ ] = {
9077 m_class_get_byval_arg (mono_defaults
.boolean_class
), // ICALL_SIG_TYPE_bool
9078 m_class_get_byval_arg (mono_defaults
.double_class
), // ICALL_SIG_TYPE_double
9079 m_class_get_byval_arg (mono_defaults
.single_class
), // ICALL_SIG_TYPE_float
9080 m_class_get_byval_arg (mono_defaults
.int_class
), // ICALL_SIG_TYPE_int
9081 m_class_get_byval_arg (mono_defaults
.int16_class
), // ICALL_SIG_TYPE_int16
9082 m_class_get_byval_arg (mono_defaults
.int32_class
), // ICALL_SIG_TYPE_int32
9083 m_class_get_byval_arg (mono_defaults
.sbyte_class
), // ICALL_SIG_TYPE_int8
9084 m_class_get_byval_arg (mono_defaults
.int64_class
), // ICALL_SIG_TYPE_long
9085 m_class_get_byval_arg (mono_defaults
.object_class
), // ICALL_SIG_TYPE_obj
9086 mono_class_get_byref_type (mono_defaults
.int_class
), // ICALL_SIG_TYPE_ptrref
9087 m_class_get_byval_arg (mono_defaults
.string_class
), // ICALL_SIG_TYPE_string
9088 m_class_get_byval_arg (mono_defaults
.uint16_class
), // ICALL_SIG_TYPE_uint16
9089 m_class_get_byval_arg (mono_defaults
.uint32_class
), // ICALL_SIG_TYPE_uint32
9090 m_class_get_byval_arg (mono_defaults
.byte_class
), // ICALL_SIG_TYPE_uint8
9091 m_class_get_byval_arg (mono_defaults
.uint64_class
), // ICALL_SIG_TYPE_ulong
9092 m_class_get_byval_arg (mono_defaults
.void_class
), // ICALL_SIG_TYPE_void
9095 MonoMethodSignature_a
*sig
= (MonoMethodSignature
*)&mono_icall_signatures
;
9097 while ((n
= sig
->param_count
)) {
9098 --sig
->param_count
; // remove ret
9099 gsize_a
*types
= (gsize
*)(sig
+ 1);
9100 for (int i
= 0; i
< n
; ++i
) {
9101 gsize index
= *types
++;
9102 g_assert (index
< G_N_ELEMENTS (lookup
));
9103 // Casts on next line are attempt to follow strict aliasing rules,
9104 // to ensure reading from *types precedes writing
9106 *(gsize
*)(i
? &sig
->params
[i
- 1] : &sig
->ret
) = (gsize
)lookup
[index
];
9108 sig
= (MonoMethodSignature
*)types
;
9113 mono_find_jit_icall_by_addr (gconstpointer addr
)
9115 MonoJitICallInfo
*info
;
9116 g_assert (jit_icall_hash_addr
);
9119 info
= (MonoJitICallInfo
*)g_hash_table_lookup (jit_icall_hash_addr
, (gpointer
)addr
);
9120 mono_icall_unlock ();
9126 mono_register_jit_icall_wrapper (MonoJitICallInfo
*info
, gconstpointer wrapper
)
9129 g_hash_table_insert (jit_icall_hash_addr
, (gpointer
)wrapper
, info
);
9130 mono_icall_unlock ();
9134 mono_register_jit_icall_info (MonoJitICallInfo
*info
, gconstpointer func
, const char *name
, MonoMethodSignature
*sig
, gboolean avoid_wrapper
, const char *c_symbol
)
9138 if (!jit_icall_hash_addr
) {
9139 jit_icall_hash_addr
= g_hash_table_new (NULL
, NULL
);
9142 // Duplicate initialization is allowed, assuming it is equivalent.
9144 g_hash_table_insert (jit_icall_hash_addr
, (gpointer
)func
, info
);
9146 mono_icall_unlock ();
9148 // Duplicate initialization is allowed and racy, assuming it is equivalent.
9153 info
->c_symbol
= c_symbol
;
9155 // Fill in wrapper ahead of time, to just be func, to avoid
9156 // later initializing it to anything else. So therefore, no wrapper.
9157 if (avoid_wrapper
) {
9158 info
->wrapper
= func
;
9160 // Leave it alone in case of a race.
9165 ves_icall_System_GC_GetCollectionCount (int generation
)
9167 return mono_gc_collection_count (generation
);
9171 ves_icall_System_GC_GetGeneration (MonoObjectHandle object
, MonoError
*error
)
9173 return mono_gc_get_generation (MONO_HANDLE_RAW (object
));
9177 ves_icall_System_GC_GetMaxGeneration (void)
9179 return mono_gc_max_generation ();
9183 ves_icall_System_GC_GetAllocatedBytesForCurrentThread (void)
9185 return 0; // TODO: implement https://github.com/mono/mono/issues/8397
9189 ves_icall_System_GC_RecordPressure (gint64 value
)
9191 mono_gc_add_memory_pressure (value
);
9195 ves_icall_System_Diagnostics_Stopwatch_GetTimestamp (void)
9197 return mono_100ns_ticks ();
9201 ves_icall_System_Threading_Timer_GetTimeMonotonic (void)
9203 return mono_100ns_ticks ();
9207 ves_icall_System_DateTime_GetSystemTimeAsFileTime (void)
9209 return mono_100ns_datetime ();
9213 ves_icall_System_Threading_Thread_SystemMaxStackSize (void)
9215 return mono_thread_info_get_system_max_stack_size ();
9219 ves_icall_System_Threading_Thread_YieldInternal (void)
9221 mono_threads_platform_yield ();
9226 ves_icall_System_Environment_get_ProcessorCount (void)
9228 return mono_cpu_count ();
9231 #if defined(ENABLE_MONODROID)
9233 G_EXTERN_C gint32
CreateNLSocket (void);
9234 G_EXTERN_C gint32
ReadEvents (gpointer sock
, gpointer buffer
, gint32 count
, gint32 size
);
9235 G_EXTERN_C gint32
CloseNLSocket (gpointer sock
);
9238 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CreateNLSocket (void)
9240 return CreateNLSocket ();
9244 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_ReadEvents (gpointer sock
, gpointer buffer
, gint32 count
, gint32 size
)
9246 return ReadEvents (sock
, buffer
, count
, size
);
9250 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CloseNLSocket (gpointer sock
)
9252 return CloseNLSocket (sock
);
9257 // Generate wrappers.
9259 #define ICALL_TYPE(id,name,first) /* nothing */
9260 #define ICALL(id,name,func) /* nothing */
9261 #define NOHANDLES(inner) /* nothing */
9263 #define MONO_HANDLE_REGISTER_ICALL(func, ret, nargs, argtypes) MONO_HANDLE_REGISTER_ICALL_IMPLEMENT (func, ret, nargs, argtypes)
9265 // Some native functions are exposed via multiple managed names.
9266 // Producing a wrapper for these results in duplicate wrappers with the same names,
9267 // which fails to compile. Do not produce such duplicate wrappers. Alternatively,
9268 // a one line native function with a different name that calls the main one could be used.
9269 // i.e. the wrapper would also have a different name.
9270 #define HANDLES_REUSE_WRAPPER(...) /* nothing */
9272 #define HANDLES(id, name, func, ret, nargs, argtypes) \
9273 MONO_HANDLE_DECLARE (id, name, func, ret, nargs, argtypes); \
9274 MONO_HANDLE_IMPLEMENT (id, name, func, ret, nargs, argtypes)
9276 #include "metadata/icall-def.h"
9279 #undef HANDLES_REUSE_WRAPPER
9283 #undef MONO_HANDLE_REGISTER_ICALL