[netcore] Improve netcore/build.sh script (#14835)
[mono-project.git] / mono / metadata / icall.c
blob093b520af5ec55d5f46c2652e30939b4fed2f7cd
1 /**
2 * \file
4 * Authors:
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.
17 #include <config.h>
19 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
20 /* Needed for _ecvt_s */
21 #define MINGW_HAS_SECURE_API 1
22 #include <stdio.h>
23 #endif
25 #include <glib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
29 #ifdef HAVE_ALLOCA_H
30 #include <alloca.h>
31 #endif
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #if defined (HAVE_WCHAR_H)
39 #include <wchar.h>
40 #endif
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>
118 #endif
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
129 static gboolean
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;
137 static void
138 icallarray_print (const char *format, ...)
140 if (!icallarray_print_enabled ())
141 return;
142 va_list args;
143 va_start (args, format);
144 g_printv (format, args);
145 va_end (args);
148 #else
149 #define icallarray_print_enabled() (FALSE)
150 #define icallarray_print(...) /* nothing */
151 #endif
153 /* Lazy class loading functions */
154 static GENERATE_GET_CLASS_WITH_CACHE (module, "System.Reflection", "Module")
156 static void
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);
168 #ifndef HOST_WIN32
169 static inline void
170 mono_icall_make_platform_path (gchar *path)
172 return;
175 static inline const gchar *
176 mono_icall_get_file_path_prefix (const gchar *path)
178 return "file://";
180 #endif /* HOST_WIN32 */
182 #if MONO_LLVM_LOADED
184 static
185 MonoJitICallInfos mono_jit_icall_info;
187 MonoJitICallInfos*
188 mono_get_jit_icall_info (void)
190 return &mono_jit_icall_info;
193 #else
195 MonoJitICallInfos mono_jit_icall_info;
197 #endif
199 MonoObjectHandle
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);
208 return NULL_HANDLE;
210 #endif
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);
219 return result;
222 MonoObjectHandle
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);
234 return NULL_HANDLE;
237 gint32 index = 0;
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);
243 return NULL_HANDLE;
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);
254 return NULL_HANDLE;
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);
269 void
270 ves_icall_System_Array_SetValueImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
272 array_set_value_impl (arr, value, pos, FALSE, error);
275 static inline void
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);
282 static void
283 array_set_value_impl (MonoArrayHandle arr_handle, MonoObjectHandle value_handle, guint32 pos, gboolean strict, MonoError *error)
285 MonoClass *ac, *vc, *ec;
286 gint32 esize, vsize;
287 gpointer *ea = NULL, *va = NULL;
289 guint64 u64 = 0;
290 gint64 i64 = 0;
291 gdouble r64 = 0;
292 gboolean castOk = FALSE;
293 gboolean et_isenum = FALSE;
294 gboolean vt_isenum = FALSE;
296 error_init (error);
298 if (!MONO_HANDLE_IS_NULL (value_handle))
299 vc = mono_handle_class (value_handle);
300 else
301 vc = NULL;
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);
312 goto leave;
314 #endif
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;
321 goto leave;
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;
329 goto leave;
332 #ifdef ENABLE_NETCORE
333 #define WIDENING_MSG NULL
334 #define WIDENING_ARG NULL
335 #else
336 #define WIDENING_MSG "not a widening conversion"
337 #define WIDENING_ARG "value"
338 #endif
340 #define NO_WIDENING_CONVERSION G_STMT_START{ \
341 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
342 break; \
343 }G_STMT_END
345 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{ \
346 if (esize < vsize + (extra)) { \
347 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
348 break; \
350 }G_STMT_END
352 #define INVALID_CAST G_STMT_START{ \
353 mono_get_runtime_callbacks ()->set_cast_details (vc, ec); \
354 mono_error_set_invalid_cast (error); \
355 break; \
356 }G_STMT_END
358 MonoTypeEnum et;
359 et = m_class_get_byval_arg (ec)->type;
360 MonoTypeEnum vt;
361 vt = m_class_get_byval_arg (vc)->type;
363 /* Check element (destination) type. */
364 switch (et) {
365 case MONO_TYPE_STRING:
366 switch (vt) {
367 case MONO_TYPE_STRING:
368 break;
369 default:
370 INVALID_CAST;
372 break;
373 case MONO_TYPE_BOOLEAN:
374 switch (vt) {
375 case MONO_TYPE_BOOLEAN:
376 break;
377 case MONO_TYPE_CHAR:
378 case MONO_TYPE_U1:
379 case MONO_TYPE_U2:
380 case MONO_TYPE_U4:
381 case MONO_TYPE_U8:
382 case MONO_TYPE_I1:
383 case MONO_TYPE_I2:
384 case MONO_TYPE_I4:
385 case MONO_TYPE_I8:
386 case MONO_TYPE_R4:
387 case MONO_TYPE_R8:
388 NO_WIDENING_CONVERSION;
389 break;
390 default:
391 INVALID_CAST;
393 break;
394 default:
395 break;
397 if (!is_ok (error))
398 goto leave;
400 castOk = mono_object_handle_isinst_mbyref_raw (value_handle, ec, error);
401 if (!is_ok (error))
402 goto leave;
404 if (!m_class_is_valuetype (ec)) {
405 if (!castOk)
406 INVALID_CAST;
407 if (is_ok (error))
408 MONO_HANDLE_ARRAY_SETREF (arr_handle, pos, value_handle);
409 goto leave;
412 if (castOk) {
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);
418 else
419 mono_gc_memmove_atomic (ea, va, esize);
420 MONO_EXIT_NO_SAFEPOINTS;
422 goto leave;
425 if (!m_class_is_valuetype (vc))
426 INVALID_CAST;
428 if (!is_ok (error))
429 goto leave;
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);
436 #if ENABLE_NETCORE
437 if (strict && et_isenum && !vt_isenum) {
438 INVALID_CAST;
439 goto leave;
441 #endif
443 if (et_isenum)
444 et = mono_class_enum_basetype_internal (m_class_get_byval_arg (ec)->data.klass)->type;
446 if (vt_isenum)
447 vt = mono_class_enum_basetype_internal (m_class_get_byval_arg (vc)->data.klass)->type;
449 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
450 switch (vt) { \
451 case MONO_TYPE_U1: \
452 case MONO_TYPE_U2: \
453 case MONO_TYPE_U4: \
454 case MONO_TYPE_U8: \
455 case MONO_TYPE_CHAR: \
456 CHECK_WIDENING_CONVERSION(0); \
457 *(etype *) ea = (etype) u64; \
458 break; \
459 /* You can't assign a signed value to an unsigned array. */ \
460 case MONO_TYPE_I1: \
461 case MONO_TYPE_I2: \
462 case MONO_TYPE_I4: \
463 case MONO_TYPE_I8: \
464 /* You can't assign a floating point number to an integer array. */ \
465 case MONO_TYPE_R4: \
466 case MONO_TYPE_R8: \
467 NO_WIDENING_CONVERSION; \
468 break; \
469 default: \
470 INVALID_CAST; \
471 break; \
473 }G_STMT_END
475 #define ASSIGN_SIGNED(etype) G_STMT_START{\
476 switch (vt) { \
477 case MONO_TYPE_I1: \
478 case MONO_TYPE_I2: \
479 case MONO_TYPE_I4: \
480 case MONO_TYPE_I8: \
481 CHECK_WIDENING_CONVERSION(0); \
482 *(etype *) ea = (etype) i64; \
483 break; \
484 /* You can assign an unsigned value to a signed array if the array's */ \
485 /* element size is larger than the value size. */ \
486 case MONO_TYPE_U1: \
487 case MONO_TYPE_U2: \
488 case MONO_TYPE_U4: \
489 case MONO_TYPE_U8: \
490 case MONO_TYPE_CHAR: \
491 CHECK_WIDENING_CONVERSION(1); \
492 *(etype *) ea = (etype) u64; \
493 break; \
494 /* You can't assign a floating point number to an integer array. */ \
495 case MONO_TYPE_R4: \
496 case MONO_TYPE_R8: \
497 NO_WIDENING_CONVERSION; \
498 break; \
499 default: \
500 INVALID_CAST; \
501 break; \
503 }G_STMT_END
505 #define ASSIGN_REAL(etype) G_STMT_START{\
506 switch (vt) { \
507 case MONO_TYPE_R4: \
508 case MONO_TYPE_R8: \
509 CHECK_WIDENING_CONVERSION(0); \
510 *(etype *) ea = (etype) r64; \
511 break; \
512 /* All integer values fit into a floating point array, so we don't */ \
513 /* need to CHECK_WIDENING_CONVERSION here. */ \
514 case MONO_TYPE_I1: \
515 case MONO_TYPE_I2: \
516 case MONO_TYPE_I4: \
517 case MONO_TYPE_I8: \
518 *(etype *) ea = (etype) i64; \
519 break; \
520 case MONO_TYPE_U1: \
521 case MONO_TYPE_U2: \
522 case MONO_TYPE_U4: \
523 case MONO_TYPE_U8: \
524 case MONO_TYPE_CHAR: \
525 *(etype *) ea = (etype) u64; \
526 break; \
527 default: \
528 INVALID_CAST; \
529 break; \
531 }G_STMT_END
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);
539 switch (vt) {
540 case MONO_TYPE_U1:
541 u64 = *(guint8 *) va;
542 break;
543 case MONO_TYPE_U2:
544 u64 = *(guint16 *) va;
545 break;
546 case MONO_TYPE_U4:
547 u64 = *(guint32 *) va;
548 break;
549 case MONO_TYPE_U8:
550 u64 = *(guint64 *) va;
551 break;
552 case MONO_TYPE_I1:
553 i64 = *(gint8 *) va;
554 break;
555 case MONO_TYPE_I2:
556 i64 = *(gint16 *) va;
557 break;
558 case MONO_TYPE_I4:
559 i64 = *(gint32 *) va;
560 break;
561 case MONO_TYPE_I8:
562 i64 = *(gint64 *) va;
563 break;
564 case MONO_TYPE_R4:
565 r64 = *(gfloat *) va;
566 break;
567 case MONO_TYPE_R8:
568 r64 = *(gdouble *) va;
569 break;
570 case MONO_TYPE_CHAR:
571 u64 = *(guint16 *) va;
572 break;
573 case MONO_TYPE_BOOLEAN:
574 /* Boolean is only compatible with itself. */
575 switch (et) {
576 case MONO_TYPE_CHAR:
577 case MONO_TYPE_U1:
578 case MONO_TYPE_U2:
579 case MONO_TYPE_U4:
580 case MONO_TYPE_U8:
581 case MONO_TYPE_I1:
582 case MONO_TYPE_I2:
583 case MONO_TYPE_I4:
584 case MONO_TYPE_I8:
585 case MONO_TYPE_R4:
586 case MONO_TYPE_R8:
587 NO_WIDENING_CONVERSION;
588 break;
589 default:
590 INVALID_CAST;
592 break;
594 /* If we can't do a direct copy, let's try a widening conversion. */
596 if (is_ok (error)) {
597 switch (et) {
598 case MONO_TYPE_CHAR:
599 ASSIGN_UNSIGNED (guint16);
600 break;
601 case MONO_TYPE_U1:
602 ASSIGN_UNSIGNED (guint8);
603 break;
604 case MONO_TYPE_U2:
605 ASSIGN_UNSIGNED (guint16);
606 break;
607 case MONO_TYPE_U4:
608 ASSIGN_UNSIGNED (guint32);
609 break;
610 case MONO_TYPE_U8:
611 ASSIGN_UNSIGNED (guint64);
612 break;
613 case MONO_TYPE_I1:
614 ASSIGN_SIGNED (gint8);
615 break;
616 case MONO_TYPE_I2:
617 ASSIGN_SIGNED (gint16);
618 break;
619 case MONO_TYPE_I4:
620 ASSIGN_SIGNED (gint32);
621 break;
622 case MONO_TYPE_I8:
623 ASSIGN_SIGNED (gint64);
624 break;
625 case MONO_TYPE_R4:
626 ASSIGN_REAL (gfloat);
627 break;
628 case MONO_TYPE_R8:
629 ASSIGN_REAL (gdouble);
630 break;
631 default:
632 INVALID_CAST;
636 MONO_EXIT_NO_SAFEPOINTS;
638 #undef INVALID_CAST
639 #undef NO_WIDENING_CONVERSION
640 #undef CHECK_WIDENING_CONVERSION
641 #undef ASSIGN_UNSIGNED
642 #undef ASSIGN_SIGNED
643 #undef ASSIGN_REAL
645 leave:
646 return;
649 void
650 ves_icall_System_Array_SetValue (MonoArrayHandle arr, MonoObjectHandle value,
651 MonoArrayHandle idxs, MonoError *error)
653 icallarray_print ("%s\n", __func__);
655 MonoArrayBounds dim;
656 MonoClass *ac, *ic;
657 gint32 idx;
658 gint32 i, pos;
660 error_init (error);
662 if (MONO_HANDLE_IS_NULL (idxs)) {
663 #ifdef ENABLE_NETCORE
664 mono_error_set_argument_null (error, "indices", "");
665 #else
666 mono_error_set_argument_null (error, "idxs", "");
667 #endif
668 return;
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, "");
678 #else
679 mono_error_set_argument (error, "idxs", "");
680 #endif
681 return;
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 ());
688 return;
691 array_set_value_impl (arr, value, idx, TRUE, error);
692 return;
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 ());
702 return;
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);
718 MonoArrayHandle
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) {
733 gint32 length = 0;
734 MONO_HANDLE_ARRAY_GETVAL (length, lengths, gint32, i);
735 if (length < 0) {
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) {
753 gint32 bound0 = 0;
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);
769 else
770 lower_bounds [i] = 0;
773 return mono_array_new_full_handle (MONO_HANDLE_DOMAIN (type), aklass, sizes, lower_bounds, error);
776 gint32
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);
783 return 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);
791 return 0;
794 return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].length)
795 : MONO_HANDLE_GETVAL (arr, max_length);
798 gint32
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);
806 return 0;
808 return (gint32)length;
811 gint64
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);
819 gint32
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);
826 return 0;
829 return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].lower_bound)
830 : 0;
833 void
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);
842 MonoBoolean
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)
849 return FALSE;
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))
857 return FALSE;
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)))
863 return FALSE;
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))
876 return FALSE;
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))
882 return FALSE;
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))
886 return FALSE;
888 if (m_class_is_native_pointer (src_class) || m_class_is_native_pointer (dest_class))
889 return FALSE;
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);
901 } else {
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;
908 } else {
909 mono_array_handle_memcpy_refs (dest, dest_idx, source, source_idx, length);
912 return TRUE;
915 void
916 ves_icall_System_Array_GetGenericValueImpl (MonoArray *arr, guint32 pos, gpointer value)
918 // FIXME?
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);
932 void
933 ves_icall_System_Array_SetGenericValueImpl (MonoArray *arr, guint32 pos, gpointer value)
935 // FIXME?
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);
952 } else {
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);
957 else
958 mono_gc_memmove_atomic (ea, value, esize);
962 void
963 #if ENABLE_NETCORE
964 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, size_t byte_count)
965 #else
966 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, guint byte_count)
967 #endif
969 mono_gc_memmove_atomic (destination, source, byte_count);
972 #if ENABLE_NETCORE
973 void
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);
978 #else
979 void
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);
984 else
985 mono_gc_wbarrier_value_copy_internal (destination, source, len, mono_class_from_mono_type_internal (type));
987 #endif
989 void
990 #if ENABLE_NETCORE
991 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, size_t byte_length)
992 #else
993 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, guint byte_length)
994 #endif
996 memset (p, 0, byte_length);
999 void
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)));
1005 int align;
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");
1010 return;
1013 MonoType *field_type = mono_field_get_type_checked (field_handle, error);
1014 if (!field_type)
1015 return;
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));
1019 return;
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");
1027 return;
1030 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
1031 #define SWAP(n) { \
1032 guint ## n *data = (guint ## n *) mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0); \
1033 guint ## n *src = (guint ## n *) field_data; \
1034 int i, \
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:
1046 case MONO_TYPE_I2:
1047 case MONO_TYPE_U2:
1048 SWAP (16);
1049 break;
1050 case MONO_TYPE_I4:
1051 case MONO_TYPE_U4:
1052 case MONO_TYPE_R4:
1053 SWAP (32);
1054 break;
1055 case MONO_TYPE_I8:
1056 case MONO_TYPE_U8:
1057 case MONO_TYPE_R8:
1058 SWAP (64);
1059 break;
1060 default:
1061 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
1062 break;
1064 #else
1065 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
1066 #endif
1069 gint
1070 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
1072 return offsetof (MonoString, chars);
1075 MonoObjectHandle
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)))
1079 return obj;
1081 return mono_object_clone_handle (obj, error);
1084 void
1085 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle, MonoError *error)
1087 MonoClass *klass;
1088 MonoVTable *vtable;
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))
1096 return;
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);
1105 void
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)
1110 return;
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);
1121 MonoBoolean
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__)
1127 // No need for now
1128 #else
1129 guint8 *stack_addr;
1130 guint8 *current;
1131 size_t stack_size;
1132 int min_size;
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 */
1137 if (!stack_addr)
1138 return TRUE;
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
1145 if (!min_size)
1146 return TRUE;
1148 current = (guint8 *)&stack_addr;
1149 if (current > stack_addr) {
1150 if ((current - stack_addr) < min_size)
1151 return FALSE;
1152 } else {
1153 if (current - (stack_addr - stack_size) < min_size)
1154 return FALSE;
1156 #endif
1157 return TRUE;
1160 #ifdef ENABLE_NETCORE
1161 MonoObjectHandle
1162 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetUninitializedObjectInternal (MonoType *handle, MonoError *error)
1164 MonoClass *klass;
1166 g_assert (handle);
1168 klass = mono_class_from_mono_type_internal (handle);
1169 if (m_class_is_string (klass)) {
1170 mono_error_set_argument (error, NULL, NULL);
1171 return NULL_HANDLE;
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);
1176 return NULL_HANDLE;
1179 if (m_class_is_byreflike (klass)) {
1180 mono_error_set_not_supported (error, NULL, NULL);
1181 return NULL_HANDLE;
1184 if (m_class_is_nullable (klass))
1185 return mono_object_new_handle (mono_domain_get (), m_class_get_nullable_elem_class (klass), error);
1186 else
1187 return mono_object_new_handle (mono_domain_get (), klass, error);
1190 void
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);
1195 return;
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);
1203 if (container) {
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);
1207 return;
1211 // FIXME: Implement
1213 #endif
1215 MonoObjectHandle
1216 ves_icall_System_Object_MemberwiseClone (MonoObjectHandle this_obj, MonoError *error)
1218 return mono_object_clone_handle (this_obj, error);
1221 gint32
1222 ves_icall_System_ValueType_InternalGetHashCode (MonoObject *this_obj, MonoArray **fields)
1224 ERROR_DECL (error);
1225 MonoClass *klass;
1226 MonoObject **values = NULL;
1227 MonoObject *o;
1228 int count = 0;
1229 gint32 result = (int)(gsize)mono_defaults.int32_class;
1230 MonoClassField* field;
1231 gpointer iter;
1233 klass = mono_object_class (this_obj);
1235 if (mono_class_num_fields (klass) == 0)
1236 return result;
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.
1243 iter = NULL;
1244 while ((field = mono_class_get_fields_internal (klass, &iter))) {
1245 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1246 continue;
1247 if (mono_field_is_deleted (field))
1248 continue;
1249 /* FIXME: Add more types */
1250 switch (field->type->type) {
1251 case MONO_TYPE_I4:
1252 result ^= *(gint32*)((guint8*)this_obj + field->offset);
1253 break;
1254 case MONO_TYPE_PTR:
1255 result ^= mono_aligned_addr_hash (*(gpointer*)((guint8*)this_obj + field->offset));
1256 break;
1257 case MONO_TYPE_STRING: {
1258 MonoString *s;
1259 s = *(MonoString**)((guint8*)this_obj + field->offset);
1260 if (s != NULL)
1261 result ^= mono_string_hash_internal (s);
1262 break;
1264 default:
1265 if (!values)
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);
1270 return 0;
1272 values [count++] = o;
1276 if (values) {
1277 int i;
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))
1280 return 0;
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]);
1284 } else {
1285 *fields = NULL;
1287 return result;
1290 MonoBoolean
1291 ves_icall_System_ValueType_Equals (MonoObject *this_obj, MonoObject *that, MonoArray **fields)
1293 ERROR_DECL (error);
1294 MonoClass *klass;
1295 MonoObject **values = NULL;
1296 MonoObject *o;
1297 MonoClassField* field;
1298 gpointer iter;
1299 int count = 0;
1301 *fields = NULL;
1303 MONO_CHECK_ARG_NULL (that, FALSE);
1305 if (this_obj->vtable != that->vtable)
1306 return FALSE;
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
1317 * managed code.
1319 iter = NULL;
1320 while ((field = mono_class_get_fields_internal (klass, &iter))) {
1321 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1322 continue;
1323 if (mono_field_is_deleted (field))
1324 continue;
1325 guint8 *this_field = (guint8 *) this_obj + field->offset;
1326 guint8 *that_field = (guint8 *) that + field->offset;
1328 #define UNALIGNED_COMPARE(type) \
1329 do { \
1330 type left, right; \
1331 memcpy (&left, this_field, sizeof (type)); \
1332 memcpy (&right, that_field, sizeof (type)); \
1333 if (left != right) \
1334 return FALSE; \
1335 } while (0)
1337 /* FIXME: Add more types */
1338 switch (field->type->type) {
1339 case MONO_TYPE_U1:
1340 case MONO_TYPE_I1:
1341 case MONO_TYPE_BOOLEAN:
1342 if (*this_field != *that_field)
1343 return FALSE;
1344 break;
1345 case MONO_TYPE_U2:
1346 case MONO_TYPE_I2:
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);
1351 else
1352 #endif
1353 if (*(gint16 *) this_field != *(gint16 *) that_field)
1354 return FALSE;
1355 break;
1356 case MONO_TYPE_U4:
1357 case MONO_TYPE_I4:
1358 #ifdef NO_UNALIGNED_ACCESS
1359 if (G_UNLIKELY ((intptr_t) this_field & 3 || (intptr_t) that_field & 3))
1360 UNALIGNED_COMPARE (gint32);
1361 else
1362 #endif
1363 if (*(gint32 *) this_field != *(gint32 *) that_field)
1364 return FALSE;
1365 break;
1366 case MONO_TYPE_U8:
1367 case MONO_TYPE_I8:
1368 #ifdef NO_UNALIGNED_ACCESS
1369 if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
1370 UNALIGNED_COMPARE (gint64);
1371 else
1372 #endif
1373 if (*(gint64 *) this_field != *(gint64 *) that_field)
1374 return FALSE;
1375 break;
1377 case MONO_TYPE_R4: {
1378 float d1, d2;
1379 #ifdef NO_UNALIGNED_ACCESS
1380 memcpy (&d1, this_field, sizeof (float));
1381 memcpy (&d2, that_field, sizeof (float));
1382 #else
1383 d1 = *(float *) this_field;
1384 d2 = *(float *) that_field;
1385 #endif
1386 if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
1387 return FALSE;
1388 break;
1390 case MONO_TYPE_R8: {
1391 double d1, d2;
1392 #ifdef NO_UNALIGNED_ACCESS
1393 memcpy (&d1, this_field, sizeof (double));
1394 memcpy (&d2, that_field, sizeof (double));
1395 #else
1396 d1 = *(double *) this_field;
1397 d2 = *(double *) that_field;
1398 #endif
1399 if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
1400 return FALSE;
1401 break;
1403 case MONO_TYPE_PTR:
1404 #ifdef NO_UNALIGNED_ACCESS
1405 if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
1406 UNALIGNED_COMPARE (gpointer);
1407 else
1408 #endif
1409 if (*(gpointer *) this_field != *(gpointer *) that_field)
1410 return FALSE;
1411 break;
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);
1417 if (s1 == s2)
1418 break;
1419 if ((s1 == NULL) || (s2 == NULL))
1420 return FALSE;
1421 s1len = mono_string_length_internal (s1);
1422 s2len = mono_string_length_internal (s2);
1423 if (s1len != s2len)
1424 return FALSE;
1426 if (memcmp (mono_string_chars_internal (s1), mono_string_chars_internal (s2), s1len * sizeof (gunichar2)) != 0)
1427 return FALSE;
1428 break;
1430 default:
1431 if (!values)
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);
1436 return FALSE;
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);
1442 return FALSE;
1444 values [count++] = o;
1447 #undef UNALIGNED_COMPARE
1449 if (m_class_is_enumtype (klass))
1450 /* enums only have one non-static field */
1451 break;
1454 if (values) {
1455 int i;
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))
1458 return FALSE;
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]);
1462 return FALSE;
1463 } else {
1464 return TRUE;
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);
1485 } else
1486 #endif
1487 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
1490 static gboolean
1491 get_executing (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1493 MonoMethod **dest = (MonoMethod **)data;
1495 /* skip unmanaged frames */
1496 if (!managed)
1497 return FALSE;
1499 if (!(*dest)) {
1500 if (!strcmp (m_class_get_name_space (m->klass), "System.Reflection"))
1501 return FALSE;
1502 *dest = m;
1503 return TRUE;
1505 return FALSE;
1508 static gboolean
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);
1515 static gboolean
1516 get_caller_no_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1518 MonoMethod **dest = (MonoMethod **)data;
1520 /* skip unmanaged frames */
1521 if (!managed)
1522 return FALSE;
1524 if (m->wrapper_type != MONO_WRAPPER_NONE)
1525 return FALSE;
1527 if (m == *dest) {
1528 *dest = NULL;
1529 return FALSE;
1532 if (in_corlib_name_space (m->klass, "System.Reflection"))
1533 return FALSE;
1535 if (!(*dest)) {
1536 *dest = m;
1537 return TRUE;
1539 return FALSE;
1542 static gboolean
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 */
1548 if (!managed)
1549 return FALSE;
1551 if (m->wrapper_type != MONO_WRAPPER_NONE)
1552 return FALSE;
1554 if (m == *dest) {
1555 *dest = NULL;
1556 return FALSE;
1559 if (in_corlib_name_space (m->klass, "System.Reflection") || in_corlib_name_space (m->klass, "System"))
1560 return FALSE;
1562 if (!(*dest)) {
1563 *dest = m;
1564 return TRUE;
1566 return FALSE;
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
1577 * callers.
1579 MonoMethod*
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);
1584 return 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.
1593 MonoAssembly*
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);
1599 if (dest)
1600 return m_class_get_image (dest->klass)->assembly;
1601 else
1602 return NULL;
1605 static MonoReflectionTypeHandle
1606 type_from_parsed_name (MonoTypeNameParse *info, MonoStackCrawlMark *stack_mark, MonoBoolean ignoreCase, MonoAssembly **caller_assembly, MonoError *error)
1608 MonoMethod *m;
1609 MonoType *type = NULL;
1610 MonoAssembly *assembly = NULL;
1611 gboolean type_resolve = FALSE;
1612 MonoImage *rootimage = NULL;
1614 error_init (error);
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;
1625 } else {
1626 assembly = mono_runtime_get_caller_from_stack_mark (stack_mark);
1628 if (assembly) {
1629 type_resolve = TRUE;
1630 rootimage = assembly->image;
1631 } else {
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);
1639 if (assembly) {
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);
1645 // XXXX - aleksey -
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) {
1654 /* try mscorlib */
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);
1664 if (!type)
1665 goto fail;
1667 return mono_type_get_object_handle (mono_domain_get (), type, error);
1668 fail:
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,
1679 MonoError *error)
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);
1691 free_info = TRUE;
1692 if (!mono_reflection_parse_type_checked (str, &info, error))
1693 goto leave;
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)) {
1702 if (throwOnError) {
1703 char *tname = info.name_space ? g_strdup_printf ("%s.%s", info.name_space, info.name) : g_strdup (info.name);
1704 char *aname;
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));
1709 else
1710 aname = g_strdup ("");
1711 mono_error_set_type_load_name (error, tname, aname, "");
1713 goto leave;
1716 leave:
1717 if (free_info)
1718 mono_reflection_free_type_info (&info);
1719 g_free (str);
1720 if (!is_ok (error)) {
1721 if (!throwOnError) {
1722 mono_error_cleanup (error);
1723 error_init (error);
1725 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
1726 } else
1727 return type;
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);
1739 MonoType*
1740 ves_icall_Mono_RuntimeClassHandle_GetTypeFromClass (MonoClass *klass, MonoError *error)
1742 return m_class_get_byval_arg (klass);
1745 void
1746 ves_icall_Mono_RuntimeGPtrArrayHandle_GPtrArrayFree (GPtrArray *ptr_array, MonoError *error)
1748 g_ptr_array_free (ptr_array, TRUE);
1751 void
1752 ves_icall_Mono_SafeStringMarshal_GFree (void *c_str, MonoError *error)
1754 g_free (c_str);
1757 char*
1758 ves_icall_Mono_SafeStringMarshal_StringToUtf8 (MonoStringHandle s, MonoError *error)
1760 return mono_string_handle_to_utf8 (s, error);
1763 /* System.TypeCode */
1764 typedef enum {
1765 TYPECODE_EMPTY,
1766 TYPECODE_OBJECT,
1767 TYPECODE_DBNULL,
1768 TYPECODE_BOOLEAN,
1769 TYPECODE_CHAR,
1770 TYPECODE_SBYTE,
1771 TYPECODE_BYTE,
1772 TYPECODE_INT16,
1773 TYPECODE_UINT16,
1774 TYPECODE_INT32,
1775 TYPECODE_UINT32,
1776 TYPECODE_INT64,
1777 TYPECODE_UINT64,
1778 TYPECODE_SINGLE,
1779 TYPECODE_DOUBLE,
1780 TYPECODE_DECIMAL,
1781 TYPECODE_DATETIME,
1782 TYPECODE_STRING = 18
1783 } TypeCode;
1785 guint32
1786 ves_icall_type_GetTypeCodeInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
1788 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
1789 int t = type->type;
1791 if (type->byref)
1792 return TYPECODE_OBJECT;
1794 handle_enum:
1795 switch (t) {
1796 case MONO_TYPE_VOID:
1797 return TYPECODE_OBJECT;
1798 case MONO_TYPE_BOOLEAN:
1799 return TYPECODE_BOOLEAN;
1800 case MONO_TYPE_U1:
1801 return TYPECODE_BYTE;
1802 case MONO_TYPE_I1:
1803 return TYPECODE_SBYTE;
1804 case MONO_TYPE_U2:
1805 return TYPECODE_UINT16;
1806 case MONO_TYPE_I2:
1807 return TYPECODE_INT16;
1808 case MONO_TYPE_CHAR:
1809 return TYPECODE_CHAR;
1810 case MONO_TYPE_PTR:
1811 case MONO_TYPE_U:
1812 case MONO_TYPE_I:
1813 return TYPECODE_OBJECT;
1814 case MONO_TYPE_U4:
1815 return TYPECODE_UINT32;
1816 case MONO_TYPE_I4:
1817 return TYPECODE_INT32;
1818 case MONO_TYPE_U8:
1819 return TYPECODE_UINT64;
1820 case MONO_TYPE_I8:
1821 return TYPECODE_INT64;
1822 case MONO_TYPE_R4:
1823 return TYPECODE_SINGLE;
1824 case MONO_TYPE_R8:
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;
1831 goto handle_enum;
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:
1847 case MONO_TYPE_VAR:
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;
1862 default:
1863 g_error ("type 0x%02x not handled in GetTypeCode()", t);
1865 return 0;
1868 static MonoType*
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);
1875 return type;
1878 guint32
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)
1889 return FALSE;
1891 if (type->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;
1904 } else {
1905 if (ot->type == MONO_TYPE_VAR || ot->type == MONO_TYPE_MVAR)
1906 return FALSE;
1908 if (m_class_is_valuetype (klass))
1909 return klass == klassc;
1910 return m_class_is_valuetype (klass) == m_class_is_valuetype (klassc);
1914 gboolean result;
1915 mono_class_is_assignable_from_checked (klass, klassc, &result, error);
1916 return (guint32)result;
1919 MonoBoolean
1920 ves_icall_RuntimeTypeHandle_is_subclass_of (MonoType *childType, MonoType *baseType)
1922 ERROR_DECL (error);
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;
1932 goto done;
1935 if (G_UNLIKELY (baseType->byref)) {
1936 result = FALSE;
1937 goto done;
1940 if (childType == baseType) {
1941 /* .NET IsSubclassOf is not reflexive */
1942 result = FALSE;
1943 goto done;
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);
1953 result = FALSE;
1954 while (c != NULL) {
1955 if (c == baseClass) {
1956 result = TRUE;
1957 break;
1959 if (!is_generic_parameter (m_class_get_byval_arg (c))) {
1960 result = mono_class_is_subclass_of_internal (c, baseClass, FALSE);
1961 break;
1962 } else
1963 c = mono_generic_param_get_base_type (c);
1965 } else {
1966 result = mono_class_is_subclass_of_internal (childClass, baseClass, FALSE);
1968 done:
1969 mono_error_set_pending_exception (error);
1970 return result;
1973 guint32
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);
1985 guint32
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;
1993 #endif
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);
2021 else {
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)
2033 MonoClass *klass;
2035 g_assert (handle);
2037 if (!type) {
2038 klass = handle->parent;
2039 } else {
2040 klass = mono_class_from_mono_type_internal (type);
2042 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
2044 if (!found)
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)
2055 MonoClass *klass;
2057 g_assert (handle);
2059 if (!type) {
2060 klass = handle->parent;
2061 } else {
2062 klass = mono_class_from_mono_type_internal (type);
2064 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
2065 if (!found)
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)
2076 MonoClass *klass;
2078 g_assert (handle);
2080 if (!type) {
2081 klass = handle->parent;
2082 } else {
2083 klass = mono_class_from_mono_type_internal (type);
2085 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
2086 if (!found)
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);
2094 MonoArrayHandle
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;
2111 void
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;
2131 guint32 callconv;
2132 if (sig->call_convention == MONO_CALL_DEFAULT)
2133 callconv = sig->sentinelpos >= 0 ? 2 : 1;
2134 else {
2135 if (sig->call_convention == MONO_CALL_VARARG || sig->sentinelpos >= 0)
2136 callconv = 2;
2137 else
2138 callconv = 1;
2140 callconv |= (sig->hasthis << 5) | (sig->explicit_this << 6);
2141 info->callconv = callconv;
2144 MonoArrayHandle
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);
2166 if (mspecs [0]) {
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);
2171 leave:
2172 for (int i = mono_method_signature_internal (method)->param_count; i >= 0; i--)
2173 if (mspecs [i])
2174 mono_metadata_free_marshal_spec (mspecs [i]);
2175 g_free (mspecs);
2177 return res;
2180 gint32
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);
2193 MonoClass *parent;
2195 if (declaring) {
2196 MonoClassField *f = MONO_HANDLE_GETVAL (field, field);
2197 parent = f->parent;
2198 } else {
2199 parent = MONO_HANDLE_GETVAL (field, klass);
2202 return mono_type_get_object_handle (domain, m_class_get_byval_arg (parent), error);
2205 MonoObject *
2206 ves_icall_RuntimeFieldInfo_GetValueInternal (MonoReflectionField *field, MonoObject *obj)
2208 ERROR_DECL (error);
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);
2217 return NULL;
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);
2223 return NULL;
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);
2234 return result;
2236 #endif
2238 MonoObject * result = mono_field_get_value_object_checked (domain, cf, obj, error);
2239 mono_error_set_pending_exception (error);
2240 return result;
2243 void
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.");
2251 return;
2254 if (mono_security_core_clr_enabled () &&
2255 !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
2256 return;
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);
2269 return;
2271 #endif
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;
2278 gchar *v = NULL;
2279 if (!type->byref) {
2280 switch (type->type) {
2281 case MONO_TYPE_U1:
2282 case MONO_TYPE_I1:
2283 case MONO_TYPE_BOOLEAN:
2284 case MONO_TYPE_U2:
2285 case MONO_TYPE_I2:
2286 case MONO_TYPE_CHAR:
2287 case MONO_TYPE_U:
2288 case MONO_TYPE_I:
2289 case MONO_TYPE_U4:
2290 case MONO_TYPE_I4:
2291 case MONO_TYPE_R4:
2292 case MONO_TYPE_U8:
2293 case MONO_TYPE_I8:
2294 case MONO_TYPE_R8:
2295 case MONO_TYPE_VALUETYPE:
2296 case MONO_TYPE_PTR:
2297 isref = FALSE;
2298 if (!MONO_HANDLE_IS_NULL (value))
2299 v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
2300 break;
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:
2306 /* Do nothing */
2307 isref = TRUE;
2308 break;
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);
2328 isref = FALSE;
2329 value_gchandle = nullable_gchandle;
2330 v = (gchar*)nval;
2332 else {
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);
2338 break;
2340 default:
2341 g_error ("type 0x%x not handled in "
2342 "ves_icall_FieldInfo_SetValueInternal", type->type);
2343 return;
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))
2359 goto leave;
2361 if (isref)
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 */
2363 else
2364 mono_field_static_set_value_internal (vtable, cf, v);
2365 } else {
2367 if (isref)
2368 MONO_HANDLE_SET_FIELD_REF (obj, cf, value);
2369 else
2370 mono_field_set_value_internal (MONO_HANDLE_RAW (obj), cf, v); /* FIXME: make mono_field_set_value take a handle for obj */
2372 leave:
2373 if (value_gchandle)
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);
2388 } else {
2389 result = mono_value_box_handle (mono_domain_get (), tref->klass, tref->value, error);
2391 HANDLE_FUNCTION_RETURN_REF (MonoObject, result);
2394 MonoObjectHandle
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)));
2405 } else {
2406 return mono_value_box_handle (mono_domain_get (), klass, (guint8*)obj->value + field->offset - sizeof (MonoObject), error);
2410 void
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);
2415 g_assert (obj);
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);
2423 } else {
2424 guint gchandle = 0;
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);
2431 MonoObject *
2432 ves_icall_RuntimeFieldInfo_GetRawConstantValue (MonoReflectionField *rfield)
2434 MonoObject *o = NULL;
2435 MonoClassField *field = rfield->field;
2436 MonoClass *klass;
2437 MonoDomain *domain = mono_object_domain (rfield);
2438 gchar *v;
2439 MonoTypeEnum def_type;
2440 const char *def_value;
2441 MonoType *t;
2442 ERROR_DECL (error);
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);
2449 return NULL;
2452 if (!(t->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT)) {
2453 mono_error_set_invalid_operation (error, NULL);
2454 mono_error_set_pending_exception (error);
2455 return NULL;
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);
2470 return NULL;
2472 } else {
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 */
2475 if (!def_value) {
2476 mono_error_set_invalid_operation (error, NULL);
2477 mono_error_set_pending_exception (error);
2478 return NULL;
2482 /*FIXME unify this with reflection.c:mono_get_object_from_blob*/
2483 switch (def_type) {
2484 case MONO_TYPE_U1:
2485 case MONO_TYPE_I1:
2486 case MONO_TYPE_BOOLEAN:
2487 case MONO_TYPE_U2:
2488 case MONO_TYPE_I2:
2489 case MONO_TYPE_CHAR:
2490 case MONO_TYPE_U:
2491 case MONO_TYPE_I:
2492 case MONO_TYPE_U4:
2493 case MONO_TYPE_I4:
2494 case MONO_TYPE_R4:
2495 case MONO_TYPE_U8:
2496 case MONO_TYPE_I8:
2497 case MONO_TYPE_R8: {
2498 MonoType *t;
2500 /* boxed value type */
2501 t = g_new0 (MonoType, 1);
2502 t->type = def_type;
2503 klass = mono_class_from_mono_type_internal (t);
2504 g_free (t);
2505 o = mono_object_new_checked (domain, klass, error);
2506 if (!is_ok (error)) {
2507 mono_error_set_pending_exception (error);
2508 return NULL;
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))
2513 return NULL;
2514 break;
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))
2520 return NULL;
2521 break;
2522 default:
2523 g_assert_not_reached ();
2526 return o;
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);
2539 void
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);
2577 } else {
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);
2591 } else {
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
2603 static gboolean
2604 add_event_other_methods_to_array (MonoDomain *domain, MonoMethod *m, MonoArrayHandle dest, int i, MonoError *error)
2606 HANDLE_FUNCTION_ENTER ();
2607 error_init (error);
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);
2611 leave:
2612 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
2615 void
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;
2638 if (event->add) {
2639 rm = mono_method_get_object_handle (domain, event->add, klass, error);
2640 return_if_nok (error);
2641 } else {
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);
2650 } else {
2651 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2654 MONO_STRUCT_SETREF_INTERNAL (info, remove_method, MONO_HANDLE_RAW (rm));
2656 if (event->raise) {
2657 rm = mono_method_get_object_handle (domain, event->raise, klass, error);
2658 return_if_nok (error);
2659 } else {
2660 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2663 MONO_STRUCT_SETREF_INTERNAL (info, raise_method, MONO_HANDLE_RAW (rm));
2665 #ifndef MONO_SMALL_CONFIG
2666 if (event->other) {
2667 int i, n = 0;
2668 while (event->other [n])
2669 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))
2677 return;
2679 #endif
2682 static void
2683 collect_interfaces (MonoClass *klass, GHashTable *ifaces, MonoError *error)
2685 int i;
2686 MonoClass *ic;
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);
2702 typedef struct {
2703 MonoArrayHandle iface_array;
2704 MonoGenericContext *context;
2705 MonoError *error;
2706 MonoDomain *domain;
2707 int next_idx;
2708 } FillIfaceArrayData;
2710 static void
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);
2731 data->next_idx++;
2733 if (inflated)
2734 mono_metadata_free_type (inflated);
2735 leave:
2736 HANDLE_FUNCTION_RETURN ();
2739 static guint
2740 get_interfaces_hash (gconstpointer v1)
2742 MonoClass *k = (MonoClass*)v1;
2744 return m_class_get_type_token (k);
2747 MonoArrayHandle
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);
2768 MonoDomain *domain;
2769 domain = MONO_HANDLE_DOMAIN (ref_type);
2771 int len;
2772 len = g_hash_table_size (iface_hash);
2773 if (len == 0) {
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;
2786 data.error = error;
2787 data.domain = domain;
2788 data.next_idx = 0;
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;
2797 fail:
2798 g_hash_table_destroy (iface_hash);
2799 return NULL_HANDLE_ARRAY;
2802 static gboolean
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 ();
2806 error_init (error);
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);
2817 leave:
2818 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
2821 void
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);
2838 if (ioffset == -1)
2839 return;
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);
2851 MonoMethod* method;
2852 int i = 0;
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))
2856 return;
2857 i ++;
2861 void
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);
2874 } else {
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
2895 if (type->byref)
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);
2901 else
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);
2911 if (type->byref)
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);
2921 guint32
2922 ves_icall_RuntimeTypeHandle_GetCorElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
2924 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2926 if (type->byref)
2927 return MONO_TYPE_BYREF;
2928 else
2929 return (guint32)type->type;
2932 MonoBoolean
2933 ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type, MonoError *error)
2935 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2936 MonoClass *klass;
2938 klass = mono_class_from_mono_type_internal (type);
2939 mono_class_init_internal (klass);
2940 return m_class_has_references (klass);
2943 MonoBoolean
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 */
2948 if (type->byref)
2949 return FALSE;
2950 MonoClass *klass = mono_class_from_mono_type_internal (type);
2951 return m_class_is_byreflike (klass);
2954 MonoBoolean
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);
2965 guint32
2966 ves_icall_reflection_get_token (MonoObjectHandle obj, MonoError *error)
2968 error_init (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);
2995 MonoClass *klass;
2997 if (type->byref)
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;
3005 } else {
3006 klass = m_class_get_nested_in (mono_class_from_mono_type_internal (type));
3009 if (!klass)
3010 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3012 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
3015 MonoStringHandle
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);
3022 if (type->byref) {
3023 char *n = g_strdup_printf ("%s&", m_class_get_name (klass));
3024 MonoStringHandle res = mono_string_new_handle (domain, n, error);
3026 g_free (n);
3028 return res;
3029 } else {
3030 return mono_string_new_handle (domain, m_class_get_name (klass), error);
3034 MonoStringHandle
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;
3046 else
3047 return mono_string_new_handle (domain, m_class_get_name_space (klass), error);
3050 gint32
3051 ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionTypeHandle ref_type, MonoError *error)
3053 error_init (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");
3058 return 0;
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);
3072 static gboolean
3073 set_type_object_in_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int i, MonoError *error)
3075 HANDLE_FUNCTION_ENTER();
3076 error_init (error);
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);
3082 leave:
3083 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3086 MonoArrayHandle
3087 ves_icall_RuntimeType_GetGenericArguments (MonoReflectionTypeHandle ref_type, MonoBoolean runtimeTypeArray, MonoError *error)
3089 error_init (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))
3104 goto leave;
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))
3113 goto leave;
3117 leave:
3118 return res;
3121 MonoBoolean
3122 ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle ref_type, MonoError *error)
3124 error_init (error);
3126 if (!IS_MONOTYPE (MONO_HANDLE_RAW(ref_type)))
3127 return FALSE;
3129 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3130 if (type->byref)
3131 return FALSE;
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)
3140 error_init (error);
3141 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3143 MonoReflectionTypeHandle ret = MONO_HANDLE_NEW (MonoReflectionType, NULL);
3145 if (type->byref)
3146 goto leave;
3148 MonoClass *klass;
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);
3154 goto leave;
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);
3165 } else {
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));
3170 leave:
3171 return ret;
3174 MonoReflectionTypeHandle
3175 ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype, MonoArrayHandle type_array, MonoError *error)
3177 error_init (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);
3195 g_free (types);
3196 if (!geninst) {
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);
3211 MonoBoolean
3212 ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type, MonoError *error)
3214 error_init (error);
3215 MonoClass *klass;
3217 if (!IS_MONOTYPE (MONO_HANDLE_RAW (ref_type)))
3218 return FALSE;
3220 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3221 if (type->byref)
3222 return FALSE;
3224 klass = mono_class_from_mono_type_internal (type);
3225 return mono_class_is_ginst (klass) || mono_class_is_gtd (klass);
3228 gint32
3229 ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_type, MonoError *error)
3231 error_init (error);
3232 if (!IS_MONOTYPE_HANDLE (ref_type))
3233 return -1;
3234 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3236 if (is_generic_parameter (type))
3237 return mono_type_get_generic_param_num (type);
3238 return -1;
3241 MonoGenericParamInfo *
3242 ves_icall_RuntimeTypeHandle_GetGenericParameterInfo (MonoReflectionTypeHandle ref_type, MonoError *error)
3244 error_init (error);
3245 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3246 return mono_generic_param_info (type->data.generic_param);
3249 MonoBoolean
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,
3259 MonoError *error)
3261 error_init (error);
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);
3272 MonoMethod *method;
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));
3281 return ret;
3284 MonoReflectionMethodHandle
3285 ves_icall_RuntimeType_get_DeclaringMethod (MonoReflectionTypeHandle ref_type, MonoError *error)
3287 error_init (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");
3293 goto leave;
3295 if (type->type == MONO_TYPE_VAR)
3296 goto leave;
3298 MonoMethod *method;
3299 method = mono_type_get_generic_param_owner (type)->owner.method;
3300 g_assert (method);
3302 MonoDomain *domain;
3303 domain = MONO_HANDLE_DOMAIN (ref_type);
3305 MONO_HANDLE_ASSIGN (ret, mono_method_get_object_handle (domain, method, method->klass, error));
3306 leave:
3307 return ret;
3310 MonoBoolean
3311 ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError *error)
3313 error_init (error);
3314 mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsTypeExportedToWindowsRuntime");
3315 return FALSE;
3318 MonoBoolean
3319 ves_icall_System_RuntimeType_IsWindowsRuntimeObjectType (MonoError *error)
3321 error_init (error);
3322 mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsWindowsRuntimeObjectType");
3323 return FALSE;
3326 void
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;
3341 error_init (error);
3343 if (image_is_dynamic (image)) {
3344 MonoReflectionMethodAux *method_aux =
3345 (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)image)->method_aux_hash, method);
3346 if (method_aux) {
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");
3353 return;
3356 else {
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)
3376 error_init (error);
3377 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3379 if (method->is_generic)
3380 return ref_method;
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))
3404 return 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);
3419 MonoBoolean
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;
3426 MonoBoolean
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;
3433 static gboolean
3434 set_array_generic_argument_handle_inflated (MonoDomain *domain, MonoGenericInst *inst, int i, MonoArrayHandle arr, MonoError *error)
3436 HANDLE_FUNCTION_ENTER ();
3437 error_init (error);
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);
3441 leave:
3442 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3445 static gboolean
3446 set_array_generic_argument_handle_gparam (MonoDomain *domain, MonoGenericContainer *container, int i, MonoArrayHandle arr, MonoError *error)
3448 HANDLE_FUNCTION_ENTER ();
3449 error_init (error);
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);
3455 leave:
3456 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3459 MonoArrayHandle
3460 ves_icall_RuntimeMethodInfo_GetGenericArguments (MonoReflectionMethodHandle ref_method, MonoError *error)
3462 error_init (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;
3469 if (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))
3476 break;
3478 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3479 return res;
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))
3490 break;
3492 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3493 return res;
3496 MonoObject *
3497 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this_arg, MonoArray *params, MonoException **exc)
3499 ERROR_DECL (error);
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);
3507 MonoImage *image;
3508 int pcount;
3509 void *obj = this_arg;
3511 *exc = NULL;
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);
3516 return NULL;
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));
3523 return NULL;
3526 if (this_arg) {
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));
3530 return NULL;
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));
3536 g_free (msg);
3537 g_free (target_name);
3538 g_free (this_name);
3539 return NULL;
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."));
3547 return NULL;
3551 if (sig->ret->byref) {
3552 #if ENABLE_NETCORE
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"));
3556 return NULL;
3558 #else
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"));
3560 return NULL;
3561 #endif
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"));
3567 return NULL;
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."));
3572 return NULL;
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."));
3578 return NULL;
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."));
3583 return NULL;
3586 if (m_class_get_rank (m->klass) && !strcmp (m->name, ".ctor")) {
3587 MonoArray *arr;
3588 int i;
3589 uintptr_t *lengths;
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);
3602 return NULL;
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);
3609 return NULL;
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);
3621 return NULL;
3624 return (MonoObject*)arr;
3625 } else {
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);
3638 return NULL;
3641 return (MonoObject*)arr;
3644 MonoObject *result = mono_runtime_invoke_array_checked (m, obj, params, error);
3645 mono_error_set_pending_exception (error);
3646 return result;
3649 #ifndef DISABLE_REMOTING
3650 static void
3651 internal_execute_field_getter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs, MonoError *error)
3653 error_init (error);
3654 MonoArray *out_args;
3655 MonoClass *k = mono_object_class (this_arg);
3656 MonoString *name;
3657 char *str;
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);
3671 do {
3672 MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
3673 if (field) {
3674 g_free (str);
3675 MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
3676 MonoObject *result;
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);
3680 } else
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);
3687 return;
3689 k = m_class_get_parent (k);
3690 } while (k);
3692 g_free (str);
3693 g_assert_not_reached ();
3696 static void
3697 internal_execute_field_setter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs, MonoError *error)
3699 error_init (error);
3700 MonoArray *out_args;
3701 MonoClass *k = mono_object_class (this_arg);
3702 MonoString *name;
3703 guint32 size;
3704 gint32 align;
3705 char *str;
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);
3719 do {
3720 MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
3721 if (field) {
3722 g_free (str);
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);
3730 } else {
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);
3737 return;
3740 k = m_class_get_parent (k);
3741 } while (k);
3743 g_free (str);
3744 g_assert_not_reached ();
3747 MonoObject *
3748 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs)
3750 ERROR_DECL (error);
3751 MonoDomain *domain = mono_object_domain (method);
3752 MonoMethod *m = method->method;
3753 MonoMethodSignature *sig = mono_method_signature_internal (m);
3754 MonoArray *out_args;
3755 MonoObject *result;
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);
3762 return NULL;
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);
3766 return NULL;
3770 for (i = 0; i < mono_array_length_internal (params); i++) {
3771 if (sig->params [i]->byref)
3772 outarg_count++;
3775 out_args = mono_array_new_checked (domain, mono_defaults.object_class, outarg_count, error);
3776 if (mono_error_set_pending_exception (error))
3777 return NULL;
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))
3787 return NULL;
3789 for (i = 0, j = 0; i < mono_array_length_internal (params); i++) {
3790 if (sig->params [i]->byref) {
3791 gpointer arg;
3792 arg = mono_array_get_internal (params, gpointer, i);
3793 mono_array_setref_internal (out_args, j, arg);
3794 j++;
3798 mono_gc_wbarrier_generic_store_internal (outArgs, (MonoObject*) out_args);
3800 return result;
3802 #endif
3804 static guint64
3805 read_enum_value (const char *mem, int type)
3807 switch (type) {
3808 case MONO_TYPE_BOOLEAN:
3809 case MONO_TYPE_U1:
3810 return *(guint8*)mem;
3811 case MONO_TYPE_I1:
3812 return *(gint8*)mem;
3813 case MONO_TYPE_CHAR:
3814 case MONO_TYPE_U2:
3815 return read16 (mem);
3816 case MONO_TYPE_I2:
3817 return (gint16) read16 (mem);
3818 case MONO_TYPE_U4:
3819 case MONO_TYPE_R4:
3820 return read32 (mem);
3821 case MONO_TYPE_I4:
3822 return (gint32) read32 (mem);
3823 case MONO_TYPE_U8:
3824 case MONO_TYPE_I8:
3825 case MONO_TYPE_R8:
3826 return read64 (mem);
3827 case MONO_TYPE_U:
3828 case MONO_TYPE_I:
3829 #if SIZEOF_REGISTER == 8
3830 return read64 (mem);
3831 #else
3832 return read32 (mem);
3833 #endif
3834 default:
3835 g_assert_not_reached ();
3837 return 0;
3840 static void
3841 write_enum_value (void *mem, int type, guint64 value)
3843 switch (type) {
3844 case MONO_TYPE_U1:
3845 case MONO_TYPE_I1:
3846 case MONO_TYPE_BOOLEAN: {
3847 guint8 *p = (guint8*)mem;
3848 *p = value;
3849 break;
3851 case MONO_TYPE_U2:
3852 case MONO_TYPE_I2:
3853 case MONO_TYPE_CHAR: {
3854 guint16 *p = (guint16 *)mem;
3855 *p = value;
3856 break;
3858 case MONO_TYPE_U4:
3859 case MONO_TYPE_I4:
3860 case MONO_TYPE_R4: {
3861 guint32 *p = (guint32 *)mem;
3862 *p = value;
3863 break;
3865 case MONO_TYPE_U8:
3866 case MONO_TYPE_I8:
3867 case MONO_TYPE_R8: {
3868 guint64 *p = (guint64 *)mem;
3869 *p = value;
3870 break;
3872 case MONO_TYPE_U:
3873 case MONO_TYPE_I: {
3874 #if SIZEOF_REGISTER == 8
3875 guint64 *p = (guint64 *)mem;
3876 *p = value;
3877 #else
3878 guint32 *p = (guint32 *)mem;
3879 *p = value;
3880 break;
3881 #endif
3882 break;
3884 default:
3885 g_assert_not_reached ();
3887 return;
3890 MonoObjectHandle
3891 ves_icall_System_Enum_ToObject (MonoReflectionTypeHandle enumType, guint64 value, MonoError *error)
3893 MonoDomain *domain;
3894 MonoClass *enumc;
3895 MonoObjectHandle resultHandle;
3896 MonoType *etype;
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;
3913 return_null:
3914 return MONO_HANDLE_NEW (MonoObject, NULL);
3917 MonoBoolean
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;
3929 MonoObjectHandle
3930 ves_icall_System_Enum_get_value (MonoObjectHandle ehandle, MonoError *error)
3932 MonoObjectHandle resultHandle;
3933 MonoClass *enumc;
3934 int size;
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;
3949 return_null:
3950 return MONO_HANDLE_NEW (MonoObject, NULL);
3953 MonoReflectionTypeHandle
3954 ves_icall_System_Enum_get_underlying_type (MonoReflectionTypeHandle type, MonoError *error)
3956 MonoType *etype;
3957 MonoClass *klass;
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);
3964 if (!etype) {
3965 mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
3966 goto return_null;
3969 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (type), etype, error);
3971 return_null:
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))
3987 return 1;
3989 if (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass != MONO_HANDLE_GETVAL (otherHandle, vtable)->klass)
3990 return 2;
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); \
4000 if (me == other) \
4001 return 0; \
4002 return me > other ? 1 : -1; \
4003 } while (0)
4005 switch (basetype->type) {
4006 case MONO_TYPE_BOOLEAN:
4007 case MONO_TYPE_U1:
4008 COMPARE_ENUM_VALUES (guint8);
4009 case MONO_TYPE_I1:
4010 COMPARE_ENUM_VALUES (gint8);
4011 case MONO_TYPE_CHAR:
4012 case MONO_TYPE_U2:
4013 COMPARE_ENUM_VALUES (guint16);
4014 case MONO_TYPE_I2:
4015 COMPARE_ENUM_VALUES (gint16);
4016 case MONO_TYPE_U4:
4017 COMPARE_ENUM_VALUES (guint32);
4018 case MONO_TYPE_I4:
4019 COMPARE_ENUM_VALUES (gint32);
4020 case MONO_TYPE_R4:
4021 COMPARE_ENUM_VALUES (gfloat);
4022 case MONO_TYPE_U8:
4023 COMPARE_ENUM_VALUES (guint64);
4024 case MONO_TYPE_I8:
4025 COMPARE_ENUM_VALUES (gint64);
4026 case MONO_TYPE_R8:
4027 COMPARE_ENUM_VALUES (gdouble);
4028 case MONO_TYPE_U:
4029 #if SIZEOF_REGISTER == 8
4030 COMPARE_ENUM_VALUES (guint64);
4031 #else
4032 COMPARE_ENUM_VALUES (guint32);
4033 #endif
4034 case MONO_TYPE_I:
4035 #if SIZEOF_REGISTER == 8
4036 COMPARE_ENUM_VALUES (gint64);
4037 #else
4038 COMPARE_ENUM_VALUES (gint32);
4039 #endif
4041 #undef COMPARE_ENUM_VALUES
4042 /* indicates that the enum was of an unsupported underlying type */
4043 return 3;
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);
4058 case MONO_TYPE_U1:
4059 return *((guint8*)data);
4060 case MONO_TYPE_CHAR:
4061 case MONO_TYPE_U2:
4062 return *((guint16*)data);
4064 case MONO_TYPE_I2: {
4065 gint16 value = *((gint16*)data);
4066 return ((int)(guint16)value | (((int)value) << 16));
4068 case MONO_TYPE_U4:
4069 case MONO_TYPE_R4:
4070 return *((guint32*)data);
4071 case MONO_TYPE_I4:
4072 return *((gint32*)data);
4073 case MONO_TYPE_U8:
4074 case MONO_TYPE_I8:
4075 case MONO_TYPE_R8: {
4076 gint64 value = *((gint64*)data);
4077 return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
4079 case MONO_TYPE_I:
4080 case MONO_TYPE_U: {
4081 #if SIZEOF_REGISTER == 8
4082 gint64 value = *((gint64*)data);
4083 return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
4084 #else
4085 return *((guint32*)data);
4086 #endif
4088 default:
4089 g_error ("Implement type 0x%02x in get_hashcode", basetype->type);
4091 return 0;
4094 static void
4095 get_enum_field (MonoDomain *domain, MonoArrayHandle names, MonoArrayHandle values, int base_type, MonoClassField *field, guint* j, guint64 *previous_value, gboolean *sorted, MonoError *error)
4097 error_init (error);
4098 HANDLE_FUNCTION_ENTER();
4099 guint64 field_value;
4100 const char *p;
4101 MonoTypeEnum def_type;
4103 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
4104 goto leave;
4105 if (strcmp ("value__", mono_field_get_name (field)) == 0)
4106 goto leave;
4107 if (mono_field_is_deleted (field))
4108 goto leave;
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)
4121 *sorted = FALSE;
4123 *previous_value = field_value;
4124 (*j)++;
4125 leave:
4126 HANDLE_FUNCTION_RETURN();
4129 MonoBoolean
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;
4135 gpointer iter;
4136 MonoClassField *field;
4137 int base_type;
4138 guint64 previous_value = 0;
4139 gboolean sorted = TRUE;
4141 error_init (error);
4142 mono_class_init_checked (enumc, error);
4143 return_val_if_nok (error, FALSE);
4145 if (!m_class_is_enumtype (enumc)) {
4146 #if ENABLE_NETCORE
4147 mono_error_set_argument (error, NULL, "Type provided must be an Enum.");
4148 #else
4149 mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
4150 #endif
4151 return TRUE;
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);
4162 iter = NULL;
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);
4165 if (!is_ok (error))
4166 break;
4168 return_val_if_nok (error, FALSE);
4170 return sorted || base_type == MONO_TYPE_R4 || base_type == MONO_TYPE_R8;
4173 enum {
4174 BFLAGS_IgnoreCase = 1,
4175 BFLAGS_DeclaredOnly = 2,
4176 BFLAGS_Instance = 4,
4177 BFLAGS_Static = 8,
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
4192 enum {
4193 MLISTTYPE_All = 0,
4194 MLISTTYPE_CaseSensitive = 1,
4195 MLISTTYPE_CaseInsensitive = 2,
4196 MLISTTYPE_HandleToInfo = 3
4199 GPtrArray*
4200 ves_icall_RuntimeType_GetFields_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 bflags, guint32 mlisttype, MonoError *error)
4202 error_init (error);
4203 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4205 if (type->byref) {
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);
4217 handle_parent:
4218 if (mono_class_has_failure (klass)) {
4219 mono_error_set_for_class_failure (error, klass);
4220 goto fail;
4223 MonoClassField *field;
4224 gpointer iter;
4225 iter = NULL;
4226 while ((field = mono_class_get_fields_lazy (klass, &iter))) {
4227 guint32 flags = mono_field_get_flags (field);
4228 int match = 0;
4229 if (mono_field_is_deleted_with_flags (field, flags))
4230 continue;
4231 if ((flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
4232 if (bflags & BFLAGS_Public)
4233 match++;
4234 } else if ((klass == startklass) || (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) != FIELD_ATTRIBUTE_PRIVATE) {
4235 if (bflags & BFLAGS_NonPublic) {
4236 match++;
4239 if (!match)
4240 continue;
4241 match = 0;
4242 if (flags & FIELD_ATTRIBUTE_STATIC) {
4243 if (bflags & BFLAGS_Static)
4244 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4245 match++;
4246 } else {
4247 if (bflags & BFLAGS_Instance)
4248 match++;
4251 if (!match)
4252 continue;
4254 if (((mlisttype != MLISTTYPE_All) && (utf8_name != NULL)) && compare_func (mono_field_get_name (field), utf8_name))
4255 continue;
4257 g_ptr_array_add (ptr_array, field);
4259 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
4260 goto handle_parent;
4262 return ptr_array;
4264 fail:
4265 g_ptr_array_free (ptr_array, TRUE);
4266 return NULL;
4269 static gboolean
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:
4276 return start_klass;
4277 case METHOD_ATTRIBUTE_PUBLIC:
4278 return FALSE;
4279 default:
4280 return TRUE;
4284 GPtrArray*
4285 mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bflags, guint32 mlisttype, gboolean allow_ctors, MonoError *error)
4287 GPtrArray *array;
4288 MonoClass *startklass;
4289 MonoMethod *method;
4290 gpointer iter;
4291 int match, nslots;
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 ();
4298 startklass = klass;
4299 error_init (error);
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);
4306 g_assert (method);
4308 g_ptr_array_add (array, method);
4309 return array;
4312 mono_class_setup_methods (klass);
4313 mono_class_setup_vtable (klass);
4314 if (mono_class_has_failure (klass))
4315 goto loader_error;
4317 if (is_generic_parameter (m_class_get_byval_arg (klass)))
4318 nslots = mono_class_get_vtable_size (m_class_get_parent (klass));
4319 else
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);
4323 } else {
4324 method_slots = method_slots_default;
4325 memset (method_slots, 0, sizeof (method_slots_default));
4327 handle_parent:
4328 mono_class_setup_methods (klass);
4329 mono_class_setup_vtable (klass);
4330 if (mono_class_has_failure (klass))
4331 goto loader_error;
4333 iter = NULL;
4334 while ((method = mono_class_get_methods (klass, &iter))) {
4335 match = 0;
4336 if (method->slot != -1) {
4337 g_assert (method->slot < nslots);
4338 if (method_slots [method->slot >> 5] & (1 << (method->slot & 0x1f)))
4339 continue;
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))
4345 continue;
4346 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
4347 if (bflags & BFLAGS_Public)
4348 match++;
4349 } else if ((bflags & BFLAGS_NonPublic) && method_nonpublic (method, (klass == startklass))) {
4350 match++;
4352 if (!match)
4353 continue;
4354 match = 0;
4355 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4356 if (bflags & BFLAGS_Static)
4357 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4358 match++;
4359 } else {
4360 if (bflags & BFLAGS_Instance)
4361 match++;
4364 if (!match)
4365 continue;
4367 if ((mlisttype != MLISTTYPE_All) && (name != NULL)) {
4368 if (compare_func (name, method->name))
4369 continue;
4372 match = 0;
4373 g_ptr_array_add (array, method);
4375 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
4376 goto handle_parent;
4377 if (method_slots != method_slots_default)
4378 g_free (method_slots);
4380 return array;
4382 loader_error:
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);
4389 return NULL;
4392 GPtrArray*
4393 ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type, const char *mname, guint32 bflags, guint32 mlisttype, MonoError *error)
4395 error_init (error);
4396 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4398 MonoClass *klass = mono_class_from_mono_type_internal (type);
4399 if (type->byref) {
4400 return g_ptr_array_new ();
4403 return mono_class_get_methods_by_name (klass, mname, bflags, mlisttype, FALSE, error);
4406 GPtrArray*
4407 ves_icall_RuntimeType_GetConstructors_native (MonoReflectionTypeHandle ref_type, guint32 bflags, MonoError *error)
4409 error_init (error);
4410 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4411 if (type->byref) {
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);
4421 return NULL;
4425 GPtrArray *res_array = g_ptr_array_sized_new (4); /* FIXME, guestimating */
4427 MonoMethod *method;
4428 gpointer iter = NULL;
4429 while ((method = mono_class_get_methods (klass, &iter))) {
4430 int match = 0;
4431 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
4432 continue;
4433 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
4434 if (bflags & BFLAGS_Public)
4435 match++;
4436 } else {
4437 if (bflags & BFLAGS_NonPublic)
4438 match++;
4440 if (!match)
4441 continue;
4442 match = 0;
4443 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4444 if (bflags & BFLAGS_Static)
4445 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4446 match++;
4447 } else {
4448 if (bflags & BFLAGS_Instance)
4449 match++;
4452 if (!match)
4453 continue;
4454 g_ptr_array_add (res_array, method);
4457 return res_array;
4460 static guint
4461 property_hash (gconstpointer data)
4463 MonoProperty *prop = (MonoProperty*)data;
4465 return g_str_hash (prop->name);
4468 static gboolean
4469 property_accessor_override (MonoMethod *method1, MonoMethod *method2)
4471 if (method1->slot != -1 && method1->slot == method2->slot)
4472 return TRUE;
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));
4484 static gboolean
4485 property_equal (MonoProperty *prop1, MonoProperty *prop2)
4487 // Properties are hide-by-name-and-signature
4488 if (!g_str_equal (prop1->name, prop2->name))
4489 return FALSE;
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
4494 distinct:
4496 class Foo<T,U> {
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))
4506 return FALSE;
4508 if (prop1->set && prop2->set && !property_accessor_override (prop1->set, prop2->set))
4509 return FALSE;
4511 return TRUE;
4514 static gboolean
4515 property_accessor_nonpublic (MonoMethod* accessor, gboolean start_klass)
4517 if (!accessor)
4518 return FALSE;
4520 return method_nonpublic (accessor, start_klass);
4523 GPtrArray*
4524 ves_icall_RuntimeType_GetPropertiesByName_native (MonoReflectionTypeHandle ref_type, gchar *propname, guint32 bflags, guint32 mlisttype, MonoError *error)
4526 #if ENABLE_NETCORE
4527 // Fetch non-public properties as well because they can hide public properties with the same name in base classes
4528 bflags |= BFLAGS_NonPublic;
4529 #endif
4530 error_init (error);
4531 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4534 if (type->byref) {
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);
4548 handle_parent:
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);
4553 goto loader_error;
4556 MonoProperty *prop;
4557 gpointer iter;
4558 iter = NULL;
4559 while ((prop = mono_class_get_properties (klass, &iter))) {
4560 int match = 0;
4561 MonoMethod *method = prop->get;
4562 if (!method)
4563 method = prop->set;
4564 guint32 flags = 0;
4565 if (method)
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)
4571 match++;
4572 } else if (bflags & BFLAGS_NonPublic) {
4573 if (property_accessor_nonpublic(prop->get, startklass == klass) ||
4574 property_accessor_nonpublic(prop->set, startklass == klass)) {
4575 match++;
4578 if (!match)
4579 continue;
4581 match = 0;
4582 if (flags & METHOD_ATTRIBUTE_STATIC) {
4583 if (bflags & BFLAGS_Static)
4584 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4585 match++;
4586 } else {
4587 if (bflags & BFLAGS_Instance)
4588 match++;
4591 if (!match)
4592 continue;
4593 match = 0;
4595 if ((mlisttype != MLISTTYPE_All) && (propname != NULL) && compare_func (propname, prop->name))
4596 continue;
4598 if (g_hash_table_lookup (properties, prop))
4599 continue;
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))) {
4606 #if ENABLE_NETCORE
4607 // BFLAGS_NonPublic should be excluded for base classes
4608 bflags &= ~BFLAGS_NonPublic;
4609 #endif
4610 goto handle_parent;
4613 g_hash_table_destroy (properties);
4615 return res_array;
4618 loader_error:
4619 if (properties)
4620 g_hash_table_destroy (properties);
4621 g_ptr_array_free (res_array, TRUE);
4623 return NULL;
4626 static guint
4627 event_hash (gconstpointer data)
4629 MonoEvent *event = (MonoEvent*)data;
4631 return g_str_hash (event->name);
4634 static gboolean
4635 event_equal (MonoEvent *event1, MonoEvent *event2)
4637 // Events are hide-by-name
4638 return g_str_equal (event1->name, event2->name);
4641 GPtrArray*
4642 ves_icall_RuntimeType_GetEvents_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 mlisttype, MonoError *error)
4644 error_init (error);
4645 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4647 if (type->byref) {
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);
4659 handle_parent:
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);
4664 goto failure;
4667 MonoEvent *event;
4668 gpointer iter;
4669 iter = NULL;
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;
4677 if (!method)
4678 method = event->remove;
4679 if (!method)
4680 method = event->raise;
4681 if (!method)
4682 continue;
4683 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PRIVATE)
4684 continue;
4687 if ((mlisttype != MLISTTYPE_All) && (utf8_name != NULL) && compare_func (event->name, utf8_name))
4688 continue;
4690 if (g_hash_table_lookup (events, event))
4691 continue;
4693 g_ptr_array_add (res_array, event);
4695 g_hash_table_insert (events, event, event);
4697 if ((klass = m_class_get_parent (klass)))
4698 goto handle_parent;
4700 g_hash_table_destroy (events);
4702 return res_array;
4704 failure:
4705 if (events != NULL)
4706 g_hash_table_destroy (events);
4708 g_ptr_array_free (res_array, TRUE);
4710 return NULL;
4713 GPtrArray *
4714 ves_icall_RuntimeType_GetNestedTypes_native (MonoReflectionTypeHandle ref_type, char *str, guint32 bflags, guint32 mlisttype, MonoError *error)
4716 error_init (error);
4717 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4719 if (type->byref) {
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 ();
4741 MonoClass *nested;
4742 gpointer iter = NULL;
4743 while ((nested = mono_class_get_nested_types (klass, &iter))) {
4744 int match = 0;
4745 if ((mono_class_get_flags (nested) & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
4746 if (bflags & BFLAGS_Public)
4747 match++;
4748 } else {
4749 if (bflags & BFLAGS_NonPublic)
4750 match++;
4752 if (!match)
4753 continue;
4755 if ((mlisttype != MLISTTYPE_All) && (str != NULL) && compare_func (m_class_get_name (nested), str))
4756 continue;
4758 g_ptr_array_add (res_array, m_class_get_byval_arg (nested));
4761 return res_array;
4764 static MonoType*
4765 get_type_from_module_builder_module (MonoArrayHandle modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
4767 HANDLE_FUNCTION_ENTER ();
4768 error_init (error);
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);
4777 static MonoType*
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 ();
4781 error_init (error);
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)
4793 error_init (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)) {
4806 g_free (str);
4807 mono_reflection_free_type_info (&info);
4808 mono_error_cleanup (parse_error);
4809 if (throwOnError) {
4810 mono_error_set_argument (error, "typeName", "failed to parse the type");
4811 goto fail;
4813 /*g_print ("failed parse\n");*/
4814 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4817 if (info.assembly.name) {
4818 g_free (str);
4819 mono_reflection_free_type_info (&info);
4820 if (throwOnError) {
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.");
4824 else
4825 mono_error_set_type_load_name (error, g_strdup (""), g_strdup (""), "Type names passed to Assembly.GetType() must not specify an assembly.");
4826 goto fail;
4828 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4831 MonoType *type;
4832 type = NULL;
4833 if (!MONO_HANDLE_IS_NULL (module)) {
4834 MonoImage *image = MONO_HANDLE_GETVAL (module, image);
4835 if (image) {
4836 type = mono_reflection_get_type_checked (image, image, &info, ignoreCase, &type_resolve, error);
4837 if (!is_ok (error)) {
4838 g_free (str);
4839 mono_reflection_free_type_info (&info);
4840 goto fail;
4844 else {
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);
4850 int i;
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)) {
4859 g_free (str);
4860 mono_reflection_free_type_info (&info);
4861 goto fail;
4863 if (type)
4864 break;
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)) {
4876 g_free (str);
4877 mono_reflection_free_type_info (&info);
4878 goto fail;
4880 if (type)
4881 break;
4885 else {
4886 type = mono_reflection_get_type_checked (assembly->image, assembly->image, &info, ignoreCase, &type_resolve, error);
4887 if (!is_ok (error)) {
4888 g_free (str);
4889 mono_reflection_free_type_info (&info);
4890 goto fail;
4894 g_free (str);
4895 mono_reflection_free_type_info (&info);
4897 if (!type) {
4898 if (throwOnError) {
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", "");
4905 goto fail;
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);
4918 goto fail;
4922 /* g_print ("got it\n"); */
4923 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (assembly_h), type, error);
4924 fail:
4925 g_assert (!is_ok (error));
4926 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4929 static gboolean
4930 replace_shadow_path (MonoDomain *domain, gchar *dirname, gchar **filename)
4932 gchar *content;
4933 gchar *shadow_ini_file;
4934 gsize len;
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);
4939 content = NULL;
4940 if (!g_file_get_contents (shadow_ini_file, &content, &len, NULL) ||
4941 !g_file_test (content, G_FILE_TEST_IS_REGULAR)) {
4942 g_free (content);
4943 content = NULL;
4945 g_free (shadow_ini_file);
4946 if (content != NULL) {
4947 g_free (*filename);
4948 *filename = content;
4949 return TRUE;
4952 return FALSE;
4955 MonoStringHandle
4956 ves_icall_System_Reflection_RuntimeAssembly_get_code_base (MonoReflectionAssemblyHandle assembly, MonoBoolean escaped, MonoError *error)
4958 error_init (error);
4959 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
4960 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
4961 gchar *absolute;
4962 gchar *dirname;
4964 if (g_path_is_absolute (mass->image->name)) {
4965 absolute = g_strdup (mass->image->name);
4966 dirname = g_path_get_dirname (absolute);
4967 } else {
4968 absolute = g_build_filename (mass->basedir, mass->image->name, NULL);
4969 dirname = g_strdup (mass->basedir);
4972 replace_shadow_path (domain, dirname, &absolute);
4973 g_free (dirname);
4975 mono_icall_make_platform_path (absolute);
4977 gchar *uri;
4978 if (escaped) {
4979 uri = g_filename_to_uri (absolute, NULL, NULL);
4980 } else {
4981 const gchar *prepend = mono_icall_get_file_path_prefix (absolute);
4982 uri = g_strconcat (prepend, absolute, NULL);
4985 g_free (absolute);
4987 MonoStringHandle res;
4988 if (uri) {
4989 res = mono_string_new_handle (domain, uri, error);
4990 g_free (uri);
4991 } else {
4992 res = MONO_HANDLE_NEW (MonoString, NULL);
4994 return res;
4997 MonoBoolean
4998 ves_icall_System_Reflection_RuntimeAssembly_get_global_assembly_cache (MonoReflectionAssemblyHandle assembly, MonoError *error)
5000 error_init (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)
5009 gchar *name;
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);
5015 MonoAssembly *res;
5016 res = mono_assembly_load_with_partial_name_internal (name, &status);
5018 g_free (name);
5020 if (res == NULL)
5021 goto leave;
5022 result = mono_assembly_get_object_handle (mono_domain_get (), res, error);
5023 leave:
5024 return result;
5027 MonoStringHandle
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);
5035 MonoBoolean
5036 ves_icall_System_Reflection_RuntimeAssembly_get_ReflectionOnly (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5038 error_init (error);
5039 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5040 return mono_asmctx_get_kind (&assembly->context) == MONO_ASMCTX_REFONLY;
5043 MonoStringHandle
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)
5055 error_init (error);
5056 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5057 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5058 MonoMethod *method;
5060 MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
5061 guint32 token = mono_image_get_entry_point (assembly->image);
5063 if (!token)
5064 goto leave;
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));
5069 leave:
5070 return res;
5073 MonoReflectionModuleHandle
5074 ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssemblyHandle assembly, MonoError *error)
5076 error_init (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);
5082 static gboolean
5083 add_manifest_resource_name_to_array (MonoDomain *domain, MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle dest, MonoError *error)
5085 HANDLE_FUNCTION_ENTER ();
5086 error_init (error);
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);
5091 leave:
5092 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5095 MonoArrayHandle
5096 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceNames (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5098 error_init (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);
5104 int i;
5106 for (i = 0; i < table->rows; ++i) {
5107 if (!add_manifest_resource_name_to_array (domain, assembly->image, table, i, result, error))
5108 goto fail;
5110 return result;
5111 fail:
5112 return NULL_HANDLE_ARRAY;
5115 MonoBoolean
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 ();
5121 if (!aotid) {
5122 return FALSE;
5123 } else {
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;
5128 return TRUE;
5132 static MonoAssemblyName*
5133 create_referenced_assembly_name (MonoDomain *domain, MonoImage *image, MonoTableInfo *t, int i, MonoError *error)
5135 error_init (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). */
5153 return aname;
5156 GPtrArray*
5157 ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflectionAssemblyHandle assembly, MonoError *error)
5159 error_init (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);
5171 if (!is_ok (error))
5172 break;
5173 g_ptr_array_add (result, aname);
5175 return result;
5178 /* move this in some file in mono/util/ */
5179 static char *
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);
5191 else
5192 return g_strconcat (dir, file, NULL);
5195 void *
5196 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
5198 error_init (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];
5202 guint32 i;
5203 guint32 cols [MONO_MANIFEST_SIZE];
5204 guint32 impl, file_idx;
5205 const char *val;
5206 MonoImage *module;
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)
5215 break;
5217 g_free (n);
5218 if (i == table->rows)
5219 return NULL;
5220 /* FIXME */
5221 impl = cols [MONO_MANIFEST_IMPLEMENTATION];
5222 if (impl) {
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)
5232 return NULL;
5234 else
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);
5245 static gboolean
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];
5252 int i;
5253 guint32 cols [MONO_MANIFEST_SIZE];
5254 guint32 file_cols [MONO_FILE_SIZE];
5255 const char *val;
5256 char *n;
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)
5267 break;
5269 g_free (n);
5270 if (i == table->rows)
5271 goto leave;
5273 if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
5274 MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST);
5276 else {
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);
5286 else
5287 MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED);
5288 break;
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);
5295 goto leave;
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);
5305 guint32 location;
5306 location = MONO_HANDLE_GETVAL (info, location);
5307 location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
5308 MONO_HANDLE_SETVAL (info, location, guint32, location);
5309 break;
5311 case MONO_IMPLEMENTATION_EXP_TYPE:
5312 g_assert_not_reached ();
5313 break;
5317 result = TRUE;
5318 leave:
5319 HANDLE_FUNCTION_RETURN_VAL (result);
5322 MonoBoolean
5323 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInfoInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info_h, MonoError *error)
5325 error_init (error);
5326 return get_manifest_resource_info_internal (assembly_h, name, info_h, error);
5329 static gboolean
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();
5333 error_init (error);
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);
5337 g_free (n);
5338 goto_if_nok (error, leave);
5339 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, str);
5340 leave:
5341 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5344 MonoObjectHandle
5345 ves_icall_System_Reflection_RuntimeAssembly_GetFilesInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoBoolean resource_modules, MonoError *error)
5347 error_init (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];
5351 int i, count;
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) {
5361 g_free (n);
5362 n = g_concat_dir_and_file (assembly->basedir, val);
5363 MonoStringHandle fn = mono_string_new_handle (domain, n, error);
5364 g_free (n);
5365 goto_if_nok (error, fail);
5366 return MONO_HANDLE_CAST (MonoObject, fn);
5369 g_free (n);
5370 return NULL_HANDLE;
5373 count = 0;
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))
5376 count ++;
5379 MonoArrayHandle result;
5380 result = mono_array_new_handle (domain, mono_defaults.string_class, count, error);
5381 goto_if_nok (error, fail);
5383 count = 0;
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))
5387 goto fail;
5388 count++;
5391 return MONO_HANDLE_CAST (MonoObject, result);
5392 fail:
5393 return NULL_HANDLE;
5396 static gboolean
5397 add_module_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int *dest_idx, MonoImage* module, MonoError *error)
5399 HANDLE_FUNCTION_ENTER ();
5400 error_init (error);
5401 if (module) {
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);
5406 ++(*dest_idx);
5409 leave:
5410 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5413 static gboolean
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 ();
5417 error_init (error);
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);
5425 } else {
5426 MonoImage *m = mono_image_load_file_for_image_checked (image, table_idx + 1, error);
5427 goto_if_nok (error, leave);
5428 if (!m) {
5429 const char *filename = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
5430 mono_error_set_file_not_found (error, filename, "%s", "");
5431 goto leave;
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);
5438 leave:
5439 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5442 MonoArrayHandle
5443 ves_icall_System_Reflection_RuntimeAssembly_GetModulesInternal (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5445 error_init (error);
5446 MonoDomain *domain = mono_domain_get();
5447 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5448 MonoClass *klass;
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)
5466 if (modules [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);
5479 j = 1;
5480 for (i = 0; i < module_count; ++i)
5481 if (!add_module_to_modules_array (domain, res, &j, modules[i], error))
5482 goto fail;
5484 for (i = 0; i < file_count; ++i, ++j) {
5485 if (!add_file_to_modules_array (domain, res, j, image, table, i, error))
5486 goto fail;
5489 return res;
5490 fail:
5491 return NULL_HANDLE_ARRAY;
5494 MonoReflectionMethodHandle
5495 ves_icall_GetCurrentMethod (MonoError *error)
5497 error_init (error);
5499 MonoMethod *m = mono_method_get_last_managed ();
5501 if (!m) {
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);
5512 static MonoMethod*
5513 mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
5515 int offset = -1, i;
5516 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
5517 ERROR_DECL (error);
5518 MonoMethod *result;
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 */
5530 return result;
5533 mono_class_setup_methods (method->klass);
5534 if (mono_class_has_failure (method->klass))
5535 return NULL;
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) {
5540 offset = i;
5541 break;
5544 mono_class_setup_methods (klass);
5545 if (mono_class_has_failure (klass))
5546 return NULL;
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)
5554 error_init (error);
5555 MonoClass *klass;
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);
5563 if (!method)
5564 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5566 } else if (type)
5567 klass = mono_class_from_mono_type_internal (type);
5568 else
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)
5576 error_init (error);
5577 return mono_method_body_get_object_handle (mono_domain_get (), method, error);
5580 #if ENABLE_NETCORE
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);
5589 #else
5590 MonoReflectionAssemblyHandle
5591 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError *error)
5593 error_init (error);
5595 MonoMethod *dest = NULL;
5596 mono_stack_walk_no_il (get_executing, &dest);
5597 g_assert (dest);
5598 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
5600 #endif
5602 MonoReflectionAssemblyHandle
5603 ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError *error)
5605 error_init (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)
5618 error_init (error);
5619 MonoMethod *m;
5620 MonoMethod *dest;
5622 dest = NULL;
5623 mono_stack_walk_no_il (get_executing, &dest);
5624 m = dest;
5625 mono_stack_walk_no_il (get_caller_no_reflection, &dest);
5626 if (!dest)
5627 dest = m;
5628 if (!m) {
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);
5635 MonoStringHandle
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;
5643 gchar *name;
5645 if (full_name)
5646 format = assembly_qualified ?
5647 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED :
5648 MONO_TYPE_NAME_FORMAT_FULL_NAME;
5649 else
5650 format = MONO_TYPE_NAME_FORMAT_REFLECTION;
5652 name = mono_type_get_name_full (type, format);
5653 if (!name)
5654 return NULL_HANDLE_STRING;
5656 if (full_name && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
5657 g_free (name);
5658 return NULL_HANDLE_STRING;
5661 res = mono_string_new_handle (domain, name, error);
5662 g_free (name);
5664 return res;
5668 ves_icall_RuntimeType_get_core_clr_security_level (MonoReflectionTypeHandle rfield, MonoError *error)
5670 error_init (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);
5693 MonoStringHandle
5694 ves_icall_System_Reflection_RuntimeAssembly_get_fullname (MonoReflectionAssemblyHandle assembly, MonoError *error)
5696 error_init (error);
5697 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5698 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
5699 gchar *name;
5701 name = mono_stringify_assembly_name (&mass->aname);
5702 MonoStringHandle res = mono_string_new_handle (domain, name, error);
5703 g_free (name);
5704 return res;
5707 MonoAssemblyName *
5708 ves_icall_System_Reflection_AssemblyName_GetNativeName (MonoAssembly *mass)
5710 return &mass->aname;
5713 void
5714 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoStringHandle fname, MonoAssemblyName *name, MonoStringHandleOut normalized_codebase, MonoError *error)
5716 char *filename;
5717 MonoImageOpenStatus status = MONO_IMAGE_OK;
5718 char *codebase = NULL;
5719 gboolean res;
5720 MonoImage *image;
5721 char *dirname;
5723 error_init (error);
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);
5730 g_free (dirname);
5732 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_ASSEMBLY, "InternalGetAssemblyName (\"%s\")", filename);
5734 image = mono_image_open_full (filename, &status, TRUE);
5736 if (!image){
5737 if (status == MONO_IMAGE_IMAGE_INVALID)
5738 mono_error_set_bad_image_by_name (error, filename, "Invalid Image");
5739 else
5740 mono_error_set_file_not_found (error, filename, "%s", "");
5741 g_free (filename);
5742 return;
5745 res = mono_assembly_fill_assembly_name_full (image, name, TRUE);
5746 if (!res) {
5747 mono_image_close (image);
5748 g_free (filename);
5749 mono_error_set_argument (error, "assemblyFile", "The file does not contain a manifest");
5750 return;
5753 if (filename != NULL && *filename != '\0') {
5754 gchar *result;
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);
5763 g_free (codebase);
5764 codebase = result;
5766 MONO_HANDLE_ASSIGN (normalized_codebase, mono_string_new_handle (mono_domain_get (), codebase, error));
5767 g_free (codebase);
5769 mono_image_close (image);
5770 g_free (filename);
5773 MonoBoolean
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)
5777 error_init (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;
5786 result = TRUE;
5788 /* SecurityAction.RequestOptional */
5789 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQOPT, &entry)) {
5790 *optional = entry.blob;
5791 *optLength = entry.size;
5792 result = TRUE;
5794 /* SecurityAction.RequestRefuse */
5795 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQREFUSE, &entry)) {
5796 *refused = entry.blob;
5797 *refLength = entry.size;
5798 result = TRUE;
5801 return result;
5804 static gboolean
5805 mono_module_type_is_visible (MonoTableInfo *tdef, MonoImage *image, int type)
5807 guint32 attrs, visibility;
5808 do {
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)
5812 return FALSE;
5814 } while ((type = mono_metadata_token_index (mono_metadata_nested_in_typedef (image, type))));
5816 return TRUE;
5819 static void
5820 image_get_type (MonoDomain *domain, MonoImage *image, MonoTableInfo *tdef, int table_idx, int count, MonoArrayHandle res, MonoArrayHandle exceptions, MonoBoolean exportedOnly, MonoError *error)
5822 error_init (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);
5827 if (klass) {
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);
5832 } else {
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];
5843 int i, count;
5845 error_init (error);
5847 /* we start the count from 1 because we skip the special type <Module> */
5848 if (exportedOnly) {
5849 count = 0;
5850 for (i = 1; i < tdef->rows; ++i) {
5851 if (mono_module_type_is_visible (tdef, image, i + 1))
5852 count++;
5854 } else {
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);
5861 count = 0;
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);
5866 count++;
5870 return res;
5873 static void
5874 append_module_types (MonoDomain *domain, MonoArrayHandleOut res, MonoArrayHandleOut exceptions, MonoImage *image, MonoBoolean exportedOnly, MonoError *error)
5876 HANDLE_FUNCTION_ENTER ();
5877 error_init (error);
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) {
5884 guint32 len1, len2;
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);
5903 leave:
5904 HANDLE_FUNCTION_RETURN ();
5907 static void
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 ();
5919 MonoArrayHandle
5920 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
5922 MonoArrayHandle exceptions = MONO_HANDLE_NEW(MonoArray, NULL);
5923 int i;
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);
5940 if (loaded_image) {
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);
5954 int ex_count = 0;
5955 GList *list = NULL;
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);
5968 } else {
5969 ex_count ++;
5973 if (list || ex_count) {
5974 GList *tmp = NULL;
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)) {
5979 g_list_free (list);
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);
5993 i ++;
5996 g_list_free (list);
5997 list = NULL;
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;
6005 return res;
6008 #if ENABLE_NETCORE
6009 MonoArrayHandle
6010 ves_icall_System_Reflection_RuntimeAssembly_GetExportedTypes (MonoReflectionAssemblyHandle assembly_handle, MonoError *error)
6012 return ves_icall_System_Reflection_Assembly_GetTypes (assembly_handle, TRUE, error);
6015 MonoArrayHandle
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];
6021 const char *name;
6022 const char *nspace;
6023 guint32 impl, assembly_idx;
6024 int count = 0;
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)
6030 count ++;
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);
6034 int aindex = 0;
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))
6038 continue;
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)
6050 continue;
6051 MonoClass *klass = mono_class_from_name_checked (image->references [assembly_idx - 1]->image, nspace, name, error);
6052 if (!is_ok (error))
6053 continue;
6054 MonoReflectionTypeHandle rt = mono_type_get_object_handle (mono_domain_get (), m_class_get_byval_arg (klass), error);
6055 if (!is_ok (error))
6056 continue;
6057 MONO_HANDLE_ARRAY_SETREF (res, aindex, rt);
6058 aindex ++;
6060 if (aindex < count) {
6061 // FIXME:
6062 mono_error_set_type_load_name (error, g_strdup (""), g_strdup (""), "");
6063 return MONO_HANDLE_NEW (MonoArray, NULL);
6065 return res;
6067 #endif
6069 void
6070 ves_icall_Mono_RuntimeMarshal_FreeAssemblyName (MonoAssemblyName *aname, MonoBoolean free_struct, MonoError *error)
6072 mono_assembly_name_free (aname);
6073 if (free_struct)
6074 g_free (aname);
6077 void
6078 ves_icall_Mono_Runtime_DisableMicrosoftTelemetry (MonoError *error)
6080 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6081 mono_merp_disable ();
6082 #else
6083 // Icall has platform check in managed too.
6084 g_assert_not_reached ();
6085 #endif
6088 void
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);
6095 #else
6096 // Icall has platform check in managed too.
6097 g_assert_not_reached ();
6098 #endif
6101 void
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 ();
6108 #else
6109 // Icall has platform check in managed too.
6110 g_assert_not_reached ();
6111 #endif
6114 // Number derived from trials on relevant hardware.
6115 // If it seems large, please confirm it's safe to shrink
6116 // before doing so.
6117 #define MONO_MAX_SUMMARY_LEN_ICALL 500000
6119 MonoStringHandle
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);
6142 g_free (output);
6143 g_free (scratch);
6144 return result;
6146 #endif
6148 *portable_hash_out = 0;
6149 *unportable_hash_out = 0;
6150 result = mono_string_new_handle (mono_domain_get (), "", error);
6151 return result;
6154 void
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);
6172 if (!success) {
6173 //g_assert_not_reached ();
6174 mono_error_set_generic_error (error, "System", "Exception", "We were unable to start the Microsoft Error Reporting client.");
6176 #else
6177 // Icall has platform check in managed too.
6178 g_assert_not_reached ();
6179 #endif
6182 void
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);
6191 #else
6192 return;
6193 #endif
6196 MonoStringHandle
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);
6221 g_free (output);
6222 g_free (scratch);
6223 #else
6224 *portable_hash = 0;
6225 *unportable_hash = 0;
6226 result = mono_string_new_handle (mono_domain_get (), "", error);
6227 #endif
6229 return result;
6233 void
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);
6239 #endif
6242 void
6243 ves_icall_Mono_Runtime_EnableCrashReportingLog (const char *directory, MonoError *error)
6245 #ifndef DISABLE_CRASH_REPORTING
6246 mono_summarize_set_timeline_dir (directory);
6247 #endif
6251 ves_icall_Mono_Runtime_CheckCrashReportingLog (const char *directory, MonoBoolean clear, MonoError *error)
6253 int ret;
6254 #ifndef DISABLE_CRASH_REPORTING
6255 ret = (int) mono_summarize_timeline_read_level (directory, clear != 0);
6256 #else
6257 ret = 0;
6258 #endif
6259 return ret;
6262 MonoStringHandle
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);
6270 char *out;
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);
6282 if (!success)
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
6290 g_free (scratch);
6292 mono_summarize_timeline_phase_log (MonoSummaryDone);
6293 #else
6294 *portable_hash = 0;
6295 *unportable_hash = 0;
6296 result = mono_string_new_handle (mono_domain_get (), "", error);
6297 #endif
6299 return result;
6302 MonoBoolean
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;
6314 return result;
6317 MonoReflectionTypeHandle
6318 ves_icall_System_Reflection_RuntimeModule_GetGlobalType (MonoImage *image, MonoError *error)
6320 MonoDomain *domain = mono_domain_get ();
6321 MonoClass *klass;
6323 g_assert (image);
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 */
6329 goto leave;
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);
6335 leave:
6336 return ret;
6339 void
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;
6352 } else {
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;
6360 #ifndef HOST_WIN32
6361 static inline gpointer
6362 mono_icall_module_get_hinstance (MonoImage *image)
6364 return (gpointer) (-1);
6366 #endif /* HOST_WIN32 */
6368 gpointer
6369 ves_icall_System_Reflection_RuntimeModule_GetHINSTANCE (MonoImage *image, MonoError *error)
6371 return mono_icall_module_get_hinstance (image);
6374 void
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;
6382 else {
6383 *pe_kind = (image->image_info->cli_cli_header.ch_flags & 0x3);
6384 *machine = image->image_info->cli_header.coff.coff_machine;
6388 gint32
6389 ves_icall_System_Reflection_RuntimeModule_GetMDStreamVersion (MonoImage *image, MonoError *error)
6391 return (image->md_version_major << 16) | (image->md_version_minor);
6394 MonoArrayHandle
6395 ves_icall_System_Reflection_RuntimeModule_InternalGetTypes (MonoImage *image, MonoError *error)
6397 error_init (error);
6399 MonoDomain *domain = mono_domain_get ();
6401 if (!image) {
6402 MonoArrayHandle arr = mono_array_new_handle (domain, mono_defaults.runtimetype_class, 0, error);
6403 return arr;
6404 } else {
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);
6418 return res;
6422 static gboolean
6423 mono_memberref_is_method (MonoImage *image, guint32 token)
6425 if (!image_is_dynamic (image)) {
6426 guint32 cols [MONO_MEMBERREF_SIZE];
6427 const char *sig;
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) {
6431 return FALSE;
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);
6437 } else {
6438 ERROR_DECL (error);
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 */
6443 return FALSE;
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;
6466 break;
6470 return mono_metadata_get_canonical_generic_inst (ginst);
6473 static void
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);
6478 } else {
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);
6483 } else {
6484 context->method_inst = NULL;
6489 static MonoType*
6490 module_resolve_type_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6492 HANDLE_FUNCTION_ENTER ();
6493 error_init (error);
6494 MonoType *result = NULL;
6495 MonoClass *klass;
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;
6506 goto leave;
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;
6515 goto leave;
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;
6523 goto leave;
6526 if ((index <= 0) || (index > image->tables [table].rows)) {
6527 *resolve_error = ResolveTokenError_OutOfRange;
6528 goto leave;
6531 init_generic_context_from_args_handles (&context, type_args, method_args);
6532 klass = mono_class_get_checked (image, token, error);
6533 if (klass)
6534 klass = mono_class_inflate_generic_class_checked (klass, &context, error);
6535 goto_if_nok (error, leave);
6537 if (klass)
6538 result = m_class_get_byval_arg (klass);
6539 leave:
6540 HANDLE_FUNCTION_RETURN_VAL (result);
6543 MonoType*
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);
6549 static MonoMethod*
6550 module_resolve_method_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6552 HANDLE_FUNCTION_ENTER ();
6553 error_init (error);
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;
6565 goto leave;
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);
6573 goto leave;
6576 if ((table == MONO_TABLE_MEMBERREF) && !(mono_memberref_is_method (image, token))) {
6577 *resolve_error = ResolveTokenError_BadTable;
6578 goto leave;
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);
6585 goto leave;
6588 if ((index <= 0) || (index > image->tables [table].rows)) {
6589 *resolve_error = ResolveTokenError_OutOfRange;
6590 goto leave;
6592 if ((table == MONO_TABLE_MEMBERREF) && (!mono_memberref_is_method (image, token))) {
6593 *resolve_error = ResolveTokenError_BadTable;
6594 goto leave;
6597 init_generic_context_from_args_handles (&context, type_args, method_args);
6598 method = mono_get_method_checked (image, token, NULL, &context, error);
6600 leave:
6601 HANDLE_FUNCTION_RETURN_VAL (method);
6604 MonoMethod*
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);
6610 MonoStringHandle
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);
6629 return result;
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 ();
6645 MonoClass *klass;
6646 int table = mono_metadata_token_table (token);
6647 int index = mono_metadata_token_index (token);
6648 MonoGenericContext context;
6649 MonoClassField *field = NULL;
6651 error_init (error);
6652 *resolve_error = ResolveTokenError_Other;
6654 /* Validate token */
6655 if ((table != MONO_TABLE_FIELD) && (table != MONO_TABLE_MEMBERREF)) {
6656 *resolve_error = ResolveTokenError_BadTable;
6657 goto leave;
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);
6665 goto leave;
6668 if (mono_memberref_is_method (image, token)) {
6669 *resolve_error = ResolveTokenError_BadTable;
6670 goto leave;
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);
6677 goto leave;
6680 if ((index <= 0) || (index > image->tables [table].rows)) {
6681 *resolve_error = ResolveTokenError_OutOfRange;
6682 goto leave;
6684 if ((table == MONO_TABLE_MEMBERREF) && (mono_memberref_is_method (image, token))) {
6685 *resolve_error = ResolveTokenError_BadTable;
6686 goto leave;
6689 init_generic_context_from_args_handles (&context, type_args, method_args);
6690 field = mono_field_from_token_checked (image, token, &klass, &context, error);
6692 leave:
6693 HANDLE_FUNCTION_RETURN_VAL (field);
6696 MonoClassField*
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);
6702 MonoObjectHandle
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;
6710 switch (table) {
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);
6715 if (t) {
6716 return MONO_HANDLE_CAST (MonoObject, mono_type_get_object_handle (mono_domain_get (), t, merror));
6718 else
6719 return NULL_HANDLE;
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);
6724 if (m) {
6725 return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
6726 } else
6727 return NULL_HANDLE;
6729 case MONO_TABLE_FIELD: {
6730 MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
6731 if (f) {
6732 return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
6734 else
6735 return NULL_HANDLE;
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);
6740 if (m) {
6741 return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
6742 } else
6743 return NULL_HANDLE;
6745 else {
6746 MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
6747 if (f) {
6748 return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
6750 else
6751 return NULL_HANDLE;
6753 break;
6755 default:
6756 *error = ResolveTokenError_BadTable;
6759 return NULL_HANDLE;
6762 MonoArrayHandle
6763 ves_icall_System_Reflection_RuntimeModule_ResolveSignature (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
6765 error_init (error);
6766 int table = mono_metadata_token_table (token);
6767 int idx = mono_metadata_token_index (token);
6768 MonoTableInfo *tables = image->tables;
6769 guint32 sig, len;
6770 const char *ptr;
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);
6791 uint32_t h;
6792 gpointer array_base = MONO_ARRAY_HANDLE_PIN (res, guint8, 0, &h);
6793 memcpy (array_base, ptr, len);
6794 mono_gchandle_free_internal (h);
6795 return res;
6798 static void
6799 check_for_invalid_type (MonoClass *klass, MonoError *error)
6801 char *name;
6803 error_init (error);
6805 if (m_class_get_byval_arg (klass)->type != MONO_TYPE_TYPEDBYREF)
6806 return;
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)
6815 error_init (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));
6822 MonoClass *aklass;
6823 if (rank == 0) //single dimension array
6824 aklass = mono_class_create_array (klass, 1);
6825 else
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)
6840 error_init (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)
6857 error_init (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);
6872 MonoObjectHandle
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__,
6887 __func__,
6888 "delegate_class->parent == mono_defaults.multicastdelegate_class");
6889 return NULL_HANDLE;
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);
6897 else
6898 mono_error_cleanup (security_error);
6899 return NULL_HANDLE;
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");
6906 return NULL_HANDLE;
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);
6920 return delegate;
6923 MonoMulticastDelegateHandle
6924 ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegateHandle delegate, MonoError *error)
6926 error_init (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));
6936 return ret;
6939 MonoReflectionMethodHandle
6940 ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegateHandle delegate, MonoError *error)
6942 error_init (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);
6950 /* System.Buffer */
6952 static inline gint32
6953 mono_array_get_byte_length (MonoArray *array)
6955 MonoClass *klass;
6956 int length;
6957 int i;
6959 klass = array->obj.vtable->klass;
6961 if (array->bounds == NULL)
6962 length = array->max_length;
6963 else {
6964 length = 1;
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) {
6971 case MONO_TYPE_I1:
6972 case MONO_TYPE_U1:
6973 case MONO_TYPE_BOOLEAN:
6974 return length;
6975 case MONO_TYPE_I2:
6976 case MONO_TYPE_U2:
6977 case MONO_TYPE_CHAR:
6978 return length << 1;
6979 case MONO_TYPE_I4:
6980 case MONO_TYPE_U4:
6981 case MONO_TYPE_R4:
6982 return length << 2;
6983 case MONO_TYPE_I:
6984 case MONO_TYPE_U:
6985 return length * sizeof (gpointer);
6986 case MONO_TYPE_I8:
6987 case MONO_TYPE_U8:
6988 case MONO_TYPE_R8:
6989 return length << 3;
6990 default:
6991 return -1;
6995 gint32
6996 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array)
6998 return mono_array_get_byte_length (array);
7001 gint8
7002 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx)
7004 return mono_array_get_internal (array, gint8, idx);
7007 void
7008 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value)
7010 mono_array_set_internal (array, gint8, idx, value);
7013 void
7014 ves_icall_System_Buffer_MemcpyInternal (gpointer dest, gconstpointer src, gint32 count)
7016 memcpy (dest, src, count);
7019 MonoBoolean
7020 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count)
7022 guint8 *src_buf, *dest_buf;
7024 if (count < 0) {
7025 ERROR_DECL (error);
7026 mono_error_set_argument (error, "count", "is negative");
7027 mono_error_set_pending_exception (error);
7028 return FALSE;
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))
7039 return FALSE;
7041 src_buf = (guint8 *)src->vector + src_offset;
7042 dest_buf = (guint8 *)dest->vector + dest_offset;
7044 if (src != dest)
7045 memcpy (dest_buf, src_buf, count);
7046 else
7047 memmove (dest_buf, src_buf, count); /* Source and dest are the same array */
7049 return TRUE;
7052 #ifndef DISABLE_REMOTING
7053 MonoObjectHandle
7054 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObjectHandle this_obj, MonoStringHandle class_name, MonoError *error)
7056 error_init (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);
7076 return NULL_HANDLE;
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);
7089 return res;
7092 MonoReflectionType *
7093 ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxy *tp)
7095 ERROR_DECL (error);
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);
7101 return ret;
7103 #endif
7105 /* System.Environment */
7107 MonoStringHandle
7108 ves_icall_System_Environment_get_UserName (MonoError *error)
7110 error_init (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);
7115 else
7116 return NULL_HANDLE_STRING;
7119 #ifndef HOST_WIN32
7120 static MonoStringHandle
7121 mono_icall_get_machine_name (MonoError *error)
7123 error_init (error);
7124 #if !defined(DISABLE_SOCKETS)
7125 MonoStringHandle result;
7126 char *buf;
7127 int n, i;
7128 #if defined _SC_HOST_NAME_MAX
7129 n = sysconf (_SC_HOST_NAME_MAX);
7130 if (n == -1)
7131 #endif
7132 n = 512;
7133 buf = (char*)g_malloc (n + 1);
7135 #if defined(HAVE_GETHOSTNAME)
7136 if (gethostname (buf, n) == 0){
7137 buf [n] = 0;
7138 // try truncating the string at the first dot
7139 for (i = 0; i < n; i++) {
7140 if (buf [i] == '.') {
7141 buf [i] = 0;
7142 break;
7145 result = mono_string_new_handle (mono_domain_get (), buf, error);
7146 } else
7147 #endif
7148 result = MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
7150 g_free (buf);
7152 return result;
7153 #else
7154 return mono_string_new_handle (mono_domain_get (), "mono", error);
7155 #endif
7157 #endif /* !HOST_WIN32 */
7159 MonoStringHandle
7160 ves_icall_System_Environment_get_MachineName (MonoError *error)
7162 error_init (error);
7163 return mono_icall_get_machine_name (error);
7166 #ifndef HOST_WIN32
7167 static inline int
7168 mono_icall_get_platform (void)
7170 #if defined(__MACH__)
7171 /* OSX */
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.
7179 return 6;
7180 #else
7181 /* Unix */
7182 return 4;
7183 #endif
7185 #endif /* !HOST_WIN32 */
7188 ves_icall_System_Environment_get_Platform (void)
7190 return mono_icall_get_platform ();
7193 #ifndef HOST_WIN32
7194 static inline MonoStringHandle
7195 mono_icall_get_new_line (MonoError *error)
7197 error_init (error);
7198 return mono_string_new_handle (mono_domain_get (), "\n", error);
7200 #endif /* !HOST_WIN32 */
7202 MonoStringHandle
7203 ves_icall_System_Environment_get_NewLine (MonoError *error)
7205 return mono_icall_get_new_line (error);
7208 #ifndef HOST_WIN32
7209 static inline MonoBoolean
7210 mono_icall_is_64bit_os (void)
7212 #if SIZEOF_VOID_P == 8
7213 return TRUE;
7214 #else
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;
7221 #endif
7222 return FALSE;
7223 #endif
7225 #endif /* !HOST_WIN32 */
7227 MonoBoolean
7228 ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
7230 return mono_icall_is_64bit_os ();
7233 MonoStringHandle
7234 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
7236 gchar *value;
7238 if (utf8_name == NULL)
7239 return NULL_HANDLE_STRING;
7241 value = g_getenv (utf8_name);
7243 if (value == 0)
7244 return NULL_HANDLE_STRING;
7246 MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
7247 g_free (value);
7248 return res;
7252 * There is no standard way to get at environ.
7254 #ifndef _MSC_VER
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)
7262 G_BEGIN_DECLS
7263 gchar ***_NSGetEnviron(void);
7264 G_END_DECLS
7265 #define environ (*_NSGetEnviron())
7266 #else
7267 static char *mono_environ[1] = { NULL };
7268 #define environ mono_environ
7269 #endif /* defined (TARGET_OSX) */
7270 #else
7271 G_BEGIN_DECLS
7272 extern
7273 char **environ;
7274 G_END_DECLS
7275 #endif
7276 #endif
7277 #endif
7279 MonoArrayHandle
7280 ves_icall_System_Environment_GetCommandLineArgs (MonoError *error)
7282 error_init (error);
7283 MonoArrayHandle result = mono_runtime_get_main_args_handle (error);
7284 return result;
7287 #ifndef HOST_WIN32
7288 static MonoArray *
7289 mono_icall_get_environment_variable_names (MonoError *error)
7291 MonoArray *names;
7292 MonoDomain *domain;
7293 MonoString *str;
7294 gchar **e, **parts;
7295 int n;
7297 error_init (error);
7298 n = 0;
7299 for (e = environ; *e != 0; ++ e)
7300 ++ n;
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);
7306 n = 0;
7307 for (e = environ; *e != 0; ++ e) {
7308 parts = g_strsplit (*e, "=", 2);
7309 if (*parts != 0) {
7310 str = mono_string_new_checked (domain, *parts, error);
7311 if (!is_ok (error)) {
7312 g_strfreev (parts);
7313 return NULL;
7315 mono_array_setref_internal (names, n, str);
7318 g_strfreev (parts);
7320 ++ n;
7323 return names;
7325 #endif /* !HOST_WIN32 */
7327 MonoArray *
7328 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
7330 ERROR_DECL (error);
7331 MonoArray *result = mono_icall_get_environment_variable_names (error);
7332 mono_error_set_pending_exception (error);
7333 return result;
7336 void
7337 ves_icall_System_Environment_InternalSetEnvironmentVariable (const gunichar2 *name, gint32 name_length,
7338 const gunichar2 *value, gint32 value_length, MonoError *error)
7340 #ifdef HOST_WIN32
7341 if (!value || !value_length || !value [0])
7342 value = NULL;
7344 SetEnvironmentVariableW (name, value);
7345 #else
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);
7354 goto exit;
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);
7361 exit:
7362 g_free (utf8_name);
7363 g_free (utf8_value);
7364 #endif
7367 void
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... */
7381 exit (result);
7384 MonoStringHandle
7385 ves_icall_System_Environment_GetGacPath (MonoError *error)
7387 return mono_string_new_handle (mono_domain_get (), mono_assembly_getrootdir (), error);
7390 #ifndef HOST_WIN32
7391 static inline MonoStringHandle
7392 mono_icall_get_windows_folder_path (int folder, MonoError *error)
7394 error_init (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 */
7400 MonoStringHandle
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)
7407 static MonoArray *
7408 mono_icall_get_logical_drives (void)
7410 ERROR_DECL (error);
7411 gunichar2 buf [256], *ptr, *dname;
7412 gunichar2 *u16;
7413 guint initial_size = 127, size = 128;
7414 gint ndrives;
7415 MonoArray *result;
7416 MonoString *drivestr;
7417 MonoDomain *domain = mono_domain_get ();
7418 gint len;
7420 buf [0] = '\0';
7421 ptr = buf;
7423 while (size > initial_size) {
7424 size = (guint) mono_w32file_get_logical_drive (initial_size, ptr);
7425 if (size > initial_size) {
7426 if (ptr != buf)
7427 g_free (ptr);
7428 ptr = (gunichar2 *)g_malloc0 ((size + 1) * sizeof (gunichar2));
7429 initial_size = size;
7430 size++;
7434 /* Count strings */
7435 dname = ptr;
7436 ndrives = 0;
7437 do {
7438 while (*dname++);
7439 ndrives++;
7440 } while (*dname);
7442 dname = ptr;
7443 result = mono_array_new_checked (domain, mono_defaults.string_class, ndrives, error);
7444 if (mono_error_set_pending_exception (error))
7445 goto leave;
7447 ndrives = 0;
7448 do {
7449 len = 0;
7450 u16 = dname;
7451 while (*u16) { u16++; len ++; }
7452 drivestr = mono_string_new_utf16_checked (domain, dname, len, error);
7453 if (mono_error_set_pending_exception (error))
7454 goto leave;
7456 mono_array_setref_internal (result, ndrives++, drivestr);
7457 while (*dname++);
7458 } while (*dname);
7460 leave:
7461 if (ptr != buf)
7462 g_free (ptr);
7464 return result;
7466 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
7468 MonoArray *
7469 ves_icall_System_Environment_GetLogicalDrives (void)
7471 return mono_icall_get_logical_drives ();
7474 MonoStringHandle
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);
7484 MonoStringHandle
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);
7490 else
7491 return NULL_HANDLE_STRING;
7494 static const char * const encodings [] = {
7495 (char *) 1,
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",
7499 (char *) 2,
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",
7503 (char *) 3,
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",
7506 (char *) 4,
7507 "utf_16", "UTF_16LE", "ucs_2", "unicode",
7508 "iso_10646_ucs2",
7509 (char *) 5,
7510 "unicodefffe", "utf_16be",
7511 (char *) 6,
7512 "iso_8859_1",
7513 (char *) 0
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
7521 MonoStringHandle
7522 ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page, MonoError *error)
7524 error_init (error);
7525 const char *cset;
7526 const char *p;
7527 char *c;
7528 char *codepage = NULL;
7529 int code;
7530 int want_name = *int_code_page;
7531 int i;
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))
7539 *c = tolower (*c);
7540 if (*c == '-')
7541 *c = '_';
7543 /* g_print ("charset: %s\n", cset); */
7545 /* handle some common aliases */
7546 p = encodings [0];
7547 code = 0;
7548 for (i = 0; p != 0; ){
7549 if ((gsize) p < 7){
7550 code = (gssize) p;
7551 p = encodings [++i];
7552 continue;
7554 if (strcmp (p, codepage) == 0){
7555 *int_code_page = code;
7556 break;
7558 p = encodings [++i];
7561 if (strstr (codepage, "utf_8") != NULL)
7562 *int_code_page |= 0x10000000;
7563 g_free (codepage);
7565 if (want_name && *int_code_page == -1)
7566 return mono_string_new_handle (mono_domain_get (), cset, error);
7567 return NULL_HANDLE_STRING;
7570 MonoBoolean
7571 ves_icall_System_Environment_get_HasShutdownStarted (void)
7573 return mono_runtime_is_shutting_down () || mono_domain_is_unloading (mono_domain_get ());
7576 #ifndef HOST_WIN32
7578 void
7579 ves_icall_System_Environment_BroadcastSettingChange (MonoError *error)
7583 #endif
7585 gint32
7586 ves_icall_System_Environment_get_TickCount (void)
7588 /* this will overflow after ~24 days */
7589 return (gint32) (mono_msec_boottime () & 0xffffffff);
7592 #if ENABLE_NETCORE
7593 gint64
7594 ves_icall_System_Environment_get_TickCount64 (void)
7596 return mono_msec_boottime ();
7598 #endif
7600 gint32
7601 ves_icall_System_Runtime_Versioning_VersioningHelper_GetRuntimeId (MonoError *error)
7603 return 9;
7606 #ifndef DISABLE_REMOTING
7607 MonoBoolean
7608 ves_icall_IsTransparentProxy (MonoObjectHandle proxy, MonoError *error)
7610 if (MONO_HANDLE_IS_NULL (proxy))
7611 return 0;
7613 if (mono_class_is_transparent_proxy (mono_handle_class (proxy)))
7614 return 1;
7616 return 0;
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", "");
7627 return ret;
7629 if (MONO_HANDLE_IS_NULL (rmethod)) {
7630 mono_error_set_argument_null (error, "method", "");
7631 return ret;
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))
7641 return ret;
7643 if (method->flags & METHOD_ATTRIBUTE_STATIC)
7644 return ret;
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))
7648 ret = rmethod;
7649 return ret;
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);
7660 if (offs >= 0)
7661 res = vtable [offs + method->slot];
7662 } else {
7663 if (!(klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE)))
7664 return ret;
7666 if (method->slot != -1)
7667 res = vtable [method->slot];
7670 if (!res)
7671 return ret;
7673 ret = mono_method_get_object_handle (mono_domain_get (), res, NULL, error);
7674 return ret;
7677 void
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 */
7689 void
7690 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
7692 g_assert_not_reached ();
7695 #endif
7697 MonoObjectHandle
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");
7707 return NULL_HANDLE;
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));
7713 } else {
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));
7722 #if !ENABLE_NETCORE
7724 MonoStringHandle
7725 ves_icall_System_IO_get_temp_path (MonoError *error)
7727 return mono_string_new_handle (mono_domain_get (), g_get_tmp_dir (), error);
7730 #endif
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);
7740 gpointer
7741 ves_icall_System_IO_Compression_DeflateStreamNative_CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data)
7743 #ifdef MONO_CROSS_COMPILE
7744 return NULL;
7745 #else
7746 return CreateZStream (compress, gzip, feeder, data);
7747 #endif
7750 gint32
7751 ves_icall_System_IO_Compression_DeflateStreamNative_CloseZStream (gpointer stream)
7753 #ifdef MONO_CROSS_COMPILE
7754 return 0;
7755 #else
7756 return CloseZStream (stream);
7757 #endif
7760 gint32
7761 ves_icall_System_IO_Compression_DeflateStreamNative_Flush (gpointer stream)
7763 #ifdef MONO_CROSS_COMPILE
7764 return 0;
7765 #else
7766 return Flush (stream);
7767 #endif
7770 gint32
7771 ves_icall_System_IO_Compression_DeflateStreamNative_ReadZStream (gpointer stream, gpointer buffer, gint32 length)
7773 #ifdef MONO_CROSS_COMPILE
7774 return 0;
7775 #else
7776 return ReadZStream (stream, buffer, length);
7777 #endif
7780 gint32
7781 ves_icall_System_IO_Compression_DeflateStreamNative_WriteZStream (gpointer stream, gpointer buffer, gint32 length)
7783 #ifdef MONO_CROSS_COMPILE
7784 return 0;
7785 #else
7786 return WriteZStream (stream, buffer, length);
7787 #endif
7790 #endif
7792 #ifndef PLATFORM_NO_DRIVEINFO
7793 MonoBoolean
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,
7796 gint32 *error)
7798 g_assert (error);
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);
7811 if (!result)
7812 *error = mono_w32error_get_last ();
7814 return result;
7816 #endif /* PLATFORM_NO_DRIVEINFO */
7818 gpointer
7819 ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method, MonoError *error)
7821 return mono_compile_method_checked (method, error);
7824 MonoStringHandle
7825 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (MonoError *error)
7827 gchar *path;
7829 const char *mono_cfg_dir = mono_get_config_dir ();
7830 if (!mono_cfg_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);
7838 g_free (path);
7840 mono_error_assert_ok (error);
7842 return mcpath;
7845 MonoStringHandle
7846 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_app_config (MonoError *error)
7848 const gchar *app_config;
7849 MonoDomain *domain;
7850 gchar *config_file_name, *config_file_path;
7851 gsize len, config_file_path_length, config_ext_length;
7852 gchar *module;
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);
7880 // Clean-up
7881 g_free (module);
7882 if (config_file_name != config_file_path)
7883 g_free (config_file_name);
7884 g_free (config_file_path);
7886 if (!app_config)
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);
7905 MonoStringHandle
7906 ves_icall_System_Environment_get_bundled_machine_config (MonoError *error)
7908 return get_bundled_machine_config (error);
7912 MonoStringHandle
7913 ves_icall_System_Configuration_DefaultConfig_get_bundled_machine_config (MonoError *error)
7915 return get_bundled_machine_config (error);
7918 MonoStringHandle
7919 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_machine_config (MonoError *error)
7921 return get_bundled_machine_config (error);
7925 MonoStringHandle
7926 ves_icall_System_Web_Util_ICalls_get_machine_install_dir (MonoError *error)
7928 const char *mono_cfg_dir = mono_get_config_dir ();
7929 if (!mono_cfg_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);
7937 g_free (path);
7939 return ipath;
7942 MonoBoolean
7943 ves_icall_get_resources_ptr (MonoReflectionAssemblyHandle assembly, gpointer *result, gint32 *size, MonoError *error)
7945 MonoPEResourceDataEntry *entry;
7946 MonoImage *image;
7948 if (MONO_HANDLE_IS_NULL (assembly) || !result || !size)
7949 return FALSE;
7951 *result = NULL;
7952 *size = 0;
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);
7956 if (!entry)
7957 return FALSE;
7959 *result = mono_image_rva_map (image, entry->rde_data_offset);
7960 if (!(*result)) {
7961 g_free (entry);
7962 return FALSE;
7964 *size = entry->rde_size;
7965 g_free (entry);
7966 return TRUE;
7969 MonoBoolean
7970 ves_icall_System_Diagnostics_Debugger_IsAttached_internal (MonoError *error)
7972 return mono_is_debugger_attached ();
7975 MonoBoolean
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 ();
7982 void
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);
7989 #ifndef HOST_WIN32
7990 static inline void
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 */
7997 void
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 */
8004 MonoObjectHandle
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 */
8016 return NULL_HANDLE;
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
8035 type. */
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)
8039 return m;
8041 return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
8044 MonoStringHandle
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);
8052 return s;
8055 void
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);
8063 iter->next_arg = 0;
8064 /* FIXME: it's not documented what start is exactly... */
8065 if (start) {
8066 iter->args = start;
8067 } else {
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); */
8075 void
8076 ves_icall_System_ArgIterator_IntGetNextArg (MonoArgIterator *iter, MonoTypedRef *res)
8078 guint32 i, arg_size;
8079 gint32 align;
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));
8090 #endif
8091 res->value = iter->args;
8092 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
8093 if (arg_size <= sizeof (gpointer)) {
8094 int dummy;
8095 int padding = arg_size - mono_type_size (res->type, &dummy);
8096 res->value = (guint8*)res->value + padding;
8098 #endif
8099 iter->args = (char*)iter->args + arg_size;
8100 iter->next_arg++;
8102 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res->type->type, arg_size, res->value); */
8105 void
8106 ves_icall_System_ArgIterator_IntGetNextArgWithType (MonoArgIterator *iter, MonoTypedRef *res, MonoType *type)
8108 guint32 i, arg_size;
8109 gint32 align;
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]))
8117 continue;
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));
8124 #endif
8125 res->value = iter->args;
8126 iter->args = (char*)iter->args + arg_size;
8127 iter->next_arg++;
8128 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value); */
8129 return;
8131 /* g_print ("arg type 0x%02x not found\n", res.type->type); */
8133 memset (res, 0, sizeof (MonoTypedRef));
8136 MonoType*
8137 ves_icall_System_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
8139 gint i;
8141 i = iter->sig->sentinelpos + iter->next_arg;
8143 g_assert (i < iter->sig->param_count);
8145 return iter->sig->params [i];
8148 MonoObjectHandle
8149 ves_icall_System_TypedReference_ToObject (MonoTypedRef* tref, MonoError *error)
8151 return typed_reference_to_object (tref, error);
8154 void
8155 ves_icall_System_TypedReference_InternalMakeTypedReference (MonoTypedRef *res, MonoObjectHandle target, MonoArrayHandle fields, MonoReflectionTypeHandle last_field, MonoError *error)
8157 MonoClass *klass;
8158 MonoType *ftype = NULL;
8159 int i;
8161 memset (res, 0, sizeof (MonoTypedRef));
8163 g_assert (mono_array_handle_length (fields) > 0);
8165 klass = mono_handle_class (target);
8167 int offset = 0;
8168 for (i = 0; i < mono_array_handle_length (fields); ++i) {
8169 MonoClassField *f;
8170 MONO_HANDLE_ARRAY_GETVAL (f, fields, MonoClassField*, i);
8172 g_assert (f);
8174 if (i == 0)
8175 offset = f->offset;
8176 else
8177 offset += f->offset - sizeof (MonoObject);
8178 klass = mono_class_from_mono_type_internal (f->type);
8179 ftype = f->type;
8182 res->type = ftype;
8183 res->klass = mono_class_from_mono_type_internal (ftype);
8184 res->value = (guint8*)MONO_HANDLE_RAW (target) + offset;
8187 static void
8188 prelink_method (MonoMethod *method, MonoError *error)
8190 error_init (error);
8191 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8192 return;
8193 mono_lookup_pinvoke_call_internal (method, error);
8194 /* create the wrapper, too? */
8197 void
8198 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethodHandle method, MonoError *error)
8200 error_init (error);
8202 prelink_method (MONO_HANDLE_GETVAL (method, method), error);
8205 void
8206 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error)
8208 error_init (error);
8209 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
8210 MonoMethod* m;
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
8226 MonoStringHandle
8227 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetRuntimeArchitecture (MonoError *error)
8229 error_init (error);
8230 return mono_string_new_handle (mono_domain_get (), mono_config_get_cpu (), error);
8234 * used by System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform
8236 MonoStringHandle
8237 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetOSName (MonoError *error)
8239 error_init (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");
8256 #endif
8258 return snprintf(buffer, bufferLength, format, value);
8261 void
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);
8266 #endif
8270 /* These parameters are "readonly" in corlib/System/NumberFormatter.cs */
8271 void
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;
8287 static gboolean
8288 add_modifier_to_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int dest_idx, MonoError *error)
8290 HANDLE_FUNCTION_ENTER ();
8291 error_init (error);
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);
8299 leave:
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)
8310 int i, count = 0;
8311 MonoDomain *domain = mono_domain_get ();
8313 int cmod_count = mono_type_custom_modifier_count (type);
8314 if (cmod_count == 0)
8315 goto fail;
8317 error_init (error);
8318 for (i = 0; i < cmod_count; ++i) {
8319 gboolean required;
8320 (void) mono_type_get_custom_modifier (type, i, &required, error);
8321 goto_if_nok (error, fail);
8322 if ((optional && !required) || (!optional && required))
8323 count++;
8325 if (!count)
8326 goto fail;
8328 MonoArrayHandle res;
8329 res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
8330 goto_if_nok (error, fail);
8331 count = 0;
8332 for (i = 0; i < cmod_count; ++i) {
8333 gboolean required;
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))
8338 goto fail;
8339 count++;
8342 return res;
8343 fail:
8344 return MONO_HANDLE_NEW (MonoArray, NULL);
8347 MonoArrayHandle
8348 ves_icall_RuntimeParameterInfo_GetTypeModifiers (MonoReflectionTypeHandle rt, MonoObjectHandle member, int pos, MonoBoolean optional, MonoError *error)
8350 error_init (error);
8351 MonoType *type = MONO_HANDLE_GETVAL (rt, type);
8352 MonoClass *member_class = mono_handle_class (member);
8353 MonoMethod *method = NULL;
8354 MonoImage *image;
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))
8362 method = prop->set;
8363 g_assert (method);
8364 } else {
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);
8367 g_free (type_name);
8368 return NULL_HANDLE_ARRAY;
8371 image = m_class_get_image (method->klass);
8372 sig = mono_method_signature_internal (method);
8373 if (pos == -1)
8374 type = sig->ret;
8375 else
8376 type = sig->params [pos];
8378 return type_array_from_modifiers (image, type, optional, error);
8381 static MonoType*
8382 get_property_type (MonoProperty *prop)
8384 MonoMethodSignature *sig;
8385 if (prop->get) {
8386 sig = mono_method_signature_internal (prop->get);
8387 return sig->ret;
8388 } else if (prop->set) {
8389 sig = mono_method_signature_internal (prop->set);
8390 return sig->params [sig->param_count - 1];
8392 return NULL;
8395 MonoArrayHandle
8396 ves_icall_RuntimePropertyInfo_GetTypeModifiers (MonoReflectionPropertyHandle property, MonoBoolean optional, MonoError *error)
8398 error_init (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);
8404 if (!type)
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.
8416 static void
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);
8427 } else
8428 type->data.klass = mono_class_from_mono_type_internal (real_type);
8431 MonoObject*
8432 ves_icall_property_info_get_default_value (MonoReflectionProperty *property)
8434 ERROR_DECL (error);
8435 MonoType blob_type;
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;
8441 MonoObject *o;
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);
8448 return NULL;
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);
8457 return o;
8460 MonoBoolean
8461 ves_icall_MonoCustomAttrs_IsDefinedInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoError *error)
8463 error_init (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);
8472 if (!cinfo)
8473 return FALSE;
8474 gboolean found = mono_custom_attrs_has_attr (cinfo, attr_class);
8475 if (!cinfo->cached)
8476 mono_custom_attrs_free (cinfo);
8477 return found;
8480 MonoArrayHandle
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))
8485 attr_class = NULL;
8486 else
8487 attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
8489 if (attr_class) {
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);
8497 MonoArrayHandle
8498 ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObjectHandle obj, MonoError *error)
8500 error_init (error);
8501 return mono_reflection_get_custom_attrs_data_checked (obj, error);
8505 MonoStringHandle
8506 ves_icall_Mono_Runtime_GetDisplayName (MonoError *error)
8508 char *info;
8509 MonoStringHandle display_name;
8511 error_init (error);
8512 info = mono_get_runtime_callbacks ()->get_runtime_build_info ();
8513 display_name = mono_string_new_handle (mono_domain_get (), info, error);
8514 g_free (info);
8515 return display_name;
8518 #ifndef HOST_WIN32
8519 static inline gint32
8520 mono_icall_wait_for_input_idle (gpointer handle, gint32 milliseconds)
8522 return WAIT_TIMEOUT;
8524 #endif /* !HOST_WIN32 */
8526 gint32
8527 ves_icall_Microsoft_Win32_NativeMethods_WaitForInputIdle (gpointer handle, gint32 milliseconds, MonoError *error)
8529 return mono_icall_wait_for_input_idle (handle, milliseconds);
8532 gint32
8533 ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcessId (MonoError *error)
8535 return mono_process_current_pid ();
8538 MonoBoolean
8539 ves_icall_Mono_TlsProviderFactory_IsBtlsSupported (MonoError *error)
8541 #if HAVE_BTLS
8542 return TRUE;
8543 #else
8544 return FALSE;
8545 #endif
8548 #ifndef DISABLE_COM
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.");
8554 return 0;
8557 MonoObjectHandle
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.");
8561 return NULL_HANDLE;
8564 void*
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.");
8568 return NULL;
8571 MonoObjectHandle
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.");
8575 return NULL_HANDLE;
8578 MonoBoolean
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.");
8582 return FALSE;
8585 void
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.");
8595 return 0;
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.");
8602 return 0;
8605 mono_unichar2*
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.");
8609 return NULL;
8612 #endif
8614 #if !ENABLE_NETCORE
8616 void
8617 ves_icall_System_IO_LogcatTextWriter_Log (const char *appname, gint32 level, const char *message)
8619 g_log (appname, (GLogLevelFlags)level, "%s", message);
8622 #endif
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;
8631 guint32 flags;
8632 } MonoIcallHashTableValue;
8634 void
8635 mono_install_icall_table_callbacks (const MonoIcallTableCallbacks *cb)
8637 g_assert (cb->version == MONO_ICALL_TABLE_CALLBACKS_VERSION);
8638 icall_table = cb;
8641 void
8642 mono_icall_init (void)
8644 #ifndef DISABLE_ICALL_TABLES
8645 mono_icall_table_init ();
8646 #endif
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);
8651 static void
8652 mono_icall_lock (void)
8654 mono_locks_os_acquire (&icall_mutex, IcallLock);
8657 static void
8658 mono_icall_unlock (void)
8660 mono_locks_os_release (&icall_mutex, IcallLock);
8663 void
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);
8671 static void
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);
8676 if (key && value) {
8677 value->method = method;
8678 value->flags = flags;
8680 mono_icall_lock ();
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>
8718 * page.
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.
8723 void
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
8735 * operation.
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.
8748 void
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.
8772 void
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);
8778 void
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.
8788 static int
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)
8795 return 0;
8796 if (nspacelen) {
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;
8805 static void
8806 no_icall_table (void)
8808 g_assert_not_reached ();
8811 gconstpointer
8812 mono_lookup_internal_call_full_with_flags (MonoMethod *method, gboolean warn_on_missing, guint32 *flags)
8814 char *sigstart;
8815 char *tmpsig;
8816 char mname [2048];
8817 char *classname;
8818 int typelen = 0, mlen, siglen;
8819 gconstpointer res;
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));
8828 if (!pos)
8829 return NULL;
8831 mname [pos++] = '/';
8832 mname [pos] = 0;
8834 typelen = concat_class_name (mname+pos, sizeof (mname)-pos-1, method->klass);
8835 if (!typelen)
8836 return NULL;
8838 typelen += pos;
8839 } else {
8840 typelen = concat_class_name (mname, sizeof (mname), method->klass);
8841 if (!typelen)
8842 return NULL;
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;
8853 *sigstart = 0;
8855 tmpsig = mono_signature_get_desc (mono_method_signature_internal (method), TRUE);
8856 siglen = strlen (tmpsig);
8857 if (typelen + mlen + siglen + 6 > sizeof (mname)) {
8858 g_free (classname);
8859 return NULL;
8861 sigstart [0] = '(';
8862 memcpy (sigstart + 1, tmpsig, siglen);
8863 sigstart [siglen + 1] = ')';
8864 sigstart [siglen + 2] = 0;
8865 g_free (tmpsig);
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;
8871 mono_icall_lock ();
8873 res = g_hash_table_lookup (icall_hash, mname);
8874 if (res) {
8875 MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
8876 if (flags)
8877 *flags = value->flags;
8878 res = value->method;
8879 mono_icall_unlock ();
8880 g_free (classname);
8881 return res;
8884 /* try without signature */
8885 *sigstart = 0;
8886 res = g_hash_table_lookup (icall_hash, mname);
8887 if (res) {
8888 MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
8889 if (flags)
8890 *flags = value->flags;
8891 res = value->method;
8892 mono_icall_unlock ();
8893 g_free (classname);
8894 return res;
8897 if (!icall_table) {
8898 mono_icall_unlock ();
8899 g_free (classname);
8900 /* Fail only when the result is actually used */
8901 return (gconstpointer)no_icall_table;
8902 } else {
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 ();
8909 g_free (classname);
8911 if (res)
8912 return res;
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");
8924 return NULL;
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.
8938 gconstpointer
8939 mono_lookup_internal_call_full (MonoMethod *method, gboolean warn_on_missing, mono_bool *uses_handles, mono_bool *foreign)
8941 if (uses_handles)
8942 *uses_handles = FALSE;
8943 if (foreign)
8944 *foreign = 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))
8952 *foreign = TRUE;
8953 return addr;
8957 * mono_lookup_internal_call:
8959 gpointer
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.
8970 const char*
8971 mono_lookup_icall_symbol (MonoMethod *m)
8973 if (!icall_table)
8974 return NULL;
8976 g_assert (icall_table->lookup_icall_symbol);
8977 gpointer func;
8978 func = (gpointer)mono_lookup_internal_call_full (m, FALSE, NULL, NULL);
8979 if (!func)
8980 return 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
8987 * we need CDECL.
8989 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION MONO_CALL_C
8990 #else
8991 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION 0
8992 #endif
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,
9018 } ICallSigType;
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.
9032 static struct {
9033 #define ICALL_SIG(n, xtypes) \
9034 struct { \
9035 MonoMethodSignature sig; \
9036 gsize types [n]; \
9037 } ICALL_SIG_NAME (n, xtypes);
9038 ICALL_SIGS
9039 MonoMethodSignature end; // terminal zeroed element
9040 } mono_icall_signatures = {
9041 #undef ICALL_SIG
9042 #define ICALL_SIG(n, types) { { \
9043 0, /* ret */ \
9044 n, /* param_count */ \
9045 -1, /* sentinelpos */ \
9046 0, /* generic_param_count */ \
9047 MONO_ICALL_SIGNATURE_CALL_CONVENTION, \
9048 0, /* hasthis */ \
9049 0, /* explicit_this */ \
9050 1, /* pinvoke */ \
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 */
9055 ICALL_SIGS
9058 #undef ICALL_SIG
9059 #define ICALL_SIG(n, types) MonoMethodSignature * const ICALL_SIG_NAME (n, types) = &mono_icall_signatures.ICALL_SIG_NAME (n, types).sig;
9060 ICALL_SIGS
9061 #undef ICALL_SIG
9063 void
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;
9096 int n;
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
9105 // to params [].
9106 *(gsize*)(i ? &sig->params [i - 1] : &sig->ret) = (gsize)lookup [index];
9108 sig = (MonoMethodSignature*)types;
9112 MonoJitICallInfo *
9113 mono_find_jit_icall_by_addr (gconstpointer addr)
9115 MonoJitICallInfo *info;
9116 g_assert (jit_icall_hash_addr);
9118 mono_icall_lock ();
9119 info = (MonoJitICallInfo *)g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
9120 mono_icall_unlock ();
9122 return info;
9125 void
9126 mono_register_jit_icall_wrapper (MonoJitICallInfo *info, gconstpointer wrapper)
9128 mono_icall_lock ();
9129 g_hash_table_insert (jit_icall_hash_addr, (gpointer)wrapper, info);
9130 mono_icall_unlock ();
9133 void
9134 mono_register_jit_icall_info (MonoJitICallInfo *info, gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean avoid_wrapper, const char *c_symbol)
9136 mono_icall_lock ();
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.
9150 info->name = name;
9151 info->func = func;
9152 info->sig = sig;
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;
9159 } else {
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 ();
9182 gint64
9183 ves_icall_System_GC_GetAllocatedBytesForCurrentThread (void)
9185 return 0; // TODO: implement https://github.com/mono/mono/issues/8397
9188 void
9189 ves_icall_System_GC_RecordPressure (gint64 value)
9191 mono_gc_add_memory_pressure (value);
9194 gint64
9195 ves_icall_System_Diagnostics_Stopwatch_GetTimestamp (void)
9197 return mono_100ns_ticks ();
9200 gint64
9201 ves_icall_System_Threading_Timer_GetTimeMonotonic (void)
9203 return mono_100ns_ticks ();
9206 gint64
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 ();
9218 MonoBoolean
9219 ves_icall_System_Threading_Thread_YieldInternal (void)
9221 mono_threads_platform_yield ();
9222 return TRUE;
9225 gint32
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);
9237 gint32
9238 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CreateNLSocket (void)
9240 return CreateNLSocket ();
9243 gint32
9244 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
9246 return ReadEvents (sock, buffer, count, size);
9249 gint32
9250 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CloseNLSocket (gpointer sock)
9252 return CloseNLSocket (sock);
9255 #endif
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"
9278 #undef HANDLES
9279 #undef HANDLES_REUSE_WRAPPER
9280 #undef ICALL_TYPE
9281 #undef ICALL
9282 #undef NOHANDLES
9283 #undef MONO_HANDLE_REGISTER_ICALL