[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mono / metadata / icall.c
blob6b73270845d30a9bf1694eeb7ee483acda198143
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/w32socket.h>
60 #include <mono/metadata/mono-endian.h>
61 #include <mono/metadata/tokentype.h>
62 #include <mono/metadata/metadata-internals.h>
63 #include <mono/metadata/class-internals.h>
64 #include <mono/metadata/class-init.h>
65 #include <mono/metadata/reflection-internals.h>
66 #include <mono/metadata/marshal.h>
67 #include <mono/metadata/gc-internals.h>
68 #include <mono/metadata/mono-gc.h>
69 #include <mono/metadata/rand.h>
70 #include <mono/metadata/appdomain-icalls.h>
71 #include <mono/metadata/string-icalls.h>
72 #include <mono/metadata/debug-helpers.h>
73 #include <mono/metadata/w32process.h>
74 #include <mono/metadata/environment.h>
75 #include <mono/metadata/profiler-private.h>
76 #include <mono/metadata/locales.h>
77 #include <mono/metadata/filewatcher.h>
78 #include <mono/metadata/security.h>
79 #include <mono/metadata/mono-config.h>
80 #include <mono/metadata/cil-coff.h>
81 #include <mono/metadata/number-formatter.h>
82 #include <mono/metadata/security-manager.h>
83 #include <mono/metadata/security-core-clr.h>
84 #include <mono/metadata/mono-perfcounters.h>
85 #include <mono/metadata/mono-debug.h>
86 #include <mono/metadata/mono-ptr-array.h>
87 #include <mono/metadata/verify-internals.h>
88 #include <mono/metadata/runtime.h>
89 #include <mono/metadata/file-mmap.h>
90 #include <mono/metadata/seq-points-data.h>
91 #include <mono/metadata/icall-table.h>
92 #include <mono/metadata/handle.h>
93 #include <mono/metadata/w32mutex.h>
94 #include <mono/metadata/w32semaphore.h>
95 #include <mono/metadata/w32event.h>
96 #include <mono/metadata/abi-details.h>
97 #include <mono/metadata/loader-internals.h>
98 #include <mono/utils/monobitset.h>
99 #include <mono/utils/mono-time.h>
100 #include <mono/utils/mono-proclib.h>
101 #include <mono/utils/mono-string.h>
102 #include <mono/utils/mono-error-internals.h>
103 #include <mono/utils/mono-mmap.h>
104 #include <mono/utils/mono-io-portability.h>
105 #include <mono/utils/mono-digest.h>
106 #include <mono/utils/bsearch.h>
107 #include <mono/utils/mono-os-mutex.h>
108 #include <mono/utils/mono-threads.h>
109 #include <mono/metadata/w32error.h>
110 #include <mono/utils/w32api.h>
111 #include <mono/utils/mono-merp.h>
112 #include <mono/utils/mono-state.h>
113 #include <mono/utils/mono-logger-internals.h>
114 #include <mono/utils/mono-math.h>
115 #if !defined(HOST_WIN32) && defined(HAVE_SYS_UTSNAME_H)
116 #include <sys/utsname.h>
117 #endif
118 #include "icall-decl.h"
119 #include "mono/utils/mono-threads-coop.h"
120 #include "mono/metadata/icall-signatures.h"
121 #include "mono/utils/mono-signal-handler.h"
123 #if _MSC_VER
124 #pragma warning(disable:4047) // FIXME differs in levels of indirection
125 #endif
127 //#define MONO_DEBUG_ICALLARRAY
129 // Inline with CoreCLR heuristics, https://github.com/dotnet/runtime/blob/69e114c1abf91241a0eeecf1ecceab4711b8aa62/src/coreclr/vm/threads.cpp#L6408.
130 // Minimum stack size should be sufficient to allow a typical non-recursive call chain to execute,
131 // including potential exception handling and garbage collection. Used for probing for available
132 // stack space through RuntimeHelpers.EnsureSufficientExecutionStack.
133 #if TARGET_SIZEOF_VOID_P == 8
134 #define MONO_MIN_EXECUTION_STACK_SIZE (128 * 1024)
135 #else
136 #define MONO_MIN_EXECUTION_STACK_SIZE (64 * 1024)
137 #endif
139 #ifdef MONO_DEBUG_ICALLARRAY
141 static char debug_icallarray; // 0:uninitialized 1:true 2:false
143 static gboolean
144 icallarray_print_enabled (void)
146 if (!debug_icallarray)
147 debug_icallarray = MONO_TRACE_IS_TRACED (G_LOG_LEVEL_DEBUG, MONO_TRACE_ICALLARRAY) ? 1 : 2;
148 return debug_icallarray == 1;
151 static void
152 icallarray_print (const char *format, ...)
154 if (!icallarray_print_enabled ())
155 return;
156 va_list args;
157 va_start (args, format);
158 g_printv (format, args);
159 va_end (args);
162 #else
163 #define icallarray_print_enabled() (FALSE)
164 #define icallarray_print(...) /* nothing */
165 #endif
167 /* Lazy class loading functions */
168 static GENERATE_GET_CLASS_WITH_CACHE (module, "System.Reflection", "Module")
170 static void
171 array_set_value_impl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, gboolean strict_enums, gboolean strict_signs, MonoError *error);
173 static MonoArrayHandle
174 type_array_from_modifiers (MonoType *type, int optional, MonoError *error);
176 static inline MonoBoolean
177 is_generic_parameter (MonoType *type)
179 return !type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR);
182 #ifndef HOST_WIN32
183 static inline void
184 mono_icall_make_platform_path (gchar *path)
186 return;
189 static inline const gchar *
190 mono_icall_get_file_path_prefix (const gchar *path)
192 return "file://";
194 #endif /* HOST_WIN32 */
196 MonoJitICallInfos mono_jit_icall_info;
198 MonoObjectHandle
199 ves_icall_System_Array_GetValueImpl (MonoArrayHandle array, guint32 pos, MonoError *error)
201 MonoClass * const array_class = mono_handle_class (array);
202 MonoClass * const element_class = m_class_get_element_class (array_class);
205 if (m_class_is_valuetype (element_class)) {
206 gsize element_size = mono_array_element_size (array_class);
207 gpointer element_address = mono_array_addr_with_size_fast (MONO_HANDLE_RAW (array), element_size, (gsize)pos);
208 return mono_value_box_handle (MONO_HANDLE_DOMAIN (array), element_class, element_address, error);
210 MonoObjectHandle result = mono_new_null ();
211 mono_handle_array_getref (result, array, pos);
212 return result;
215 MonoObjectHandle
216 ves_icall_System_Array_GetValue (MonoArrayHandle arr, MonoArrayHandle indices, MonoError *error)
218 MONO_CHECK_ARG_NULL_HANDLE (indices, NULL_HANDLE);
220 MonoClass * const indices_class = mono_handle_class (indices);
221 MonoClass * const array_class = mono_handle_class (arr);
223 g_assert (m_class_get_rank (indices_class) == 1);
225 if (MONO_HANDLE_GETVAL (indices, bounds) || MONO_HANDLE_GETVAL (indices, max_length) != m_class_get_rank (array_class)) {
226 mono_error_set_argument (error, NULL, NULL);
227 return NULL_HANDLE;
230 gint32 index = 0;
232 if (!MONO_HANDLE_GETVAL (arr, bounds)) {
233 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, 0);
234 if (index < 0 || index >= MONO_HANDLE_GETVAL (arr, max_length)) {
235 mono_error_set_index_out_of_range (error);
236 return NULL_HANDLE;
239 return ves_icall_System_Array_GetValueImpl (arr, index, error);
242 for (gint32 i = 0; i < m_class_get_rank (array_class); i++) {
243 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, i);
244 if ((index < MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound)) ||
245 (index >= (mono_array_lower_bound_t)MONO_HANDLE_GETVAL (arr, bounds [i].length) + MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound))) {
246 mono_error_set_index_out_of_range (error);
247 return NULL_HANDLE;
251 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, 0);
252 gint32 pos = index - MONO_HANDLE_GETVAL (arr, bounds [0].lower_bound);
253 for (gint32 i = 1; i < m_class_get_rank (array_class); i++) {
254 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, i);
255 pos = pos * MONO_HANDLE_GETVAL (arr, bounds [i].length) + index -
256 MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound);
259 return ves_icall_System_Array_GetValueImpl (arr, pos, error);
262 void
263 ves_icall_System_Array_SetValueImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
265 array_set_value_impl (arr, value, pos, FALSE, TRUE, error);
268 static inline void
269 set_invalid_cast (MonoError *error, MonoClass *src_class, MonoClass *dst_class)
271 mono_get_runtime_callbacks ()->set_cast_details (src_class, dst_class);
272 mono_error_set_invalid_cast (error);
276 static void
277 array_set_value_impl (MonoArrayHandle arr_handle, MonoObjectHandle value_handle, guint32 pos, gboolean strict_enums, gboolean strict_signs, MonoError *error)
279 MonoClass *ac, *vc, *ec;
280 gint32 esize, vsize;
281 gpointer *ea = NULL, *va = NULL;
283 guint64 u64 = 0;
284 gint64 i64 = 0;
285 gdouble r64 = 0;
286 gboolean castOk = FALSE;
287 gboolean et_isenum = FALSE;
288 gboolean vt_isenum = FALSE;
290 error_init (error);
292 if (!MONO_HANDLE_IS_NULL (value_handle))
293 vc = mono_handle_class (value_handle);
294 else
295 vc = NULL;
297 ac = mono_handle_class (arr_handle);
298 ec = m_class_get_element_class (ac);
299 esize = mono_array_element_size (ac);
301 if (mono_class_is_nullable (ec)) {
302 MONO_ENTER_NO_SAFEPOINTS;
303 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
304 if (!MONO_HANDLE_IS_NULL (value_handle))
305 va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
306 mono_nullable_init_unboxed ((guint8*)ea, va, ec);
307 MONO_EXIT_NO_SAFEPOINTS;
308 goto leave;
311 if (MONO_HANDLE_IS_NULL (value_handle)) {
312 MONO_ENTER_NO_SAFEPOINTS;
313 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
314 mono_gc_bzero_atomic (ea, esize);
315 MONO_EXIT_NO_SAFEPOINTS;
316 goto leave;
319 #define WIDENING_MSG "not a widening conversion"
320 #define WIDENING_ARG "value"
322 #define NO_WIDENING_CONVERSION G_STMT_START{ \
323 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
324 break; \
325 }G_STMT_END
327 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{ \
328 if (esize < vsize + (extra)) { \
329 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
330 break; \
332 }G_STMT_END
334 #define INVALID_CAST G_STMT_START{ \
335 mono_get_runtime_callbacks ()->set_cast_details (vc, ec); \
336 mono_error_set_invalid_cast (error); \
337 break; \
338 }G_STMT_END
340 MonoTypeEnum et;
341 et = m_class_get_byval_arg (ec)->type;
342 MonoTypeEnum vt;
343 vt = m_class_get_byval_arg (vc)->type;
345 /* Check element (destination) type. */
346 switch (et) {
347 case MONO_TYPE_STRING:
348 switch (vt) {
349 case MONO_TYPE_STRING:
350 break;
351 default:
352 INVALID_CAST;
354 break;
355 case MONO_TYPE_BOOLEAN:
356 switch (vt) {
357 case MONO_TYPE_BOOLEAN:
358 break;
359 case MONO_TYPE_CHAR:
360 case MONO_TYPE_U1:
361 case MONO_TYPE_U2:
362 case MONO_TYPE_U4:
363 case MONO_TYPE_U8:
364 case MONO_TYPE_I1:
365 case MONO_TYPE_I2:
366 case MONO_TYPE_I4:
367 case MONO_TYPE_I8:
368 case MONO_TYPE_R4:
369 case MONO_TYPE_R8:
370 NO_WIDENING_CONVERSION;
371 break;
372 default:
373 INVALID_CAST;
375 break;
376 default:
377 break;
379 if (!is_ok (error))
380 goto leave;
382 castOk = mono_object_handle_isinst_mbyref_raw (value_handle, ec, error);
383 if (!is_ok (error))
384 goto leave;
386 if (!m_class_is_valuetype (ec)) {
387 if (!castOk)
388 INVALID_CAST;
389 if (is_ok (error))
390 MONO_HANDLE_ARRAY_SETREF (arr_handle, pos, value_handle);
391 goto leave;
394 if (castOk) {
395 MONO_ENTER_NO_SAFEPOINTS;
396 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
397 va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
398 if (m_class_has_references (ec))
399 mono_value_copy_internal (ea, va, ec);
400 else
401 mono_gc_memmove_atomic (ea, va, esize);
402 MONO_EXIT_NO_SAFEPOINTS;
404 goto leave;
407 if (!m_class_is_valuetype (vc))
408 INVALID_CAST;
410 if (!is_ok (error))
411 goto leave;
413 vsize = mono_class_value_size (vc, NULL);
415 et_isenum = et == MONO_TYPE_VALUETYPE && m_class_is_enumtype (m_class_get_byval_arg (ec)->data.klass);
416 vt_isenum = vt == MONO_TYPE_VALUETYPE && m_class_is_enumtype (m_class_get_byval_arg (vc)->data.klass);
419 if (et_isenum)
420 et = mono_class_enum_basetype_internal (m_class_get_byval_arg (ec)->data.klass)->type;
422 if (vt_isenum)
423 vt = mono_class_enum_basetype_internal (m_class_get_byval_arg (vc)->data.klass)->type;
426 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
427 switch (vt) { \
428 case MONO_TYPE_U1: \
429 case MONO_TYPE_U2: \
430 case MONO_TYPE_U4: \
431 case MONO_TYPE_U8: \
432 case MONO_TYPE_CHAR: \
433 CHECK_WIDENING_CONVERSION(0); \
434 *(etype *) ea = (etype) u64; \
435 break; \
436 /* You can't assign a signed value to an unsigned array. */ \
437 case MONO_TYPE_I1: \
438 case MONO_TYPE_I2: \
439 case MONO_TYPE_I4: \
440 case MONO_TYPE_I8: \
441 if (!strict_signs) { \
442 CHECK_WIDENING_CONVERSION(0); \
443 *(etype *) ea = (etype) i64; \
444 break; \
446 /* You can't assign a floating point number to an integer array. */ \
447 case MONO_TYPE_R4: \
448 case MONO_TYPE_R8: \
449 NO_WIDENING_CONVERSION; \
450 break; \
451 default: \
452 INVALID_CAST; \
453 break; \
455 }G_STMT_END
457 #define ASSIGN_SIGNED(etype) G_STMT_START{\
458 switch (vt) { \
459 case MONO_TYPE_I1: \
460 case MONO_TYPE_I2: \
461 case MONO_TYPE_I4: \
462 case MONO_TYPE_I8: \
463 CHECK_WIDENING_CONVERSION(0); \
464 *(etype *) ea = (etype) i64; \
465 break; \
466 /* You can assign an unsigned value to a signed array if the array's */ \
467 /* element size is larger than the value size. */ \
468 case MONO_TYPE_U1: \
469 case MONO_TYPE_U2: \
470 case MONO_TYPE_U4: \
471 case MONO_TYPE_U8: \
472 case MONO_TYPE_CHAR: \
473 CHECK_WIDENING_CONVERSION(strict_signs ? 1 : 0); \
474 *(etype *) ea = (etype) u64; \
475 break; \
476 /* You can't assign a floating point number to an integer array. */ \
477 case MONO_TYPE_R4: \
478 case MONO_TYPE_R8: \
479 NO_WIDENING_CONVERSION; \
480 break; \
481 default: \
482 INVALID_CAST; \
483 break; \
485 }G_STMT_END
487 #define ASSIGN_REAL(etype) G_STMT_START{\
488 switch (vt) { \
489 case MONO_TYPE_R4: \
490 case MONO_TYPE_R8: \
491 CHECK_WIDENING_CONVERSION(0); \
492 *(etype *) ea = (etype) r64; \
493 break; \
494 /* All integer values fit into a floating point array, so we don't */ \
495 /* need to CHECK_WIDENING_CONVERSION here. */ \
496 case MONO_TYPE_I1: \
497 case MONO_TYPE_I2: \
498 case MONO_TYPE_I4: \
499 case MONO_TYPE_I8: \
500 *(etype *) ea = (etype) i64; \
501 break; \
502 case MONO_TYPE_U1: \
503 case MONO_TYPE_U2: \
504 case MONO_TYPE_U4: \
505 case MONO_TYPE_U8: \
506 case MONO_TYPE_CHAR: \
507 *(etype *) ea = (etype) u64; \
508 break; \
509 default: \
510 INVALID_CAST; \
511 break; \
513 }G_STMT_END
515 MONO_ENTER_NO_SAFEPOINTS;
516 g_assert (!MONO_HANDLE_IS_NULL (value_handle));
517 g_assert (m_class_is_valuetype (vc));
518 va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
519 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
521 switch (vt) {
522 case MONO_TYPE_U1:
523 u64 = *(guint8 *) va;
524 break;
525 case MONO_TYPE_U2:
526 u64 = *(guint16 *) va;
527 break;
528 case MONO_TYPE_U4:
529 u64 = *(guint32 *) va;
530 break;
531 case MONO_TYPE_U8:
532 u64 = *(guint64 *) va;
533 break;
534 case MONO_TYPE_I1:
535 i64 = *(gint8 *) va;
536 break;
537 case MONO_TYPE_I2:
538 i64 = *(gint16 *) va;
539 break;
540 case MONO_TYPE_I4:
541 i64 = *(gint32 *) va;
542 break;
543 case MONO_TYPE_I8:
544 i64 = *(gint64 *) va;
545 break;
546 case MONO_TYPE_R4:
547 r64 = *(gfloat *) va;
548 break;
549 case MONO_TYPE_R8:
550 r64 = *(gdouble *) va;
551 break;
552 case MONO_TYPE_CHAR:
553 u64 = *(guint16 *) va;
554 break;
555 case MONO_TYPE_BOOLEAN:
556 /* Boolean is only compatible with itself. */
557 switch (et) {
558 case MONO_TYPE_CHAR:
559 case MONO_TYPE_U1:
560 case MONO_TYPE_U2:
561 case MONO_TYPE_U4:
562 case MONO_TYPE_U8:
563 case MONO_TYPE_I1:
564 case MONO_TYPE_I2:
565 case MONO_TYPE_I4:
566 case MONO_TYPE_I8:
567 case MONO_TYPE_R4:
568 case MONO_TYPE_R8:
569 NO_WIDENING_CONVERSION;
570 break;
571 default:
572 INVALID_CAST;
574 break;
575 default:
576 break;
578 /* If we can't do a direct copy, let's try a widening conversion. */
580 if (is_ok (error)) {
581 switch (et) {
582 case MONO_TYPE_CHAR:
583 ASSIGN_UNSIGNED (guint16);
584 break;
585 case MONO_TYPE_U1:
586 ASSIGN_UNSIGNED (guint8);
587 break;
588 case MONO_TYPE_U2:
589 ASSIGN_UNSIGNED (guint16);
590 break;
591 case MONO_TYPE_U4:
592 ASSIGN_UNSIGNED (guint32);
593 break;
594 case MONO_TYPE_U8:
595 ASSIGN_UNSIGNED (guint64);
596 break;
597 case MONO_TYPE_I1:
598 ASSIGN_SIGNED (gint8);
599 break;
600 case MONO_TYPE_I2:
601 ASSIGN_SIGNED (gint16);
602 break;
603 case MONO_TYPE_I4:
604 ASSIGN_SIGNED (gint32);
605 break;
606 case MONO_TYPE_I8:
607 ASSIGN_SIGNED (gint64);
608 break;
609 case MONO_TYPE_R4:
610 ASSIGN_REAL (gfloat);
611 break;
612 case MONO_TYPE_R8:
613 ASSIGN_REAL (gdouble);
614 break;
615 default:
616 INVALID_CAST;
620 MONO_EXIT_NO_SAFEPOINTS;
622 #undef INVALID_CAST
623 #undef NO_WIDENING_CONVERSION
624 #undef CHECK_WIDENING_CONVERSION
625 #undef ASSIGN_UNSIGNED
626 #undef ASSIGN_SIGNED
627 #undef ASSIGN_REAL
629 leave:
630 return;
633 void
634 ves_icall_System_Array_SetValue (MonoArrayHandle arr, MonoObjectHandle value,
635 MonoArrayHandle idxs, MonoError *error)
637 icallarray_print ("%s\n", __func__);
639 MonoArrayBounds dim;
640 MonoClass *ac, *ic;
641 gint32 idx;
642 gint32 i, pos;
644 error_init (error);
646 if (MONO_HANDLE_IS_NULL (idxs)) {
647 mono_error_set_argument_null (error, "idxs", "");
648 return;
651 ic = mono_handle_class (idxs);
652 ac = mono_handle_class (arr);
654 g_assert (m_class_get_rank (ic) == 1);
655 if (mono_handle_array_has_bounds (idxs) || MONO_HANDLE_GETVAL (idxs, max_length) != m_class_get_rank (ac)) {
656 mono_error_set_argument (error, "idxs", "");
657 return;
660 if (!mono_handle_array_has_bounds (arr)) {
661 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
662 if (idx < 0 || idx >= MONO_HANDLE_GETVAL (arr, max_length)) {
663 mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
664 return;
667 array_set_value_impl (arr, value, idx, TRUE, TRUE, error);
668 return;
671 gint32 ac_rank = m_class_get_rank (ac);
672 for (i = 0; i < ac_rank; i++) {
673 mono_handle_array_get_bounds_dim (arr, i, &dim);
674 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
675 if ((idx < dim.lower_bound) ||
676 (idx >= (mono_array_lower_bound_t)dim.length + dim.lower_bound)) {
677 mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
678 return;
682 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
683 mono_handle_array_get_bounds_dim (arr, 0, &dim);
684 pos = idx - dim.lower_bound;
685 for (i = 1; i < ac_rank; i++) {
686 mono_handle_array_get_bounds_dim (arr, i, &dim);
687 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
688 pos = pos * dim.length + idx - dim.lower_bound;
691 array_set_value_impl (arr, value, pos, TRUE, TRUE, error);
695 MonoArrayHandle
696 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionTypeHandle type, MonoArrayHandle lengths, MonoArrayHandle bounds, MonoError *error)
698 // FIXME? fixed could be used for lengths, bounds.
700 icallarray_print ("%s type:%p length:%p bounds:%p\n", __func__, type, lengths, bounds);
702 MONO_CHECK_ARG_NULL_HANDLE (type, NULL_HANDLE_ARRAY);
703 MONO_CHECK_ARG_NULL_HANDLE (lengths, NULL_HANDLE_ARRAY);
705 MONO_CHECK_ARG (lengths, mono_array_handle_length (lengths) > 0, NULL_HANDLE_ARRAY);
706 if (!MONO_HANDLE_IS_NULL (bounds))
707 MONO_CHECK_ARG (bounds, mono_array_handle_length (lengths) == mono_array_handle_length (bounds), NULL_HANDLE_ARRAY);
709 for (uintptr_t i = 0; i < mono_array_handle_length (lengths); ++i) {
710 gint32 length = 0;
711 MONO_HANDLE_ARRAY_GETVAL (length, lengths, gint32, i);
712 if (length < 0) {
713 mono_error_set_argument_out_of_range (error, NULL, "MonoArgumentException:NULL");
714 return NULL_HANDLE_ARRAY;
718 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
719 if (!mono_class_init_checked (klass, error))
720 return NULL_HANDLE_ARRAY;
722 if (m_class_get_byval_arg (m_class_get_element_class (klass))->type == MONO_TYPE_VOID) {
723 mono_error_set_not_supported (error, "Arrays of System.Void are not supported.");
724 return NULL_HANDLE_ARRAY;
727 /* vectors are not the same as one dimensional arrays with non-zero bounds */
728 gboolean bounded = FALSE;
729 if (!MONO_HANDLE_IS_NULL (bounds) && mono_array_handle_length (bounds) == 1) {
730 gint32 bound0 = 0;
731 MONO_HANDLE_ARRAY_GETVAL (bound0, bounds, gint32, 0);
732 bounded = bound0 != 0;
735 MonoClass * const aklass = mono_class_create_bounded_array (klass, mono_array_handle_length (lengths), bounded);
736 uintptr_t const aklass_rank = m_class_get_rank (aklass);
737 uintptr_t * const sizes = g_newa (uintptr_t, aklass_rank);
738 intptr_t * const lower_bounds = g_newa (intptr_t, aklass_rank);
740 // Copy lengths and lower_bounds from gint32 to [u]intptr_t.
742 for (uintptr_t i = 0; i < aklass_rank; ++i) {
743 MONO_HANDLE_ARRAY_GETVAL (sizes [i], lengths, gint32, i);
744 if (!MONO_HANDLE_IS_NULL (bounds))
745 MONO_HANDLE_ARRAY_GETVAL (lower_bounds [i], bounds, gint32, i);
746 else
747 lower_bounds [i] = 0;
750 return mono_array_new_full_handle (MONO_HANDLE_DOMAIN (type), aklass, sizes, lower_bounds, error);
753 gint32
754 ves_icall_System_Array_GetRank (MonoObjectHandle arr, MonoError *error)
756 gint32 const result = m_class_get_rank (mono_handle_class (arr));
758 icallarray_print ("%s arr:%p res:%d\n", __func__, MONO_HANDLE_RAW (arr), result);
760 return result;
764 static mono_array_size_t
765 mono_array_get_length (MonoArrayHandle arr, gint32 dimension, MonoError *error)
767 if (dimension < 0 || dimension >= m_class_get_rank (mono_handle_class (arr))) {
768 mono_error_set_index_out_of_range (error);
769 return 0;
772 return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].length)
773 : MONO_HANDLE_GETVAL (arr, max_length);
776 gint32
777 ves_icall_System_Array_GetLength (MonoArrayHandle arr, gint32 dimension, MonoError *error)
779 icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
781 mono_array_size_t const length = mono_array_get_length (arr, dimension, error);
782 if (length > G_MAXINT32) {
783 mono_error_set_overflow (error);
784 return 0;
786 return (gint32)length;
789 gint64
790 ves_icall_System_Array_GetLongLength (MonoArrayHandle arr, gint32 dimension, MonoError *error)
792 icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
794 return (gint64)mono_array_get_length (arr, dimension, error);
797 gint32
798 ves_icall_System_Array_GetLowerBound (MonoArrayHandle arr, gint32 dimension, MonoError *error)
800 icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
802 if (dimension < 0 || dimension >= m_class_get_rank (mono_handle_class (arr))) {
803 mono_error_set_index_out_of_range (error);
804 return 0;
807 return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].lower_bound)
808 : 0;
811 void
812 ves_icall_System_Array_ClearInternal (MonoArrayHandle arr, int idx, int length, MonoError *error)
814 icallarray_print ("%s arr:%p idx:%d len:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)idx, (int)length);
816 int sz = mono_array_element_size (mono_handle_class (arr));
817 mono_gc_bzero_atomic (mono_array_addr_with_size_fast (MONO_HANDLE_RAW (arr), sz, idx), length * sz);
820 MonoBoolean
821 ves_icall_System_Array_FastCopy (MonoArrayHandle source, int source_idx, MonoArrayHandle dest, int dest_idx, int length, MonoError *error)
823 MonoVTable * const src_vtable = MONO_HANDLE_GETVAL (source, obj.vtable);
824 MonoVTable * const dest_vtable = MONO_HANDLE_GETVAL (dest, obj.vtable);
826 if (src_vtable->rank != dest_vtable->rank)
827 return FALSE;
829 MonoArrayBounds *source_bounds = MONO_HANDLE_GETVAL (source, bounds);
830 MonoArrayBounds *dest_bounds = MONO_HANDLE_GETVAL (dest, bounds);
832 for (int i = 0; i < src_vtable->rank; i++) {
833 if ((source_bounds && source_bounds [i].lower_bound > 0) ||
834 (dest_bounds && dest_bounds [i].lower_bound > 0))
835 return FALSE;
838 /* there's no integer overflow since mono_array_length_internal returns an unsigned integer */
839 if ((dest_idx + length > mono_array_handle_length (dest)) ||
840 (source_idx + length > mono_array_handle_length (source)))
841 return FALSE;
843 MonoClass * const src_class = m_class_get_element_class (src_vtable->klass);
844 MonoClass * const dest_class = m_class_get_element_class (dest_vtable->klass);
847 * Handle common cases.
850 /* Case1: object[] -> valuetype[] (ArrayList::ToArray)
851 We fallback to managed here since we need to typecheck each boxed valuetype before storing them in the dest array.
853 if (src_class == mono_defaults.object_class && m_class_is_valuetype (dest_class))
854 return FALSE;
856 /* Check if we're copying a char[] <==> (u)short[] */
857 if (src_class != dest_class) {
858 if (m_class_is_valuetype (dest_class) || m_class_is_enumtype (dest_class) ||
859 m_class_is_valuetype (src_class) || m_class_is_valuetype (src_class))
860 return FALSE;
862 /* 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. */
863 if (!mono_class_is_subclass_of_internal (src_class, dest_class, FALSE))
864 return FALSE;
866 if (m_class_is_native_pointer (src_class) || m_class_is_native_pointer (dest_class))
867 return FALSE;
870 if (m_class_is_valuetype (dest_class)) {
871 gsize const element_size = mono_array_element_size (MONO_HANDLE_GETVAL (source, obj.vtable->klass));
873 MONO_ENTER_NO_SAFEPOINTS; // gchandle would also work here, is slow, breaks profiler tests.
875 gconstpointer const source_addr =
876 mono_array_addr_with_size_fast (MONO_HANDLE_RAW (source), element_size, source_idx);
877 if (m_class_has_references (dest_class)) {
878 mono_value_copy_array_handle (dest, dest_idx, source_addr, length);
879 } else {
880 gpointer const dest_addr =
881 mono_array_addr_with_size_fast (MONO_HANDLE_RAW (dest), element_size, dest_idx);
882 mono_gc_memmove_atomic (dest_addr, source_addr, element_size * length);
885 MONO_EXIT_NO_SAFEPOINTS;
886 } else {
887 mono_array_handle_memcpy_refs (dest, dest_idx, source, source_idx, length);
890 return TRUE;
893 void
894 ves_icall_System_Array_GetGenericValue_icall (MonoArray **arr, guint32 pos, gpointer value)
896 icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__, *arr, pos, value);
898 MONO_REQ_GC_UNSAFE_MODE; // because of gpointer value
900 MonoClass * const ac = mono_object_class (*arr);
901 gsize const esize = mono_array_element_size (ac);
902 gconstpointer * const ea = (gconstpointer*)((char*)(*arr)->vector + (pos * esize));
904 mono_gc_memmove_atomic (value, ea, esize);
907 void
908 ves_icall_System_Array_SetGenericValue_icall (MonoArray **arr, guint32 pos, gpointer value)
910 icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__, *arr, pos, value);
912 MONO_REQ_GC_UNSAFE_MODE; // because of gpointer value
914 MonoClass * const ac = mono_object_class (*arr);
915 MonoClass * const ec = m_class_get_element_class (ac);
917 gsize const esize = mono_array_element_size (ac);
918 gpointer * const ea = (gpointer*)((char*)(*arr)->vector + (pos * esize));
920 if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (ec))) {
921 g_assert (esize == sizeof (gpointer));
922 mono_gc_wbarrier_generic_store_internal (ea, *(MonoObject **)value);
923 } else {
924 g_assert (m_class_is_inited (ec));
925 g_assert (esize == mono_class_value_size (ec, NULL));
926 if (m_class_has_references (ec))
927 mono_gc_wbarrier_value_copy_internal (ea, value, 1, ec);
928 else
929 mono_gc_memmove_atomic (ea, value, esize);
933 void
934 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, guint byte_count)
936 mono_gc_memmove_atomic (destination, source, byte_count);
939 void
940 ves_icall_System_Runtime_RuntimeImports_Memmove_wbarrier (guint8 *destination, guint8 *source, guint len, MonoType *type)
942 if (MONO_TYPE_IS_REFERENCE (type))
943 mono_gc_wbarrier_arrayref_copy_internal (destination, source, len);
944 else
945 mono_gc_wbarrier_value_copy_internal (destination, source, len, mono_class_from_mono_type_internal (type));
948 void
949 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, guint byte_length)
951 memset (p, 0, byte_length);
954 void
955 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArrayHandle array, MonoClassField *field_handle, MonoError *error)
957 MonoClass *klass = mono_handle_class (array);
958 guint32 size = mono_array_element_size (klass);
959 MonoType *type = mono_type_get_underlying_type (m_class_get_byval_arg (m_class_get_element_class (klass)));
960 int align;
961 const char *field_data;
963 if (MONO_TYPE_IS_REFERENCE (type) || type->type == MONO_TYPE_VALUETYPE) {
964 mono_error_set_argument (error, "array", "Cannot initialize array of non-primitive type");
965 return;
968 MonoType *field_type = mono_field_get_type_checked (field_handle, error);
969 if (!field_type)
970 return;
972 if (!(field_type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)) {
973 mono_error_set_argument_format (error, "field_handle", "Field '%s' doesn't have an RVA", mono_field_get_name (field_handle));
974 return;
977 size *= MONO_HANDLE_GETVAL(array, max_length);
978 field_data = mono_field_get_data (field_handle);
980 if (size > mono_type_size (field_handle->type, &align)) {
981 mono_error_set_argument (error, "field_handle", "Field not large enough to fill array");
982 return;
985 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
986 #define SWAP(n) { \
987 guint ## n *data = (guint ## n *) mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0); \
988 guint ## n *src = (guint ## n *) field_data; \
989 int i, \
990 nEnt = (size / sizeof(guint ## n)); \
992 for (i = 0; i < nEnt; i++) { \
993 data[i] = read ## n (&src[i]); \
997 /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
999 switch (type->type) {
1000 case MONO_TYPE_CHAR:
1001 case MONO_TYPE_I2:
1002 case MONO_TYPE_U2:
1003 SWAP (16);
1004 break;
1005 case MONO_TYPE_I4:
1006 case MONO_TYPE_U4:
1007 case MONO_TYPE_R4:
1008 SWAP (32);
1009 break;
1010 case MONO_TYPE_I8:
1011 case MONO_TYPE_U8:
1012 case MONO_TYPE_R8:
1013 SWAP (64);
1014 break;
1015 default:
1016 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
1017 break;
1019 #else
1020 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
1021 #endif
1024 gint
1025 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
1027 return offsetof (MonoString, chars);
1030 MonoObjectHandle
1031 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObjectHandle obj, MonoError *error)
1033 if (MONO_HANDLE_IS_NULL (obj) || !m_class_is_valuetype (mono_handle_class (obj)))
1034 return obj;
1036 return mono_object_clone_handle (obj, error);
1039 void
1040 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle, MonoError *error)
1042 MonoClass *klass;
1043 MonoVTable *vtable;
1045 MONO_CHECK_ARG_NULL (handle,);
1047 klass = mono_class_from_mono_type_internal (handle);
1048 MONO_CHECK_ARG (handle, klass,);
1050 if (mono_class_is_gtd (klass))
1051 return;
1053 vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
1054 return_if_nok (error);
1056 /* This will call the type constructor */
1057 mono_runtime_class_init_full (vtable, error);
1060 void
1061 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (MonoImage *image, MonoError *error)
1063 mono_image_check_for_module_cctor (image);
1064 if (!image->has_module_cctor)
1065 return;
1067 MonoClass *module_klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | 1, error);
1068 return_if_nok (error);
1070 MonoVTable * vtable = mono_class_vtable_checked (mono_domain_get (), module_klass, error);
1071 return_if_nok (error);
1073 mono_runtime_class_init_full (vtable, error);
1076 MonoBoolean
1077 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
1079 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
1080 // It does not work on win32
1081 #elif defined(TARGET_ANDROID) || defined(__linux__)
1082 // No need for now
1083 #else
1084 guint8 *stack_addr;
1085 guint8 *current;
1086 size_t stack_size;
1087 int min_size;
1088 MonoInternalThread *thread;
1090 mono_thread_info_get_stack_bounds (&stack_addr, &stack_size);
1091 /* if we have no info we are optimistic and assume there is enough room */
1092 if (!stack_addr)
1093 return TRUE;
1095 thread = mono_thread_internal_current ();
1096 // .net seems to check that at least 50% of stack is available
1097 min_size = thread->stack_size / 2;
1099 // TODO: It's not always set
1100 if (!min_size)
1101 return TRUE;
1103 current = (guint8 *)&stack_addr;
1104 if (current > stack_addr) {
1105 if ((current - stack_addr) < min_size)
1106 return FALSE;
1107 } else {
1108 if (current - (stack_addr - stack_size) < min_size)
1109 return FALSE;
1111 #endif
1112 return TRUE;
1116 MonoObjectHandle
1117 ves_icall_System_Object_MemberwiseClone (MonoObjectHandle this_obj, MonoError *error)
1119 return mono_object_clone_handle (this_obj, error);
1122 gint32
1123 ves_icall_System_ValueType_InternalGetHashCode (MonoObjectHandle this_obj, MonoArrayHandleOut fields, MonoError *error)
1125 MonoClass *klass;
1126 MonoClassField **unhandled = NULL;
1127 int count = 0;
1128 gint32 result = (int)(gsize)mono_defaults.int32_class;
1129 MonoClassField* field;
1130 gpointer iter;
1132 klass = mono_handle_class (this_obj);
1134 if (mono_class_num_fields (klass) == 0)
1135 return result;
1138 * Compute the starting value of the hashcode for fields of primitive
1139 * types, and return the remaining fields in an array to the managed side.
1140 * This way, we can avoid costly reflection operations in managed code.
1142 iter = NULL;
1143 while ((field = mono_class_get_fields_internal (klass, &iter))) {
1144 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1145 continue;
1146 if (mono_field_is_deleted (field))
1147 continue;
1148 gpointer addr = (guint8*)MONO_HANDLE_RAW (this_obj) + field->offset;
1149 /* FIXME: Add more types */
1150 switch (field->type->type) {
1151 case MONO_TYPE_I4:
1152 result ^= *(gint32*)addr;
1153 break;
1154 case MONO_TYPE_PTR:
1155 result ^= mono_aligned_addr_hash (*(gpointer*)addr);
1156 break;
1157 case MONO_TYPE_STRING: {
1158 MonoString *s;
1159 s = *(MonoString**)addr;
1160 if (s != NULL)
1161 result ^= mono_string_hash_internal (s);
1162 break;
1164 default:
1165 if (!unhandled)
1166 unhandled = g_newa (MonoClassField*, mono_class_num_fields (klass));
1167 unhandled [count ++] = field;
1171 if (unhandled) {
1172 MonoArrayHandle fields_arr = mono_array_new_handle (mono_domain_get (), mono_defaults.object_class, count, error);
1173 return_val_if_nok (error, 0);
1174 MONO_HANDLE_ASSIGN (fields, fields_arr);
1175 MonoObjectHandle h = MONO_HANDLE_NEW (MonoObject, NULL);
1176 for (int i = 0; i < count; ++i) {
1177 MonoObject *o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (this_obj), error);
1178 return_val_if_nok (error, 0);
1179 MONO_HANDLE_ASSIGN_RAW (h, o);
1180 mono_array_handle_setref (fields_arr, i, h);
1182 } else {
1183 MONO_HANDLE_ASSIGN (fields, NULL_HANDLE);
1185 return result;
1188 MonoBoolean
1189 ves_icall_System_ValueType_Equals (MonoObjectHandle this_obj, MonoObjectHandle that, MonoArrayHandleOut fields, MonoError *error)
1191 MonoClass *klass;
1192 MonoClassField **unhandled = NULL;
1193 MonoClassField* field;
1194 gpointer iter;
1195 int count = 0;
1197 MONO_CHECK_ARG_NULL_HANDLE (that, FALSE);
1199 MONO_HANDLE_ASSIGN (fields, NULL_HANDLE);
1201 if (mono_handle_vtable (this_obj) != mono_handle_vtable (that))
1202 return FALSE;
1204 klass = mono_handle_class (this_obj);
1206 if (m_class_is_enumtype (klass) && mono_class_enum_basetype_internal (klass) && mono_class_enum_basetype_internal (klass)->type == MONO_TYPE_I4)
1207 return *(gint32*)mono_handle_get_data_unsafe (this_obj) == *(gint32*)mono_handle_get_data_unsafe (that);
1210 * Do the comparison for fields of primitive type and return a result if
1211 * possible. Otherwise, return the remaining fields in an array to the
1212 * managed side. This way, we can avoid costly reflection operations in
1213 * managed code.
1215 iter = NULL;
1216 while ((field = mono_class_get_fields_internal (klass, &iter))) {
1217 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1218 continue;
1219 if (mono_field_is_deleted (field))
1220 continue;
1221 guint8 *this_field = (guint8 *)MONO_HANDLE_RAW (this_obj) + field->offset;
1222 guint8 *that_field = (guint8 *)MONO_HANDLE_RAW (that) + field->offset;
1224 #define UNALIGNED_COMPARE(type) \
1225 do { \
1226 type left, right; \
1227 memcpy (&left, this_field, sizeof (type)); \
1228 memcpy (&right, that_field, sizeof (type)); \
1229 if (left != right) \
1230 return FALSE; \
1231 } while (0)
1233 /* FIXME: Add more types */
1234 switch (field->type->type) {
1235 case MONO_TYPE_U1:
1236 case MONO_TYPE_I1:
1237 case MONO_TYPE_BOOLEAN:
1238 if (*this_field != *that_field)
1239 return FALSE;
1240 break;
1241 case MONO_TYPE_U2:
1242 case MONO_TYPE_I2:
1243 case MONO_TYPE_CHAR:
1244 #ifdef NO_UNALIGNED_ACCESS
1245 if (G_UNLIKELY ((intptr_t) this_field & 1 || (intptr_t) that_field & 1))
1246 UNALIGNED_COMPARE (gint16);
1247 else
1248 #endif
1249 if (*(gint16 *) this_field != *(gint16 *) that_field)
1250 return FALSE;
1251 break;
1252 case MONO_TYPE_U4:
1253 case MONO_TYPE_I4:
1254 #ifdef NO_UNALIGNED_ACCESS
1255 if (G_UNLIKELY ((intptr_t) this_field & 3 || (intptr_t) that_field & 3))
1256 UNALIGNED_COMPARE (gint32);
1257 else
1258 #endif
1259 if (*(gint32 *) this_field != *(gint32 *) that_field)
1260 return FALSE;
1261 break;
1262 case MONO_TYPE_U8:
1263 case MONO_TYPE_I8:
1264 #ifdef NO_UNALIGNED_ACCESS
1265 if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
1266 UNALIGNED_COMPARE (gint64);
1267 else
1268 #endif
1269 if (*(gint64 *) this_field != *(gint64 *) that_field)
1270 return FALSE;
1271 break;
1273 case MONO_TYPE_R4: {
1274 float d1, d2;
1275 #ifdef NO_UNALIGNED_ACCESS
1276 memcpy (&d1, this_field, sizeof (float));
1277 memcpy (&d2, that_field, sizeof (float));
1278 #else
1279 d1 = *(float *) this_field;
1280 d2 = *(float *) that_field;
1281 #endif
1282 if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
1283 return FALSE;
1284 break;
1286 case MONO_TYPE_R8: {
1287 double d1, d2;
1288 #ifdef NO_UNALIGNED_ACCESS
1289 memcpy (&d1, this_field, sizeof (double));
1290 memcpy (&d2, that_field, sizeof (double));
1291 #else
1292 d1 = *(double *) this_field;
1293 d2 = *(double *) that_field;
1294 #endif
1295 if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
1296 return FALSE;
1297 break;
1299 case MONO_TYPE_PTR:
1300 #ifdef NO_UNALIGNED_ACCESS
1301 if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
1302 UNALIGNED_COMPARE (gpointer);
1303 else
1304 #endif
1305 if (*(gpointer *) this_field != *(gpointer *) that_field)
1306 return FALSE;
1307 break;
1308 case MONO_TYPE_STRING: {
1309 MonoString *s1, *s2;
1310 guint32 s1len, s2len;
1311 s1 = *(MonoString**)this_field;
1312 s2 = *(MonoString**)that_field;
1313 if (s1 == s2)
1314 break;
1315 if ((s1 == NULL) || (s2 == NULL))
1316 return FALSE;
1317 s1len = mono_string_length_internal (s1);
1318 s2len = mono_string_length_internal (s2);
1319 if (s1len != s2len)
1320 return FALSE;
1322 if (memcmp (mono_string_chars_internal (s1), mono_string_chars_internal (s2), s1len * sizeof (gunichar2)) != 0)
1323 return FALSE;
1324 break;
1326 default:
1327 if (!unhandled)
1328 unhandled = g_newa (MonoClassField*, mono_class_num_fields (klass));
1329 unhandled [count ++] = field;
1332 #undef UNALIGNED_COMPARE
1334 if (m_class_is_enumtype (klass))
1335 /* enums only have one non-static field */
1336 break;
1339 if (unhandled) {
1340 MonoArrayHandle fields_arr = mono_array_new_handle (mono_domain_get (), mono_defaults.object_class, count * 2, error);
1341 return_val_if_nok (error, 0);
1342 MONO_HANDLE_ASSIGN (fields, fields_arr);
1343 MonoObjectHandle h = MONO_HANDLE_NEW (MonoObject, NULL);
1344 for (int i = 0; i < count; ++i) {
1345 MonoObject *o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (this_obj), error);
1346 return_val_if_nok (error, FALSE);
1347 MONO_HANDLE_ASSIGN_RAW (h, o);
1348 mono_array_handle_setref (fields_arr, i * 2, h);
1350 o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (that), error);
1351 return_val_if_nok (error, FALSE);
1352 MONO_HANDLE_ASSIGN_RAW (h, o);
1353 mono_array_handle_setref (fields_arr, (i * 2) + 1, h);
1355 return FALSE;
1356 } else {
1357 return TRUE;
1361 MonoReflectionTypeHandle
1362 ves_icall_System_Object_GetType (MonoObjectHandle obj, MonoError *error)
1364 MonoDomain *domain = MONO_HANDLE_DOMAIN (obj);
1365 MonoClass *klass = mono_handle_class (obj);
1366 #ifndef DISABLE_REMOTING
1367 if (mono_class_is_transparent_proxy (klass)) {
1368 MonoTransparentProxyHandle proxy_obj = MONO_HANDLE_CAST (MonoTransparentProxy, obj);
1369 MonoRemoteClass *remote_class = MONO_HANDLE_GETVAL (proxy_obj, remote_class);
1370 /* If it's a transparent proxy for an interface, return the
1371 * interface type, not the unhelpful proxy_class class (which
1372 * is just MarshalByRefObject). */
1373 MonoType *proxy_type =
1374 mono_remote_class_is_interface_proxy (remote_class) ?
1375 m_class_get_byval_arg (remote_class->interfaces[0]) :
1376 m_class_get_byval_arg (remote_class->proxy_class);
1377 return mono_type_get_object_handle (domain, proxy_type, error);
1378 } else
1379 #endif
1380 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
1383 static gboolean
1384 get_executing (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1386 MonoMethod **dest = (MonoMethod **)data;
1388 /* skip unmanaged frames */
1389 if (!managed)
1390 return FALSE;
1392 if (!(*dest)) {
1393 if (!strcmp (m_class_get_name_space (m->klass), "System.Reflection"))
1394 return FALSE;
1395 *dest = m;
1396 return TRUE;
1398 return FALSE;
1401 static gboolean
1402 in_corlib_name_space (MonoClass *klass, const char *name_space)
1404 return m_class_get_image (klass) == mono_defaults.corlib &&
1405 !strcmp (m_class_get_name_space (klass), name_space);
1408 static gboolean
1409 get_caller_no_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1411 MonoMethod **dest = (MonoMethod **)data;
1413 /* skip unmanaged frames */
1414 if (!managed)
1415 return FALSE;
1417 if (m->wrapper_type != MONO_WRAPPER_NONE)
1418 return FALSE;
1420 if (m == *dest) {
1421 *dest = NULL;
1422 return FALSE;
1425 if (in_corlib_name_space (m->klass, "System.Reflection"))
1426 return FALSE;
1428 if (!(*dest)) {
1429 *dest = m;
1430 return TRUE;
1432 return FALSE;
1435 static gboolean
1436 get_caller_no_system_or_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1438 MonoMethod **dest = (MonoMethod **)data;
1440 /* skip unmanaged frames */
1441 if (!managed)
1442 return FALSE;
1444 if (m->wrapper_type != MONO_WRAPPER_NONE)
1445 return FALSE;
1447 if (m == *dest) {
1448 *dest = NULL;
1449 return FALSE;
1452 if (in_corlib_name_space (m->klass, "System.Reflection") || in_corlib_name_space (m->klass, "System"))
1453 return FALSE;
1455 if (!(*dest)) {
1456 *dest = m;
1457 return TRUE;
1459 return FALSE;
1463 * mono_runtime_get_caller_no_system_or_reflection:
1465 * Walk the stack of the current thread and find the first managed method that
1466 * is not in the mscorlib System or System.Reflection namespace. This skips
1467 * unmanaged callers and wrapper methods.
1469 * \returns a pointer to the \c MonoMethod or NULL if we walked past all the
1470 * callers.
1472 MonoMethod*
1473 mono_runtime_get_caller_no_system_or_reflection (void)
1475 MonoMethod *dest = NULL;
1476 mono_stack_walk_no_il (get_caller_no_system_or_reflection, &dest);
1477 return dest;
1481 * mono_runtime_get_caller_from_stack_mark:
1483 * Walk the stack and return the assembly of the method referenced
1484 * by the stack mark STACK_MARK.
1486 MonoAssembly*
1487 mono_runtime_get_caller_from_stack_mark (MonoStackCrawlMark *stack_mark)
1489 // FIXME: Use the stack mark
1490 MonoMethod *dest = NULL;
1491 mono_stack_walk_no_il (get_caller_no_system_or_reflection, &dest);
1492 if (dest)
1493 return m_class_get_image (dest->klass)->assembly;
1494 else
1495 return NULL;
1498 static MonoReflectionTypeHandle
1499 type_from_parsed_name (MonoTypeNameParse *info, MonoStackCrawlMark *stack_mark, MonoBoolean ignoreCase, MonoAssembly **caller_assembly, MonoError *error)
1501 MonoMethod *m;
1502 MonoType *type = NULL;
1503 MonoAssembly *assembly = NULL;
1504 gboolean type_resolve = FALSE;
1505 MonoImage *rootimage = NULL;
1506 MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
1508 error_init (error);
1511 * We must compute the calling assembly as type loading must happen under a metadata context.
1512 * For example. The main assembly is a.exe and Type.GetType is called from dir/b.dll. Without
1513 * the metadata context (basedir currently) set to dir/b.dll we won't be able to load a dir/c.dll.
1515 m = mono_method_get_last_managed ();
1516 if (m && m_class_get_image (m->klass) != mono_defaults.corlib) {
1517 /* Happens with inlining */
1518 assembly = m_class_get_image (m->klass)->assembly;
1519 } else {
1520 assembly = mono_runtime_get_caller_from_stack_mark (stack_mark);
1523 if (assembly) {
1524 type_resolve = TRUE;
1525 rootimage = assembly->image;
1526 } else {
1527 // FIXME: once wasm can use stack marks, consider turning all this into an assert
1528 g_warning (G_STRLOC);
1531 *caller_assembly = assembly;
1533 if (info->assembly.name) {
1534 MonoAssemblyByNameRequest req;
1535 mono_assembly_request_prepare_byname (&req, MONO_ASMCTX_DEFAULT, alc);
1536 req.requesting_assembly = assembly;
1537 req.basedir = assembly ? assembly->basedir : NULL;
1538 assembly = mono_assembly_request_byname (&info->assembly, &req, NULL);
1541 if (assembly) {
1542 /* When loading from the current assembly, AppDomain.TypeResolve will not be called yet */
1543 type = mono_reflection_get_type_checked (alc, rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error);
1544 goto_if_nok (error, fail);
1547 // XXXX - aleksey -
1548 // Say we're looking for System.Generic.Dict<int, Local>
1549 // we FAIL the get type above, because S.G.Dict isn't in assembly->image. So we drop down here.
1550 // but then we FAIL AGAIN because now we pass null as the image and the rootimage and everything
1551 // is messed up when we go to construct the Local as the type arg...
1553 // By contrast, if we started with Mine<System.Generic.Dict<int, Local>> we'd go in with assembly->image
1554 // as the root and then even the detour into generics would still not cause issues when we went to load Local.
1555 if (!info->assembly.name && !type) {
1556 /* try mscorlib */
1557 type = mono_reflection_get_type_checked (alc, rootimage, NULL, info, ignoreCase, TRUE, &type_resolve, error);
1558 goto_if_nok (error, fail);
1560 if (assembly && !type && type_resolve) {
1561 type_resolve = FALSE; /* This will invoke TypeResolve if not done in the first 'if' */
1562 type = mono_reflection_get_type_checked (alc, rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error);
1563 goto_if_nok (error, fail);
1566 if (!type)
1567 goto fail;
1569 return mono_type_get_object_handle (mono_domain_get (), type, error);
1570 fail:
1571 return MONO_HANDLE_NEW (MonoReflectionType, NULL);
1574 MonoReflectionTypeHandle
1575 ves_icall_System_RuntimeTypeHandle_internal_from_name (MonoStringHandle name,
1576 MonoStackCrawlMark *stack_mark,
1577 MonoReflectionAssemblyHandle callerAssembly,
1578 MonoBoolean throwOnError,
1579 MonoBoolean ignoreCase,
1580 MonoBoolean reflectionOnly,
1581 MonoError *error)
1583 MonoTypeNameParse info;
1584 gboolean free_info = FALSE;
1585 MonoAssembly *caller_assembly;
1586 MonoReflectionTypeHandle type = MONO_HANDLE_NEW (MonoReflectionType, NULL);
1588 /* The callerAssembly argument is unused for now */
1590 char *str = mono_string_handle_to_utf8 (name, error);
1591 goto_if_nok (error, leave);
1593 free_info = TRUE;
1594 if (!mono_reflection_parse_type_checked (str, &info, error))
1595 goto leave;
1597 /* mono_reflection_parse_type() mangles the string */
1599 MONO_HANDLE_ASSIGN (type, type_from_parsed_name (&info, (MonoStackCrawlMark*)stack_mark, ignoreCase, &caller_assembly, error));
1601 goto_if_nok (error, leave);
1603 if (MONO_HANDLE_IS_NULL (type)) {
1604 if (throwOnError) {
1605 char *tname = info.name_space ? g_strdup_printf ("%s.%s", info.name_space, info.name) : g_strdup (info.name);
1606 char *aname;
1607 if (info.assembly.name)
1608 aname = mono_stringify_assembly_name (&info.assembly);
1609 else if (caller_assembly)
1610 aname = mono_stringify_assembly_name (mono_assembly_get_name_internal (caller_assembly));
1611 else
1612 aname = g_strdup ("");
1613 mono_error_set_type_load_name (error, tname, aname, "");
1615 goto leave;
1618 leave:
1619 if (free_info)
1620 mono_reflection_free_type_info (&info);
1621 g_free (str);
1622 if (!is_ok (error)) {
1623 if (!throwOnError) {
1624 mono_error_cleanup (error);
1625 error_init (error);
1627 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
1628 } else
1629 return type;
1633 MonoReflectionTypeHandle
1634 ves_icall_System_Type_internal_from_handle (MonoType *handle, MonoError *error)
1636 MonoDomain *domain = mono_domain_get ();
1638 return mono_type_get_object_handle (domain, handle, error);
1641 MonoType*
1642 ves_icall_Mono_RuntimeClassHandle_GetTypeFromClass (MonoClass *klass)
1644 return m_class_get_byval_arg (klass);
1647 void
1648 ves_icall_Mono_RuntimeGPtrArrayHandle_GPtrArrayFree (GPtrArray *ptr_array)
1650 g_ptr_array_free (ptr_array, TRUE);
1653 void
1654 ves_icall_Mono_SafeStringMarshal_GFree (void *c_str)
1656 g_free (c_str);
1659 char*
1660 ves_icall_Mono_SafeStringMarshal_StringToUtf8 (MonoString *volatile* s)
1662 ERROR_DECL (error);
1663 char *result = mono_string_to_utf8_checked_internal (*s, error);
1664 mono_error_set_pending_exception (error);
1665 return result;
1668 /* System.TypeCode */
1669 typedef enum {
1670 TYPECODE_EMPTY,
1671 TYPECODE_OBJECT,
1672 TYPECODE_DBNULL,
1673 TYPECODE_BOOLEAN,
1674 TYPECODE_CHAR,
1675 TYPECODE_SBYTE,
1676 TYPECODE_BYTE,
1677 TYPECODE_INT16,
1678 TYPECODE_UINT16,
1679 TYPECODE_INT32,
1680 TYPECODE_UINT32,
1681 TYPECODE_INT64,
1682 TYPECODE_UINT64,
1683 TYPECODE_SINGLE,
1684 TYPECODE_DOUBLE,
1685 TYPECODE_DECIMAL,
1686 TYPECODE_DATETIME,
1687 TYPECODE_STRING = 18
1688 } TypeCode;
1690 guint32
1691 ves_icall_type_GetTypeCodeInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
1693 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
1695 if (type->byref)
1696 return TYPECODE_OBJECT;
1698 handle_enum:
1699 switch (type->type) {
1700 case MONO_TYPE_VOID:
1701 return TYPECODE_OBJECT;
1702 case MONO_TYPE_BOOLEAN:
1703 return TYPECODE_BOOLEAN;
1704 case MONO_TYPE_U1:
1705 return TYPECODE_BYTE;
1706 case MONO_TYPE_I1:
1707 return TYPECODE_SBYTE;
1708 case MONO_TYPE_U2:
1709 return TYPECODE_UINT16;
1710 case MONO_TYPE_I2:
1711 return TYPECODE_INT16;
1712 case MONO_TYPE_CHAR:
1713 return TYPECODE_CHAR;
1714 case MONO_TYPE_PTR:
1715 case MONO_TYPE_U:
1716 case MONO_TYPE_I:
1717 return TYPECODE_OBJECT;
1718 case MONO_TYPE_U4:
1719 return TYPECODE_UINT32;
1720 case MONO_TYPE_I4:
1721 return TYPECODE_INT32;
1722 case MONO_TYPE_U8:
1723 return TYPECODE_UINT64;
1724 case MONO_TYPE_I8:
1725 return TYPECODE_INT64;
1726 case MONO_TYPE_R4:
1727 return TYPECODE_SINGLE;
1728 case MONO_TYPE_R8:
1729 return TYPECODE_DOUBLE;
1730 case MONO_TYPE_VALUETYPE: {
1731 MonoClass *klass = type->data.klass;
1733 if (m_class_is_enumtype (klass)) {
1734 type = mono_class_enum_basetype_internal (klass);
1735 goto handle_enum;
1736 } else if (mono_is_corlib_image (m_class_get_image (klass))) {
1737 if (strcmp (m_class_get_name_space (klass), "System") == 0) {
1738 if (strcmp (m_class_get_name (klass), "Decimal") == 0)
1739 return TYPECODE_DECIMAL;
1740 else if (strcmp (m_class_get_name (klass), "DateTime") == 0)
1741 return TYPECODE_DATETIME;
1744 return TYPECODE_OBJECT;
1746 case MONO_TYPE_STRING:
1747 return TYPECODE_STRING;
1748 case MONO_TYPE_SZARRAY:
1749 case MONO_TYPE_ARRAY:
1750 case MONO_TYPE_OBJECT:
1751 case MONO_TYPE_VAR:
1752 case MONO_TYPE_MVAR:
1753 case MONO_TYPE_TYPEDBYREF:
1754 return TYPECODE_OBJECT;
1755 case MONO_TYPE_CLASS:
1757 MonoClass *klass = type->data.klass;
1758 if (m_class_get_image (klass) == mono_defaults.corlib && strcmp (m_class_get_name_space (klass), "System") == 0) {
1759 if (strcmp (m_class_get_name (klass), "DBNull") == 0)
1760 return TYPECODE_DBNULL;
1763 return TYPECODE_OBJECT;
1764 case MONO_TYPE_GENERICINST:
1765 if (m_class_is_enumtype (type->data.generic_class->container_class)) {
1766 type = mono_class_enum_basetype_internal (type->data.generic_class->container_class);
1767 goto handle_enum;
1769 return TYPECODE_OBJECT;
1770 default:
1771 g_error ("type 0x%02x not handled in GetTypeCode()", type->type);
1773 return 0;
1776 guint32
1777 ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_c, MonoError *error)
1779 g_assert (!MONO_HANDLE_IS_NULL (ref_type));
1781 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
1782 MonoClass *klass = mono_class_from_mono_type_internal (type);
1783 MonoType *ctype = MONO_HANDLE_GETVAL (ref_c, type);
1784 MonoClass *klassc = mono_class_from_mono_type_internal (ctype);
1786 if (type->byref ^ ctype->byref)
1787 return FALSE;
1789 if (type->byref) {
1790 return mono_byref_type_is_assignable_from (type, ctype, FALSE);
1793 gboolean result;
1794 mono_class_is_assignable_from_checked (klass, klassc, &result, error);
1795 return (guint32)result;
1798 MonoBoolean
1799 ves_icall_RuntimeTypeHandle_is_subclass_of (MonoType *childType, MonoType *baseType)
1801 ERROR_DECL (error);
1802 mono_bool result = FALSE;
1803 MonoClass *childClass;
1804 MonoClass *baseClass;
1806 childClass = mono_class_from_mono_type_internal (childType);
1807 baseClass = mono_class_from_mono_type_internal (baseType);
1809 if (G_UNLIKELY (childType->byref)) {
1810 result = !baseType->byref && baseClass == mono_defaults.object_class;
1811 goto done;
1814 if (G_UNLIKELY (baseType->byref)) {
1815 result = FALSE;
1816 goto done;
1819 if (childType == baseType) {
1820 /* .NET IsSubclassOf is not reflexive */
1821 result = FALSE;
1822 goto done;
1825 if (G_UNLIKELY (is_generic_parameter (childType))) {
1826 /* slow path: walk the type hierarchy looking at base types
1827 * until we see baseType. If the current type is not a gparam,
1828 * break out of the loop and use is_subclass_of.
1830 MonoClass *c = mono_generic_param_get_base_type (childClass);
1832 result = FALSE;
1833 while (c != NULL) {
1834 if (c == baseClass) {
1835 result = TRUE;
1836 break;
1838 if (!is_generic_parameter (m_class_get_byval_arg (c))) {
1839 result = mono_class_is_subclass_of_internal (c, baseClass, FALSE);
1840 break;
1841 } else
1842 c = mono_generic_param_get_base_type (c);
1844 } else {
1845 result = mono_class_is_subclass_of_internal (childClass, baseClass, FALSE);
1847 done:
1848 mono_error_set_pending_exception (error);
1849 return result;
1852 guint32
1853 ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type, MonoObjectHandle obj, MonoError *error)
1855 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
1856 MonoClass *klass = mono_class_from_mono_type_internal (type);
1857 mono_class_init_checked (klass, error);
1858 return_val_if_nok (error, FALSE);
1859 MonoObjectHandle inst = mono_object_handle_isinst (obj, klass, error);
1860 return_val_if_nok (error, FALSE);
1861 return !MONO_HANDLE_IS_NULL (inst);
1864 guint32
1865 ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionTypeHandle ref_type, MonoError *error)
1867 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
1870 MonoClass *klass = mono_class_from_mono_type_internal (type);
1871 return mono_class_get_flags (klass);
1874 MonoReflectionMarshalAsAttributeHandle
1875 ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionFieldHandle field_h, MonoError *error)
1877 MonoDomain *domain = MONO_HANDLE_DOMAIN (field_h);
1878 MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
1879 MonoClass *klass = field->parent;
1881 MonoGenericClass *gklass = mono_class_try_get_generic_class (klass);
1882 if (mono_class_is_gtd (klass) ||
1883 (gklass && gklass->context.class_inst->is_open))
1884 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
1886 MonoType *ftype = mono_field_get_type_internal (field);
1887 if (ftype && !(ftype->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL))
1888 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
1890 MonoMarshalType *info = mono_marshal_load_type_info (klass);
1892 for (int i = 0; i < info->num_fields; ++i) {
1893 if (info->fields [i].field == field) {
1894 if (!info->fields [i].mspec)
1895 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
1896 else {
1897 return mono_reflection_marshal_as_attribute_from_marshal_spec (domain, klass, info->fields [i].mspec, error);
1902 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
1905 MonoReflectionFieldHandle
1906 ves_icall_System_Reflection_FieldInfo_internal_from_handle_type (MonoClassField *handle, MonoType *type, MonoError *error)
1908 MonoClass *klass;
1910 g_assert (handle);
1912 if (!type) {
1913 klass = handle->parent;
1914 } else {
1915 klass = mono_class_from_mono_type_internal (type);
1917 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
1919 if (!found)
1920 /* The managed code will throw the exception */
1921 return MONO_HANDLE_CAST (MonoReflectionField, NULL_HANDLE);
1924 return mono_field_get_object_handle (mono_domain_get (), klass, handle, error);
1927 MonoReflectionEventHandle
1928 ves_icall_System_Reflection_EventInfo_internal_from_handle_type (MonoEvent *handle, MonoType *type, MonoError *error)
1930 MonoClass *klass;
1932 g_assert (handle);
1934 if (!type) {
1935 klass = handle->parent;
1936 } else {
1937 klass = mono_class_from_mono_type_internal (type);
1939 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
1940 if (!found)
1941 /* Managed code will throw an exception */
1942 return MONO_HANDLE_CAST (MonoReflectionEvent, NULL_HANDLE);
1945 return mono_event_get_object_handle (mono_domain_get (), klass, handle, error);
1948 MonoReflectionPropertyHandle
1949 ves_icall_System_Reflection_RuntimePropertyInfo_internal_from_handle_type (MonoProperty *handle, MonoType *type, MonoError *error)
1951 MonoClass *klass;
1953 g_assert (handle);
1955 if (!type) {
1956 klass = handle->parent;
1957 } else {
1958 klass = mono_class_from_mono_type_internal (type);
1960 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
1961 if (!found)
1962 /* Managed code will throw an exception */
1963 return MONO_HANDLE_CAST (MonoReflectionProperty, NULL_HANDLE);
1966 return mono_property_get_object_handle (mono_domain_get (), klass, handle, error);
1969 MonoArrayHandle
1970 ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionFieldHandle field_h, MonoBoolean optional, MonoError *error)
1972 MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
1974 MonoType *type = mono_field_get_type_checked (field, error);
1975 return_val_if_nok (error, NULL_HANDLE_ARRAY);
1977 return type_array_from_modifiers (type, optional, error);
1981 ves_icall_get_method_attributes (MonoMethod *method)
1983 return method->flags;
1986 void
1987 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info, MonoError *error)
1989 MonoDomain *domain = mono_domain_get ();
1991 MonoMethodSignature* sig = mono_method_signature_checked (method, error);
1992 return_if_nok (error);
1994 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (method->klass), error);
1995 return_if_nok (error);
1997 MONO_STRUCT_SETREF_INTERNAL (info, parent, MONO_HANDLE_RAW (rt));
1999 MONO_HANDLE_ASSIGN (rt, mono_type_get_object_handle (domain, sig->ret, error));
2000 return_if_nok (error);
2002 MONO_STRUCT_SETREF_INTERNAL (info, ret, MONO_HANDLE_RAW (rt));
2004 info->attrs = method->flags;
2005 info->implattrs = method->iflags;
2006 guint32 callconv;
2007 if (sig->call_convention == MONO_CALL_DEFAULT)
2008 callconv = sig->sentinelpos >= 0 ? 2 : 1;
2009 else {
2010 if (sig->call_convention == MONO_CALL_VARARG || sig->sentinelpos >= 0)
2011 callconv = 2;
2012 else
2013 callconv = 1;
2015 callconv |= (sig->hasthis << 5) | (sig->explicit_this << 6);
2016 info->callconv = callconv;
2019 MonoArrayHandle
2020 ves_icall_System_Reflection_MonoMethodInfo_get_parameter_info (MonoMethod *method, MonoReflectionMethodHandle member, MonoError *error)
2022 MonoDomain *domain = mono_domain_get ();
2024 MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
2025 MONO_HANDLE_GET (reftype, member, reftype);
2026 MonoClass *klass = NULL;
2027 if (!MONO_HANDLE_IS_NULL (reftype))
2028 klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (reftype, type));
2029 return mono_param_get_objects_internal (domain, method, klass, error);
2032 MonoReflectionMarshalAsAttributeHandle
2033 ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod *method, MonoError *error)
2035 MonoDomain *domain = mono_domain_get ();
2036 MonoReflectionMarshalAsAttributeHandle res = MONO_HANDLE_NEW (MonoReflectionMarshalAsAttribute, NULL);
2038 MonoMarshalSpec **mspecs = g_new (MonoMarshalSpec*, mono_method_signature_internal (method)->param_count + 1);
2039 mono_method_get_marshal_info (method, mspecs);
2041 if (mspecs [0]) {
2042 MONO_HANDLE_ASSIGN (res, mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [0], error));
2043 goto_if_nok (error, leave);
2046 leave:
2047 for (int i = mono_method_signature_internal (method)->param_count; i >= 0; i--)
2048 if (mspecs [i])
2049 mono_metadata_free_marshal_spec (mspecs [i]);
2050 g_free (mspecs);
2052 return res;
2055 gint32
2056 ves_icall_RuntimeFieldInfo_GetFieldOffset (MonoReflectionFieldHandle field, MonoError *error)
2058 MonoClassField *class_field = MONO_HANDLE_GETVAL (field, field);
2059 mono_class_setup_fields (class_field->parent);
2061 return class_field->offset - MONO_ABI_SIZEOF (MonoObject);
2064 MonoReflectionTypeHandle
2065 ves_icall_RuntimeFieldInfo_GetParentType (MonoReflectionFieldHandle field, MonoBoolean declaring, MonoError *error)
2067 MonoDomain *domain = MONO_HANDLE_DOMAIN (field);
2068 MonoClass *parent;
2070 if (declaring) {
2071 MonoClassField *f = MONO_HANDLE_GETVAL (field, field);
2072 parent = f->parent;
2073 } else {
2074 parent = MONO_HANDLE_GETVAL (field, klass);
2077 return mono_type_get_object_handle (domain, m_class_get_byval_arg (parent), error);
2080 MonoObjectHandle
2081 ves_icall_RuntimeFieldInfo_GetValueInternal (MonoReflectionFieldHandle field_handle, MonoObjectHandle obj_handle, MonoError *error)
2083 MonoReflectionField * const field = MONO_HANDLE_RAW (field_handle);
2084 MonoClass *fklass = field->klass;
2085 MonoClassField *cf = field->field;
2087 if (mono_asmctx_get_kind (&m_class_get_image (fklass)->assembly->context) == MONO_ASMCTX_REFONLY) {
2088 mono_error_set_invalid_operation (error,
2089 "It is illegal to get the value on a field on a type loaded using the ReflectionOnly methods.");
2090 return NULL_HANDLE;
2093 if (mono_security_core_clr_enabled () &&
2094 !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
2095 return NULL_HANDLE;
2098 MonoObject * const obj = MONO_HANDLE_RAW (obj_handle);
2099 MonoObject *result;
2101 #ifndef DISABLE_REMOTING
2102 if (G_UNLIKELY (obj != NULL && mono_class_is_transparent_proxy (mono_object_class (obj)))) {
2103 /* We get here if someone used a
2104 * System.Reflection.FieldInfo:GetValue on a
2105 * ContextBoundObject's or cross-domain MarshalByRefObject's
2106 * transparent proxy. */
2107 result = mono_load_remote_field_new_checked (obj, fklass, cf, error);
2108 } else
2109 #endif
2110 result = mono_field_get_value_object_checked (mono_object_domain (field), cf, obj, error);
2112 return MONO_HANDLE_NEW (MonoObject, result);
2115 void
2116 ves_icall_RuntimeFieldInfo_SetValueInternal (MonoReflectionFieldHandle field, MonoObjectHandle obj, MonoObjectHandle value, MonoError *error)
2118 MonoClassField *cf = MONO_HANDLE_GETVAL (field, field);
2120 MonoClass *field_klass = MONO_HANDLE_GETVAL (field, klass);
2121 if (mono_asmctx_get_kind (&m_class_get_image (field_klass)->assembly->context) == MONO_ASMCTX_REFONLY) {
2122 mono_error_set_invalid_operation (error, "It is illegal to set the value on a field on a type loaded using the ReflectionOnly methods.");
2123 return;
2126 if (mono_security_core_clr_enabled () &&
2127 !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
2128 return;
2131 #ifndef DISABLE_REMOTING
2132 if (G_UNLIKELY (!MONO_HANDLE_IS_NULL (obj) && mono_class_is_transparent_proxy (mono_handle_class (obj)))) {
2133 /* We get here if someone used a
2134 * System.Reflection.FieldInfo:SetValue on a
2135 * ContextBoundObject's or cross-domain MarshalByRefObject's
2136 * transparent proxy. */
2137 /* FIXME: use handles for mono_store_remote_field_new_checked */
2138 MonoObject *v = MONO_HANDLE_RAW (value);
2139 MonoObject *o = MONO_HANDLE_RAW (obj);
2140 mono_store_remote_field_new_checked (o, field_klass, cf, v, error);
2141 return;
2143 #endif
2145 MonoType *type = mono_field_get_type_checked (cf, error);
2146 return_if_nok (error);
2148 gboolean isref = FALSE;
2149 MonoGCHandle value_gchandle = 0;
2150 gchar *v = NULL;
2151 if (!type->byref) {
2152 switch (type->type) {
2153 case MONO_TYPE_U1:
2154 case MONO_TYPE_I1:
2155 case MONO_TYPE_BOOLEAN:
2156 case MONO_TYPE_U2:
2157 case MONO_TYPE_I2:
2158 case MONO_TYPE_CHAR:
2159 case MONO_TYPE_U:
2160 case MONO_TYPE_I:
2161 case MONO_TYPE_U4:
2162 case MONO_TYPE_I4:
2163 case MONO_TYPE_R4:
2164 case MONO_TYPE_U8:
2165 case MONO_TYPE_I8:
2166 case MONO_TYPE_R8:
2167 case MONO_TYPE_VALUETYPE:
2168 case MONO_TYPE_PTR:
2169 isref = FALSE;
2170 if (!MONO_HANDLE_IS_NULL (value)) {
2171 if (m_class_is_valuetype (mono_handle_class (value)))
2172 v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
2173 else {
2174 char* n = g_strdup_printf ("Object of type '%s' cannot be converted to type '%s'.", m_class_get_name (mono_handle_class (value)), m_class_get_name (mono_class_from_mono_type_internal (type)));
2175 mono_error_set_argument (error, cf->name, n);
2176 g_free (n);
2177 return;
2180 break;
2181 case MONO_TYPE_STRING:
2182 case MONO_TYPE_OBJECT:
2183 case MONO_TYPE_CLASS:
2184 case MONO_TYPE_ARRAY:
2185 case MONO_TYPE_SZARRAY:
2186 /* Do nothing */
2187 isref = TRUE;
2188 break;
2189 case MONO_TYPE_GENERICINST: {
2190 MonoGenericClass *gclass = type->data.generic_class;
2191 g_assert (!gclass->context.class_inst->is_open);
2193 if (mono_class_is_nullable (mono_class_from_mono_type_internal (type))) {
2194 MonoClass *nklass = mono_class_from_mono_type_internal (type);
2197 * Convert the boxed vtype into a Nullable structure.
2198 * This is complicated by the fact that Nullables have
2199 * a variable structure.
2201 MonoObjectHandle nullable = mono_object_new_handle (mono_domain_get (), nklass, error);
2202 return_if_nok (error);
2204 MonoGCHandle nullable_gchandle = 0;
2205 guint8 *nval = (guint8*)mono_object_handle_pin_unbox (nullable, &nullable_gchandle);
2206 mono_nullable_init_from_handle (nval, value, nklass);
2208 isref = FALSE;
2209 value_gchandle = nullable_gchandle;
2210 v = (gchar*)nval;
2212 else {
2213 isref = !m_class_is_valuetype (gclass->container_class);
2214 if (!isref && !MONO_HANDLE_IS_NULL (value)) {
2215 v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
2218 break;
2220 default:
2221 g_error ("type 0x%x not handled in "
2222 "ves_icall_FieldInfo_SetValueInternal", type->type);
2223 return;
2227 /* either value is a reference type, or it's a value type and we pinned
2228 * it and v points to the payload. */
2229 g_assert ((isref && v == NULL && value_gchandle == 0) ||
2230 (!isref && v != NULL && value_gchandle != 0) ||
2231 (!isref && v == NULL && value_gchandle == 0));
2233 if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
2234 MonoVTable *vtable = mono_class_vtable_checked (MONO_HANDLE_DOMAIN (field), cf->parent, error);
2235 goto_if_nok (error, leave);
2237 if (!vtable->initialized) {
2238 if (!mono_runtime_class_init_full (vtable, error))
2239 goto leave;
2241 if (isref)
2242 mono_field_static_set_value_internal (vtable, cf, MONO_HANDLE_RAW (value)); /* FIXME make mono_field_static_set_value work with handles for value */
2243 else
2244 mono_field_static_set_value_internal (vtable, cf, v);
2245 } else {
2247 if (isref)
2248 MONO_HANDLE_SET_FIELD_REF (obj, cf, value);
2249 else
2250 mono_field_set_value_internal (MONO_HANDLE_RAW (obj), cf, v); /* FIXME: make mono_field_set_value take a handle for obj */
2252 leave:
2253 if (value_gchandle)
2254 mono_gchandle_free_internal (value_gchandle);
2257 static MonoObjectHandle
2258 typed_reference_to_object (MonoTypedRef *tref, MonoError *error)
2260 HANDLE_FUNCTION_ENTER ();
2261 MonoObjectHandle result;
2262 if (MONO_TYPE_IS_REFERENCE (tref->type)) {
2263 MonoObject** objp = (MonoObject **)tref->value;
2264 result = MONO_HANDLE_NEW (MonoObject, *objp);
2265 } else if (mono_type_is_pointer (tref->type)) {
2266 /* Boxed as UIntPtr */
2267 result = mono_value_box_handle (mono_domain_get (), mono_get_uintptr_class (), tref->value, error);
2268 } else {
2269 result = mono_value_box_handle (mono_domain_get (), tref->klass, tref->value, error);
2271 HANDLE_FUNCTION_RETURN_REF (MonoObject, result);
2274 MonoObjectHandle
2275 ves_icall_System_RuntimeFieldHandle_GetValueDirect (MonoReflectionFieldHandle field_h, MonoReflectionTypeHandle field_type_h, MonoTypedRef *obj, MonoReflectionTypeHandle context_type_h, MonoError *error)
2277 MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
2278 MonoClass *klass = mono_class_from_mono_type_internal (field->type);
2280 if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (field->parent))) {
2281 mono_error_set_not_implemented (error, "");
2282 return MONO_HANDLE_NEW (MonoObject, NULL);
2283 } else if (MONO_TYPE_IS_REFERENCE (field->type)) {
2284 return MONO_HANDLE_NEW (MonoObject, *(MonoObject**)((guint8*)obj->value + field->offset - sizeof (MonoObject)));
2285 } else {
2286 return mono_value_box_handle (mono_domain_get (), klass, (guint8*)obj->value + field->offset - sizeof (MonoObject), error);
2290 void
2291 ves_icall_System_RuntimeFieldHandle_SetValueDirect (MonoReflectionFieldHandle field_h, MonoReflectionTypeHandle field_type_h, MonoTypedRef *obj, MonoObjectHandle value_h, MonoReflectionTypeHandle context_type_h, MonoError *error)
2293 MonoClassField *f = MONO_HANDLE_GETVAL (field_h, field);
2295 g_assert (obj);
2297 mono_class_setup_fields (f->parent);
2299 if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (f->parent))) {
2300 MonoObjectHandle objHandle = typed_reference_to_object (obj, error);
2301 return_if_nok (error);
2302 ves_icall_RuntimeFieldInfo_SetValueInternal (field_h, objHandle, value_h, error);
2303 } else if (MONO_TYPE_IS_REFERENCE (f->type)) {
2304 mono_copy_value (f->type, (guint8*)obj->value + m_field_get_offset (f) - sizeof (MonoObject), MONO_HANDLE_RAW (value_h), FALSE);
2305 } else {
2306 MonoGCHandle gchandle = NULL;
2307 g_assert (MONO_HANDLE_RAW (value_h));
2308 mono_copy_value (f->type, (guint8*)obj->value + m_field_get_offset (f) - sizeof (MonoObject), mono_object_handle_pin_unbox (value_h, &gchandle), FALSE);
2309 mono_gchandle_free_internal (gchandle);
2313 MonoObjectHandle
2314 ves_icall_RuntimeFieldInfo_GetRawConstantValue (MonoReflectionFieldHandle rfield, MonoError* error)
2316 MonoObjectHandle o_handle = NULL_HANDLE_INIT;
2318 MonoObject *o = NULL;
2319 MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
2320 MonoClass *klass;
2321 MonoDomain *domain = MONO_HANDLE_DOMAIN (rfield);
2322 gchar *v;
2323 MonoTypeEnum def_type;
2324 const char *def_value;
2325 MonoType *t;
2326 MonoStringHandle string_handle = MONO_HANDLE_NEW (MonoString, NULL); // FIXME? Not always needed.
2328 mono_class_init_internal (field->parent);
2330 t = mono_field_get_type_checked (field, error);
2331 goto_if_nok (error, return_null);
2333 if (!(t->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
2334 goto invalid_operation;
2336 if (image_is_dynamic (m_class_get_image (field->parent))) {
2337 MonoClass *klass = field->parent;
2338 int fidx = field - m_class_get_fields (klass);
2339 MonoFieldDefaultValue *def_values = mono_class_get_field_def_values (klass);
2341 g_assert (def_values);
2342 def_type = def_values [fidx].def_type;
2343 def_value = def_values [fidx].data;
2345 if (def_type == MONO_TYPE_END)
2346 goto invalid_operation;
2347 } else {
2348 def_value = mono_class_get_field_default_value (field, &def_type);
2349 /* FIXME, maybe we should try to raise TLE if field->parent is broken */
2350 if (!def_value)
2351 goto invalid_operation;
2354 /*FIXME unify this with reflection.c:mono_get_object_from_blob*/
2355 switch (def_type) {
2356 case MONO_TYPE_U1:
2357 case MONO_TYPE_I1:
2358 case MONO_TYPE_BOOLEAN:
2359 case MONO_TYPE_U2:
2360 case MONO_TYPE_I2:
2361 case MONO_TYPE_CHAR:
2362 case MONO_TYPE_U:
2363 case MONO_TYPE_I:
2364 case MONO_TYPE_U4:
2365 case MONO_TYPE_I4:
2366 case MONO_TYPE_R4:
2367 case MONO_TYPE_U8:
2368 case MONO_TYPE_I8:
2369 case MONO_TYPE_R8: {
2370 MonoType *t;
2372 /* boxed value type */
2373 t = g_new0 (MonoType, 1);
2374 t->type = def_type;
2375 klass = mono_class_from_mono_type_internal (t);
2376 g_free (t);
2377 o = mono_object_new_checked (domain, klass, error);
2378 goto_if_nok (error, return_null);
2379 o_handle = MONO_HANDLE_NEW (MonoObject, o);
2380 v = ((gchar *) o) + sizeof (MonoObject);
2381 (void)mono_get_constant_value_from_blob (domain, def_type, def_value, v, string_handle, error);
2382 goto_if_nok (error, return_null);
2383 break;
2385 case MONO_TYPE_STRING:
2386 case MONO_TYPE_CLASS:
2387 (void)mono_get_constant_value_from_blob (domain, def_type, def_value, &o, string_handle, error);
2388 goto_if_nok (error, return_null);
2389 o_handle = MONO_HANDLE_NEW (MonoObject, o);
2390 break;
2391 default:
2392 g_assert_not_reached ();
2395 goto exit;
2396 invalid_operation:
2397 mono_error_set_invalid_operation (error, NULL);
2398 // fall through
2399 return_null:
2400 o_handle = NULL_HANDLE;
2401 // fall through
2402 exit:
2403 return o_handle;
2406 MonoReflectionTypeHandle
2407 ves_icall_RuntimeFieldInfo_ResolveType (MonoReflectionFieldHandle ref_field, MonoError *error)
2409 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_field);
2410 MonoClassField *field = MONO_HANDLE_GETVAL (ref_field, field);
2411 MonoType *type = mono_field_get_type_checked (field, error);
2412 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
2413 return mono_type_get_object_handle (domain, type, error);
2416 void
2417 ves_icall_RuntimePropertyInfo_get_property_info (MonoReflectionPropertyHandle property, MonoPropertyInfo *info, PInfo req_info, MonoError *error)
2419 MonoDomain *domain = MONO_HANDLE_DOMAIN (property);
2420 const MonoProperty *pproperty = MONO_HANDLE_GETVAL (property, property);
2422 if ((req_info & PInfo_ReflectedType) != 0) {
2423 MonoClass *klass = MONO_HANDLE_GETVAL (property, klass);
2424 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
2425 return_if_nok (error);
2427 MONO_STRUCT_SETREF_INTERNAL (info, parent, MONO_HANDLE_RAW (rt));
2429 if ((req_info & PInfo_DeclaringType) != 0) {
2430 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (pproperty->parent), error);
2431 return_if_nok (error);
2433 MONO_STRUCT_SETREF_INTERNAL (info, declaring_type, MONO_HANDLE_RAW (rt));
2436 if ((req_info & PInfo_Name) != 0) {
2437 MonoStringHandle name = mono_string_new_handle (domain, pproperty->name, error);
2438 return_if_nok (error);
2440 MONO_STRUCT_SETREF_INTERNAL (info, name, MONO_HANDLE_RAW (name));
2443 if ((req_info & PInfo_Attributes) != 0)
2444 info->attrs = pproperty->attrs;
2446 if ((req_info & PInfo_GetMethod) != 0) {
2447 MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
2448 MonoReflectionMethodHandle rm;
2449 if (pproperty->get &&
2450 (((pproperty->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
2451 pproperty->get->klass == property_klass)) {
2452 rm = mono_method_get_object_handle (domain, pproperty->get, property_klass, error);
2453 return_if_nok (error);
2454 } else {
2455 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2458 MONO_STRUCT_SETREF_INTERNAL (info, get, MONO_HANDLE_RAW (rm));
2460 if ((req_info & PInfo_SetMethod) != 0) {
2461 MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
2462 MonoReflectionMethodHandle rm;
2463 if (pproperty->set &&
2464 (((pproperty->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
2465 pproperty->set->klass == property_klass)) {
2466 rm = mono_method_get_object_handle (domain, pproperty->set, property_klass, error);
2467 return_if_nok (error);
2468 } else {
2469 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2472 MONO_STRUCT_SETREF_INTERNAL (info, set, MONO_HANDLE_RAW (rm));
2475 * There may be other methods defined for properties, though, it seems they are not exposed
2476 * in the reflection API
2480 static gboolean
2481 add_event_other_methods_to_array (MonoDomain *domain, MonoMethod *m, MonoArrayHandle dest, int i, MonoError *error)
2483 HANDLE_FUNCTION_ENTER ();
2484 error_init (error);
2485 MonoReflectionMethodHandle rm = mono_method_get_object_handle (domain, m, NULL, error);
2486 goto_if_nok (error, leave);
2487 MONO_HANDLE_ARRAY_SETREF (dest, i, rm);
2488 leave:
2489 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
2492 void
2493 ves_icall_RuntimeEventInfo_get_event_info (MonoReflectionMonoEventHandle ref_event, MonoEventInfo *info, MonoError *error)
2495 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_event);
2497 MonoClass *klass = MONO_HANDLE_GETVAL (ref_event, klass);
2498 MonoEvent *event = MONO_HANDLE_GETVAL (ref_event, event);
2500 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
2501 return_if_nok (error);
2502 MONO_STRUCT_SETREF_INTERNAL (info, reflected_type, MONO_HANDLE_RAW (rt));
2504 rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (event->parent), error);
2505 return_if_nok (error);
2506 MONO_STRUCT_SETREF_INTERNAL (info, declaring_type, MONO_HANDLE_RAW (rt));
2508 MonoStringHandle ev_name = mono_string_new_handle (domain, event->name, error);
2509 return_if_nok (error);
2510 MONO_STRUCT_SETREF_INTERNAL (info, name, MONO_HANDLE_RAW (ev_name));
2512 info->attrs = event->attrs;
2514 MonoReflectionMethodHandle rm;
2515 if (event->add) {
2516 rm = mono_method_get_object_handle (domain, event->add, klass, error);
2517 return_if_nok (error);
2518 } else {
2519 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2522 MONO_STRUCT_SETREF_INTERNAL (info, add_method, MONO_HANDLE_RAW (rm));
2524 if (event->remove) {
2525 rm = mono_method_get_object_handle (domain, event->remove, klass, error);
2526 return_if_nok (error);
2527 } else {
2528 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2531 MONO_STRUCT_SETREF_INTERNAL (info, remove_method, MONO_HANDLE_RAW (rm));
2533 if (event->raise) {
2534 rm = mono_method_get_object_handle (domain, event->raise, klass, error);
2535 return_if_nok (error);
2536 } else {
2537 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2540 MONO_STRUCT_SETREF_INTERNAL (info, raise_method, MONO_HANDLE_RAW (rm));
2542 #ifndef MONO_SMALL_CONFIG
2543 if (event->other) {
2544 int i, n = 0;
2545 while (event->other [n])
2546 n++;
2547 MonoArrayHandle info_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, n, error);
2548 return_if_nok (error);
2550 MONO_STRUCT_SETREF_INTERNAL (info, other_methods, MONO_HANDLE_RAW (info_arr));
2552 for (i = 0; i < n; i++)
2553 if (!add_event_other_methods_to_array (domain, event->other [i], info_arr, i, error))
2554 return;
2556 #endif
2559 static void
2560 collect_interfaces (MonoClass *klass, GHashTable *ifaces, MonoError *error)
2562 int i;
2563 MonoClass *ic;
2565 mono_class_setup_interfaces (klass, error);
2566 return_if_nok (error);
2568 int klass_interface_count = m_class_get_interface_count (klass);
2569 MonoClass **klass_interfaces = m_class_get_interfaces (klass);
2570 for (i = 0; i < klass_interface_count; i++) {
2571 ic = klass_interfaces [i];
2572 g_hash_table_insert (ifaces, ic, ic);
2574 collect_interfaces (ic, ifaces, error);
2575 return_if_nok (error);
2579 typedef struct {
2580 MonoArrayHandle iface_array;
2581 MonoGenericContext *context;
2582 MonoError *error;
2583 MonoDomain *domain;
2584 int next_idx;
2585 } FillIfaceArrayData;
2587 static void
2588 fill_iface_array (gpointer key, gpointer value, gpointer user_data)
2590 HANDLE_FUNCTION_ENTER ();
2591 FillIfaceArrayData *data = (FillIfaceArrayData *)user_data;
2592 MonoClass *ic = (MonoClass *)key;
2593 MonoType *ret = m_class_get_byval_arg (ic), *inflated = NULL;
2594 MonoError *error = data->error;
2596 goto_if_nok (error, leave);
2598 if (data->context && mono_class_is_ginst (ic) && mono_class_get_generic_class (ic)->context.class_inst->is_open) {
2599 inflated = ret = mono_class_inflate_generic_type_checked (ret, data->context, error);
2600 goto_if_nok (error, leave);
2603 MonoReflectionTypeHandle rt;
2604 rt = mono_type_get_object_handle (data->domain, ret, error);
2605 goto_if_nok (error, leave);
2607 MONO_HANDLE_ARRAY_SETREF (data->iface_array, data->next_idx, rt);
2608 data->next_idx++;
2610 if (inflated)
2611 mono_metadata_free_type (inflated);
2612 leave:
2613 HANDLE_FUNCTION_RETURN ();
2616 static guint
2617 get_interfaces_hash (gconstpointer v1)
2619 MonoClass *k = (MonoClass*)v1;
2621 return m_class_get_type_token (k);
2624 MonoArrayHandle
2625 ves_icall_RuntimeType_GetInterfaces (MonoReflectionTypeHandle ref_type, MonoError *error)
2627 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2628 MonoClass *klass = mono_class_from_mono_type_internal (type);
2630 GHashTable *iface_hash = g_hash_table_new (get_interfaces_hash, NULL);
2632 MonoGenericContext *context = NULL;
2633 if (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst->is_open) {
2634 context = mono_class_get_context (klass);
2635 klass = mono_class_get_generic_class (klass)->container_class;
2638 for (MonoClass *parent = klass; parent; parent = m_class_get_parent (parent)) {
2639 mono_class_setup_interfaces (parent, error);
2640 goto_if_nok (error, fail);
2641 collect_interfaces (parent, iface_hash, error);
2642 goto_if_nok (error, fail);
2645 MonoDomain *domain;
2646 domain = MONO_HANDLE_DOMAIN (ref_type);
2648 int len;
2649 len = g_hash_table_size (iface_hash);
2650 if (len == 0) {
2651 g_hash_table_destroy (iface_hash);
2652 if (!domain->empty_types) {
2653 domain->empty_types = mono_array_new_cached (domain, mono_defaults.runtimetype_class, 0, error);
2654 goto_if_nok (error, fail);
2656 return MONO_HANDLE_NEW (MonoArray, domain->empty_types);
2659 FillIfaceArrayData data;
2660 data.iface_array = MONO_HANDLE_NEW (MonoArray, mono_array_new_cached (domain, mono_defaults.runtimetype_class, len, error));
2661 goto_if_nok (error, fail);
2662 data.context = context;
2663 data.error = error;
2664 data.domain = domain;
2665 data.next_idx = 0;
2667 g_hash_table_foreach (iface_hash, fill_iface_array, &data);
2669 goto_if_nok (error, fail);
2671 g_hash_table_destroy (iface_hash);
2672 return data.iface_array;
2674 fail:
2675 g_hash_table_destroy (iface_hash);
2676 return NULL_HANDLE_ARRAY;
2679 static gboolean
2680 method_is_reabstracted (MonoMethod *method)
2682 /* only on interfaces */
2683 /* method is marked "final abstract" */
2684 /* FIXME: we need some other way to detect reabstracted methods. "final" is an incidental detail of the spec. */
2685 return m_method_is_final (method) && m_method_is_abstract (method);
2688 static gboolean
2689 method_is_dim (MonoMethod *method)
2691 /* only valid on interface methods*/
2692 /* method is marked "virtual" but not "virtual abstract" */
2693 return m_method_is_virtual (method) && !m_method_is_abstract (method);
2696 static gboolean
2697 set_interface_map_data_method_object (MonoDomain *domain, MonoMethod *method, MonoClass *iclass, int ioffset, MonoClass *klass, MonoArrayHandle targets, MonoArrayHandle methods, int i, MonoError *error)
2699 HANDLE_FUNCTION_ENTER ();
2700 error_init (error);
2701 MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, iclass, error);
2702 goto_if_nok (error, leave);
2704 MONO_HANDLE_ARRAY_SETREF (methods, i, member);
2706 MonoMethod* foundMethod = m_class_get_vtable (klass) [i + ioffset];
2708 if (mono_class_has_dim_conflicts (klass) && mono_class_is_interface (foundMethod->klass)) {
2709 GSList* conflicts = mono_class_get_dim_conflicts (klass);
2710 GSList* l;
2711 MonoMethod* decl = method;
2713 if (decl->is_inflated)
2714 decl = ((MonoMethodInflated*)decl)->declaring;
2716 gboolean in_conflict = FALSE;
2717 for (l = conflicts; l; l = l->next) {
2718 if (decl == l->data) {
2719 in_conflict = TRUE;
2720 break;
2723 if (in_conflict) {
2724 MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
2725 goto leave;
2730 * if the iterface method is reabstracted, and either the found implementation method is abstract, or the found
2731 * implementation method is from another DIM (meaning neither klass nor any of its ancestor classes implemented
2732 * the method), then say the target method is null.
2734 if (method_is_reabstracted (method) &&
2735 (m_method_is_abstract (foundMethod) ||
2736 (mono_class_is_interface (foundMethod->klass) && method_is_dim (foundMethod))))
2737 MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
2738 else if (mono_class_is_interface (foundMethod->klass) && method_is_reabstracted (foundMethod) && !m_class_is_abstract (klass)) {
2739 /* if the method we found is a reabstracted DIM method, but the class isn't abstract, return NULL */
2741 * (C# doesn't seem to allow constructing such types, it requires the whole class to be abstract - in
2742 * which case we are supposed to return the reabstracted interface method. But in IL we can make a
2743 * non-abstract class with reabstracted interface methods - which is supposed to fail with an
2744 * EntryPointNotFoundException at invoke time, but does not prevent the class from loading.)
2746 MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
2747 } else {
2748 MONO_HANDLE_ASSIGN (member, mono_method_get_object_handle (domain, foundMethod, mono_class_is_interface (foundMethod->klass) ? foundMethod->klass : klass, error));
2749 goto_if_nok (error, leave);
2750 MONO_HANDLE_ARRAY_SETREF (targets, i, member);
2753 leave:
2754 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
2757 void
2758 ves_icall_RuntimeType_GetInterfaceMapData (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_iface, MonoArrayHandleOut targets, MonoArrayHandleOut methods, MonoError *error)
2760 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2761 MonoClass *klass = mono_class_from_mono_type_internal (type);
2762 MonoType *iface = MONO_HANDLE_GETVAL (ref_iface, type);
2763 MonoClass *iclass = mono_class_from_mono_type_internal (iface);
2765 mono_class_init_checked (klass, error);
2766 return_if_nok (error);
2767 mono_class_init_checked (iclass, error);
2768 return_if_nok (error);
2770 mono_class_setup_vtable (klass);
2772 gboolean variance_used;
2773 int ioffset = mono_class_interface_offset_with_variance (klass, iclass, &variance_used);
2774 if (ioffset == -1)
2775 return;
2777 MonoMethod* method;
2778 int i = 0;
2779 gpointer iter = NULL;
2780 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
2782 while ((method = mono_class_get_methods(iclass, &iter))) {
2783 if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
2784 i++;
2787 MonoArrayHandle targets_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, i, error);
2788 return_if_nok (error);
2789 MONO_HANDLE_ASSIGN (targets, targets_arr);
2791 MonoArrayHandle methods_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, i, error);
2792 return_if_nok (error);
2793 MONO_HANDLE_ASSIGN (methods, methods_arr);
2795 i = 0;
2796 iter = NULL;
2798 while ((method = mono_class_get_methods (iclass, &iter))) {
2799 if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2800 continue;
2801 if (!set_interface_map_data_method_object (domain, method, iclass, ioffset, klass, targets, methods, i, error))
2802 return;
2803 i ++;
2807 void
2808 ves_icall_RuntimeType_GetPacking (MonoReflectionTypeHandle ref_type, guint32 *packing, guint32 *size, MonoError *error)
2810 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2811 MonoClass *klass = mono_class_from_mono_type_internal (type);
2813 mono_class_init_checked (klass, error);
2814 return_if_nok (error);
2816 if (image_is_dynamic (m_class_get_image (klass))) {
2817 MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, ref_type);
2818 *packing = MONO_HANDLE_GETVAL (tb, packing_size);
2819 *size = MONO_HANDLE_GETVAL (tb, class_size);
2820 } else {
2821 mono_metadata_packing_from_typedef (m_class_get_image (klass), m_class_get_type_token (klass), packing, size);
2825 MonoReflectionTypeHandle
2826 ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
2828 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
2829 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2831 if (!type->byref && type->type == MONO_TYPE_SZARRAY) {
2832 return mono_type_get_object_handle (domain, m_class_get_byval_arg (type->data.klass), error);
2835 MonoClass *klass = mono_class_from_mono_type_internal (type);
2836 mono_class_init_checked (klass, error);
2837 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
2839 // GetElementType should only return a type for:
2840 // Array Pointer PassedByRef
2841 if (type->byref)
2842 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
2843 else if (m_class_get_element_class (klass) && MONO_CLASS_IS_ARRAY (klass))
2844 return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_element_class (klass)), error);
2845 else if (m_class_get_element_class (klass) && type->type == MONO_TYPE_PTR)
2846 return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_element_class (klass)), error);
2847 else
2848 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
2851 MonoReflectionTypeHandle
2852 ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionTypeHandle ref_type, MonoError *error)
2854 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
2855 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2857 if (type->byref)
2858 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
2860 MonoClass *klass = mono_class_from_mono_type_internal (type);
2861 if (!m_class_get_parent (klass))
2862 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
2864 return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_parent (klass)), error);
2867 guint32
2868 ves_icall_RuntimeTypeHandle_GetCorElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
2870 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2872 if (type->byref)
2873 return MONO_TYPE_BYREF;
2874 else
2875 return (guint32)type->type;
2878 MonoBoolean
2879 ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type, MonoError *error)
2881 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2882 MonoClass *klass;
2884 klass = mono_class_from_mono_type_internal (type);
2885 mono_class_init_internal (klass);
2886 return m_class_has_references (klass);
2889 MonoBoolean
2890 ves_icall_RuntimeTypeHandle_IsByRefLike (MonoReflectionTypeHandle ref_type, MonoError *error)
2892 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2893 /* .NET Core says byref types are not IsByRefLike */
2894 if (type->byref)
2895 return FALSE;
2896 MonoClass *klass = mono_class_from_mono_type_internal (type);
2897 return m_class_is_byreflike (klass);
2900 MonoBoolean
2901 ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionTypeHandle ref_type, MonoError *error)
2903 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2904 MonoClass *klass = mono_class_from_mono_type_internal (type);
2905 mono_class_init_checked (klass, error);
2906 return_val_if_nok (error, FALSE);
2908 return mono_class_is_com_object (klass);
2911 guint32
2912 ves_icall_reflection_get_token (MonoObjectHandle obj, MonoError *error)
2914 error_init (error);
2915 return mono_reflection_get_token_checked (obj, error);
2918 MonoReflectionModuleHandle
2919 ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionTypeHandle type, MonoError *error)
2921 MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
2922 MonoType *t = MONO_HANDLE_GETVAL (type, type);
2923 MonoClass *klass = mono_class_from_mono_type_internal (t);
2924 return mono_module_get_object_handle (domain, m_class_get_image (klass), error);
2927 MonoReflectionAssemblyHandle
2928 ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionTypeHandle type, MonoError *error)
2930 MonoDomain *domain = mono_domain_get ();
2931 MonoType *t = MONO_HANDLE_GETVAL (type, type);
2932 MonoClass *klass = mono_class_from_mono_type_internal (t);
2933 return mono_assembly_get_object_handle (domain, m_class_get_image (klass)->assembly, error);
2936 MonoReflectionTypeHandle
2937 ves_icall_RuntimeType_get_DeclaringType (MonoReflectionTypeHandle ref_type, MonoError *error)
2939 MonoDomain *domain = mono_domain_get ();
2940 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2941 MonoClass *klass;
2943 if (type->byref)
2944 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
2945 if (type->type == MONO_TYPE_VAR) {
2946 MonoGenericContainer *param = mono_type_get_generic_param_owner (type);
2947 klass = param ? param->owner.klass : NULL;
2948 } else if (type->type == MONO_TYPE_MVAR) {
2949 MonoGenericContainer *param = mono_type_get_generic_param_owner (type);
2950 klass = param ? param->owner.method->klass : NULL;
2951 } else {
2952 klass = m_class_get_nested_in (mono_class_from_mono_type_internal (type));
2955 if (!klass)
2956 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
2958 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
2961 MonoStringHandle
2962 ves_icall_RuntimeType_get_Name (MonoReflectionTypeHandle reftype, MonoError *error)
2964 MonoDomain *domain = mono_domain_get ();
2965 MonoType *type = MONO_HANDLE_RAW(reftype)->type;
2966 MonoClass *klass = mono_class_from_mono_type_internal (type);
2967 // FIXME: this should be escaped in some scenarios with mono_identifier_escape_type_name_chars
2968 // Determining exactly when to do so is fairly difficult, so for now we don't bother to avoid regressions
2969 const char *klass_name = m_class_get_name (klass);
2971 if (type->byref) {
2972 char *n = g_strdup_printf ("%s&", klass_name);
2973 MonoStringHandle res = mono_string_new_handle (domain, n, error);
2975 g_free (n);
2977 return res;
2978 } else {
2979 return mono_string_new_handle (domain, klass_name, error);
2983 MonoStringHandle
2984 ves_icall_RuntimeType_get_Namespace (MonoReflectionTypeHandle type, MonoError *error)
2986 MonoDomain *domain = mono_domain_get ();
2987 MonoClass *klass = mono_class_from_mono_type_handle (type);
2989 MonoClass *klass_nested_in;
2990 while ((klass_nested_in = m_class_get_nested_in (klass)))
2991 klass = klass_nested_in;
2993 if (m_class_get_name_space (klass) [0] == '\0')
2994 return NULL_HANDLE_STRING;
2996 char *escaped = mono_identifier_escape_type_name_chars (m_class_get_name_space (klass));
2997 MonoStringHandle res = mono_string_new_handle (domain, escaped, error);
2998 g_free (escaped);
2999 return res;
3002 gint32
3003 ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionTypeHandle ref_type, MonoError *error)
3005 error_init (error);
3006 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3008 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
3009 mono_error_set_argument (error, "type", "Type must be an array type");
3010 return 0;
3013 MonoClass *klass = mono_class_from_mono_type_internal (type);
3015 return m_class_get_rank (klass);
3018 static MonoArrayHandle
3019 create_type_array (MonoDomain *domain, MonoBoolean runtimeTypeArray, int count, MonoError *error)
3021 return mono_array_new_handle (domain, runtimeTypeArray ? mono_defaults.runtimetype_class : mono_defaults.systemtype_class, count, error);
3024 static gboolean
3025 set_type_object_in_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int i, MonoError *error)
3027 HANDLE_FUNCTION_ENTER();
3028 error_init (error);
3029 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, type, error);
3030 goto_if_nok (error, leave);
3032 MONO_HANDLE_ARRAY_SETREF (dest, i, rt);
3034 leave:
3035 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3038 void
3039 ves_icall_RuntimeType_GetGUID (MonoReflectionTypeHandle type_handle, MonoArrayHandle guid_handle, MonoError *error)
3041 error_init (error);
3043 g_assert (mono_array_handle_length (guid_handle) == 16);
3044 if (MONO_HANDLE_IS_NULL (type_handle)) {
3045 mono_error_set_argument_null (error, "type", "");
3046 return;
3049 MonoType *type = MONO_HANDLE_GETVAL (type_handle, type);
3050 MonoClass *klass = mono_class_from_mono_type_internal (type);
3051 if (!mono_class_init_checked (klass, error))
3052 return;
3054 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_handle), 1, 0);
3055 mono_metadata_get_class_guid (klass, data, error);
3058 MonoArrayHandle
3059 ves_icall_RuntimeType_GetGenericArguments (MonoReflectionTypeHandle ref_type, MonoBoolean runtimeTypeArray, MonoError *error)
3061 error_init (error);
3062 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3064 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3065 MonoClass *klass = mono_class_from_mono_type_internal (type);
3067 MonoArrayHandle res = MONO_HANDLE_NEW (MonoArray, NULL);
3068 if (mono_class_is_gtd (klass)) {
3069 MonoGenericContainer *container = mono_class_get_generic_container (klass);
3070 MONO_HANDLE_ASSIGN (res, create_type_array (domain, runtimeTypeArray, container->type_argc, error));
3071 goto_if_nok (error, leave);
3072 for (int i = 0; i < container->type_argc; ++i) {
3073 MonoClass *pklass = mono_class_create_generic_parameter (mono_generic_container_get_param (container, i));
3075 if (!set_type_object_in_array (domain, m_class_get_byval_arg (pklass), res, i, error))
3076 goto leave;
3079 } else if (mono_class_is_ginst (klass)) {
3080 MonoGenericInst *inst = mono_class_get_generic_class (klass)->context.class_inst;
3081 MONO_HANDLE_ASSIGN (res, create_type_array (domain, runtimeTypeArray, inst->type_argc, error));
3082 goto_if_nok (error, leave);
3083 for (int i = 0; i < inst->type_argc; ++i) {
3084 if (!set_type_object_in_array (domain, inst->type_argv [i], res, i, error))
3085 goto leave;
3089 leave:
3090 return res;
3093 MonoBoolean
3094 ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle ref_type, MonoError *error)
3096 error_init (error);
3098 if (!IS_MONOTYPE (MONO_HANDLE_RAW(ref_type)))
3099 return FALSE;
3101 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3102 if (type->byref)
3103 return FALSE;
3105 MonoClass *klass = mono_class_from_mono_type_internal (type);
3106 return mono_class_is_gtd (klass);
3109 MonoReflectionTypeHandle
3110 ves_icall_RuntimeTypeHandle_GetGenericTypeDefinition_impl (MonoReflectionTypeHandle ref_type, MonoError *error)
3112 error_init (error);
3113 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3115 MonoReflectionTypeHandle ret = MONO_HANDLE_NEW (MonoReflectionType, NULL);
3117 if (type->byref)
3118 goto leave;
3120 MonoClass *klass;
3121 klass = mono_class_from_mono_type_internal (type);
3123 if (mono_class_is_gtd (klass)) {
3124 /* check this one */
3125 MONO_HANDLE_ASSIGN (ret, ref_type);
3126 goto leave;
3128 if (mono_class_is_ginst (klass)) {
3129 MonoClass *generic_class = mono_class_get_generic_class (klass)->container_class;
3131 MonoGCHandle ref_info_handle = mono_class_get_ref_info_handle (generic_class);
3133 if (m_class_was_typebuilder (generic_class) && ref_info_handle) {
3134 MonoObjectHandle tb = mono_gchandle_get_target_handle (ref_info_handle);
3135 g_assert (!MONO_HANDLE_IS_NULL (tb));
3136 MONO_HANDLE_ASSIGN (ret, tb);
3137 } else {
3138 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3139 MONO_HANDLE_ASSIGN (ret, mono_type_get_object_handle (domain, m_class_get_byval_arg (generic_class), error));
3142 leave:
3143 return ret;
3146 MonoReflectionTypeHandle
3147 ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype, MonoArrayHandle type_array, MonoError *error)
3149 error_init (error);
3150 MonoDomain *domain = MONO_HANDLE_DOMAIN (reftype);
3152 g_assert (IS_MONOTYPE_HANDLE (reftype));
3153 MonoType *type = MONO_HANDLE_GETVAL (reftype, type);
3154 mono_class_init_checked (mono_class_from_mono_type_internal (type), error);
3155 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
3157 int count = mono_array_handle_length (type_array);
3158 MonoType **types = g_new0 (MonoType *, count);
3160 MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
3161 for (int i = 0; i < count; i++) {
3162 MONO_HANDLE_ARRAY_GETREF (t, type_array, i);
3163 types [i] = MONO_HANDLE_GETVAL (t, type);
3166 MonoType *geninst = mono_reflection_bind_generic_parameters (reftype, count, types, error);
3167 g_free (types);
3168 if (!geninst) {
3169 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3172 MonoClass *klass = mono_class_from_mono_type_internal (geninst);
3174 /*we might inflate to the GTD*/
3175 if (mono_class_is_ginst (klass) && !mono_verifier_class_is_valid_generic_instantiation (klass)) {
3176 mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
3177 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3180 return mono_type_get_object_handle (domain, geninst, error);
3183 MonoBoolean
3184 ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type, MonoError *error)
3186 error_init (error);
3187 MonoClass *klass;
3189 if (!IS_MONOTYPE (MONO_HANDLE_RAW (ref_type)))
3190 return FALSE;
3192 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3193 if (type->byref)
3194 return FALSE;
3196 klass = mono_class_from_mono_type_internal (type);
3197 return mono_class_is_ginst (klass) || mono_class_is_gtd (klass);
3200 gint32
3201 ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_type, MonoError *error)
3203 error_init (error);
3204 if (!IS_MONOTYPE_HANDLE (ref_type))
3205 return -1;
3206 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3208 if (is_generic_parameter (type))
3209 return mono_type_get_generic_param_num (type);
3210 return -1;
3213 MonoGenericParamInfo *
3214 ves_icall_RuntimeTypeHandle_GetGenericParameterInfo (MonoReflectionTypeHandle ref_type, MonoError *error)
3216 error_init (error);
3217 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3218 return mono_generic_param_info (type->data.generic_param);
3221 MonoBoolean
3222 ves_icall_RuntimeTypeHandle_IsGenericVariable (MonoReflectionTypeHandle ref_type, MonoError *error)
3224 MonoType *type = MONO_HANDLE_GETVAL(ref_type, type);
3225 return is_generic_parameter (type);
3228 MonoReflectionMethodHandle
3229 ves_icall_RuntimeType_GetCorrespondingInflatedMethod (MonoReflectionTypeHandle ref_type,
3230 MonoReflectionMethodHandle generic,
3231 MonoError *error)
3233 error_init (error);
3234 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3235 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3236 MonoClass *klass = mono_class_from_mono_type_internal (type);
3238 mono_class_init_checked (klass, error);
3239 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
3241 MonoMethod *generic_method = MONO_HANDLE_GETVAL (generic, method);
3243 MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
3244 MonoMethod *method;
3245 gpointer iter = NULL;
3246 while ((method = mono_class_get_methods (klass, &iter))) {
3247 if (method->token == generic_method->token) {
3248 ret = mono_method_get_object_handle (domain, method, klass, error);
3249 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
3253 return ret;
3256 MonoReflectionMethodHandle
3257 ves_icall_RuntimeType_get_DeclaringMethod (MonoReflectionTypeHandle ref_type, MonoError *error)
3259 error_init (error);
3260 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3261 MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
3263 if (type->byref || (type->type != MONO_TYPE_MVAR && type->type != MONO_TYPE_VAR)) {
3264 mono_error_set_invalid_operation (error, "DeclaringMethod can only be used on generic arguments");
3265 goto leave;
3267 if (type->type == MONO_TYPE_VAR)
3268 goto leave;
3270 MonoMethod *method;
3271 method = mono_type_get_generic_param_owner (type)->owner.method;
3272 g_assert (method);
3274 MonoDomain *domain;
3275 domain = MONO_HANDLE_DOMAIN (ref_type);
3277 MONO_HANDLE_ASSIGN (ret, mono_method_get_object_handle (domain, method, method->klass, error));
3278 leave:
3279 return ret;
3282 MonoBoolean
3283 ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError *error)
3285 error_init (error);
3286 mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsTypeExportedToWindowsRuntime");
3287 return FALSE;
3290 MonoBoolean
3291 ves_icall_System_RuntimeType_IsWindowsRuntimeObjectType (MonoError *error)
3293 error_init (error);
3294 mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsWindowsRuntimeObjectType");
3295 return FALSE;
3298 void
3299 ves_icall_RuntimeMethodInfo_GetPInvoke (MonoReflectionMethodHandle ref_method, int* flags, MonoStringHandleOut entry_point, MonoStringHandleOut dll_name, MonoError *error)
3301 MonoDomain *domain = mono_domain_get ();
3302 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3303 MonoImage *image = m_class_get_image (method->klass);
3304 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
3305 MonoTableInfo *tables = image->tables;
3306 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
3307 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
3308 guint32 im_cols [MONO_IMPLMAP_SIZE];
3309 guint32 scope_token;
3310 const char *import = NULL;
3311 const char *scope = NULL;
3313 error_init (error);
3315 if (image_is_dynamic (image)) {
3316 MonoReflectionMethodAux *method_aux =
3317 (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)image)->method_aux_hash, method);
3318 if (method_aux) {
3319 import = method_aux->dllentry;
3320 scope = method_aux->dll;
3323 if (!import || !scope) {
3324 mono_error_set_argument (error, "method", "System.Refleciton.Emit method with invalid pinvoke information");
3325 return;
3328 else {
3329 if (piinfo->implmap_idx) {
3330 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
3332 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
3333 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
3334 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
3335 scope = mono_metadata_string_heap (image, scope_token);
3339 *flags = piinfo->piflags;
3340 MONO_HANDLE_ASSIGN (entry_point, mono_string_new_handle (domain, import, error));
3341 return_if_nok (error);
3342 MONO_HANDLE_ASSIGN (dll_name, mono_string_new_handle (domain, scope, error));
3345 MonoReflectionMethodHandle
3346 ves_icall_RuntimeMethodInfo_GetGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *error)
3348 error_init (error);
3349 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3351 if (method->is_generic)
3352 return ref_method;
3354 if (!method->is_inflated)
3355 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
3357 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
3359 MonoMethod *result = imethod->declaring;
3360 /* Not a generic method. */
3361 if (!result->is_generic)
3362 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
3364 if (image_is_dynamic (m_class_get_image (method->klass))) {
3365 MonoDynamicImage *image = (MonoDynamicImage*)m_class_get_image (method->klass);
3368 * FIXME: Why is this stuff needed at all ? Why can't the code below work for
3369 * the dynamic case as well ?
3371 mono_image_lock ((MonoImage*)image);
3372 MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, (MonoReflectionMethod*)mono_g_hash_table_lookup (image->generic_def_objects, imethod));
3373 mono_image_unlock ((MonoImage*)image);
3375 if (!MONO_HANDLE_IS_NULL (res))
3376 return res;
3379 if (imethod->context.class_inst) {
3380 MonoClass *klass = ((MonoMethod *) imethod)->klass;
3381 /*Generic methods gets the context of the GTD.*/
3382 if (mono_class_get_context (klass)) {
3383 result = mono_class_inflate_generic_method_full_checked (result, klass, mono_class_get_context (klass), error);
3384 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
3388 return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (ref_method), result, NULL, error);
3392 MonoBoolean
3393 ves_icall_RuntimeMethodInfo_get_IsGenericMethod (MonoReflectionMethodHandle ref_method, MonoError *erro)
3395 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3396 return mono_method_signature_internal (method)->generic_param_count != 0;
3399 MonoBoolean
3400 ves_icall_RuntimeMethodInfo_get_IsGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *Error)
3402 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3403 return method->is_generic;
3406 static gboolean
3407 set_array_generic_argument_handle_inflated (MonoDomain *domain, MonoGenericInst *inst, int i, MonoArrayHandle arr, MonoError *error)
3409 HANDLE_FUNCTION_ENTER ();
3410 error_init (error);
3411 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, inst->type_argv [i], error);
3412 goto_if_nok (error, leave);
3413 MONO_HANDLE_ARRAY_SETREF (arr, i, rt);
3414 leave:
3415 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3418 static gboolean
3419 set_array_generic_argument_handle_gparam (MonoDomain *domain, MonoGenericContainer *container, int i, MonoArrayHandle arr, MonoError *error)
3421 HANDLE_FUNCTION_ENTER ();
3422 error_init (error);
3423 MonoGenericParam *param = mono_generic_container_get_param (container, i);
3424 MonoClass *pklass = mono_class_create_generic_parameter (param);
3425 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (pklass), error);
3426 goto_if_nok (error, leave);
3427 MONO_HANDLE_ARRAY_SETREF (arr, i, rt);
3428 leave:
3429 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3432 MonoArrayHandle
3433 ves_icall_RuntimeMethodInfo_GetGenericArguments (MonoReflectionMethodHandle ref_method, MonoError *error)
3435 error_init (error);
3436 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_method);
3437 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3439 if (method->is_inflated) {
3440 MonoGenericInst *inst = mono_method_get_context (method)->method_inst;
3442 if (inst) {
3443 int count = inst->type_argc;
3444 MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
3445 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3447 for (int i = 0; i < count; i++) {
3448 if (!set_array_generic_argument_handle_inflated (domain, inst, i, res, error))
3449 break;
3451 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3452 return res;
3456 int count = mono_method_signature_internal (method)->generic_param_count;
3457 MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
3458 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3460 MonoGenericContainer *container = mono_method_get_generic_container (method);
3461 for (int i = 0; i < count; i++) {
3462 if (!set_array_generic_argument_handle_gparam (domain, container, i, res, error))
3463 break;
3465 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3466 return res;
3469 MonoObjectHandle
3470 ves_icall_InternalInvoke (MonoReflectionMethodHandle method_handle, MonoObjectHandle this_arg_handle,
3471 MonoArrayHandle params_handle, MonoExceptionHandleOut exception_out, MonoError *error)
3473 MonoReflectionMethod* const method = MONO_HANDLE_RAW (method_handle);
3474 MonoObject* const this_arg = MONO_HANDLE_RAW (this_arg_handle);
3475 MonoArray* const params = MONO_HANDLE_RAW (params_handle);
3478 * Invoke from reflection is supposed to always be a virtual call (the API
3479 * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
3480 * greater flexibility.
3482 MonoMethod *m = method->method;
3483 MonoMethodSignature* const sig = mono_method_signature_internal (m);
3484 MonoImage *image = NULL;
3485 int pcount = 0;
3486 void *obj = this_arg;
3487 char *this_name = NULL;
3488 char *target_name = NULL;
3489 char *msg = NULL;
3490 MonoObject *result = NULL;
3491 MonoArray *arr = NULL;
3492 MonoException *exception = NULL;
3494 *MONO_HANDLE_REF (exception_out) = NULL;
3496 if (mono_security_core_clr_enabled () &&
3497 !mono_security_core_clr_ensure_reflection_access_method (m, error)) {
3498 goto return_null;
3501 if (!(m->flags & METHOD_ATTRIBUTE_STATIC)) {
3502 if (!mono_class_vtable_checked (mono_object_domain (method), m->klass, error)) {
3503 mono_error_cleanup (error); /* FIXME does this make sense? */
3504 error_init_reuse (error);
3505 exception = mono_class_get_exception_for_failure (m->klass);
3506 goto return_null;
3509 if (this_arg) {
3510 if (!mono_object_isinst_checked (this_arg, m->klass, error)) {
3511 if (!is_ok (error)) {
3512 exception = mono_error_convert_to_exception (error);
3513 goto return_null;
3515 this_name = mono_type_get_full_name (mono_object_class (this_arg));
3516 target_name = mono_type_get_full_name (m->klass);
3517 msg = g_strdup_printf ("Object of type '%s' doesn't match target type '%s'", this_name, target_name);
3518 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", msg);
3519 goto return_null;
3521 m = mono_object_get_virtual_method_internal (this_arg, m);
3522 /* must pass the pointer to the value for valuetype methods */
3523 if (m_class_is_valuetype (m->klass)) {
3524 obj = mono_object_unbox_internal (this_arg);
3525 // FIXMEcoop? Does obj need to be put into a handle?
3527 } else if (strcmp (m->name, ".ctor") && !m->wrapper_type) {
3528 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", "Non-static method requires a target.");
3529 goto return_null;
3533 if ((m->klass != NULL && m_class_is_byreflike (m->klass)) || m_class_is_byreflike (mono_class_from_mono_type_internal (sig->ret))) {
3534 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method with stack pointers via reflection");
3535 goto return_null;
3537 if (sig->ret->byref) {
3538 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method returning ByRef type via reflection");
3539 goto return_null;
3542 pcount = params? mono_array_length_internal (params): 0;
3543 if (pcount != sig->param_count) {
3544 exception = mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetParameterCountException");
3545 goto return_null;
3548 if (mono_class_is_abstract (m->klass) && !strcmp (m->name, ".ctor") && !this_arg) {
3549 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", "Cannot invoke constructor of an abstract class.");
3550 goto return_null;
3553 image = m_class_get_image (m->klass);
3554 if (mono_asmctx_get_kind (&image->assembly->context) == MONO_ASMCTX_REFONLY) {
3555 exception = mono_get_exception_invalid_operation ("It is illegal to invoke a method on a type loaded using the ReflectionOnly api.");
3556 goto return_null;
3559 if (image_is_dynamic (image) && !((MonoDynamicImage*)image)->run) {
3560 exception = mono_get_exception_not_supported ("Cannot invoke a method in a dynamic assembly without run access.");
3561 goto return_null;
3564 if (m_class_get_rank (m->klass) && !strcmp (m->name, ".ctor")) {
3565 int i;
3566 pcount = mono_array_length_internal (params);
3567 uintptr_t * const lengths = g_newa (uintptr_t, pcount);
3568 /* Note: the synthetized array .ctors have int32 as argument type */
3569 for (i = 0; i < pcount; ++i)
3570 lengths [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, i) + sizeof (MonoObject));
3572 if (m_class_get_rank (m->klass) == 1 && sig->param_count == 2 && m_class_get_rank (m_class_get_element_class (m->klass))) {
3573 /* This is a ctor for jagged arrays. MS creates an array of arrays. */
3574 arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, NULL, error);
3575 goto_if_nok (error, return_null);
3577 MonoArrayHandle subarray_handle = MONO_HANDLE_NEW (MonoArray, NULL);
3579 for (i = 0; i < mono_array_length_internal (arr); ++i) {
3580 MonoArray *subarray = mono_array_new_full_checked (mono_object_domain (params), m_class_get_element_class (m->klass), &lengths [1], NULL, error);
3581 goto_if_nok (error, return_null);
3582 MONO_HANDLE_ASSIGN_RAW (subarray_handle, subarray); // FIXME? Overkill?
3583 mono_array_setref_fast (arr, i, subarray);
3585 goto exit;
3588 if (m_class_get_rank (m->klass) == pcount) {
3589 /* Only lengths provided. */
3590 arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, NULL, error);
3591 goto_if_nok (error, return_null);
3592 goto exit;
3593 } else {
3594 g_assert (pcount == (m_class_get_rank (m->klass) * 2));
3595 /* The arguments are lower-bound-length pairs */
3596 intptr_t * const lower_bounds = (intptr_t *)g_alloca (sizeof (intptr_t) * pcount);
3598 for (i = 0; i < pcount / 2; ++i) {
3599 lower_bounds [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, (i * 2)) + sizeof (MonoObject));
3600 lengths [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, (i * 2) + 1) + sizeof (MonoObject));
3603 arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, lower_bounds, error);
3604 goto_if_nok (error, return_null);
3605 goto exit;
3608 result = mono_runtime_invoke_array_checked (m, obj, params, error);
3609 goto exit;
3610 return_null:
3611 result = NULL;
3612 arr = NULL;
3613 exit:
3614 if (exception) {
3615 MONO_HANDLE_NEW (MonoException, exception); // FIXME? overkill?
3616 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (exception_out), (MonoObject*)exception);
3618 g_free (target_name);
3619 g_free (this_name);
3620 g_free (msg);
3621 g_assert (!result || !arr); // only one, or neither, should be set
3622 return result ? MONO_HANDLE_NEW (MonoObject, result) : arr ? MONO_HANDLE_NEW (MonoObject, (MonoObject*)arr) : NULL_HANDLE;
3625 #ifndef DISABLE_REMOTING
3626 static void
3627 internal_execute_field_getter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArrayHandleOut outArgs, MonoError *error)
3629 MonoArray *out_args;
3630 MonoClass *k = mono_object_class (this_arg);
3631 MonoString *name;
3632 char *str;
3634 // FIXME Refactor/inline internal_execute_field_setter and internal_execute_field_getter.
3636 /* If this is a proxy, then it must be a CBO */
3637 if (mono_class_is_transparent_proxy (k)) {
3638 MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
3639 this_arg = tp->rp->unwrapped_server;
3640 MONO_HANDLE_NEW (MonoObject, this_arg);
3641 g_assert (this_arg);
3642 k = mono_object_class (this_arg);
3645 name = mono_array_get_internal (params, MonoString *, 1);
3646 MONO_HANDLE_NEW (MonoString, name);
3648 str = mono_string_to_utf8_checked_internal (name, error);
3649 return_if_nok (error);
3651 do {
3652 MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
3653 if (field) {
3654 g_free (str);
3655 MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
3656 MonoObject *result;
3657 if (m_class_is_valuetype (field_klass)) {
3658 result = mono_value_box_checked (domain, field_klass, (char *)this_arg + field->offset, error);
3659 return_if_nok (error);
3660 } else
3661 result = (MonoObject *)*((gpointer *)((char *)this_arg + field->offset));
3663 MONO_HANDLE_NEW (MonoObject, result);
3665 out_args = mono_array_new_checked (domain, mono_defaults.object_class, 1, error);
3666 return_if_nok (error);
3667 MONO_HANDLE_NEW (MonoArray, out_args); // FIXME? overkill?
3668 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*) out_args);
3669 mono_array_setref_internal (out_args, 0, result);
3670 return;
3672 k = m_class_get_parent (k);
3673 } while (k);
3675 g_free (str);
3676 g_assert_not_reached ();
3679 static void
3680 internal_execute_field_setter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArrayHandleOut outArgs, MonoError *error)
3682 MonoArray *out_args;
3683 MonoClass *k = mono_object_class (this_arg);
3684 MonoString *name;
3685 guint32 size;
3686 gint32 align;
3687 char *str;
3689 /* If this is a proxy, then it must be a CBO */
3690 if (mono_class_is_transparent_proxy (k)) {
3691 MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
3692 this_arg = tp->rp->unwrapped_server;
3693 MONO_HANDLE_NEW (MonoObject, this_arg);
3694 g_assert (this_arg);
3695 k = mono_object_class (this_arg);
3698 name = mono_array_get_internal (params, MonoString *, 1);
3699 MONO_HANDLE_NEW (MonoString, name);
3701 str = mono_string_to_utf8_checked_internal (name, error);
3702 return_if_nok (error);
3704 do {
3705 MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
3706 if (field) {
3707 g_free (str);
3708 MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
3709 MonoObject *val = (MonoObject *)mono_array_get_internal (params, gpointer, 2);
3710 MONO_HANDLE_NEW (MonoObject, val);
3712 if (m_class_is_valuetype (field_klass)) {
3713 size = mono_type_size (field->type, &align);
3714 g_assert (size == mono_class_value_size (field_klass, NULL));
3715 mono_gc_wbarrier_value_copy_internal ((char *)this_arg + field->offset, (char*)val + sizeof (MonoObject), 1, field_klass);
3716 } else {
3717 mono_gc_wbarrier_set_field_internal (this_arg, (char*)this_arg + field->offset, val);
3720 out_args = mono_array_new_checked (domain, mono_defaults.object_class, 0, error);
3721 return_if_nok (error);
3722 MONO_HANDLE_NEW (MonoArray, out_args); // FIXME? overkill?
3723 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*) out_args);
3724 return;
3727 k = m_class_get_parent (k);
3728 } while (k);
3730 g_free (str);
3731 g_assert_not_reached ();
3734 MonoObjectHandle
3735 ves_icall_InternalExecute (MonoReflectionMethodHandle method_handle, MonoObjectHandle this_arg_handle, MonoArrayHandle params_handle, MonoArrayHandleOut outArgs, MonoError* error)
3737 MONO_HANDLE_ASSIGN_RAW (outArgs, NULL);
3739 MonoReflectionMethod* const method = MONO_HANDLE_RAW (method_handle);
3740 MonoObject* const this_arg = MONO_HANDLE_RAW (this_arg_handle);
3741 MonoArray* const params = MONO_HANDLE_RAW (params_handle);
3742 MonoObjectHandle null_handle = NULL_HANDLE_INIT;
3744 MonoDomain *domain = mono_object_domain (method);
3745 MonoMethod *m = method->method;
3746 MonoMethodSignature *sig = mono_method_signature_internal (m);
3747 MonoArray *out_args;
3748 MonoObject *result;
3749 int i, j, outarg_count = 0;
3751 if (m->klass == mono_defaults.object_class) {
3753 if (!strcmp (m->name, "FieldGetter")) {
3754 internal_execute_field_getter (domain, this_arg, params, outArgs, error);
3755 return null_handle;
3756 } else if (!strcmp (m->name, "FieldSetter")) {
3757 internal_execute_field_setter (domain, this_arg, params, outArgs, error);
3758 return null_handle;
3762 for (i = 0; i < mono_array_length_internal (params); i++) {
3763 if (sig->params [i]->byref)
3764 outarg_count++;
3767 out_args = mono_array_new_checked (domain, mono_defaults.object_class, outarg_count, error);
3768 return_val_if_nok (error, null_handle);
3769 MONO_HANDLE_NEW (MonoArray, out_args);
3771 /* handle constructors only for objects already allocated */
3772 if (!strcmp (method->method->name, ".ctor"))
3773 g_assert (this_arg);
3775 /* This can be called only on MBR objects, so no need to unbox for valuetypes. */
3776 g_assert (!m_class_is_valuetype (method->method->klass));
3777 result = mono_runtime_invoke_array_checked (method->method, this_arg, params, error);
3778 return_val_if_nok (error, null_handle);
3780 MonoObjectHandle result_handle = MONO_HANDLE_NEW (MonoObject, result);
3781 MonoObjectHandle arg_handle = MONO_HANDLE_NEW (MonoObject, NULL);
3783 for (i = 0, j = 0; i < mono_array_length_internal (params); i++) {
3784 if (sig->params [i]->byref) {
3785 gpointer arg;
3786 arg = mono_array_get_internal (params, gpointer, i);
3787 MONO_HANDLE_ASSIGN_RAW (arg_handle, arg); // FIXME? overkill?
3788 mono_array_setref_internal (out_args, j, arg);
3789 j++;
3793 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*)out_args);
3795 return result_handle;
3797 #endif
3799 static guint64
3800 read_enum_value (const char *mem, int type)
3802 switch (type) {
3803 case MONO_TYPE_BOOLEAN:
3804 case MONO_TYPE_U1:
3805 return *(guint8*)mem;
3806 case MONO_TYPE_I1:
3807 return *(gint8*)mem;
3808 case MONO_TYPE_CHAR:
3809 case MONO_TYPE_U2:
3810 return read16 (mem);
3811 case MONO_TYPE_I2:
3812 return (gint16) read16 (mem);
3813 case MONO_TYPE_U4:
3814 case MONO_TYPE_R4:
3815 return read32 (mem);
3816 case MONO_TYPE_I4:
3817 return (gint32) read32 (mem);
3818 case MONO_TYPE_U8:
3819 case MONO_TYPE_I8:
3820 case MONO_TYPE_R8:
3821 return read64 (mem);
3822 case MONO_TYPE_U:
3823 case MONO_TYPE_I:
3824 #if SIZEOF_REGISTER == 8
3825 return read64 (mem);
3826 #else
3827 return read32 (mem);
3828 #endif
3829 default:
3830 g_assert_not_reached ();
3832 return 0;
3835 static void
3836 write_enum_value (void *mem, int type, guint64 value)
3838 switch (type) {
3839 case MONO_TYPE_U1:
3840 case MONO_TYPE_I1:
3841 case MONO_TYPE_BOOLEAN: {
3842 guint8 *p = (guint8*)mem;
3843 *p = value;
3844 break;
3846 case MONO_TYPE_U2:
3847 case MONO_TYPE_I2:
3848 case MONO_TYPE_CHAR: {
3849 guint16 *p = (guint16 *)mem;
3850 *p = value;
3851 break;
3853 case MONO_TYPE_U4:
3854 case MONO_TYPE_I4:
3855 case MONO_TYPE_R4: {
3856 guint32 *p = (guint32 *)mem;
3857 *p = value;
3858 break;
3860 case MONO_TYPE_U8:
3861 case MONO_TYPE_I8:
3862 case MONO_TYPE_R8: {
3863 guint64 *p = (guint64 *)mem;
3864 *p = value;
3865 break;
3867 case MONO_TYPE_U:
3868 case MONO_TYPE_I: {
3869 #if SIZEOF_REGISTER == 8
3870 guint64 *p = (guint64 *)mem;
3871 *p = value;
3872 #else
3873 guint32 *p = (guint32 *)mem;
3874 *p = value;
3875 break;
3876 #endif
3877 break;
3879 default:
3880 g_assert_not_reached ();
3882 return;
3885 MonoObjectHandle
3886 ves_icall_System_Enum_ToObject (MonoReflectionTypeHandle enumType, guint64 value, MonoError *error)
3888 MonoDomain *domain;
3889 MonoClass *enumc;
3890 MonoObjectHandle resultHandle;
3891 MonoType *etype;
3893 domain = MONO_HANDLE_DOMAIN (enumType);
3894 enumc = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (enumType, type));
3896 mono_class_init_checked (enumc, error);
3897 goto_if_nok (error, return_null);
3899 etype = mono_class_enum_basetype_internal (enumc);
3901 resultHandle = mono_object_new_handle (domain, enumc, error);
3902 goto_if_nok (error, return_null);
3904 write_enum_value (mono_handle_unbox_unsafe (resultHandle), etype->type, value);
3906 return resultHandle;
3908 return_null:
3909 return MONO_HANDLE_NEW (MonoObject, NULL);
3912 MonoBoolean
3913 ves_icall_System_Enum_InternalHasFlag (MonoObjectHandle a, MonoObjectHandle b, MonoError *error)
3915 int size = mono_class_value_size (mono_handle_class (a), NULL);
3916 guint64 a_val = 0, b_val = 0;
3918 memcpy (&a_val, mono_handle_unbox_unsafe (a), size);
3919 memcpy (&b_val, mono_handle_unbox_unsafe (b), size);
3921 return (a_val & b_val) == b_val;
3924 MonoObjectHandle
3925 ves_icall_System_Enum_get_value (MonoObjectHandle ehandle, MonoError *error)
3927 MonoObjectHandle resultHandle;
3928 MonoClass *enumc;
3929 int size;
3931 goto_if (MONO_HANDLE_IS_NULL (ehandle), return_null);
3933 g_assert (m_class_is_enumtype (mono_handle_class (ehandle)));
3935 enumc = mono_class_from_mono_type_internal (mono_class_enum_basetype_internal (mono_handle_class (ehandle)));
3937 resultHandle = mono_object_new_handle (MONO_HANDLE_DOMAIN (ehandle), enumc, error);
3938 goto_if_nok (error, return_null);
3939 size = mono_class_value_size (enumc, NULL);
3941 memcpy (mono_handle_unbox_unsafe (resultHandle), mono_handle_unbox_unsafe (ehandle), size);
3943 return resultHandle;
3944 return_null:
3945 return MONO_HANDLE_NEW (MonoObject, NULL);
3948 MonoReflectionTypeHandle
3949 ves_icall_System_Enum_get_underlying_type (MonoReflectionTypeHandle type, MonoError *error)
3951 MonoType *etype;
3952 MonoClass *klass;
3954 klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
3955 mono_class_init_checked (klass, error);
3956 goto_if_nok (error, return_null);
3958 etype = mono_class_enum_basetype_internal (klass);
3959 if (!etype) {
3960 mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
3961 goto return_null;
3964 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (type), etype, error);
3966 return_null:
3967 return MONO_HANDLE_NEW (MonoReflectionType, NULL);
3971 ves_icall_System_Enum_InternalGetCorElementType (MonoObjectHandle this_handle, MonoError *error)
3973 MonoClass *klass = MONO_HANDLE_GETVAL (this_handle, vtable)->klass;
3975 return (int)m_class_get_byval_arg (m_class_get_element_class (klass))->type;
3979 ves_icall_System_Enum_compare_value_to (MonoObjectHandle enumHandle, MonoObjectHandle otherHandle, MonoError *error)
3981 if (MONO_HANDLE_IS_NULL (otherHandle))
3982 return 1;
3984 if (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass != MONO_HANDLE_GETVAL (otherHandle, vtable)->klass)
3985 return 2;
3987 gpointer tdata = mono_handle_unbox_unsafe (enumHandle);
3988 gpointer odata = mono_handle_unbox_unsafe (otherHandle);
3989 MonoType *basetype = mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass);
3990 g_assert (basetype);
3992 #define COMPARE_ENUM_VALUES(ENUM_TYPE) do { \
3993 ENUM_TYPE me = *((ENUM_TYPE*)tdata); \
3994 ENUM_TYPE other = *((ENUM_TYPE*)odata); \
3995 if (me == other) \
3996 return 0; \
3997 return me > other ? 1 : -1; \
3998 } while (0)
4000 switch (basetype->type) {
4001 case MONO_TYPE_BOOLEAN:
4002 case MONO_TYPE_U1:
4003 COMPARE_ENUM_VALUES (guint8);
4004 case MONO_TYPE_I1:
4005 COMPARE_ENUM_VALUES (gint8);
4006 case MONO_TYPE_CHAR:
4007 case MONO_TYPE_U2:
4008 COMPARE_ENUM_VALUES (guint16);
4009 case MONO_TYPE_I2:
4010 COMPARE_ENUM_VALUES (gint16);
4011 case MONO_TYPE_U4:
4012 COMPARE_ENUM_VALUES (guint32);
4013 case MONO_TYPE_I4:
4014 COMPARE_ENUM_VALUES (gint32);
4015 case MONO_TYPE_R4:
4016 COMPARE_ENUM_VALUES (gfloat);
4017 case MONO_TYPE_U8:
4018 COMPARE_ENUM_VALUES (guint64);
4019 case MONO_TYPE_I8:
4020 COMPARE_ENUM_VALUES (gint64);
4021 case MONO_TYPE_R8:
4022 COMPARE_ENUM_VALUES (gdouble);
4023 case MONO_TYPE_U:
4024 #if SIZEOF_REGISTER == 8
4025 COMPARE_ENUM_VALUES (guint64);
4026 #else
4027 COMPARE_ENUM_VALUES (guint32);
4028 #endif
4029 case MONO_TYPE_I:
4030 #if SIZEOF_REGISTER == 8
4031 COMPARE_ENUM_VALUES (gint64);
4032 #else
4033 COMPARE_ENUM_VALUES (gint32);
4034 #endif
4035 default:
4036 break;
4038 #undef COMPARE_ENUM_VALUES
4039 /* indicates that the enum was of an unsupported underlying type */
4040 return 3;
4044 ves_icall_System_Enum_get_hashcode (MonoObjectHandle enumHandle, MonoError *error)
4046 gpointer data = mono_handle_unbox_unsafe (enumHandle);
4047 MonoType *basetype = mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass);
4048 g_assert (basetype);
4050 switch (basetype->type) {
4051 case MONO_TYPE_I1: {
4052 gint8 value = *((gint8*)data);
4053 return ((int)value ^ (int)value << 8);
4055 case MONO_TYPE_U1:
4056 return *((guint8*)data);
4057 case MONO_TYPE_CHAR:
4058 case MONO_TYPE_U2:
4059 return *((guint16*)data);
4061 case MONO_TYPE_I2: {
4062 gint16 value = *((gint16*)data);
4063 return ((int)(guint16)value | (((int)value) << 16));
4065 case MONO_TYPE_U4:
4066 case MONO_TYPE_R4:
4067 return *((guint32*)data);
4068 case MONO_TYPE_I4:
4069 return *((gint32*)data);
4070 case MONO_TYPE_U8:
4071 case MONO_TYPE_I8:
4072 case MONO_TYPE_R8: {
4073 gint64 value = *((gint64*)data);
4074 return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
4076 case MONO_TYPE_I:
4077 case MONO_TYPE_U: {
4078 #if SIZEOF_REGISTER == 8
4079 gint64 value = *((gint64*)data);
4080 return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
4081 #else
4082 return *((guint32*)data);
4083 #endif
4085 default:
4086 g_error ("Implement type 0x%02x in get_hashcode", basetype->type);
4088 return 0;
4091 static void
4092 get_enum_field (MonoDomain *domain, MonoArrayHandle names, MonoArrayHandle values, int base_type, MonoClassField *field, guint* j, guint64 *previous_value, gboolean *sorted, MonoError *error)
4094 error_init (error);
4095 HANDLE_FUNCTION_ENTER();
4096 guint64 field_value;
4097 const char *p;
4098 MonoTypeEnum def_type;
4100 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
4101 goto leave;
4102 if (strcmp ("value__", mono_field_get_name (field)) == 0)
4103 goto leave;
4104 if (mono_field_is_deleted (field))
4105 goto leave;
4106 MonoStringHandle name;
4107 name = mono_string_new_handle (domain, mono_field_get_name (field), error);
4108 goto_if_nok (error, leave);
4109 MONO_HANDLE_ARRAY_SETREF (names, *j, name);
4111 p = mono_class_get_field_default_value (field, &def_type);
4112 /* len = */ mono_metadata_decode_blob_size (p, &p);
4114 field_value = read_enum_value (p, base_type);
4115 MONO_HANDLE_ARRAY_SETVAL (values, guint64, *j, field_value);
4117 if (*previous_value > field_value)
4118 *sorted = FALSE;
4120 *previous_value = field_value;
4121 (*j)++;
4122 leave:
4123 HANDLE_FUNCTION_RETURN();
4126 MonoBoolean
4127 ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionTypeHandle type, MonoArrayHandleOut values, MonoArrayHandleOut names, MonoError *error)
4129 MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
4130 MonoClass *enumc = mono_class_from_mono_type_internal (MONO_HANDLE_RAW(type)->type);
4131 guint j = 0, nvalues;
4132 gpointer iter;
4133 MonoClassField *field;
4134 int base_type;
4135 guint64 previous_value = 0;
4136 gboolean sorted = TRUE;
4138 error_init (error);
4139 mono_class_init_checked (enumc, error);
4140 return_val_if_nok (error, FALSE);
4142 if (!m_class_is_enumtype (enumc)) {
4143 mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
4144 return TRUE;
4147 base_type = mono_class_enum_basetype_internal (enumc)->type;
4149 nvalues = mono_class_num_fields (enumc) > 0 ? mono_class_num_fields (enumc) - 1 : 0;
4150 MONO_HANDLE_ASSIGN(names, mono_array_new_handle (domain, mono_defaults.string_class, nvalues, error));
4151 return_val_if_nok (error, FALSE);
4152 MONO_HANDLE_ASSIGN(values, mono_array_new_handle (domain, mono_defaults.uint64_class, nvalues, error));
4153 return_val_if_nok (error, FALSE);
4155 iter = NULL;
4156 while ((field = mono_class_get_fields_internal (enumc, &iter))) {
4157 get_enum_field(domain, names, values, base_type, field, &j, &previous_value, &sorted, error);
4158 if (!is_ok (error))
4159 break;
4161 return_val_if_nok (error, FALSE);
4163 return sorted || base_type == MONO_TYPE_R4 || base_type == MONO_TYPE_R8;
4166 enum {
4167 BFLAGS_IgnoreCase = 1,
4168 BFLAGS_DeclaredOnly = 2,
4169 BFLAGS_Instance = 4,
4170 BFLAGS_Static = 8,
4171 BFLAGS_Public = 0x10,
4172 BFLAGS_NonPublic = 0x20,
4173 BFLAGS_FlattenHierarchy = 0x40,
4174 BFLAGS_InvokeMethod = 0x100,
4175 BFLAGS_CreateInstance = 0x200,
4176 BFLAGS_GetField = 0x400,
4177 BFLAGS_SetField = 0x800,
4178 BFLAGS_GetProperty = 0x1000,
4179 BFLAGS_SetProperty = 0x2000,
4180 BFLAGS_ExactBinding = 0x10000,
4181 BFLAGS_SuppressChangeType = 0x20000,
4182 BFLAGS_OptionalParamBinding = 0x40000
4185 enum {
4186 MLISTTYPE_All = 0,
4187 MLISTTYPE_CaseSensitive = 1,
4188 MLISTTYPE_CaseInsensitive = 2,
4189 MLISTTYPE_HandleToInfo = 3
4192 GPtrArray*
4193 ves_icall_RuntimeType_GetFields_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 bflags, guint32 mlisttype, MonoError *error)
4195 error_init (error);
4196 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4198 if (type->byref) {
4199 return g_ptr_array_new ();
4202 int (*compare_func) (const char *s1, const char *s2) = NULL;
4203 compare_func = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
4205 MonoClass *startklass, *klass;
4206 klass = startklass = mono_class_from_mono_type_internal (type);
4208 GPtrArray *ptr_array = g_ptr_array_sized_new (16);
4210 handle_parent:
4211 if (mono_class_has_failure (klass)) {
4212 mono_error_set_for_class_failure (error, klass);
4213 goto fail;
4216 MonoClassField *field;
4217 gpointer iter;
4218 iter = NULL;
4219 while ((field = mono_class_get_fields_lazy (klass, &iter))) {
4220 guint32 flags = mono_field_get_flags (field);
4221 int match = 0;
4222 if (mono_field_is_deleted_with_flags (field, flags))
4223 continue;
4224 if ((flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
4225 if (bflags & BFLAGS_Public)
4226 match++;
4227 } else if ((klass == startklass) || (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) != FIELD_ATTRIBUTE_PRIVATE) {
4228 if (bflags & BFLAGS_NonPublic) {
4229 match++;
4232 if (!match)
4233 continue;
4234 match = 0;
4235 if (flags & FIELD_ATTRIBUTE_STATIC) {
4236 if (bflags & BFLAGS_Static)
4237 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4238 match++;
4239 } else {
4240 if (bflags & BFLAGS_Instance)
4241 match++;
4244 if (!match)
4245 continue;
4247 if (((mlisttype != MLISTTYPE_All) && (utf8_name != NULL)) && compare_func (mono_field_get_name (field), utf8_name))
4248 continue;
4250 g_ptr_array_add (ptr_array, field);
4252 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
4253 goto handle_parent;
4255 return ptr_array;
4257 fail:
4258 g_ptr_array_free (ptr_array, TRUE);
4259 return NULL;
4262 static gboolean
4263 method_nonpublic (MonoMethod* method, gboolean start_klass)
4265 switch (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) {
4266 case METHOD_ATTRIBUTE_ASSEM:
4267 return TRUE;
4268 case METHOD_ATTRIBUTE_PRIVATE:
4269 return start_klass;
4270 case METHOD_ATTRIBUTE_PUBLIC:
4271 return FALSE;
4272 default:
4273 return TRUE;
4277 GPtrArray*
4278 mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bflags, guint32 mlisttype, gboolean allow_ctors, MonoError *error)
4280 GPtrArray *array;
4281 MonoClass *startklass;
4282 MonoMethod *method;
4283 gpointer iter;
4284 int match, nslots;
4285 /*FIXME, use MonoBitSet*/
4286 guint32 method_slots_default [8];
4287 guint32 *method_slots = NULL;
4288 int (*compare_func) (const char *s1, const char *s2) = NULL;
4290 array = g_ptr_array_new ();
4291 startklass = klass;
4292 error_init (error);
4294 compare_func = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
4296 /* An optimization for calls made from Delegate:CreateDelegate () */
4297 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))) {
4298 method = mono_get_delegate_invoke_internal (klass);
4299 g_assert (method);
4301 g_ptr_array_add (array, method);
4302 return array;
4305 mono_class_setup_methods (klass);
4306 mono_class_setup_vtable (klass);
4307 if (mono_class_has_failure (klass))
4308 goto loader_error;
4310 if (is_generic_parameter (m_class_get_byval_arg (klass)))
4311 nslots = mono_class_get_vtable_size (m_class_get_parent (klass));
4312 else
4313 nslots = MONO_CLASS_IS_INTERFACE_INTERNAL (klass) ? mono_class_num_methods (klass) : mono_class_get_vtable_size (klass);
4314 if (nslots >= sizeof (method_slots_default) * 8) {
4315 method_slots = g_new0 (guint32, nslots / 32 + 1);
4316 } else {
4317 method_slots = method_slots_default;
4318 memset (method_slots, 0, sizeof (method_slots_default));
4320 handle_parent:
4321 mono_class_setup_methods (klass);
4322 mono_class_setup_vtable (klass);
4323 if (mono_class_has_failure (klass))
4324 goto loader_error;
4326 iter = NULL;
4327 while ((method = mono_class_get_methods (klass, &iter))) {
4328 match = 0;
4329 if (method->slot != -1) {
4330 g_assert (method->slot < nslots);
4331 if (method_slots [method->slot >> 5] & (1 << (method->slot & 0x1f)))
4332 continue;
4333 if (!(method->flags & METHOD_ATTRIBUTE_NEW_SLOT))
4334 method_slots [method->slot >> 5] |= 1 << (method->slot & 0x1f);
4337 if (!allow_ctors && method->name [0] == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0))
4338 continue;
4339 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
4340 if (bflags & BFLAGS_Public)
4341 match++;
4342 } else if ((bflags & BFLAGS_NonPublic) && method_nonpublic (method, (klass == startklass))) {
4343 match++;
4345 if (!match)
4346 continue;
4347 match = 0;
4348 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4349 if (bflags & BFLAGS_Static)
4350 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4351 match++;
4352 } else {
4353 if (bflags & BFLAGS_Instance)
4354 match++;
4357 if (!match)
4358 continue;
4360 if ((mlisttype != MLISTTYPE_All) && (name != NULL)) {
4361 if (compare_func (name, method->name))
4362 continue;
4365 match = 0;
4366 g_ptr_array_add (array, method);
4368 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
4369 goto handle_parent;
4370 if (method_slots != method_slots_default)
4371 g_free (method_slots);
4373 return array;
4375 loader_error:
4376 if (method_slots != method_slots_default)
4377 g_free (method_slots);
4378 g_ptr_array_free (array, TRUE);
4380 g_assert (mono_class_has_failure (klass));
4381 mono_error_set_for_class_failure (error, klass);
4382 return NULL;
4385 GPtrArray*
4386 ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type, const char *mname, guint32 bflags, guint32 mlisttype, MonoError *error)
4388 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4390 MonoClass *klass = mono_class_from_mono_type_internal (type);
4391 if (type->byref) {
4392 return g_ptr_array_new ();
4395 return mono_class_get_methods_by_name (klass, mname, bflags, mlisttype, FALSE, error);
4398 GPtrArray*
4399 ves_icall_RuntimeType_GetConstructors_native (MonoReflectionTypeHandle ref_type, guint32 bflags, MonoError *error)
4401 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4402 if (type->byref) {
4403 return g_ptr_array_new ();
4406 MonoClass *startklass, *klass;
4407 klass = startklass = mono_class_from_mono_type_internal (type);
4409 mono_class_setup_methods (klass);
4410 if (mono_class_has_failure (klass)) {
4411 mono_error_set_for_class_failure (error, klass);
4412 return NULL;
4416 GPtrArray *res_array = g_ptr_array_sized_new (4); /* FIXME, guestimating */
4418 MonoMethod *method;
4419 gpointer iter = NULL;
4420 while ((method = mono_class_get_methods (klass, &iter))) {
4421 int match = 0;
4422 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
4423 continue;
4424 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
4425 if (bflags & BFLAGS_Public)
4426 match++;
4427 } else {
4428 if (bflags & BFLAGS_NonPublic)
4429 match++;
4431 if (!match)
4432 continue;
4433 match = 0;
4434 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4435 if (bflags & BFLAGS_Static)
4436 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4437 match++;
4438 } else {
4439 if (bflags & BFLAGS_Instance)
4440 match++;
4443 if (!match)
4444 continue;
4445 g_ptr_array_add (res_array, method);
4448 return res_array;
4451 static guint
4452 property_hash (gconstpointer data)
4454 MonoProperty *prop = (MonoProperty*)data;
4456 return g_str_hash (prop->name);
4459 static gboolean
4460 property_accessor_override (MonoMethod *method1, MonoMethod *method2)
4462 if (method1->slot != -1 && method1->slot == method2->slot)
4463 return TRUE;
4465 if (mono_class_get_generic_type_definition (method1->klass) == mono_class_get_generic_type_definition (method2->klass)) {
4466 if (method1->is_inflated)
4467 method1 = ((MonoMethodInflated*) method1)->declaring;
4468 if (method2->is_inflated)
4469 method2 = ((MonoMethodInflated*) method2)->declaring;
4472 return mono_metadata_signature_equal (mono_method_signature_internal (method1), mono_method_signature_internal (method2));
4475 static gboolean
4476 property_equal (MonoProperty *prop1, MonoProperty *prop2)
4478 // Properties are hide-by-name-and-signature
4479 if (!g_str_equal (prop1->name, prop2->name))
4480 return FALSE;
4482 /* If we see a property in a generic method, we want to
4483 compare the generic signatures, not the inflated signatures
4484 because we might conflate two properties that were
4485 distinct:
4487 class Foo<T,U> {
4488 public T this[T t] { getter { return t; } } // method 1
4489 public U this[U u] { getter { return u; } } // method 2
4492 If we see int Foo<int,int>::Item[int] we need to know if
4493 the indexer came from method 1 or from method 2, and we
4494 shouldn't conflate them. (Bugzilla 36283)
4496 if (prop1->get && prop2->get && !property_accessor_override (prop1->get, prop2->get))
4497 return FALSE;
4499 if (prop1->set && prop2->set && !property_accessor_override (prop1->set, prop2->set))
4500 return FALSE;
4502 return TRUE;
4505 static gboolean
4506 property_accessor_nonpublic (MonoMethod* accessor, gboolean start_klass)
4508 if (!accessor)
4509 return FALSE;
4511 return method_nonpublic (accessor, start_klass);
4514 GPtrArray*
4515 ves_icall_RuntimeType_GetPropertiesByName_native (MonoReflectionTypeHandle ref_type, gchar *propname, guint32 bflags, guint32 mlisttype, MonoError *error)
4517 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4519 if (type->byref) {
4520 return g_ptr_array_new ();
4524 MonoClass *startklass, *klass;
4525 klass = startklass = mono_class_from_mono_type_internal (type);
4527 int (*compare_func) (const char *s1, const char *s2) = (mlisttype == MLISTTYPE_CaseInsensitive) ? mono_utf8_strcasecmp : strcmp;
4529 GPtrArray *res_array = g_ptr_array_sized_new (8); /*This the average for ASP.NET types*/
4531 GHashTable *properties = g_hash_table_new (property_hash, (GEqualFunc)property_equal);
4533 handle_parent:
4534 mono_class_setup_methods (klass);
4535 mono_class_setup_vtable (klass);
4536 if (mono_class_has_failure (klass)) {
4537 mono_error_set_for_class_failure (error, klass);
4538 goto loader_error;
4541 MonoProperty *prop;
4542 gpointer iter;
4543 iter = NULL;
4544 while ((prop = mono_class_get_properties (klass, &iter))) {
4545 int match = 0;
4546 MonoMethod *method = prop->get;
4547 if (!method)
4548 method = prop->set;
4549 guint32 flags = 0;
4550 if (method)
4551 flags = method->flags;
4553 if ((prop->get && ((prop->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)) ||
4554 (prop->set && ((prop->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC))) {
4555 if (bflags & BFLAGS_Public)
4556 match++;
4557 } else if (bflags & BFLAGS_NonPublic) {
4558 if (property_accessor_nonpublic(prop->get, startklass == klass) ||
4559 property_accessor_nonpublic(prop->set, startklass == klass)) {
4560 match++;
4563 if (!match)
4564 continue;
4566 match = 0;
4567 if (flags & METHOD_ATTRIBUTE_STATIC) {
4568 if (bflags & BFLAGS_Static)
4569 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4570 match++;
4571 } else {
4572 if (bflags & BFLAGS_Instance)
4573 match++;
4576 if (!match)
4577 continue;
4578 match = 0;
4580 if ((mlisttype != MLISTTYPE_All) && (propname != NULL) && compare_func (propname, prop->name))
4581 continue;
4583 if (g_hash_table_lookup (properties, prop))
4584 continue;
4586 g_ptr_array_add (res_array, prop);
4588 g_hash_table_insert (properties, prop, prop);
4590 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass))) {
4591 goto handle_parent;
4594 g_hash_table_destroy (properties);
4596 return res_array;
4599 loader_error:
4600 if (properties)
4601 g_hash_table_destroy (properties);
4602 g_ptr_array_free (res_array, TRUE);
4604 return NULL;
4607 static guint
4608 event_hash (gconstpointer data)
4610 MonoEvent *event = (MonoEvent*)data;
4612 return g_str_hash (event->name);
4615 static gboolean
4616 event_equal (MonoEvent *event1, MonoEvent *event2)
4618 // Events are hide-by-name
4619 return g_str_equal (event1->name, event2->name);
4622 GPtrArray*
4623 ves_icall_RuntimeType_GetEvents_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 mlisttype, MonoError *error)
4625 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4627 if (type->byref) {
4628 return g_ptr_array_new ();
4631 int (*compare_func) (const char *s1, const char *s2) = (mlisttype == MLISTTYPE_CaseInsensitive) ? mono_utf8_strcasecmp : strcmp;
4633 GPtrArray *res_array = g_ptr_array_sized_new (4);
4635 MonoClass *startklass, *klass;
4636 klass = startklass = mono_class_from_mono_type_internal (type);
4638 GHashTable *events = g_hash_table_new (event_hash, (GEqualFunc)event_equal);
4639 handle_parent:
4640 mono_class_setup_methods (klass);
4641 mono_class_setup_vtable (klass);
4642 if (mono_class_has_failure (klass)) {
4643 mono_error_set_for_class_failure (error, klass);
4644 goto failure;
4647 MonoEvent *event;
4648 gpointer iter;
4649 iter = NULL;
4650 while ((event = mono_class_get_events (klass, &iter))) {
4652 // Remove inherited privates and inherited
4653 // without add/remove/raise methods
4654 if (klass != startklass)
4656 MonoMethod *method = event->add;
4657 if (!method)
4658 method = event->remove;
4659 if (!method)
4660 method = event->raise;
4661 if (!method)
4662 continue;
4663 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PRIVATE)
4664 continue;
4667 if ((mlisttype != MLISTTYPE_All) && (utf8_name != NULL) && compare_func (event->name, utf8_name))
4668 continue;
4670 if (g_hash_table_lookup (events, event))
4671 continue;
4673 g_ptr_array_add (res_array, event);
4675 g_hash_table_insert (events, event, event);
4677 if ((klass = m_class_get_parent (klass)))
4678 goto handle_parent;
4680 g_hash_table_destroy (events);
4682 return res_array;
4684 failure:
4685 if (events != NULL)
4686 g_hash_table_destroy (events);
4688 g_ptr_array_free (res_array, TRUE);
4690 return NULL;
4693 GPtrArray *
4694 ves_icall_RuntimeType_GetNestedTypes_native (MonoReflectionTypeHandle ref_type, char *str, guint32 bflags, guint32 mlisttype, MonoError *error)
4696 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4698 if (type->byref) {
4699 return g_ptr_array_new ();
4702 int (*compare_func) (const char *s1, const char *s2) = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
4704 MonoClass *klass = mono_class_from_mono_type_internal (type);
4707 * If a nested type is generic, return its generic type definition.
4708 * Note that this means that the return value is essentially the set
4709 * of nested types of the generic type definition of @klass.
4711 * A note in MSDN claims that a generic type definition can have
4712 * nested types that aren't generic. In any case, the container of that
4713 * nested type would be the generic type definition.
4715 if (mono_class_is_ginst (klass))
4716 klass = mono_class_get_generic_class (klass)->container_class;
4718 GPtrArray *res_array = g_ptr_array_new ();
4720 MonoClass *nested;
4721 gpointer iter = NULL;
4722 while ((nested = mono_class_get_nested_types (klass, &iter))) {
4723 int match = 0;
4724 if ((mono_class_get_flags (nested) & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
4725 if (bflags & BFLAGS_Public)
4726 match++;
4727 } else {
4728 if (bflags & BFLAGS_NonPublic)
4729 match++;
4731 if (!match)
4732 continue;
4734 if ((mlisttype != MLISTTYPE_All) && (str != NULL) && compare_func (m_class_get_name (nested), str))
4735 continue;
4737 g_ptr_array_add (res_array, m_class_get_byval_arg (nested));
4740 return res_array;
4743 static MonoType*
4744 get_type_from_module_builder_module (MonoAssemblyLoadContext *alc, MonoArrayHandle modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
4746 HANDLE_FUNCTION_ENTER ();
4747 MonoType *type = NULL;
4748 MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
4749 MONO_HANDLE_ARRAY_GETREF (mb, modules, i);
4750 MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image);
4751 type = mono_reflection_get_type_checked (alc, &dynamic_image->image, &dynamic_image->image, info, ignoreCase, FALSE, type_resolve, error);
4752 HANDLE_FUNCTION_RETURN_VAL (type);
4755 static MonoType*
4756 get_type_from_module_builder_loaded_modules (MonoAssemblyLoadContext *alc, MonoArrayHandle loaded_modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
4758 HANDLE_FUNCTION_ENTER ();
4759 MonoType *type = NULL;
4760 MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
4761 MONO_HANDLE_ARRAY_GETREF (mod, loaded_modules, i);
4762 MonoImage *image = MONO_HANDLE_GETVAL (mod, image);
4763 type = mono_reflection_get_type_checked (alc, image, image, info, ignoreCase, FALSE, type_resolve, error);
4764 HANDLE_FUNCTION_RETURN_VAL (type);
4767 MonoReflectionTypeHandle
4768 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHandle assembly_h, MonoReflectionModuleHandle module, MonoStringHandle name, MonoBoolean throwOnError, MonoBoolean ignoreCase, MonoError *error)
4770 ERROR_DECL (parse_error);
4772 MonoTypeNameParse info;
4773 gboolean type_resolve;
4774 MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
4776 /* On MS.NET, this does not fire a TypeResolve event */
4777 type_resolve = TRUE;
4778 char *str = mono_string_handle_to_utf8 (name, error);
4779 goto_if_nok (error, fail);
4781 /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
4782 if (!mono_reflection_parse_type_checked (str, &info, parse_error)) {
4783 g_free (str);
4784 mono_reflection_free_type_info (&info);
4785 mono_error_cleanup (parse_error);
4786 if (throwOnError) {
4787 mono_error_set_argument (error, "typeName", "failed to parse the type");
4788 goto fail;
4790 /*g_print ("failed parse\n");*/
4791 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4794 if (info.assembly.name) {
4795 g_free (str);
4796 mono_reflection_free_type_info (&info);
4797 if (throwOnError) {
4798 mono_error_set_argument (error, NULL, "Type names passed to Assembly.GetType() must not specify an assembly.");
4799 goto fail;
4801 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4804 MonoType *type;
4805 type = NULL;
4806 if (!MONO_HANDLE_IS_NULL (module)) {
4807 MonoImage *image = MONO_HANDLE_GETVAL (module, image);
4808 if (image) {
4809 type = mono_reflection_get_type_checked (alc, image, image, &info, ignoreCase, FALSE, &type_resolve, error);
4810 if (!is_ok (error)) {
4811 g_free (str);
4812 mono_reflection_free_type_info (&info);
4813 goto fail;
4817 else {
4818 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
4819 if (assembly_is_dynamic (assembly)) {
4820 /* Enumerate all modules */
4821 MonoReflectionAssemblyBuilderHandle abuilder = MONO_HANDLE_NEW (MonoReflectionAssemblyBuilder, NULL);
4822 MONO_HANDLE_ASSIGN (abuilder, assembly_h);
4823 int i;
4825 MonoArrayHandle modules = MONO_HANDLE_NEW (MonoArray, NULL);
4826 MONO_HANDLE_GET (modules, abuilder, modules);
4827 if (!MONO_HANDLE_IS_NULL (modules)) {
4828 int n = mono_array_handle_length (modules);
4829 for (i = 0; i < n; ++i) {
4830 type = get_type_from_module_builder_module (alc, modules, i, &info, ignoreCase, &type_resolve, error);
4831 if (!is_ok (error)) {
4832 g_free (str);
4833 mono_reflection_free_type_info (&info);
4834 goto fail;
4836 if (type)
4837 break;
4841 MonoArrayHandle loaded_modules = MONO_HANDLE_NEW (MonoArray, NULL);
4842 MONO_HANDLE_GET (loaded_modules, abuilder, loaded_modules);
4843 if (!type && !MONO_HANDLE_IS_NULL (loaded_modules)) {
4844 int n = mono_array_handle_length (loaded_modules);
4845 for (i = 0; i < n; ++i) {
4846 type = get_type_from_module_builder_loaded_modules (alc, loaded_modules, i, &info, ignoreCase, &type_resolve, error);
4848 if (!is_ok (error)) {
4849 g_free (str);
4850 mono_reflection_free_type_info (&info);
4851 goto fail;
4853 if (type)
4854 break;
4858 else {
4859 type = mono_reflection_get_type_checked (alc, assembly->image, assembly->image, &info, ignoreCase, FALSE, &type_resolve, error);
4860 if (!is_ok (error)) {
4861 g_free (str);
4862 mono_reflection_free_type_info (&info);
4863 goto fail;
4867 g_free (str);
4868 mono_reflection_free_type_info (&info);
4870 if (!type) {
4871 if (throwOnError) {
4872 ERROR_DECL (inner_error);
4873 char *type_name = mono_string_handle_to_utf8 (name, inner_error);
4874 mono_error_assert_ok (inner_error);
4875 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
4876 char *assmname = mono_stringify_assembly_name (&assembly->aname);
4877 mono_error_set_type_load_name (error, type_name, assmname, "%s", "");
4878 goto fail;
4881 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4884 if (type->type == MONO_TYPE_CLASS) {
4885 MonoClass *klass = mono_type_get_class_internal (type);
4887 /* need to report exceptions ? */
4888 if (throwOnError && mono_class_has_failure (klass)) {
4889 /* report SecurityException (or others) that occured when loading the assembly */
4890 mono_error_set_for_class_failure (error, klass);
4891 goto fail;
4895 /* g_print ("got it\n"); */
4896 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (assembly_h), type, error);
4897 fail:
4898 g_assert (!is_ok (error));
4899 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
4902 static gboolean
4903 replace_shadow_path (MonoDomain *domain, gchar *dirname, gchar **filename)
4905 gchar *content;
4906 gchar *shadow_ini_file;
4907 gsize len;
4909 /* Check for shadow-copied assembly */
4910 if (mono_is_shadow_copy_enabled (domain, dirname)) {
4911 shadow_ini_file = g_build_filename (dirname, "__AssemblyInfo__.ini", (const char*)NULL);
4912 content = NULL;
4913 if (!g_file_get_contents (shadow_ini_file, &content, &len, NULL) ||
4914 !g_file_test (content, G_FILE_TEST_IS_REGULAR)) {
4915 g_free (content);
4916 content = NULL;
4918 g_free (shadow_ini_file);
4919 if (content != NULL) {
4920 g_free (*filename);
4921 *filename = content;
4922 return TRUE;
4925 return FALSE;
4928 MonoStringHandle
4929 ves_icall_System_Reflection_RuntimeAssembly_get_code_base (MonoReflectionAssemblyHandle assembly, MonoBoolean escaped, MonoError *error)
4931 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
4932 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
4933 gchar *absolute;
4934 gchar *dirname;
4936 if (g_path_is_absolute (mass->image->name)) {
4937 absolute = g_strdup (mass->image->name);
4938 dirname = g_path_get_dirname (absolute);
4939 } else {
4940 absolute = g_build_filename (mass->basedir, mass->image->name, (const char*)NULL);
4941 dirname = g_strdup (mass->basedir);
4944 replace_shadow_path (domain, dirname, &absolute);
4945 g_free (dirname);
4947 mono_icall_make_platform_path (absolute);
4949 gchar *uri;
4950 if (escaped) {
4951 uri = g_filename_to_uri (absolute, NULL, NULL);
4952 } else {
4953 const gchar *prepend = mono_icall_get_file_path_prefix (absolute);
4954 uri = g_strconcat (prepend, absolute, (const char*)NULL);
4957 g_free (absolute);
4959 MonoStringHandle res;
4960 if (uri) {
4961 res = mono_string_new_handle (domain, uri, error);
4962 g_free (uri);
4963 } else {
4964 res = MONO_HANDLE_NEW (MonoString, NULL);
4966 return res;
4969 MonoBoolean
4970 ves_icall_System_Reflection_RuntimeAssembly_get_global_assembly_cache (MonoReflectionAssemblyHandle assembly, MonoError *error)
4972 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly,assembly);
4974 return mass->in_gac;
4977 MonoReflectionAssemblyHandle
4978 ves_icall_System_Reflection_Assembly_load_with_partial_name (MonoStringHandle mname, MonoObjectHandle evidence, MonoError *error)
4980 gchar *name;
4981 MonoImageOpenStatus status;
4982 MonoReflectionAssemblyHandle result = MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
4984 name = mono_string_handle_to_utf8 (mname, error);
4985 goto_if_nok (error, leave);
4986 MonoAssembly *res;
4987 res = mono_assembly_load_with_partial_name_internal (name, mono_domain_default_alc (mono_domain_get ()), &status);
4989 g_free (name);
4991 if (res == NULL)
4992 goto leave;
4993 result = mono_assembly_get_object_handle (mono_domain_get (), res, error);
4994 leave:
4995 return result;
4998 MonoStringHandle
4999 ves_icall_System_Reflection_RuntimeAssembly_get_location (MonoReflectionAssemblyHandle refassembly, MonoError *error)
5001 MonoDomain *domain = MONO_HANDLE_DOMAIN (refassembly);
5002 MonoAssembly *assembly = MONO_HANDLE_GETVAL (refassembly, assembly);
5003 const char *image_name = m_image_get_filename (assembly->image);
5004 return mono_string_new_handle (domain, image_name != NULL ? image_name : "", error);
5007 MonoBoolean
5008 ves_icall_System_Reflection_RuntimeAssembly_get_ReflectionOnly (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5010 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5011 return mono_asmctx_get_kind (&assembly->context) == MONO_ASMCTX_REFONLY;
5014 MonoStringHandle
5015 ves_icall_System_Reflection_RuntimeAssembly_InternalImageRuntimeVersion (MonoReflectionAssemblyHandle refassembly, MonoError *error)
5017 MonoDomain *domain = MONO_HANDLE_DOMAIN (refassembly);
5018 MonoAssembly *assembly = MONO_HANDLE_GETVAL (refassembly, assembly);
5020 return mono_string_new_handle (domain, assembly->image->version, error);
5023 MonoReflectionMethodHandle
5024 ves_icall_System_Reflection_RuntimeAssembly_get_EntryPoint (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5026 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5027 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5028 MonoMethod *method;
5030 MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
5031 guint32 token = mono_image_get_entry_point (assembly->image);
5033 if (!token)
5034 goto leave;
5035 method = mono_get_method_checked (assembly->image, token, NULL, NULL, error);
5036 goto_if_nok (error, leave);
5038 MONO_HANDLE_ASSIGN (res, mono_method_get_object_handle (domain, method, NULL, error));
5039 leave:
5040 return res;
5043 MonoReflectionModuleHandle
5044 ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssemblyHandle assembly, MonoError *error)
5046 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5047 MonoAssembly *a = MONO_HANDLE_GETVAL (assembly, assembly);
5048 return mono_module_get_object_handle (domain, a->image, error);
5051 static gboolean
5052 add_manifest_resource_name_to_array (MonoDomain *domain, MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle dest, MonoError *error)
5054 HANDLE_FUNCTION_ENTER ();
5055 const char *val = mono_metadata_string_heap (image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
5056 MonoStringHandle str = mono_string_new_handle (domain, val, error);
5057 goto_if_nok (error, leave);
5058 MONO_HANDLE_ARRAY_SETREF (dest, i, str);
5059 leave:
5060 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5063 MonoArrayHandle
5064 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceNames (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5066 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5067 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5068 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
5069 MonoArrayHandle result = mono_array_new_handle (domain, mono_defaults.string_class, table->rows, error);
5070 goto_if_nok (error, fail);
5071 int i;
5073 for (i = 0; i < table->rows; ++i) {
5074 if (!add_manifest_resource_name_to_array (domain, assembly->image, table, i, result, error))
5075 goto fail;
5077 return result;
5078 fail:
5079 return NULL_HANDLE_ARRAY;
5082 MonoBoolean
5083 ves_icall_System_Reflection_RuntimeAssembly_GetAotIdInternal (MonoArrayHandle guid_h, MonoError *error)
5085 g_assert (mono_array_handle_length (guid_h) == 16);
5087 guint8 *aotid = mono_runtime_get_aotid_arr ();
5088 if (!aotid) {
5089 return FALSE;
5090 } else {
5091 MONO_ENTER_NO_SAFEPOINTS;
5092 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
5093 memcpy (data, aotid, 16);
5094 MONO_EXIT_NO_SAFEPOINTS;
5095 return TRUE;
5099 static MonoAssemblyName*
5100 create_referenced_assembly_name (MonoDomain *domain, MonoImage *image, int i, MonoError *error)
5102 MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
5104 mono_assembly_get_assemblyref_checked (image, i, aname, error);
5105 return_val_if_nok (error, NULL);
5106 aname->hash_alg = ASSEMBLY_HASH_SHA1 /* SHA1 (default) */;
5107 /* name and culture are pointers into the image tables, but we need
5108 * real malloc'd strings (so that we can g_free() them later from
5109 * Mono.RuntimeMarshal.FreeAssemblyName) */
5110 aname->name = g_strdup (aname->name);
5111 aname->culture = g_strdup (aname->culture);
5112 /* Don't need the hash value in managed */
5113 aname->hash_value = NULL;
5114 aname->hash_len = 0;
5115 g_assert (aname->public_key == NULL);
5117 /* note: this function doesn't return the codebase on purpose (i.e. it can
5118 be used under partial trust as path information isn't present). */
5119 return aname;
5122 GPtrArray*
5123 ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflectionAssemblyHandle assembly, MonoError *error)
5125 error_init (error);
5126 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5127 MonoAssembly *ass = MONO_HANDLE_GETVAL(assembly, assembly);
5128 MonoImage *image = ass->image;
5129 int count;
5131 if (image_is_dynamic (ass->image)) {
5132 MonoDynamicTable *t = &(((MonoDynamicImage*) image)->tables [MONO_TABLE_ASSEMBLYREF]);
5133 count = t->rows;
5135 else {
5136 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
5137 count = t->rows;
5140 GPtrArray *result = g_ptr_array_sized_new (count);
5142 for (int i = 0; i < count; i++) {
5143 MonoAssemblyName *aname = create_referenced_assembly_name (domain, image, i, error);
5144 if (!is_ok (error))
5145 break;
5146 g_ptr_array_add (result, aname);
5148 return result;
5151 /* move this in some file in mono/util/ */
5152 static char *
5153 g_concat_dir_and_file (const char *dir, const char *file)
5155 g_return_val_if_fail (dir != NULL, NULL);
5156 g_return_val_if_fail (file != NULL, NULL);
5159 * If the directory name doesn't have a / on the end, we need
5160 * to add one so we get a proper path to the file
5162 if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
5163 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, (const char*)NULL);
5164 else
5165 return g_strconcat (dir, file, (const char*)NULL);
5169 static void *
5170 get_manifest_resource_internal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
5172 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5173 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5174 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
5175 guint32 i;
5176 guint32 cols [MONO_MANIFEST_SIZE];
5177 guint32 impl, file_idx;
5178 const char *val;
5179 MonoImage *module;
5181 char *n = mono_string_handle_to_utf8 (name, error);
5182 return_val_if_nok (error, NULL);
5184 for (i = 0; i < table->rows; ++i) {
5185 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
5186 val = mono_metadata_string_heap (assembly->image, cols [MONO_MANIFEST_NAME]);
5187 if (strcmp (val, n) == 0)
5188 break;
5190 g_free (n);
5191 if (i == table->rows)
5192 return NULL;
5193 /* FIXME */
5194 impl = cols [MONO_MANIFEST_IMPLEMENTATION];
5195 if (impl) {
5197 * this code should only be called after obtaining the
5198 * ResourceInfo and handling the other cases.
5200 g_assert ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE);
5201 file_idx = impl >> MONO_IMPLEMENTATION_BITS;
5203 module = mono_image_load_file_for_image_checked (assembly->image, file_idx, error);
5204 if (!is_ok (error) || !module)
5205 return NULL;
5207 else
5208 module = assembly->image;
5211 MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
5212 return_val_if_nok (error, NULL);
5213 MONO_HANDLE_ASSIGN (ref_module, rm);
5215 return (void*)mono_image_get_resource (module, cols [MONO_MANIFEST_OFFSET], (guint32*)size);
5218 void *
5219 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
5221 gpointer ret = get_manifest_resource_internal (assembly_h, name, size, ref_module, error);
5224 return ret;
5227 static gboolean
5228 get_manifest_resource_info_internal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info, MonoError *error)
5230 HANDLE_FUNCTION_ENTER ();
5231 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5232 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5233 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
5234 int i;
5235 guint32 cols [MONO_MANIFEST_SIZE];
5236 guint32 file_cols [MONO_FILE_SIZE];
5237 const char *val;
5238 char *n;
5240 gboolean result = FALSE;
5242 n = mono_string_handle_to_utf8 (name, error);
5243 goto_if_nok (error, leave);
5245 for (i = 0; i < table->rows; ++i) {
5246 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
5247 val = mono_metadata_string_heap (assembly->image, cols [MONO_MANIFEST_NAME]);
5248 if (strcmp (val, n) == 0)
5249 break;
5251 g_free (n);
5252 if (i == table->rows)
5253 goto leave;
5255 if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
5256 MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST);
5258 else {
5259 switch (cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) {
5260 case MONO_IMPLEMENTATION_FILE:
5261 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> MONO_IMPLEMENTATION_BITS;
5262 table = &assembly->image->tables [MONO_TABLE_FILE];
5263 mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
5264 val = mono_metadata_string_heap (assembly->image, file_cols [MONO_FILE_NAME]);
5265 MONO_HANDLE_SET (info, filename, mono_string_new_handle (domain, val, error));
5266 if (file_cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA)
5267 MONO_HANDLE_SETVAL (info, location, guint32, 0);
5268 else
5269 MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED);
5270 break;
5272 case MONO_IMPLEMENTATION_ASSEMBLYREF:
5273 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> MONO_IMPLEMENTATION_BITS;
5274 mono_assembly_load_reference (assembly->image, i - 1);
5275 if (assembly->image->references [i - 1] == REFERENCE_MISSING) {
5276 mono_error_set_file_not_found (error, NULL, "Assembly %d referenced from assembly %s not found ", i - 1, assembly->image->name);
5277 goto leave;
5279 MonoReflectionAssemblyHandle assm_obj;
5280 assm_obj = mono_assembly_get_object_handle (mono_domain_get (), assembly->image->references [i - 1], error);
5281 goto_if_nok (error, leave);
5282 MONO_HANDLE_SET (info, assembly, assm_obj);
5284 /* Obtain info recursively */
5285 get_manifest_resource_info_internal (assm_obj, name, info, error);
5286 goto_if_nok (error, leave);
5287 guint32 location;
5288 location = MONO_HANDLE_GETVAL (info, location);
5289 location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
5290 MONO_HANDLE_SETVAL (info, location, guint32, location);
5291 break;
5293 case MONO_IMPLEMENTATION_EXP_TYPE:
5294 g_assert_not_reached ();
5295 break;
5299 result = TRUE;
5300 leave:
5301 HANDLE_FUNCTION_RETURN_VAL (result);
5304 MonoBoolean
5305 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInfoInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info_h, MonoError *error)
5307 return get_manifest_resource_info_internal (assembly_h, name, info_h, error);
5310 static gboolean
5311 add_filename_to_files_array (MonoDomain *domain, MonoAssembly * assembly, MonoTableInfo *table, int i, MonoArrayHandle dest, int dest_idx, MonoError *error)
5313 HANDLE_FUNCTION_ENTER();
5314 const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
5315 char *n = g_concat_dir_and_file (assembly->basedir, val);
5316 MonoStringHandle str = mono_string_new_handle (domain, n, error);
5317 g_free (n);
5318 goto_if_nok (error, leave);
5319 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, str);
5320 leave:
5321 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5324 MonoObjectHandle
5325 ves_icall_System_Reflection_RuntimeAssembly_GetFilesInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoBoolean resource_modules, MonoError *error)
5327 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5328 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5329 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_FILE];
5330 int i, count;
5332 /* check hash if needed */
5333 if (!MONO_HANDLE_IS_NULL(name)) {
5334 char *n = mono_string_handle_to_utf8 (name, error);
5335 goto_if_nok (error, fail);
5337 for (i = 0; i < table->rows; ++i) {
5338 const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
5339 if (strcmp (val, n) == 0) {
5340 g_free (n);
5341 n = g_concat_dir_and_file (assembly->basedir, val);
5342 MonoStringHandle fn = mono_string_new_handle (domain, n, error);
5343 g_free (n);
5344 goto_if_nok (error, fail);
5345 return MONO_HANDLE_CAST (MonoObject, fn);
5348 g_free (n);
5349 return NULL_HANDLE;
5352 count = 0;
5353 for (i = 0; i < table->rows; ++i) {
5354 if (resource_modules || !(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA))
5355 count ++;
5358 MonoArrayHandle result;
5359 result = mono_array_new_handle (domain, mono_defaults.string_class, count, error);
5360 goto_if_nok (error, fail);
5362 count = 0;
5363 for (i = 0; i < table->rows; ++i) {
5364 if (resource_modules || !(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
5365 if (!add_filename_to_files_array (domain, assembly, table, i, result, count, error))
5366 goto fail;
5367 count++;
5370 return MONO_HANDLE_CAST (MonoObject, result);
5371 fail:
5372 return NULL_HANDLE;
5375 static gboolean
5376 add_module_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int *dest_idx, MonoImage* module, MonoError *error)
5378 HANDLE_FUNCTION_ENTER ();
5379 error_init (error);
5380 if (module) {
5381 MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
5382 goto_if_nok (error, leave);
5384 MONO_HANDLE_ARRAY_SETREF (dest, *dest_idx, rm);
5385 ++(*dest_idx);
5388 leave:
5389 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5392 static gboolean
5393 add_file_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int dest_idx, MonoImage *image, MonoTableInfo *table, int table_idx, MonoError *error)
5395 HANDLE_FUNCTION_ENTER ();
5397 guint32 cols [MONO_FILE_SIZE];
5398 mono_metadata_decode_row (table, table_idx, cols, MONO_FILE_SIZE);
5399 if (cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA) {
5400 MonoReflectionModuleHandle rm = mono_module_file_get_object_handle (domain, image, table_idx, error);
5401 goto_if_nok (error, leave);
5402 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rm);
5403 } else {
5404 MonoImage *m = mono_image_load_file_for_image_checked (image, table_idx + 1, error);
5405 goto_if_nok (error, leave);
5406 if (!m) {
5407 const char *filename = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
5408 gboolean refonly = FALSE;
5409 if (image->assembly)
5410 refonly = mono_asmctx_get_kind (&image->assembly->context) == MONO_ASMCTX_REFONLY;
5411 mono_error_set_simple_file_not_found (error, filename, refonly);
5412 goto leave;
5414 MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, m, error);
5415 goto_if_nok (error, leave);
5416 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rm);
5419 leave:
5420 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5423 MonoArrayHandle
5424 ves_icall_System_Reflection_RuntimeAssembly_GetModulesInternal (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5426 error_init (error);
5427 MonoDomain *domain = mono_domain_get();
5428 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5429 MonoClass *klass;
5430 int i, j, file_count = 0;
5431 MonoImage **modules;
5432 guint32 module_count, real_module_count;
5433 MonoTableInfo *table;
5434 MonoImage *image = assembly->image;
5436 g_assert (image != NULL);
5437 g_assert (!assembly_is_dynamic (assembly));
5439 table = &image->tables [MONO_TABLE_FILE];
5440 file_count = table->rows;
5442 modules = image->modules;
5443 module_count = image->module_count;
5445 real_module_count = 0;
5446 for (i = 0; i < module_count; ++i)
5447 if (modules [i])
5448 real_module_count ++;
5450 klass = mono_class_get_module_class ();
5451 MonoArrayHandle res = mono_array_new_handle (domain, klass, 1 + real_module_count + file_count, error);
5452 goto_if_nok (error, fail);
5454 MonoReflectionModuleHandle image_obj;
5455 image_obj = mono_module_get_object_handle (domain, image, error);
5456 goto_if_nok (error, fail);
5458 MONO_HANDLE_ARRAY_SETREF (res, 0, image_obj);
5460 j = 1;
5461 for (i = 0; i < module_count; ++i)
5462 if (!add_module_to_modules_array (domain, res, &j, modules[i], error))
5463 goto fail;
5465 for (i = 0; i < file_count; ++i, ++j) {
5466 if (!add_file_to_modules_array (domain, res, j, image, table, i, error))
5467 goto fail;
5470 return res;
5471 fail:
5472 return NULL_HANDLE_ARRAY;
5475 MonoReflectionMethodHandle
5476 ves_icall_GetCurrentMethod (MonoError *error)
5478 MonoMethod *m = mono_method_get_last_managed ();
5480 if (!m) {
5481 mono_error_set_not_supported (error, "Stack walks are not supported on this platform.");
5482 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5485 while (m->is_inflated)
5486 m = ((MonoMethodInflated*)m)->declaring;
5488 return mono_method_get_object_handle (mono_domain_get (), m, NULL, error);
5491 static MonoMethod*
5492 mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
5494 int offset = -1, i;
5495 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
5496 ERROR_DECL (error);
5497 MonoMethod *result;
5498 MonoMethodInflated *inflated = (MonoMethodInflated*)method;
5499 //method is inflated, we should inflate it on the other class
5500 MonoGenericContext ctx;
5501 ctx.method_inst = inflated->context.method_inst;
5502 ctx.class_inst = inflated->context.class_inst;
5503 if (mono_class_is_ginst (klass))
5504 ctx.class_inst = mono_class_get_generic_class (klass)->context.class_inst;
5505 else if (mono_class_is_gtd (klass))
5506 ctx.class_inst = mono_class_get_generic_container (klass)->context.class_inst;
5507 result = mono_class_inflate_generic_method_full_checked (inflated->declaring, klass, &ctx, error);
5508 g_assert (is_ok (error)); /* FIXME don't swallow the error */
5509 return result;
5512 mono_class_setup_methods (method->klass);
5513 if (mono_class_has_failure (method->klass))
5514 return NULL;
5515 int mcount = mono_class_get_method_count (method->klass);
5516 MonoMethod **method_klass_methods = m_class_get_methods (method->klass);
5517 for (i = 0; i < mcount; ++i) {
5518 if (method_klass_methods [i] == method) {
5519 offset = i;
5520 break;
5523 mono_class_setup_methods (klass);
5524 if (mono_class_has_failure (klass))
5525 return NULL;
5526 g_assert (offset >= 0 && offset < mono_class_get_method_count (klass));
5527 return m_class_get_methods (klass) [offset];
5530 MonoReflectionMethodHandle
5531 ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodFromHandleInternalType_native (MonoMethod *method, MonoType *type, MonoBoolean generic_check, MonoError *error)
5533 MonoClass *klass;
5534 if (type && generic_check) {
5535 klass = mono_class_from_mono_type_internal (type);
5536 if (mono_class_get_generic_type_definition (method->klass) != mono_class_get_generic_type_definition (klass))
5537 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5539 if (method->klass != klass) {
5540 method = mono_method_get_equivalent_method (method, klass);
5541 if (!method)
5542 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5544 } else if (type)
5545 klass = mono_class_from_mono_type_internal (type);
5546 else
5547 klass = method->klass;
5548 return mono_method_get_object_handle (mono_domain_get (), method, klass, error);
5551 MonoReflectionMethodBodyHandle
5552 ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodBodyInternal (MonoMethod *method, MonoError *error)
5554 return mono_method_body_get_object_handle (mono_domain_get (), method, error);
5557 MonoReflectionAssemblyHandle
5558 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError *error)
5560 MonoMethod *dest = NULL;
5561 mono_stack_walk_no_il (get_executing, &dest);
5562 g_assert (dest);
5563 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
5566 MonoReflectionAssemblyHandle
5567 ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError *error)
5569 MonoDomain* domain = mono_domain_get ();
5571 if (!domain->entry_assembly)
5572 return MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
5574 return mono_assembly_get_object_handle (domain, domain->entry_assembly, error);
5577 MonoReflectionAssemblyHandle
5578 ves_icall_System_Reflection_Assembly_GetCallingAssembly (MonoError *error)
5580 error_init (error);
5581 MonoMethod *m;
5582 MonoMethod *dest;
5584 dest = NULL;
5585 mono_stack_walk_no_il (get_executing, &dest);
5586 m = dest;
5587 mono_stack_walk_no_il (get_caller_no_reflection, &dest);
5588 if (!dest)
5589 dest = m;
5590 if (!m) {
5591 mono_error_set_not_supported (error, "Stack walks are not supported on this platform.");
5592 return MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
5594 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
5597 MonoStringHandle
5598 ves_icall_System_RuntimeType_getFullName (MonoReflectionTypeHandle object, MonoBoolean full_name,
5599 MonoBoolean assembly_qualified, MonoError *error)
5601 MonoDomain *domain = mono_object_domain (MONO_HANDLE_RAW (object));
5602 MonoType *type = MONO_HANDLE_RAW (object)->type;
5603 MonoTypeNameFormat format;
5604 MonoStringHandle res;
5605 gchar *name;
5607 if (full_name)
5608 format = assembly_qualified ?
5609 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED :
5610 MONO_TYPE_NAME_FORMAT_FULL_NAME;
5611 else
5612 format = MONO_TYPE_NAME_FORMAT_REFLECTION;
5614 name = mono_type_get_name_full (type, format);
5615 if (!name)
5616 return NULL_HANDLE_STRING;
5618 if (full_name && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
5619 g_free (name);
5620 return NULL_HANDLE_STRING;
5623 res = mono_string_new_handle (domain, name, error);
5624 g_free (name);
5626 return res;
5630 ves_icall_RuntimeType_get_core_clr_security_level (MonoReflectionTypeHandle rfield, MonoError *error)
5632 MonoType *type = MONO_HANDLE_GETVAL (rfield, type);
5633 MonoClass *klass = mono_class_from_mono_type_internal (type);
5635 mono_class_init_checked (klass, error);
5636 return_val_if_nok (error, -1);
5637 return mono_security_core_clr_class_level (klass);
5641 ves_icall_RuntimeFieldInfo_get_core_clr_security_level (MonoReflectionFieldHandle rfield, MonoError *error)
5643 MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
5644 return mono_security_core_clr_field_level (field, TRUE);
5648 ves_icall_RuntimeMethodInfo_get_core_clr_security_level (MonoReflectionMethodHandle rfield, MonoError *error)
5650 MonoMethod *method = MONO_HANDLE_GETVAL (rfield, method);
5651 return mono_security_core_clr_method_level (method, TRUE);
5654 MonoStringHandle
5655 ves_icall_System_Reflection_RuntimeAssembly_get_fullname (MonoReflectionAssemblyHandle assembly, MonoError *error)
5657 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5658 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
5659 gchar *name;
5661 name = mono_stringify_assembly_name (&mass->aname);
5662 MonoStringHandle res = mono_string_new_handle (domain, name, error);
5663 g_free (name);
5664 return res;
5667 MonoAssemblyName *
5668 ves_icall_System_Reflection_AssemblyName_GetNativeName (MonoAssembly *mass)
5670 return &mass->aname;
5673 void
5674 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoStringHandle fname, MonoAssemblyName *name, MonoStringHandleOut normalized_codebase, MonoError *error)
5676 char *filename;
5677 MonoImageOpenStatus status = MONO_IMAGE_OK;
5678 char *codebase = NULL;
5679 gboolean res;
5680 MonoImage *image;
5681 char *dirname;
5683 error_init (error);
5685 MonoDomain *domain = MONO_HANDLE_DOMAIN (fname);
5686 filename = mono_string_handle_to_utf8 (fname, error);
5687 return_if_nok (error);
5689 dirname = g_path_get_dirname (filename);
5690 replace_shadow_path (mono_domain_get (), dirname, &filename);
5691 g_free (dirname);
5693 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_ASSEMBLY, "InternalGetAssemblyName (\"%s\")", filename);
5695 MonoAssemblyLoadContext *alc = mono_domain_default_alc (domain);
5696 image = mono_image_open_a_lot (alc, filename, &status, TRUE, FALSE);
5698 if (!image){
5699 if (status == MONO_IMAGE_IMAGE_INVALID)
5700 mono_error_set_bad_image_by_name (error, filename, "Invalid Image: %s", filename);
5701 else
5702 mono_error_set_simple_file_not_found (error, filename, FALSE);
5703 g_free (filename);
5704 return;
5707 res = mono_assembly_fill_assembly_name_full (image, name, TRUE);
5708 if (!res) {
5709 mono_image_close (image);
5710 g_free (filename);
5711 mono_error_set_argument (error, "assemblyFile", "The file does not contain a manifest");
5712 return;
5715 if (filename != NULL && *filename != '\0') {
5716 gchar *result;
5718 codebase = g_strdup (filename);
5720 mono_icall_make_platform_path (codebase);
5722 const gchar *prepend = mono_icall_get_file_path_prefix (codebase);
5724 result = g_strconcat (prepend, codebase, (const char*)NULL);
5725 g_free (codebase);
5726 codebase = result;
5728 MONO_HANDLE_ASSIGN (normalized_codebase, mono_string_new_handle (mono_domain_get (), codebase, error));
5729 g_free (codebase);
5731 mono_image_close (image);
5732 g_free (filename);
5735 MonoBoolean
5736 ves_icall_System_Reflection_RuntimeAssembly_LoadPermissions (MonoReflectionAssemblyHandle assembly_h,
5737 char **minimum, guint32 *minLength, char **optional, guint32 *optLength, char **refused, guint32 *refLength, MonoError *error)
5739 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5740 MonoBoolean result = FALSE;
5741 MonoDeclSecurityEntry entry;
5743 /* SecurityAction.RequestMinimum */
5744 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQMIN, &entry)) {
5745 *minimum = entry.blob;
5746 *minLength = entry.size;
5747 result = TRUE;
5749 /* SecurityAction.RequestOptional */
5750 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQOPT, &entry)) {
5751 *optional = entry.blob;
5752 *optLength = entry.size;
5753 result = TRUE;
5755 /* SecurityAction.RequestRefuse */
5756 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQREFUSE, &entry)) {
5757 *refused = entry.blob;
5758 *refLength = entry.size;
5759 result = TRUE;
5762 return result;
5765 static gboolean
5766 mono_module_type_is_visible (MonoTableInfo *tdef, MonoImage *image, int type)
5768 guint32 attrs, visibility;
5769 do {
5770 attrs = mono_metadata_decode_row_col (tdef, type - 1, MONO_TYPEDEF_FLAGS);
5771 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
5772 if (visibility != TYPE_ATTRIBUTE_PUBLIC && visibility != TYPE_ATTRIBUTE_NESTED_PUBLIC)
5773 return FALSE;
5775 } while ((type = mono_metadata_token_index (mono_metadata_nested_in_typedef (image, type))));
5777 return TRUE;
5780 static void
5781 image_get_type (MonoDomain *domain, MonoImage *image, MonoTableInfo *tdef, int table_idx, int count, MonoArrayHandle res, MonoArrayHandle exceptions, MonoBoolean exportedOnly, MonoError *error)
5783 error_init (error);
5784 HANDLE_FUNCTION_ENTER ();
5785 ERROR_DECL (klass_error);
5786 MonoClass *klass = mono_class_get_checked (image, table_idx | MONO_TOKEN_TYPE_DEF, klass_error);
5788 if (klass) {
5789 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
5790 return_if_nok (error);
5792 MONO_HANDLE_ARRAY_SETREF (res, count, rt);
5793 } else {
5794 MonoExceptionHandle ex = mono_error_convert_to_exception_handle (klass_error);
5795 MONO_HANDLE_ARRAY_SETREF (exceptions, count, ex);
5797 HANDLE_FUNCTION_RETURN ();
5800 static MonoArrayHandle
5801 mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArrayHandleOut exceptions, MonoBoolean exportedOnly, MonoError *error)
5803 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
5804 int i, count;
5806 error_init (error);
5808 /* we start the count from 1 because we skip the special type <Module> */
5809 if (exportedOnly) {
5810 count = 0;
5811 for (i = 1; i < tdef->rows; ++i) {
5812 if (mono_module_type_is_visible (tdef, image, i + 1))
5813 count++;
5815 } else {
5816 count = tdef->rows - 1;
5818 MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.runtimetype_class, count, error);
5819 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5820 MONO_HANDLE_ASSIGN (exceptions, mono_array_new_handle (domain, mono_defaults.exception_class, count, error));
5821 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5822 count = 0;
5823 for (i = 1; i < tdef->rows; ++i) {
5824 if (!exportedOnly || mono_module_type_is_visible (tdef, image, i+1)) {
5825 image_get_type (domain, image, tdef, i + 1, count, res, exceptions, exportedOnly, error);
5826 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5827 count++;
5831 return res;
5834 static void
5835 append_module_types (MonoDomain *domain, MonoArrayHandleOut res, MonoArrayHandleOut exceptions, MonoImage *image, MonoBoolean exportedOnly, MonoError *error)
5837 HANDLE_FUNCTION_ENTER ();
5838 error_init (error);
5839 MonoArrayHandle ex2 = MONO_HANDLE_NEW (MonoArray, NULL);
5840 MonoArrayHandle res2 = mono_module_get_types (domain, image, ex2, exportedOnly, error);
5841 goto_if_nok (error, leave);
5843 /* Append the new types to the end of the array */
5844 if (mono_array_handle_length (res2) > 0) {
5845 guint32 len1, len2;
5847 len1 = mono_array_handle_length (res);
5848 len2 = mono_array_handle_length (res2);
5850 MonoArrayHandle res3 = mono_array_new_handle (domain, mono_defaults.runtimetype_class, len1 + len2, error);
5851 goto_if_nok (error, leave);
5853 mono_array_handle_memcpy_refs (res3, 0, res, 0, len1);
5854 mono_array_handle_memcpy_refs (res3, len1, res2, 0, len2);
5855 MONO_HANDLE_ASSIGN (res, res3);
5857 MonoArrayHandle ex3 = mono_array_new_handle (domain, mono_defaults.runtimetype_class, len1 + len2, error);
5858 goto_if_nok (error, leave);
5860 mono_array_handle_memcpy_refs (ex3, 0, exceptions, 0, len1);
5861 mono_array_handle_memcpy_refs (ex3, len1, ex2, 0, len2);
5862 MONO_HANDLE_ASSIGN (exceptions, ex3);
5864 leave:
5865 HANDLE_FUNCTION_RETURN ();
5868 static void
5869 set_class_failure_in_array (MonoArrayHandle exl, int i, MonoClass *klass)
5871 HANDLE_FUNCTION_ENTER ();
5872 ERROR_DECL (unboxed_error);
5873 mono_error_set_for_class_failure (unboxed_error, klass);
5875 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_error_convert_to_exception (unboxed_error));
5876 MONO_HANDLE_ARRAY_SETREF (exl, i, exc);
5877 HANDLE_FUNCTION_RETURN ();
5880 static MonoArrayHandle
5881 assembly_get_types (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
5883 MonoArrayHandle exceptions = MONO_HANDLE_NEW(MonoArray, NULL);
5884 int i;
5886 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_handle);
5887 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_handle, assembly);
5889 g_assert (!assembly_is_dynamic (assembly));
5890 MonoImage *image = assembly->image;
5891 MonoTableInfo *table = &image->tables [MONO_TABLE_FILE];
5892 MonoArrayHandle res = mono_module_get_types (domain, image, exceptions, exportedOnly, error);
5893 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5895 /* Append data from all modules in the assembly */
5896 for (i = 0; i < table->rows; ++i) {
5897 if (!(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
5898 MonoImage *loaded_image = mono_assembly_load_module_checked (image->assembly, i + 1, error);
5899 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5901 if (loaded_image) {
5902 append_module_types (domain, res, exceptions, loaded_image, exportedOnly, error);
5903 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5908 /* the ReflectionTypeLoadException must have all the types (Types property),
5909 * NULL replacing types which throws an exception. The LoaderException must
5910 * contain all exceptions for NULL items.
5913 int len = mono_array_handle_length (res);
5915 int ex_count = 0;
5916 GList *list = NULL;
5917 MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
5918 for (i = 0; i < len; i++) {
5919 MONO_HANDLE_ARRAY_GETREF (t, res, i);
5921 if (!MONO_HANDLE_IS_NULL (t)) {
5922 MonoClass *klass = mono_type_get_class_internal (MONO_HANDLE_GETVAL (t, type));
5923 if ((klass != NULL) && mono_class_has_failure (klass)) {
5924 /* keep the class in the list */
5925 list = g_list_append (list, klass);
5926 /* and replace Type with NULL */
5927 MONO_HANDLE_ARRAY_SETREF (res, i, NULL_HANDLE);
5929 } else {
5930 ex_count ++;
5934 if (list || ex_count) {
5935 GList *tmp = NULL;
5936 int j, length = g_list_length (list) + ex_count;
5938 MonoArrayHandle exl = mono_array_new_handle (domain, mono_defaults.exception_class, length, error);
5939 if (!is_ok (error)) {
5940 g_list_free (list);
5941 return NULL_HANDLE_ARRAY;
5943 /* Types for which mono_class_get_checked () succeeded */
5944 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, NULL);
5945 for (i = 0, tmp = list; tmp; i++, tmp = tmp->next) {
5946 set_class_failure_in_array (exl, i, (MonoClass*)tmp->data);
5948 /* Types for which it don't */
5949 for (j = 0; j < mono_array_handle_length (exceptions); ++j) {
5950 MONO_HANDLE_ARRAY_GETREF (exc, exceptions, j);
5951 if (!MONO_HANDLE_IS_NULL (exc)) {
5952 g_assert (i < length);
5953 MONO_HANDLE_ARRAY_SETREF (exl, i, exc);
5954 i ++;
5957 g_list_free (list);
5958 list = NULL;
5960 MONO_HANDLE_ASSIGN (exc, mono_get_exception_reflection_type_load_checked (res, exl, error));
5961 return_val_if_nok (error, NULL_HANDLE_ARRAY);
5962 mono_error_set_exception_handle (error, exc);
5963 return NULL_HANDLE_ARRAY;
5966 return res;
5969 MonoArrayHandle
5970 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
5972 return assembly_get_types (assembly_handle, exportedOnly, error);
5976 void
5977 ves_icall_Mono_RuntimeMarshal_FreeAssemblyName (MonoAssemblyName *aname, MonoBoolean free_struct)
5979 mono_assembly_name_free_internal (aname);
5980 if (free_struct)
5981 g_free (aname);
5984 void
5985 ves_icall_Mono_Runtime_DisableMicrosoftTelemetry (void)
5987 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
5988 mono_merp_disable ();
5989 #else
5990 // Icall has platform check in managed too.
5991 g_assert_not_reached ();
5992 #endif
5995 void
5996 ves_icall_Mono_Runtime_AnnotateMicrosoftTelemetry (const char *key, const char *value)
5998 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
5999 if (!mono_merp_enabled ())
6000 g_error ("Cannot add attributes to telemetry without enabling subsystem");
6001 mono_merp_add_annotation (key, value);
6002 #else
6003 // Icall has platform check in managed too.
6004 g_assert_not_reached ();
6005 #endif
6008 void
6009 ves_icall_Mono_Runtime_EnableMicrosoftTelemetry (const char *appBundleID, const char *appSignature, const char *appVersion, const char *merpGUIPath, const char *appPath, const char *configDir, MonoError *error)
6011 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6012 mono_merp_enable (appBundleID, appSignature, appVersion, merpGUIPath, appPath, configDir);
6014 mono_get_runtime_callbacks ()->install_state_summarizer ();
6015 #else
6016 // Icall has platform check in managed too.
6017 g_assert_not_reached ();
6018 #endif
6021 // Number derived from trials on relevant hardware.
6022 // If it seems large, please confirm it's safe to shrink
6023 // before doing so.
6024 #define MONO_MAX_SUMMARY_LEN_ICALL 500000
6026 MonoStringHandle
6027 ves_icall_Mono_Runtime_ExceptionToState (MonoExceptionHandle exc_handle, guint64 *portable_hash_out, guint64 *unportable_hash_out, MonoError *error)
6029 MonoStringHandle result;
6031 #ifndef DISABLE_CRASH_REPORTING
6032 if (mono_get_eh_callbacks ()->mono_summarize_exception) {
6033 // FIXME: Push handles down into mini/mini-exceptions.c
6034 MonoException *exc = MONO_HANDLE_RAW (exc_handle);
6035 MonoThreadSummary out;
6036 mono_summarize_timeline_start ("ExceptionToState");
6037 mono_summarize_timeline_phase_log (MonoSummarySuspendHandshake);
6038 mono_summarize_timeline_phase_log (MonoSummaryUnmanagedStacks);
6039 mono_get_eh_callbacks ()->mono_summarize_exception (exc, &out);
6040 mono_summarize_timeline_phase_log (MonoSummaryManagedStacks);
6042 *portable_hash_out = (guint64) out.hashes.offset_free_hash;
6043 *unportable_hash_out = (guint64) out.hashes.offset_rich_hash;
6045 MonoStateWriter writer;
6046 char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
6047 mono_state_writer_init (&writer, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
6048 mono_native_state_init (&writer);
6049 mono_summarize_timeline_phase_log (MonoSummaryStateWriter);
6050 gboolean first_thread_added = TRUE;
6051 mono_native_state_add_thread (&writer, &out, NULL, first_thread_added, TRUE);
6052 char *output = mono_native_state_free (&writer, FALSE);
6053 mono_summarize_timeline_phase_log (MonoSummaryStateWriterDone);
6054 result = mono_string_new_handle (mono_domain_get (), output, error);
6055 g_free (output);
6056 g_free (scratch);
6057 return result;
6059 #endif
6061 *portable_hash_out = 0;
6062 *unportable_hash_out = 0;
6063 result = mono_string_new_handle (mono_domain_get (), "", error);
6064 return result;
6067 void
6068 ves_icall_Mono_Runtime_SendMicrosoftTelemetry (const char *payload, guint64 portable_hash, guint64 unportable_hash, MonoError *error)
6070 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6071 if (!mono_merp_enabled ())
6072 g_error ("Cannot send telemetry without registering parameters first");
6074 pid_t crashed_pid = getpid ();
6076 MonoStackHash hashes;
6077 memset (&hashes, 0, sizeof (MonoStackHash));
6078 hashes.offset_free_hash = portable_hash;
6079 hashes.offset_rich_hash = unportable_hash;
6081 const char *signal = "MANAGED_EXCEPTION";
6083 gboolean success = mono_merp_invoke (crashed_pid, signal, payload, &hashes);
6084 if (!success) {
6085 //g_assert_not_reached ();
6086 mono_error_set_generic_error (error, "System", "Exception", "We were unable to start the Microsoft Error Reporting client.");
6088 #else
6089 // Icall has platform check in managed too.
6090 g_assert_not_reached ();
6091 #endif
6094 void
6095 ves_icall_Mono_Runtime_DumpTelemetry (const char *payload, guint64 portable_hash, guint64 unportable_hash, MonoError *error)
6097 #ifndef DISABLE_CRASH_REPORTING
6098 MonoStackHash hashes;
6099 memset (&hashes, 0, sizeof (MonoStackHash));
6100 hashes.offset_free_hash = portable_hash;
6101 hashes.offset_rich_hash = unportable_hash;
6102 mono_crash_dump (payload, &hashes);
6103 #else
6104 return;
6105 #endif
6108 MonoStringHandle
6109 ves_icall_Mono_Runtime_DumpStateSingle (guint64 *portable_hash, guint64 *unportable_hash, MonoError *error)
6111 MonoStringHandle result;
6113 #ifndef DISABLE_CRASH_REPORTING
6114 MonoStackHash hashes;
6115 memset (&hashes, 0, sizeof (MonoStackHash));
6116 MonoContext *ctx = NULL;
6118 MonoThreadSummary this_thread;
6119 if (!mono_threads_summarize_one (&this_thread, ctx))
6120 return mono_string_new_handle (mono_domain_get (), "", error);
6122 *portable_hash = (guint64) this_thread.hashes.offset_free_hash;
6123 *unportable_hash = (guint64) this_thread.hashes.offset_rich_hash;
6125 MonoStateWriter writer;
6126 char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
6127 mono_state_writer_init (&writer, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
6128 mono_native_state_init (&writer);
6129 gboolean first_thread_added = TRUE;
6130 mono_native_state_add_thread (&writer, &this_thread, NULL, first_thread_added, TRUE);
6131 char *output = mono_native_state_free (&writer, FALSE);
6132 result = mono_string_new_handle (mono_domain_get (), output, error);
6133 g_free (output);
6134 g_free (scratch);
6135 #else
6136 *portable_hash = 0;
6137 *unportable_hash = 0;
6138 result = mono_string_new_handle (mono_domain_get (), "", error);
6139 #endif
6141 return result;
6145 void
6146 ves_icall_Mono_Runtime_RegisterReportingForNativeLib (const char *path_suffix, const char *module_name)
6148 #ifndef DISABLE_CRASH_REPORTING
6149 if (mono_get_eh_callbacks ()->mono_register_native_library)
6150 mono_get_eh_callbacks ()->mono_register_native_library (path_suffix, module_name);
6151 #endif
6154 void
6155 ves_icall_Mono_Runtime_RegisterReportingForAllNativeLibs ()
6157 #ifndef DISABLE_CRASH_REPORTING
6158 if (mono_get_eh_callbacks ()->mono_allow_all_native_libraries)
6159 mono_get_eh_callbacks ()->mono_allow_all_native_libraries ();
6160 #endif
6163 void
6164 ves_icall_Mono_Runtime_EnableCrashReportingLog (const char *directory)
6166 #ifndef DISABLE_CRASH_REPORTING
6167 mono_summarize_set_timeline_dir (directory);
6168 #endif
6172 ves_icall_Mono_Runtime_CheckCrashReportingLog (const char *directory, MonoBoolean clear)
6174 int ret = 0;
6175 #ifndef DISABLE_CRASH_REPORTING
6176 ret = (int) mono_summarize_timeline_read_level (directory, clear != 0);
6177 #endif
6178 return ret;
6181 MonoStringHandle
6182 ves_icall_Mono_Runtime_DumpStateTotal (guint64 *portable_hash, guint64 *unportable_hash, MonoError *error)
6184 MonoStringHandle result;
6186 #ifndef DISABLE_CRASH_REPORTING
6187 char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
6189 char *out;
6190 MonoStackHash hashes;
6191 memset (&hashes, 0, sizeof (MonoStackHash));
6192 MonoContext *ctx = NULL;
6194 while (!mono_dump_start ())
6195 g_usleep (1000); // wait around for other dump to finish
6197 mono_get_runtime_callbacks ()->install_state_summarizer ();
6199 mono_summarize_timeline_start ("DumpStateTotal");
6201 gboolean success = mono_threads_summarize (ctx, &out, &hashes, TRUE, FALSE, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
6202 mono_summarize_timeline_phase_log (MonoSummaryCleanup);
6204 if (!success)
6205 return mono_string_new_handle (mono_domain_get (), "", error);
6207 *portable_hash = (guint64) hashes.offset_free_hash;
6208 *unportable_hash = (guint64) hashes.offset_rich_hash;
6209 result = mono_string_new_handle (mono_domain_get (), out, error);
6211 // out is now a pointer into garbage memory
6212 g_free (scratch);
6214 mono_summarize_timeline_phase_log (MonoSummaryDone);
6216 mono_dump_complete ();
6217 #else
6218 *portable_hash = 0;
6219 *unportable_hash = 0;
6220 result = mono_string_new_handle (mono_domain_get (), "", error);
6221 #endif
6223 return result;
6227 MonoBoolean
6228 ves_icall_System_Reflection_AssemblyName_ParseAssemblyName (const char *name, MonoAssemblyName *aname, MonoBoolean *is_version_defined_arg, MonoBoolean *is_token_defined_arg)
6230 gboolean is_version_defined = FALSE;
6231 gboolean is_token_defined = FALSE;
6232 gboolean result = FALSE;
6234 result = mono_assembly_name_parse_full (name, aname, TRUE, &is_version_defined, &is_token_defined);
6236 *is_version_defined_arg = (MonoBoolean)is_version_defined;
6237 *is_token_defined_arg = (MonoBoolean)is_token_defined;
6239 return result;
6242 MonoReflectionTypeHandle
6243 ves_icall_System_Reflection_RuntimeModule_GetGlobalType (MonoImage *image, MonoError *error)
6245 MonoDomain *domain = mono_domain_get ();
6246 MonoClass *klass;
6248 g_assert (image);
6250 MonoReflectionTypeHandle ret = MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
6252 if (image_is_dynamic (image) && ((MonoDynamicImage*)image)->initial_image)
6253 /* These images do not have a global type */
6254 goto leave;
6256 klass = mono_class_get_checked (image, 1 | MONO_TOKEN_TYPE_DEF, error);
6257 goto_if_nok (error, leave);
6259 ret = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
6260 leave:
6261 return ret;
6264 void
6265 ves_icall_System_Reflection_RuntimeModule_GetGuidInternal (MonoImage *image, MonoArrayHandle guid_h, MonoError *error)
6267 g_assert (mono_array_handle_length (guid_h) == 16);
6269 if (!image->metadata_only) {
6270 g_assert (image->heap_guid.data);
6271 g_assert (image->heap_guid.size >= 16);
6273 MONO_ENTER_NO_SAFEPOINTS;
6274 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
6275 memcpy (data, (guint8*)image->heap_guid.data, 16);
6276 MONO_EXIT_NO_SAFEPOINTS;
6277 } else {
6278 MONO_ENTER_NO_SAFEPOINTS;
6279 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
6280 memset (data, 0, 16);
6281 MONO_EXIT_NO_SAFEPOINTS;
6285 #ifndef HOST_WIN32
6286 static inline gpointer
6287 mono_icall_module_get_hinstance (MonoImage *image)
6289 return (gpointer) (-1);
6291 #endif /* HOST_WIN32 */
6293 gpointer
6294 ves_icall_System_Reflection_RuntimeModule_GetHINSTANCE (MonoImage *image, MonoError *error)
6296 return mono_icall_module_get_hinstance (image);
6299 void
6300 ves_icall_System_Reflection_RuntimeModule_GetPEKind (MonoImage *image, gint32 *pe_kind, gint32 *machine, MonoError *error)
6302 if (image_is_dynamic (image)) {
6303 MonoDynamicImage *dyn = (MonoDynamicImage*)image;
6304 *pe_kind = dyn->pe_kind;
6305 *machine = dyn->machine;
6307 else {
6308 *pe_kind = (image->image_info->cli_cli_header.ch_flags & 0x3);
6309 *machine = image->image_info->cli_header.coff.coff_machine;
6313 gint32
6314 ves_icall_System_Reflection_RuntimeModule_GetMDStreamVersion (MonoImage *image, MonoError *error)
6316 return (image->md_version_major << 16) | (image->md_version_minor);
6319 MonoArrayHandle
6320 ves_icall_System_Reflection_RuntimeModule_InternalGetTypes (MonoImage *image, MonoError *error)
6322 MonoDomain *domain = mono_domain_get ();
6324 if (!image) {
6325 MonoArrayHandle arr = mono_array_new_handle (domain, mono_defaults.runtimetype_class, 0, error);
6326 return arr;
6327 } else {
6328 MonoArrayHandle exceptions = MONO_HANDLE_NEW (MonoArray, NULL);
6329 MonoArrayHandle res = mono_module_get_types (domain, image, exceptions, FALSE, error);
6330 return_val_if_nok (error, MONO_HANDLE_CAST(MonoArray, NULL_HANDLE));
6332 int n = mono_array_handle_length (exceptions);
6333 MonoExceptionHandle ex = MONO_HANDLE_NEW (MonoException, NULL);
6334 for (int i = 0; i < n; ++i) {
6335 MONO_HANDLE_ARRAY_GETREF(ex, exceptions, i);
6336 if (!MONO_HANDLE_IS_NULL (ex)) {
6337 mono_error_set_exception_handle (error, ex);
6338 return MONO_HANDLE_CAST(MonoArray, NULL_HANDLE);
6341 return res;
6345 static gboolean
6346 mono_memberref_is_method (MonoImage *image, guint32 token)
6348 if (!image_is_dynamic (image)) {
6349 guint32 cols [MONO_MEMBERREF_SIZE];
6350 const char *sig;
6351 const MonoTableInfo *table = &image->tables [MONO_TABLE_MEMBERREF];
6352 int idx = mono_metadata_token_index (token) - 1;
6353 if (idx < 0 || table->rows <= idx) {
6354 return FALSE;
6356 mono_metadata_decode_row (table, idx, cols, MONO_MEMBERREF_SIZE);
6357 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
6358 mono_metadata_decode_blob_size (sig, &sig);
6359 return (*sig != 0x6);
6360 } else {
6361 ERROR_DECL (error);
6362 MonoClass *handle_class;
6364 if (!mono_lookup_dynamic_token_class (image, token, FALSE, &handle_class, NULL, error)) {
6365 mono_error_cleanup (error); /* just probing, ignore error */
6366 return FALSE;
6369 return mono_defaults.methodhandle_class == handle_class;
6373 static MonoGenericInst *
6374 get_generic_inst_from_array_handle (MonoArrayHandle type_args)
6376 int type_argc = mono_array_handle_length (type_args);
6377 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
6379 MonoGenericInst *ginst = (MonoGenericInst *)g_alloca (size);
6380 memset (ginst, 0, MONO_SIZEOF_GENERIC_INST);
6381 ginst->type_argc = type_argc;
6382 for (int i = 0; i < type_argc; i++) {
6383 MONO_HANDLE_ARRAY_GETVAL (ginst->type_argv[i], type_args, MonoType*, i);
6385 ginst->is_open = FALSE;
6386 for (int i = 0; i < type_argc; i++) {
6387 if (mono_class_is_open_constructed_type (ginst->type_argv[i])) {
6388 ginst->is_open = TRUE;
6389 break;
6393 return mono_metadata_get_canonical_generic_inst (ginst);
6396 static void
6397 init_generic_context_from_args_handles (MonoGenericContext *context, MonoArrayHandle type_args, MonoArrayHandle method_args)
6399 if (!MONO_HANDLE_IS_NULL (type_args)) {
6400 context->class_inst = get_generic_inst_from_array_handle (type_args);
6401 } else {
6402 context->class_inst = NULL;
6404 if (!MONO_HANDLE_IS_NULL (method_args)) {
6405 context->method_inst = get_generic_inst_from_array_handle (method_args);
6406 } else {
6407 context->method_inst = NULL;
6412 static MonoType*
6413 module_resolve_type_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6415 HANDLE_FUNCTION_ENTER ();
6416 error_init (error);
6417 MonoType *result = NULL;
6418 MonoClass *klass;
6419 int table = mono_metadata_token_table (token);
6420 int index = mono_metadata_token_index (token);
6421 MonoGenericContext context;
6423 *resolve_error = ResolveTokenError_Other;
6425 /* Validate token */
6426 if ((table != MONO_TABLE_TYPEDEF) && (table != MONO_TABLE_TYPEREF) &&
6427 (table != MONO_TABLE_TYPESPEC)) {
6428 *resolve_error = ResolveTokenError_BadTable;
6429 goto leave;
6432 if (image_is_dynamic (image)) {
6433 if ((table == MONO_TABLE_TYPEDEF) || (table == MONO_TABLE_TYPEREF)) {
6434 ERROR_DECL (inner_error);
6435 klass = (MonoClass *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
6436 mono_error_cleanup (inner_error);
6437 result = klass ? m_class_get_byval_arg (klass) : NULL;
6438 goto leave;
6441 init_generic_context_from_args_handles (&context, type_args, method_args);
6442 ERROR_DECL (inner_error);
6443 klass = (MonoClass *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
6444 mono_error_cleanup (inner_error);
6445 result = klass ? m_class_get_byval_arg (klass) : NULL;
6446 goto leave;
6449 if ((index <= 0) || (index > image->tables [table].rows)) {
6450 *resolve_error = ResolveTokenError_OutOfRange;
6451 goto leave;
6454 init_generic_context_from_args_handles (&context, type_args, method_args);
6455 klass = mono_class_get_checked (image, token, error);
6456 if (klass)
6457 klass = mono_class_inflate_generic_class_checked (klass, &context, error);
6458 goto_if_nok (error, leave);
6460 if (klass)
6461 result = m_class_get_byval_arg (klass);
6462 leave:
6463 HANDLE_FUNCTION_RETURN_VAL (result);
6466 MonoType*
6467 ves_icall_System_Reflection_RuntimeModule_ResolveTypeToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6469 return module_resolve_type_token (image, token, type_args, method_args, resolve_error, error);
6472 static MonoMethod*
6473 module_resolve_method_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6475 HANDLE_FUNCTION_ENTER ();
6476 error_init (error);
6477 MonoMethod *method = NULL;
6478 int table = mono_metadata_token_table (token);
6479 int index = mono_metadata_token_index (token);
6480 MonoGenericContext context;
6482 *resolve_error = ResolveTokenError_Other;
6484 /* Validate token */
6485 if ((table != MONO_TABLE_METHOD) && (table != MONO_TABLE_METHODSPEC) &&
6486 (table != MONO_TABLE_MEMBERREF)) {
6487 *resolve_error = ResolveTokenError_BadTable;
6488 goto leave;
6491 if (image_is_dynamic (image)) {
6492 if (table == MONO_TABLE_METHOD) {
6493 ERROR_DECL (inner_error);
6494 method = (MonoMethod *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
6495 mono_error_cleanup (inner_error);
6496 goto leave;
6499 if ((table == MONO_TABLE_MEMBERREF) && !(mono_memberref_is_method (image, token))) {
6500 *resolve_error = ResolveTokenError_BadTable;
6501 goto leave;
6504 init_generic_context_from_args_handles (&context, type_args, method_args);
6505 ERROR_DECL (inner_error);
6506 method = (MonoMethod *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
6507 mono_error_cleanup (inner_error);
6508 goto leave;
6511 if ((index <= 0) || (index > image->tables [table].rows)) {
6512 *resolve_error = ResolveTokenError_OutOfRange;
6513 goto leave;
6515 if ((table == MONO_TABLE_MEMBERREF) && (!mono_memberref_is_method (image, token))) {
6516 *resolve_error = ResolveTokenError_BadTable;
6517 goto leave;
6520 init_generic_context_from_args_handles (&context, type_args, method_args);
6521 method = mono_get_method_checked (image, token, NULL, &context, error);
6523 leave:
6524 HANDLE_FUNCTION_RETURN_VAL (method);
6527 MonoMethod*
6528 ves_icall_System_Reflection_RuntimeModule_ResolveMethodToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6530 return module_resolve_method_token (image, token, type_args, method_args, resolve_error, error);
6533 MonoStringHandle
6534 ves_icall_System_Reflection_RuntimeModule_ResolveStringToken (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
6536 int index = mono_metadata_token_index (token);
6538 *resolve_error = ResolveTokenError_Other;
6540 /* Validate token */
6541 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
6542 *resolve_error = ResolveTokenError_BadTable;
6543 return NULL_HANDLE_STRING;
6546 if (image_is_dynamic (image)) {
6547 ERROR_DECL (ignore_inner_error);
6548 // FIXME ignoring error
6549 // FIXME Push MONO_HANDLE_NEW to lower layers.
6550 MonoStringHandle result = MONO_HANDLE_NEW (MonoString, (MonoString*)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, ignore_inner_error));
6551 mono_error_cleanup (ignore_inner_error);
6552 return result;
6555 if ((index <= 0) || (index >= image->heap_us.size)) {
6556 *resolve_error = ResolveTokenError_OutOfRange;
6557 return NULL_HANDLE_STRING;
6560 /* FIXME: What to do if the index points into the middle of a string ? */
6561 return mono_ldstr_handle (mono_domain_get (), image, index, error);
6564 static MonoClassField*
6565 module_resolve_field_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6567 HANDLE_FUNCTION_ENTER ();
6568 MonoClass *klass;
6569 int table = mono_metadata_token_table (token);
6570 int index = mono_metadata_token_index (token);
6571 MonoGenericContext context;
6572 MonoClassField *field = NULL;
6574 error_init (error);
6575 *resolve_error = ResolveTokenError_Other;
6577 /* Validate token */
6578 if ((table != MONO_TABLE_FIELD) && (table != MONO_TABLE_MEMBERREF)) {
6579 *resolve_error = ResolveTokenError_BadTable;
6580 goto leave;
6583 if (image_is_dynamic (image)) {
6584 if (table == MONO_TABLE_FIELD) {
6585 ERROR_DECL (inner_error);
6586 field = (MonoClassField *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
6587 mono_error_cleanup (inner_error);
6588 goto leave;
6591 if (mono_memberref_is_method (image, token)) {
6592 *resolve_error = ResolveTokenError_BadTable;
6593 goto leave;
6596 init_generic_context_from_args_handles (&context, type_args, method_args);
6597 ERROR_DECL (inner_error);
6598 field = (MonoClassField *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
6599 mono_error_cleanup (inner_error);
6600 goto leave;
6603 if ((index <= 0) || (index > image->tables [table].rows)) {
6604 *resolve_error = ResolveTokenError_OutOfRange;
6605 goto leave;
6607 if ((table == MONO_TABLE_MEMBERREF) && (mono_memberref_is_method (image, token))) {
6608 *resolve_error = ResolveTokenError_BadTable;
6609 goto leave;
6612 init_generic_context_from_args_handles (&context, type_args, method_args);
6613 field = mono_field_from_token_checked (image, token, &klass, &context, error);
6615 leave:
6616 HANDLE_FUNCTION_RETURN_VAL (field);
6619 MonoClassField*
6620 ves_icall_System_Reflection_RuntimeModule_ResolveFieldToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6622 return module_resolve_field_token (image, token, type_args, method_args, resolve_error, error);
6625 MonoObjectHandle
6626 ves_icall_System_Reflection_RuntimeModule_ResolveMemberToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *error, MonoError *merror)
6628 int table = mono_metadata_token_table (token);
6630 *error = ResolveTokenError_Other;
6632 switch (table) {
6633 case MONO_TABLE_TYPEDEF:
6634 case MONO_TABLE_TYPEREF:
6635 case MONO_TABLE_TYPESPEC: {
6636 MonoType *t = module_resolve_type_token (image, token, type_args, method_args, error, merror);
6637 if (t) {
6638 return MONO_HANDLE_CAST (MonoObject, mono_type_get_object_handle (mono_domain_get (), t, merror));
6640 else
6641 return NULL_HANDLE;
6643 case MONO_TABLE_METHOD:
6644 case MONO_TABLE_METHODSPEC: {
6645 MonoMethod *m = module_resolve_method_token (image, token, type_args, method_args, error, merror);
6646 if (m) {
6647 return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
6648 } else
6649 return NULL_HANDLE;
6651 case MONO_TABLE_FIELD: {
6652 MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
6653 if (f) {
6654 return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
6656 else
6657 return NULL_HANDLE;
6659 case MONO_TABLE_MEMBERREF:
6660 if (mono_memberref_is_method (image, token)) {
6661 MonoMethod *m = module_resolve_method_token (image, token, type_args, method_args, error, merror);
6662 if (m) {
6663 return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
6664 } else
6665 return NULL_HANDLE;
6667 else {
6668 MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
6669 if (f) {
6670 return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
6672 else
6673 return NULL_HANDLE;
6675 break;
6677 default:
6678 *error = ResolveTokenError_BadTable;
6681 return NULL_HANDLE;
6684 MonoArrayHandle
6685 ves_icall_System_Reflection_RuntimeModule_ResolveSignature (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
6687 int table = mono_metadata_token_table (token);
6688 int idx = mono_metadata_token_index (token);
6689 MonoTableInfo *tables = image->tables;
6690 guint32 sig, len;
6691 const char *ptr;
6693 *resolve_error = ResolveTokenError_OutOfRange;
6695 /* FIXME: Support other tables ? */
6696 if (table != MONO_TABLE_STANDALONESIG)
6697 return NULL_HANDLE_ARRAY;
6699 if (image_is_dynamic (image))
6700 return NULL_HANDLE_ARRAY;
6702 if ((idx == 0) || (idx > tables [MONO_TABLE_STANDALONESIG].rows))
6703 return NULL_HANDLE_ARRAY;
6705 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
6707 ptr = mono_metadata_blob_heap (image, sig);
6708 len = mono_metadata_decode_blob_size (ptr, &ptr);
6710 MonoArrayHandle res = mono_array_new_handle (mono_domain_get (), mono_defaults.byte_class, len, error);
6711 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6713 // FIXME MONO_ENTER_NO_SAFEPOINTS instead of pin/gchandle.
6715 MonoGCHandle h;
6716 gpointer array_base = MONO_ARRAY_HANDLE_PIN (res, guint8, 0, &h);
6717 memcpy (array_base, ptr, len);
6718 mono_gchandle_free_internal (h);
6720 return res;
6723 static void
6724 check_for_invalid_array_type (MonoClass *klass, MonoError *error)
6726 char *name;
6728 error_init (error);
6730 if (m_class_get_byval_arg (klass)->type != MONO_TYPE_TYPEDBYREF)
6731 return;
6733 name = mono_type_get_full_name (klass);
6734 mono_error_set_type_load_name (error, name, g_strdup (""), "");
6737 static void
6738 check_for_invalid_byref_or_pointer_type (MonoClass *klass, MonoError *error)
6740 check_for_invalid_array_type (klass, error);
6743 MonoReflectionTypeHandle
6744 ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int rank, MonoError *error)
6746 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
6748 MonoClass *klass = mono_class_from_mono_type_internal (type);
6749 check_for_invalid_array_type (klass, error);
6750 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
6752 MonoClass *aklass;
6753 if (rank == 0) //single dimension array
6754 aklass = mono_class_create_array (klass, 1);
6755 else
6756 aklass = mono_class_create_bounded_array (klass, rank, TRUE);
6758 if (mono_class_has_failure (aklass)) {
6759 mono_error_set_for_class_failure (error, aklass);
6760 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
6763 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
6764 return mono_type_get_object_handle (domain, m_class_get_byval_arg (aklass), error);
6767 MonoReflectionTypeHandle
6768 ves_icall_RuntimeType_make_byref_type (MonoReflectionTypeHandle ref_type, MonoError *error)
6770 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
6772 MonoClass *klass = mono_class_from_mono_type_internal (type);
6773 mono_class_init_checked (klass, error);
6774 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
6776 check_for_invalid_byref_or_pointer_type (klass, error);
6777 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
6779 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
6780 return mono_type_get_object_handle (domain, m_class_get_this_arg (klass), error);
6783 MonoReflectionTypeHandle
6784 ves_icall_RuntimeType_MakePointerType (MonoReflectionTypeHandle ref_type, MonoError *error)
6786 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
6787 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
6788 MonoClass *klass = mono_class_from_mono_type_internal (type);
6789 mono_class_init_checked (klass, error);
6790 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
6792 check_for_invalid_byref_or_pointer_type (klass, error);
6793 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
6795 MonoClass *pklass = mono_class_create_ptr (type);
6797 return mono_type_get_object_handle (domain, m_class_get_byval_arg (pklass), error);
6800 MonoObjectHandle
6801 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionTypeHandle ref_type, MonoObjectHandle target,
6802 MonoReflectionMethodHandle info, MonoBoolean throwOnBindFailure, MonoError *error)
6804 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
6805 MonoClass *delegate_class = mono_class_from_mono_type_internal (type);
6806 MonoMethod *method = MONO_HANDLE_GETVAL (info, method);
6807 MonoMethodSignature *sig = mono_method_signature_internal (method);
6809 mono_class_init_checked (delegate_class, error);
6810 return_val_if_nok (error, NULL_HANDLE);
6812 if (!(m_class_get_parent (delegate_class) == mono_defaults.multicastdelegate_class)) {
6813 /* FIXME improve this exception message */
6814 mono_error_set_execution_engine (error, "file %s: line %d (%s): assertion failed: (%s)", __FILE__, __LINE__,
6815 __func__,
6816 "delegate_class->parent == mono_defaults.multicastdelegate_class");
6817 return NULL_HANDLE;
6820 if (mono_security_core_clr_enabled ()) {
6821 ERROR_DECL (security_error);
6822 if (!mono_security_core_clr_ensure_delegate_creation (method, security_error)) {
6823 if (throwOnBindFailure)
6824 mono_error_move (error, security_error);
6825 else
6826 mono_error_cleanup (security_error);
6827 return NULL_HANDLE;
6831 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
6832 if (!method->is_inflated) {
6833 mono_error_set_argument (error, "method", " Cannot bind to the target method because its signature differs from that of the delegate type");
6834 return NULL_HANDLE;
6838 MonoObjectHandle delegate = mono_object_new_handle (MONO_HANDLE_DOMAIN (ref_type), delegate_class, error);
6839 return_val_if_nok (error, NULL_HANDLE);
6841 if (!method_is_dynamic (method) && (!MONO_HANDLE_IS_NULL (target) && method->flags & METHOD_ATTRIBUTE_VIRTUAL && method->klass != mono_handle_class (target))) {
6842 method = mono_object_handle_get_virtual_method (target, method, error);
6843 return_val_if_nok (error, NULL_HANDLE);
6846 mono_delegate_ctor (delegate, target, NULL, method, error);
6847 return_val_if_nok (error, NULL_HANDLE);
6848 return delegate;
6851 MonoMulticastDelegateHandle
6852 ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegateHandle delegate, MonoError *error)
6854 MonoClass *klass = mono_handle_class (delegate);
6855 g_assert (mono_class_has_parent (klass, mono_defaults.multicastdelegate_class));
6857 MonoMulticastDelegateHandle ret = MONO_HANDLE_CAST (MonoMulticastDelegate, mono_object_new_handle (MONO_HANDLE_DOMAIN (delegate), klass, error));
6858 return_val_if_nok (error, MONO_HANDLE_CAST (MonoMulticastDelegate, NULL_HANDLE));
6860 MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoDelegate, ret), invoke_impl, gpointer, mono_runtime_create_delegate_trampoline (klass));
6862 return ret;
6865 MonoReflectionMethodHandle
6866 ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegateHandle delegate, MonoError *error)
6868 MonoObjectHandle delegate_target = MONO_HANDLE_NEW_GET (MonoObject, delegate, target);
6869 MonoMethod *m = mono_object_handle_get_virtual_method (delegate_target, MONO_HANDLE_GETVAL (delegate, method), error);
6870 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
6871 return mono_method_get_object_handle (mono_domain_get (), m, m->klass, error);
6874 /* System.Buffer */
6876 static gint32
6877 mono_array_get_byte_length (MonoArrayHandle array)
6879 int length;
6881 MonoClass * const klass = mono_handle_class (array);
6883 // This resembles mono_array_get_length, but adds the loop.
6885 if (mono_handle_array_has_bounds (array)) {
6886 length = 1;
6887 const int klass_rank = m_class_get_rank (klass);
6888 for (int i = 0; i < klass_rank; ++ i)
6889 length *= MONO_HANDLE_GETVAL (array, bounds [i].length);
6890 } else {
6891 length = mono_array_handle_length (array);
6894 switch (m_class_get_byval_arg (m_class_get_element_class (klass))->type) {
6895 case MONO_TYPE_I1:
6896 case MONO_TYPE_U1:
6897 case MONO_TYPE_BOOLEAN:
6898 return length;
6899 case MONO_TYPE_I2:
6900 case MONO_TYPE_U2:
6901 case MONO_TYPE_CHAR:
6902 return length << 1;
6903 case MONO_TYPE_I4:
6904 case MONO_TYPE_U4:
6905 case MONO_TYPE_R4:
6906 return length << 2;
6907 case MONO_TYPE_I:
6908 case MONO_TYPE_U:
6909 return length * sizeof (gpointer);
6910 case MONO_TYPE_I8:
6911 case MONO_TYPE_U8:
6912 case MONO_TYPE_R8:
6913 return length << 3;
6914 default:
6915 return -1;
6919 gint32
6920 ves_icall_System_Buffer_ByteLengthInternal (MonoArrayHandle array, MonoError* error)
6922 return mono_array_get_byte_length (array);
6925 void
6926 ves_icall_System_Buffer_MemcpyInternal (gpointer dest, gconstpointer src, gint32 count)
6928 memcpy (dest, src, count);
6931 MonoBoolean
6932 ves_icall_System_Buffer_BlockCopyInternal (MonoArrayHandle src, gint32 src_offset, MonoArrayHandle dest, gint32 dest_offset, gint32 count, MonoError* error)
6934 if (count < 0) {
6935 mono_error_set_argument (error, "count", "is negative");
6936 return FALSE;
6939 /* This is called directly from the class libraries without going through the managed wrapper */
6940 MONO_CHECK_ARG_NULL_HANDLE (src, FALSE);
6941 MONO_CHECK_ARG_NULL_HANDLE (dest, FALSE);
6943 /* watch out for integer overflow */
6944 if ((src_offset > mono_array_get_byte_length (src) - count) || (dest_offset > mono_array_get_byte_length (dest) - count))
6945 return FALSE;
6947 MONO_ENTER_NO_SAFEPOINTS;
6949 guint8 const * const src_buf = (guint8*)MONO_HANDLE_RAW (src)->vector + src_offset;
6950 guint8* const dest_buf = (guint8*)MONO_HANDLE_RAW (dest)->vector + dest_offset;
6952 #if !HOST_WIN32
6954 // Windows memcpy is memmove and checks for overlap anyway, so skip
6955 // the check here that would not help.
6957 if (MONO_HANDLE_RAW (src) != MONO_HANDLE_RAW (dest))
6958 memcpy (dest_buf, src_buf, count);
6959 else
6960 #endif
6961 memmove (dest_buf, src_buf, count); /* Source and dest are the same array */
6963 MONO_EXIT_NO_SAFEPOINTS;
6965 return TRUE;
6968 #ifndef DISABLE_REMOTING
6969 MonoObjectHandle
6970 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObjectHandle this_obj, MonoStringHandle class_name, MonoError *error)
6972 MonoDomain *domain = MONO_HANDLE_DOMAIN (this_obj);
6973 MonoRealProxyHandle rp = MONO_HANDLE_CAST (MonoRealProxy, this_obj);
6975 MonoObjectHandle res = mono_object_new_handle (domain, mono_defaults.transparent_proxy_class, error);
6976 return_val_if_nok (error, NULL_HANDLE);
6978 MonoTransparentProxyHandle tp = MONO_HANDLE_CAST (MonoTransparentProxy, res);
6980 MONO_HANDLE_SET (tp, rp, rp);
6982 MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
6983 MONO_HANDLE_GET (reftype, rp, class_to_proxy);
6984 MonoType *type = MONO_HANDLE_GETVAL (reftype, type);
6985 MonoClass *klass = mono_class_from_mono_type_internal (type);
6987 // mono_remote_class_vtable cannot handle errors well, so force any loading error to occur early
6988 mono_class_setup_vtable (klass);
6989 if (mono_class_has_failure (klass)) {
6990 mono_error_set_for_class_failure (error, klass);
6991 return NULL_HANDLE;
6994 MonoObjectHandle remoting_obj = mono_object_handle_isinst (this_obj, mono_defaults.iremotingtypeinfo_class, error);
6995 return_val_if_nok (error, NULL_HANDLE);
6996 MONO_HANDLE_SETVAL (tp, custom_type_info, MonoBoolean, !MONO_HANDLE_IS_NULL (remoting_obj));
6998 MonoRemoteClass *remote_class = (MonoRemoteClass*)mono_remote_class (domain, class_name, klass, error);
6999 return_val_if_nok (error, NULL_HANDLE);
7000 MONO_HANDLE_SETVAL (tp, remote_class, MonoRemoteClass*, remote_class);
7002 MONO_HANDLE_SETVAL (res, vtable, MonoVTable*, (MonoVTable*)mono_remote_class_vtable (domain, remote_class, rp, error));
7003 return_val_if_nok (error, NULL_HANDLE);
7004 return res;
7007 MonoReflectionTypeHandle
7008 ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxyHandle tp, MonoError *error)
7010 MonoRemoteClass *remote_class;
7012 g_assert (mono_handle_class (tp) == mono_defaults.transparent_proxy_class);
7013 remote_class = MONO_HANDLE_RAW (tp)->remote_class;
7014 g_assert (remote_class != NULL && remote_class->proxy_class != NULL);
7015 return mono_type_get_object_handle (mono_handle_domain (tp), m_class_get_byval_arg (remote_class->proxy_class), error);
7017 #endif
7019 /* System.Environment */
7021 MonoStringHandle
7022 ves_icall_System_Environment_get_UserName (MonoError *error)
7024 /* using glib is more portable */
7025 const gchar *user_name = g_get_user_name ();
7026 if (user_name != NULL)
7027 return mono_string_new_handle (mono_domain_get (), user_name, error);
7028 else
7029 return NULL_HANDLE_STRING;
7032 #ifndef HOST_WIN32
7033 static MonoStringHandle
7034 mono_icall_get_machine_name (MonoError *error)
7036 #if !defined(DISABLE_SOCKETS)
7037 MonoStringHandle result;
7038 char *buf;
7039 int n;
7040 #if defined _SC_HOST_NAME_MAX
7041 n = sysconf (_SC_HOST_NAME_MAX);
7042 if (n == -1)
7043 #endif
7044 n = 512;
7045 buf = (char*)g_malloc (n + 1);
7047 #if defined(HAVE_GETHOSTNAME)
7048 if (gethostname (buf, n) == 0){
7049 buf [n] = 0;
7050 int i;
7051 // try truncating the string at the first dot
7052 for (i = 0; i < n; i++) {
7053 if (buf [i] == '.') {
7054 buf [i] = 0;
7055 break;
7058 result = mono_string_new_handle (mono_domain_get (), buf, error);
7059 } else
7060 #endif
7061 result = MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
7063 g_free (buf);
7065 return result;
7066 #else
7067 return mono_string_new_handle (mono_domain_get (), "mono", error);
7068 #endif
7070 #endif /* !HOST_WIN32 */
7072 MonoStringHandle
7073 ves_icall_System_Environment_get_MachineName (MonoError *error)
7075 return mono_icall_get_machine_name (error);
7078 #ifndef HOST_WIN32
7079 static int
7080 mono_icall_get_platform (void)
7082 #if defined(__MACH__)
7083 /* OSX */
7085 // Notice that the value is hidden from user code, and only exposed
7086 // to mscorlib. This is due to Mono's Unix/MacOS code predating the
7087 // define and making assumptions based on Unix/128/4 values before there
7088 // was a MacOS define. Lots of code would assume that not-Unix meant
7089 // Windows, but in this case, it would be OSX.
7091 return 6;
7092 #else
7093 /* Unix */
7094 return 4;
7095 #endif
7097 #endif /* !HOST_WIN32 */
7100 ves_icall_System_Environment_get_Platform (void)
7102 return mono_icall_get_platform ();
7105 #ifndef HOST_WIN32
7106 static MonoStringHandle
7107 mono_icall_get_new_line (MonoError *error)
7109 return mono_string_new_handle (mono_domain_get (), "\n", error);
7111 #endif /* !HOST_WIN32 */
7113 MonoStringHandle
7114 ves_icall_System_Environment_get_NewLine (MonoError *error)
7116 return mono_icall_get_new_line (error);
7119 #ifndef HOST_WIN32
7120 static inline MonoBoolean
7121 mono_icall_is_64bit_os (void)
7123 #if SIZEOF_VOID_P == 8
7124 return TRUE;
7125 #else
7126 #if defined(HAVE_SYS_UTSNAME_H)
7127 struct utsname name;
7129 if (uname (&name) >= 0) {
7130 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;
7132 #endif
7133 return FALSE;
7134 #endif
7136 #endif /* !HOST_WIN32 */
7138 MonoBoolean
7139 ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
7141 return mono_icall_is_64bit_os ();
7144 MonoStringHandle
7145 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
7147 gchar *value;
7149 if (utf8_name == NULL)
7150 return NULL_HANDLE_STRING;
7152 value = g_getenv (utf8_name);
7154 if (value == 0)
7155 return NULL_HANDLE_STRING;
7157 MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
7158 g_free (value);
7159 return res;
7163 * There is no standard way to get at environ.
7165 #ifndef _MSC_VER
7166 #ifndef __MINGW32_VERSION
7167 #if defined(__APPLE__)
7168 #if defined (TARGET_OSX)
7169 /* Apple defines this in crt_externs.h but doesn't provide that header for
7170 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
7171 * in fact exist on all implementations (so far)
7173 G_BEGIN_DECLS
7174 gchar ***_NSGetEnviron(void);
7175 G_END_DECLS
7176 #define environ (*_NSGetEnviron())
7177 #else
7178 static char *mono_environ[1] = { NULL };
7179 #define environ mono_environ
7180 #endif /* defined (TARGET_OSX) */
7181 #else
7182 G_BEGIN_DECLS
7183 extern
7184 char **environ;
7185 G_END_DECLS
7186 #endif
7187 #endif
7188 #endif
7190 MonoArrayHandle
7191 ves_icall_System_Environment_GetCommandLineArgs (MonoError *error)
7193 MonoArrayHandle result = mono_runtime_get_main_args_handle (error);
7194 return result;
7197 #ifndef HOST_WIN32
7198 static MonoArrayHandle
7199 mono_icall_get_environment_variable_names (MonoError *error)
7201 MonoArrayHandle names;
7202 MonoDomain *domain;
7203 MonoStringHandle str;
7204 gchar **e, **parts;
7205 int n;
7207 n = 0;
7208 for (e = environ; *e != 0; ++ e)
7209 ++ n;
7211 domain = mono_domain_get ();
7212 names = mono_array_new_handle (domain, mono_defaults.string_class, n, error);
7213 return_val_if_nok (error, NULL_HANDLE_ARRAY);
7215 str = MONO_HANDLE_NEW (MonoString, NULL);
7216 n = 0;
7217 for (e = environ; *e != 0; ++ e) {
7218 parts = g_strsplit (*e, "=", 2);
7219 if (*parts != 0) {
7220 MonoString *s = mono_string_new_checked (domain, *parts, error);
7221 MONO_HANDLE_ASSIGN_RAW (str, s);
7222 if (!is_ok (error)) {
7223 g_strfreev (parts);
7224 return NULL_HANDLE_ARRAY;
7226 mono_array_handle_setref (names, n, str);
7229 g_strfreev (parts);
7231 ++ n;
7234 return names;
7236 #endif /* !HOST_WIN32 */
7238 MonoArrayHandle
7239 ves_icall_System_Environment_GetEnvironmentVariableNames (MonoError *error)
7241 return mono_icall_get_environment_variable_names (error);
7244 void
7245 ves_icall_System_Environment_InternalSetEnvironmentVariable (const gunichar2 *name, gint32 name_length,
7246 const gunichar2 *value, gint32 value_length, MonoError *error)
7248 #ifdef HOST_WIN32
7249 if (!value || !value_length || !value [0])
7250 value = NULL;
7252 SetEnvironmentVariableW (name, value);
7253 #else
7254 char *utf8_name = NULL;
7255 char *utf8_value = NULL;
7257 utf8_name = mono_utf16_to_utf8 (name, name_length, error); // FIXME: this should be ascii
7258 goto_if_nok (error, exit);
7260 if (!value || !value_length || !value [0]) {
7261 g_unsetenv (utf8_name);
7262 goto exit;
7265 utf8_value = mono_utf16_to_utf8 (value, value_length, error);
7266 goto_if_nok (error, exit);
7268 g_setenv (utf8_name, utf8_value, TRUE);
7269 exit:
7270 g_free (utf8_name);
7271 g_free (utf8_value);
7272 #endif
7275 void
7276 ves_icall_System_Environment_Exit (int result)
7278 mono_environment_exitcode_set (result);
7280 if (!mono_runtime_try_shutdown ())
7281 mono_thread_exit ();
7283 /* Suspend all managed threads since the runtime is going away */
7284 mono_thread_suspend_all_other_threads ();
7286 mono_runtime_quit_internal ();
7288 /* we may need to do some cleanup here... */
7289 exit (result);
7292 void
7293 ves_icall_System_Environment_FailFast (MonoStringHandle message, MonoExceptionHandle exception, MonoStringHandle errorSource, MonoError *error)
7295 if (MONO_HANDLE_IS_NULL (message)) {
7296 g_warning ("CLR: Managed code called FailFast without specifying a reason.");
7297 } else {
7298 char *msg = mono_string_handle_to_utf8 (message, error);
7299 g_warning ("CLR: Managed code called FailFast, saying \"%s\"", msg);
7300 #ifndef DISABLE_CRASH_REPORTING
7301 char *old_msg = mono_crash_save_failfast_msg (msg);
7302 g_free (old_msg);
7303 #else
7304 g_free (msg);
7305 #endif
7308 if (!MONO_HANDLE_IS_NULL (exception)) {
7309 mono_print_unhandled_exception_internal ((MonoObject *) MONO_HANDLE_RAW (exception));
7312 // NOTE: While this does trigger WER on Windows it doesn't quite provide all the
7313 // information in the error dump that CoreCLR would. On Windows 7+ we should call
7314 // RaiseFailFastException directly instead of relying on the C runtime doing it
7315 // for us and pass it as much information as possible. On Windows 8+ we can also
7316 // use the __fastfail intrinsic.
7317 abort ();
7320 MonoStringHandle
7321 ves_icall_System_Environment_GetGacPath (MonoError *error)
7323 return mono_string_new_handle (mono_domain_get (), mono_assembly_getrootdir (), error);
7326 #ifndef HOST_WIN32
7327 static inline MonoStringHandle
7328 mono_icall_get_windows_folder_path (int folder, MonoError *error)
7330 error_init (error);
7331 g_warning ("ves_icall_System_Environment_GetWindowsFolderPath should only be called on Windows!");
7332 return mono_string_new_handle (mono_domain_get (), "", error);
7334 #endif /* !HOST_WIN32 */
7336 MonoStringHandle
7337 ves_icall_System_Environment_GetWindowsFolderPath (int folder, MonoError *error)
7339 return mono_icall_get_windows_folder_path (folder, error);
7342 static MonoArrayHandle
7343 mono_icall_get_logical_drives (MonoError *error)
7345 gunichar2 buf [256], *ptr, *dname;
7346 gunichar2 *u16;
7347 guint initial_size = 127, size = 128;
7348 gint ndrives;
7349 MonoArrayHandle result = NULL_HANDLE_ARRAY;
7350 MonoStringHandle drivestr;
7351 MonoDomain *domain = mono_domain_get ();
7352 gint len;
7354 buf [0] = '\0';
7355 ptr = buf;
7357 while (size > initial_size) {
7358 size = (guint) mono_w32file_get_logical_drive (initial_size, ptr, error);
7359 if (!is_ok (error))
7360 goto leave;
7361 if (size > initial_size) {
7362 if (ptr != buf)
7363 g_free (ptr);
7364 ptr = (gunichar2 *)g_malloc0 ((size + 1) * sizeof (gunichar2));
7365 initial_size = size;
7366 size++;
7370 /* Count strings */
7371 dname = ptr;
7372 ndrives = 0;
7373 do {
7374 while (*dname++);
7375 ndrives++;
7376 } while (*dname);
7378 dname = ptr;
7379 result = mono_array_new_handle (domain, mono_defaults.string_class, ndrives, error);
7380 goto_if_nok (error, leave);
7382 drivestr = MONO_HANDLE_NEW (MonoString, NULL);
7383 ndrives = 0;
7384 do {
7385 len = 0;
7386 u16 = dname;
7387 while (*u16) {
7388 u16++; len ++;
7390 MonoString *s = mono_string_new_utf16_checked (domain, dname, len, error);
7391 goto_if_nok (error, leave);
7392 MONO_HANDLE_ASSIGN_RAW (drivestr, s);
7394 mono_array_handle_setref (result, ndrives, drivestr);
7395 ndrives ++;
7396 while (*dname++);
7397 } while (*dname);
7399 leave:
7400 if (ptr != buf)
7401 g_free (ptr);
7403 return result;
7406 MonoArrayHandle
7407 ves_icall_System_Environment_GetLogicalDrivesInternal (MonoError *error)
7409 return mono_icall_get_logical_drives (error);
7412 guint32
7413 ves_icall_System_IO_DriveInfo_GetDriveType (const gunichar2 *root_path_name, gint32 root_path_name_length, MonoError *error)
7415 return mono_w32file_get_drive_type (root_path_name, root_path_name_length, error);
7418 MonoStringHandle
7419 ves_icall_System_IO_DriveInfo_GetDriveFormat (const gunichar2 *path, gint32 path_length, MonoError *error)
7421 gunichar2 volume_name [MAX_PATH + 1];
7423 if (mono_w32file_get_file_system_type (path, volume_name, MAX_PATH + 1) == FALSE)
7424 return NULL_HANDLE_STRING;
7425 return mono_string_new_utf16_handle (mono_domain_get (), volume_name, g_utf16_len (volume_name), error);
7428 MonoStringHandle
7429 ves_icall_System_Environment_InternalGetHome (MonoError *error)
7431 const gchar *home_dir = g_get_home_dir ();
7432 if (home_dir != NULL)
7433 return mono_string_new_handle (mono_domain_get (), home_dir, error);
7434 else
7435 return NULL_HANDLE_STRING;
7438 static const char * const encodings [] = {
7439 (char *) 1,
7440 "ascii", "us_ascii", "us", "ansi_x3.4_1968",
7441 "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
7442 "iso_ir_6", "iso646_us", "iso_646.irv:1991",
7443 (char *) 2,
7444 "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
7445 "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
7446 "x_unicode_2_0_utf_7",
7447 (char *) 3,
7448 "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
7449 "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
7450 (char *) 4,
7451 "utf_16", "UTF_16LE", "ucs_2", "unicode",
7452 "iso_10646_ucs2",
7453 (char *) 5,
7454 "unicodefffe", "utf_16be",
7455 (char *) 6,
7456 "iso_8859_1",
7457 (char *) 0
7461 * Returns the internal codepage, if the value of "int_code_page" is
7462 * 1 at entry, and we can not compute a suitable code page number,
7463 * returns the code page as a string
7465 MonoStringHandle
7466 ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page, MonoError *error)
7468 error_init (error);
7469 const char *cset;
7470 const char *p;
7471 char *c;
7472 char *codepage = NULL;
7473 int code;
7474 int want_name = *int_code_page;
7475 int i;
7477 *int_code_page = -1;
7479 g_get_charset (&cset);
7480 c = codepage = g_strdup (cset);
7481 for (c = codepage; *c; c++){
7482 if (isascii (*c) && isalpha (*c))
7483 *c = tolower (*c);
7484 if (*c == '-')
7485 *c = '_';
7487 /* g_print ("charset: %s\n", cset); */
7489 /* handle some common aliases */
7490 p = encodings [0];
7491 code = 0;
7492 for (i = 0; p != 0; ){
7493 if ((gsize) p < 7){
7494 code = (gssize) p;
7495 p = encodings [++i];
7496 continue;
7498 if (strcmp (p, codepage) == 0){
7499 *int_code_page = code;
7500 break;
7502 p = encodings [++i];
7505 if (strstr (codepage, "utf_8") != NULL)
7506 *int_code_page |= 0x10000000;
7507 g_free (codepage);
7509 if (want_name && *int_code_page == -1)
7510 return mono_string_new_handle (mono_domain_get (), cset, error);
7511 return NULL_HANDLE_STRING;
7514 MonoBoolean
7515 ves_icall_System_Environment_get_HasShutdownStarted (void)
7517 return mono_runtime_is_shutting_down () || mono_domain_is_unloading (mono_domain_get ());
7520 #ifndef HOST_WIN32
7522 void
7523 ves_icall_System_Environment_BroadcastSettingChange (MonoError *error)
7527 #endif
7529 gint32
7530 ves_icall_System_Environment_get_TickCount (void)
7532 /* this will overflow after ~24 days */
7533 return (gint32) (mono_msec_boottime () & 0xffffffff);
7537 gint32
7538 ves_icall_System_Runtime_Versioning_VersioningHelper_GetRuntimeId (MonoError *error)
7540 return 9;
7543 #ifndef DISABLE_REMOTING
7544 MonoBoolean
7545 ves_icall_IsTransparentProxy (MonoObjectHandle proxy, MonoError *error)
7547 if (MONO_HANDLE_IS_NULL (proxy))
7548 return 0;
7550 if (mono_class_is_transparent_proxy (mono_handle_class (proxy)))
7551 return 1;
7553 return 0;
7556 MonoReflectionMethodHandle
7557 ves_icall_Remoting_RemotingServices_GetVirtualMethod (
7558 MonoReflectionTypeHandle rtype, MonoReflectionMethodHandle rmethod, MonoError *error)
7560 MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
7562 if (MONO_HANDLE_IS_NULL (rtype)) {
7563 mono_error_set_argument_null (error, "type", "");
7564 return ret;
7566 if (MONO_HANDLE_IS_NULL (rmethod)) {
7567 mono_error_set_argument_null (error, "method", "");
7568 return ret;
7571 MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
7572 MonoType *type = MONO_HANDLE_GETVAL (rtype, type);
7573 MonoClass *klass = mono_class_from_mono_type_internal (type);
7574 mono_class_init_checked (klass, error);
7575 return_val_if_nok (error, ret);
7577 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass))
7578 return ret;
7580 if (method->flags & METHOD_ATTRIBUTE_STATIC)
7581 return ret;
7583 if ((method->flags & METHOD_ATTRIBUTE_FINAL) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
7584 if (klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE))
7585 ret = rmethod;
7586 return ret;
7589 mono_class_setup_vtable (klass);
7590 MonoMethod **vtable = m_class_get_vtable (klass);
7592 MonoMethod *res = NULL;
7593 if (mono_class_is_interface (method->klass)) {
7594 gboolean variance_used = FALSE;
7595 /*MS fails with variant interfaces but it's the right thing to do anyway.*/
7596 int offs = mono_class_interface_offset_with_variance (klass, method->klass, &variance_used);
7597 if (offs >= 0)
7598 res = vtable [offs + method->slot];
7599 } else {
7600 if (!(klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE)))
7601 return ret;
7603 if (method->slot != -1)
7604 res = vtable [method->slot];
7607 if (!res)
7608 return ret;
7610 ret = mono_method_get_object_handle (mono_domain_get (), res, NULL, error);
7611 return ret;
7614 void
7615 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
7617 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
7618 MonoVTable *vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
7619 return_if_nok (error);
7621 mono_vtable_set_is_remote (vtable, enable);
7624 #else /* DISABLE_REMOTING */
7626 void
7627 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
7629 g_assert_not_reached ();
7632 #endif
7634 MonoObjectHandle
7635 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionTypeHandle type, MonoError *error)
7637 MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
7638 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
7639 mono_class_init_checked (klass, error);
7640 return_val_if_nok (error, NULL_HANDLE);
7642 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass) || mono_class_is_abstract (klass)) {
7643 mono_error_set_argument (error, "type", "Type cannot be instantiated");
7644 return NULL_HANDLE;
7647 if (m_class_get_rank (klass) >= 1) {
7648 g_assert (m_class_get_rank (klass) == 1);
7649 return MONO_HANDLE_CAST (MonoObject, mono_array_new_handle (domain, m_class_get_element_class (klass), 0, error));
7650 } else {
7651 MonoVTable *vtable = mono_class_vtable_checked (domain, klass, error);
7652 return_val_if_nok (error, NULL_HANDLE);
7654 /* Bypass remoting object creation check */
7655 return MONO_HANDLE_NEW (MonoObject, mono_object_new_alloc_specific_checked (vtable, error));
7659 MonoStringHandle
7660 ves_icall_System_IO_get_temp_path (MonoError *error)
7662 return mono_string_new_handle (mono_domain_get (), g_get_tmp_dir (), error);
7665 #if defined(ENABLE_MONODROID) || defined(ENABLE_MONOTOUCH) || defined(TARGET_WASM)
7667 // FIXME? Names should start "mono"?
7668 G_EXTERN_C gpointer CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data);
7669 G_EXTERN_C gint32 CloseZStream (gpointer stream);
7670 G_EXTERN_C gint32 Flush (gpointer stream);
7671 G_EXTERN_C gint32 ReadZStream (gpointer stream, gpointer buffer, gint32 length);
7672 G_EXTERN_C gint32 WriteZStream (gpointer stream, gpointer buffer, gint32 length);
7674 gpointer
7675 ves_icall_System_IO_Compression_DeflateStreamNative_CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data)
7677 #ifdef MONO_CROSS_COMPILE
7678 return NULL;
7679 #else
7680 return CreateZStream (compress, gzip, feeder, data);
7681 #endif
7684 gint32
7685 ves_icall_System_IO_Compression_DeflateStreamNative_CloseZStream (gpointer stream)
7687 #ifdef MONO_CROSS_COMPILE
7688 return 0;
7689 #else
7690 return CloseZStream (stream);
7691 #endif
7694 gint32
7695 ves_icall_System_IO_Compression_DeflateStreamNative_Flush (gpointer stream)
7697 #ifdef MONO_CROSS_COMPILE
7698 return 0;
7699 #else
7700 return Flush (stream);
7701 #endif
7704 gint32
7705 ves_icall_System_IO_Compression_DeflateStreamNative_ReadZStream (gpointer stream, gpointer buffer, gint32 length)
7707 #ifdef MONO_CROSS_COMPILE
7708 return 0;
7709 #else
7710 return ReadZStream (stream, buffer, length);
7711 #endif
7714 gint32
7715 ves_icall_System_IO_Compression_DeflateStreamNative_WriteZStream (gpointer stream, gpointer buffer, gint32 length)
7717 #ifdef MONO_CROSS_COMPILE
7718 return 0;
7719 #else
7720 return WriteZStream (stream, buffer, length);
7721 #endif
7724 #endif
7726 #if defined(TARGET_WASM)
7727 G_EXTERN_C void mono_timezone_get_local_name (MonoString **result);
7728 void
7729 ves_icall_System_TimeZoneInfo_mono_timezone_get_local_name (MonoString **result)
7731 // MONO_CROSS_COMPILE returns undefined symbol "_mono_timezone_get_local_name"
7732 // The icall offsets will be generated and linked at build time
7733 // This is defined outside the runtime within the webassembly sdk
7734 #ifndef MONO_CROSS_COMPILE
7735 return mono_timezone_get_local_name (result);
7736 #endif
7738 #endif
7741 #ifndef PLATFORM_NO_DRIVEINFO
7742 MonoBoolean
7743 ves_icall_System_IO_DriveInfo_GetDiskFreeSpace (const gunichar2 *path_name, gint32 path_name_length, guint64 *free_bytes_avail,
7744 guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes,
7745 gint32 *error)
7747 g_assert (error);
7748 g_assert (free_bytes_avail);
7749 g_assert (total_number_of_bytes);
7750 g_assert (total_number_of_free_bytes);
7752 // FIXME check for embedded nuls here or managed
7754 *error = ERROR_SUCCESS;
7755 *free_bytes_avail = (guint64)-1;
7756 *total_number_of_bytes = (guint64)-1;
7757 *total_number_of_free_bytes = (guint64)-1;
7759 gboolean result = mono_w32file_get_disk_free_space (path_name, free_bytes_avail, total_number_of_bytes, total_number_of_free_bytes);
7760 if (!result)
7761 *error = mono_w32error_get_last ();
7763 return result;
7765 #endif /* PLATFORM_NO_DRIVEINFO */
7767 gpointer
7768 ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method, MonoError *error)
7770 return mono_compile_method_checked (method, error);
7774 MonoStringHandle
7775 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (MonoError *error)
7777 gchar *path;
7779 const char *mono_cfg_dir = mono_get_config_dir ();
7780 if (!mono_cfg_dir)
7781 return mono_string_new_handle (mono_domain_get (), "", error);
7783 path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", mono_get_runtime_info ()->framework_version, "machine.config", (const char*)NULL);
7785 mono_icall_make_platform_path (path);
7787 MonoStringHandle mcpath = mono_string_new_handle (mono_domain_get (), path, error);
7788 g_free (path);
7790 mono_error_assert_ok (error);
7792 return mcpath;
7795 MonoStringHandle
7796 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_app_config (MonoError *error)
7798 const gchar *app_config;
7799 MonoDomain *domain;
7800 gchar *config_file_name, *config_file_path;
7801 gsize len, config_file_path_length, config_ext_length;
7802 gchar *module;
7804 domain = mono_domain_get ();
7805 MonoStringHandle file = MONO_HANDLE_NEW (MonoString, domain->setup->configuration_file);
7806 if (MONO_HANDLE_IS_NULL (file) || MONO_HANDLE_GETVAL (file, length) == 0)
7807 return MONO_HANDLE_CAST (MonoString, mono_new_null ());
7809 // Retrieve config file and remove the extension
7810 config_file_name = mono_string_handle_to_utf8 (file, error);
7811 return_val_if_nok (error, MONO_HANDLE_CAST (MonoString, NULL_HANDLE));
7813 config_file_path = mono_portability_find_file (config_file_name, TRUE);
7814 if (!config_file_path)
7815 config_file_path = config_file_name;
7817 config_file_path_length = strlen (config_file_path);
7818 config_ext_length = strlen (".config");
7819 if (config_file_path_length <= config_ext_length) {
7820 if (config_file_name != config_file_path)
7821 g_free (config_file_name);
7822 return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
7825 len = config_file_path_length - config_ext_length;
7826 module = (gchar *)g_malloc0 (len + 1);
7827 memcpy (module, config_file_path, len);
7828 // Get the config file from the module name
7829 app_config = mono_config_string_for_assembly_file (module);
7830 // Clean-up
7831 g_free (module);
7832 if (config_file_name != config_file_path)
7833 g_free (config_file_name);
7834 g_free (config_file_path);
7836 if (!app_config)
7837 return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
7839 return mono_string_new_handle (mono_domain_get (), app_config, error);
7842 static MonoStringHandle
7843 get_bundled_machine_config (MonoError *error)
7845 const gchar *machine_config;
7847 machine_config = mono_get_machine_config ();
7849 if (!machine_config)
7850 return NULL_HANDLE_STRING;
7852 return mono_string_new_handle (mono_domain_get (), machine_config, error);
7855 MonoStringHandle
7856 ves_icall_System_Environment_get_bundled_machine_config (MonoError *error)
7858 return get_bundled_machine_config (error);
7861 MonoStringHandle
7862 ves_icall_System_Configuration_DefaultConfig_get_bundled_machine_config (MonoError *error)
7864 return get_bundled_machine_config (error);
7867 MonoStringHandle
7868 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_machine_config (MonoError *error)
7870 return get_bundled_machine_config (error);
7873 MonoStringHandle
7874 ves_icall_System_Web_Util_ICalls_get_machine_install_dir (MonoError *error)
7876 const char *mono_cfg_dir = mono_get_config_dir ();
7877 if (!mono_cfg_dir)
7878 return mono_string_new_handle (mono_domain_get (), "", error);
7880 char *path = g_path_get_dirname (mono_cfg_dir);
7882 mono_icall_make_platform_path (path);
7884 MonoStringHandle ipath = mono_string_new_handle (mono_domain_get (), path, error);
7885 g_free (path);
7887 return ipath;
7890 MonoBoolean
7891 ves_icall_get_resources_ptr (MonoReflectionAssemblyHandle assembly, gpointer *result, gint32 *size, MonoError *error)
7893 MonoPEResourceDataEntry *entry;
7894 MonoImage *image;
7896 if (MONO_HANDLE_IS_NULL (assembly) || !result || !size)
7897 return FALSE;
7899 *result = NULL;
7900 *size = 0;
7901 MonoAssembly *assm = MONO_HANDLE_GETVAL (assembly, assembly);
7902 image = assm->image;
7903 entry = (MonoPEResourceDataEntry *)mono_image_lookup_resource (image, MONO_PE_RESOURCE_ID_ASPNET_STRING, 0, NULL);
7904 if (!entry)
7905 return FALSE;
7907 *result = mono_image_rva_map (image, entry->rde_data_offset);
7908 if (!(*result)) {
7909 g_free (entry);
7910 return FALSE;
7912 *size = entry->rde_size;
7913 g_free (entry);
7914 return TRUE;
7918 MonoBoolean
7919 ves_icall_System_Diagnostics_Debugger_IsAttached_internal (void)
7921 return mono_is_debugger_attached ();
7924 MonoBoolean
7925 ves_icall_System_Diagnostics_Debugger_IsLogging (void)
7927 return mono_get_runtime_callbacks ()->debug_log_is_enabled
7928 && mono_get_runtime_callbacks ()->debug_log_is_enabled ();
7931 void
7932 ves_icall_System_Diagnostics_Debugger_Log (int level, MonoString *volatile* category, MonoString *volatile* message)
7934 if (mono_get_runtime_callbacks ()->debug_log)
7935 mono_get_runtime_callbacks ()->debug_log (level, *category, *message);
7938 #ifndef HOST_WIN32
7939 static inline void
7940 mono_icall_write_windows_debug_string (const gunichar2 *message)
7942 g_warning ("WriteWindowsDebugString called and HOST_WIN32 not defined!\n");
7944 #endif /* !HOST_WIN32 */
7946 void
7947 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (const gunichar2 *message, MonoError *error)
7949 mono_icall_write_windows_debug_string (message);
7952 /* Only used for value types */
7953 MonoObjectHandle
7954 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
7956 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
7957 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
7958 MonoClass *klass = mono_class_from_mono_type_internal (type);
7959 (void)klass;
7961 mono_class_init_checked (klass, error);
7962 return_val_if_nok (error, NULL_HANDLE);
7964 if (mono_class_is_nullable (klass))
7965 /* No arguments -> null */
7966 return NULL_HANDLE;
7968 return mono_object_new_handle (domain, klass, error);
7971 MonoReflectionMethodHandle
7972 ves_icall_RuntimeMethodInfo_get_base_method (MonoReflectionMethodHandle m, MonoBoolean definition, MonoError *error)
7974 MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
7976 MonoMethod *base = mono_method_get_base_method (method, definition, error);
7977 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
7978 if (base == method) {
7979 /* we want to short-circuit and return 'm' here. But we should
7980 return the same method object that
7981 mono_method_get_object_handle, below would return. Since
7982 that call takes NULL for the reftype argument, it will take
7983 base->klass as the reflected type for the MonoMethod. So we
7984 need to check that m also has base->klass as the reflected
7985 type. */
7986 MonoReflectionTypeHandle orig_reftype = MONO_HANDLE_NEW_GET (MonoReflectionType, m, reftype);
7987 MonoClass *orig_klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (orig_reftype, type));
7988 if (base->klass == orig_klass)
7989 return m;
7991 return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
7994 MonoStringHandle
7995 ves_icall_RuntimeMethodInfo_get_name (MonoReflectionMethodHandle m, MonoError *error)
7997 MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
7999 MonoStringHandle s = mono_string_new_handle (MONO_HANDLE_DOMAIN (m), method->name, error);
8000 return_val_if_nok (error, NULL_HANDLE_STRING);
8001 MONO_HANDLE_SET (m, name, s);
8002 return s;
8005 void
8006 ves_icall_System_ArgIterator_Setup (MonoArgIterator *iter, char* argsp, char* start)
8008 iter->sig = *(MonoMethodSignature**)argsp;
8010 g_assert (iter->sig->sentinelpos <= iter->sig->param_count);
8011 g_assert (iter->sig->call_convention == MONO_CALL_VARARG);
8013 iter->next_arg = 0;
8014 /* FIXME: it's not documented what start is exactly... */
8015 if (start) {
8016 iter->args = start;
8017 } else {
8018 iter->args = argsp + sizeof (gpointer);
8020 iter->num_args = iter->sig->param_count - iter->sig->sentinelpos;
8022 /* g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos); */
8025 void
8026 ves_icall_System_ArgIterator_IntGetNextArg (MonoArgIterator *iter, MonoTypedRef *res)
8028 guint32 i, arg_size;
8029 gint32 align;
8031 i = iter->sig->sentinelpos + iter->next_arg;
8033 g_assert (i < iter->sig->param_count);
8035 res->type = iter->sig->params [i];
8036 res->klass = mono_class_from_mono_type_internal (res->type);
8037 arg_size = mono_type_stack_size (res->type, &align);
8038 #if defined(__arm__) || defined(__mips__)
8039 iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
8040 #endif
8041 res->value = iter->args;
8042 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
8043 if (arg_size <= sizeof (gpointer)) {
8044 int dummy;
8045 int padding = arg_size - mono_type_size (res->type, &dummy);
8046 res->value = (guint8*)res->value + padding;
8048 #endif
8049 iter->args = (char*)iter->args + arg_size;
8050 iter->next_arg++;
8052 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res->type->type, arg_size, res->value); */
8055 void
8056 ves_icall_System_ArgIterator_IntGetNextArgWithType (MonoArgIterator *iter, MonoTypedRef *res, MonoType *type)
8058 guint32 i, arg_size;
8059 gint32 align;
8061 i = iter->sig->sentinelpos + iter->next_arg;
8063 g_assert (i < iter->sig->param_count);
8065 while (i < iter->sig->param_count) {
8066 if (!mono_metadata_type_equal (type, iter->sig->params [i]))
8067 continue;
8068 res->type = iter->sig->params [i];
8069 res->klass = mono_class_from_mono_type_internal (res->type);
8070 /* FIXME: endianess issue... */
8071 arg_size = mono_type_stack_size (res->type, &align);
8072 #if defined(__arm__) || defined(__mips__)
8073 iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
8074 #endif
8075 res->value = iter->args;
8076 iter->args = (char*)iter->args + arg_size;
8077 iter->next_arg++;
8078 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value); */
8079 return;
8081 /* g_print ("arg type 0x%02x not found\n", res.type->type); */
8083 memset (res, 0, sizeof (MonoTypedRef));
8086 MonoType*
8087 ves_icall_System_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
8089 gint i;
8091 i = iter->sig->sentinelpos + iter->next_arg;
8093 g_assert (i < iter->sig->param_count);
8095 return iter->sig->params [i];
8098 MonoObjectHandle
8099 ves_icall_System_TypedReference_ToObject (MonoTypedRef* tref, MonoError *error)
8101 return typed_reference_to_object (tref, error);
8104 void
8105 ves_icall_System_TypedReference_InternalMakeTypedReference (MonoTypedRef *res, MonoObjectHandle target, MonoArrayHandle fields, MonoReflectionTypeHandle last_field, MonoError *error)
8107 MonoType *ftype = NULL;
8108 int i;
8110 memset (res, 0, sizeof (MonoTypedRef));
8112 g_assert (mono_array_handle_length (fields) > 0);
8114 (void)mono_handle_class (target);
8116 int offset = 0;
8117 for (i = 0; i < mono_array_handle_length (fields); ++i) {
8118 MonoClassField *f;
8119 MONO_HANDLE_ARRAY_GETVAL (f, fields, MonoClassField*, i);
8121 g_assert (f);
8123 if (i == 0)
8124 offset = f->offset;
8125 else
8126 offset += f->offset - sizeof (MonoObject);
8127 (void)mono_class_from_mono_type_internal (f->type);
8128 ftype = f->type;
8131 res->type = ftype;
8132 res->klass = mono_class_from_mono_type_internal (ftype);
8133 res->value = (guint8*)MONO_HANDLE_RAW (target) + offset;
8136 static void
8137 prelink_method (MonoMethod *method, MonoError *error)
8139 error_init (error);
8140 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8141 return;
8142 mono_lookup_pinvoke_call_internal (method, error);
8143 /* create the wrapper, too? */
8146 void
8147 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethodHandle method, MonoError *error)
8149 error_init (error);
8151 prelink_method (MONO_HANDLE_GETVAL (method, method), error);
8154 void
8155 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error);
8157 void
8158 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error)
8160 error_init (error);
8161 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
8162 MonoMethod* m;
8163 gpointer iter = NULL;
8165 mono_class_init_checked (klass, error);
8166 return_if_nok (error);
8168 while ((m = mono_class_get_methods (klass, &iter))) {
8169 prelink_method (m, error);
8170 return_if_nok (error);
8175 * used by System.Runtime.InteropServices.RuntimeInformation.(OS|Process)Architecture;
8176 * which use them in different ways for filling in an enum
8178 MonoStringHandle
8179 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetRuntimeArchitecture (MonoError *error)
8181 return mono_string_new_handle (mono_domain_get (), mono_config_get_cpu (), error);
8185 * used by System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform
8187 MonoStringHandle
8188 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetOSName (MonoError *error)
8190 return mono_string_new_handle (mono_domain_get (), mono_config_get_os (), error);
8194 ves_icall_Interop_Sys_DoubleToString(double value, char *format, char *buffer, int bufferLength)
8196 #if defined(TARGET_ARM)
8197 /* workaround for faulty vcmp.f64 implementation on some 32bit ARM CPUs */
8198 guint64 bits = *(guint64 *) &value;
8199 if (bits == 0x1) { /* 4.9406564584124654E-324 */
8200 g_assert (!strcmp (format, "%.40e"));
8201 return snprintf (buffer, bufferLength, "%s", "4.9406564584124654417656879286822137236506e-324");
8202 } else if (bits == 0x4) { /* 2E-323 */
8203 g_assert (!strcmp (format, "%.40e"));
8204 return snprintf (buffer, bufferLength, "%s", "1.9762625833649861767062751714728854894602e-323");
8206 #endif
8208 return snprintf(buffer, bufferLength, format, value);
8211 void
8212 ves_icall_System_Runtime_RuntimeImports_ecvt_s(char *buffer, size_t sizeInBytes, double value, int count, int* dec, int* sign)
8214 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
8215 _ecvt_s(buffer, sizeInBytes, value, count, dec, sign);
8216 #endif
8220 /* These parameters are "readonly" in corlib/System/NumberFormatter.cs */
8221 void
8222 ves_icall_System_NumberFormatter_GetFormatterTables (guint64 const **mantissas,
8223 gint32 const **exponents,
8224 gunichar2 const **digitLowerTable,
8225 gunichar2 const **digitUpperTable,
8226 gint64 const **tenPowersList,
8227 gint32 const **decHexDigits)
8229 *mantissas = Formatter_MantissaBitsTable;
8230 *exponents = Formatter_TensExponentTable;
8231 *digitLowerTable = Formatter_DigitLowerTable;
8232 *digitUpperTable = Formatter_DigitUpperTable;
8233 *tenPowersList = Formatter_TenPowersList;
8234 *decHexDigits = Formatter_DecHexDigits;
8237 static gboolean
8238 add_modifier_to_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int dest_idx, MonoError *error)
8240 HANDLE_FUNCTION_ENTER ();
8241 error_init (error);
8242 MonoClass *klass = mono_class_from_mono_type_internal (type);
8244 MonoReflectionTypeHandle rt;
8245 rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
8246 goto_if_nok (error, leave);
8248 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rt);
8249 leave:
8250 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
8254 * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
8255 * and avoid useless allocations.
8257 static MonoArrayHandle
8258 type_array_from_modifiers (MonoType *type, int optional, MonoError *error)
8260 int i, count = 0;
8261 MonoDomain *domain = mono_domain_get ();
8263 int cmod_count = mono_type_custom_modifier_count (type);
8264 if (cmod_count == 0)
8265 goto fail;
8267 error_init (error);
8268 for (i = 0; i < cmod_count; ++i) {
8269 gboolean required;
8270 (void) mono_type_get_custom_modifier (type, i, &required, error);
8271 goto_if_nok (error, fail);
8272 if ((optional && !required) || (!optional && required))
8273 count++;
8275 if (!count)
8276 goto fail;
8278 MonoArrayHandle res;
8279 res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
8280 goto_if_nok (error, fail);
8281 count = 0;
8282 for (i = 0; i < cmod_count; ++i) {
8283 gboolean required;
8284 MonoType *cmod_type = mono_type_get_custom_modifier (type, i, &required, error);
8285 goto_if_nok (error, fail);
8286 if ((optional && !required) || (!optional && required)) {
8287 if (!add_modifier_to_array (domain, cmod_type, res, count, error))
8288 goto fail;
8289 count++;
8292 return res;
8293 fail:
8294 return MONO_HANDLE_NEW (MonoArray, NULL);
8297 MonoArrayHandle
8298 ves_icall_RuntimeParameterInfo_GetTypeModifiers (MonoReflectionTypeHandle rt, MonoObjectHandle member, int pos, MonoBoolean optional, MonoError *error)
8300 error_init (error);
8301 MonoType *type = MONO_HANDLE_GETVAL (rt, type);
8302 MonoClass *member_class = mono_handle_class (member);
8303 MonoMethod *method = NULL;
8304 MonoImage *image;
8305 MonoMethodSignature *sig;
8307 if (mono_class_is_reflection_method_or_constructor (member_class)) {
8308 method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member), method);
8309 } else if (m_class_get_image (member_class) == mono_defaults.corlib && !strcmp ("RuntimePropertyInfo", m_class_get_name (member_class))) {
8310 MonoProperty *prop = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionProperty, member), property);
8311 if (!(method = prop->get))
8312 method = prop->set;
8313 g_assert (method);
8314 } else if (strcmp (m_class_get_name (member_class), "DynamicMethod") == 0 && strcmp (m_class_get_name_space (member_class), "System.Reflection.Emit") == 0) {
8315 MonoArrayHandle params = MONO_HANDLE_NEW_GET (MonoArray, MONO_HANDLE_CAST (MonoReflectionDynamicMethod, member), parameters);
8316 MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
8317 MONO_HANDLE_ARRAY_GETREF (t, params, pos);
8318 type = mono_reflection_type_handle_mono_type (t, error);
8319 return type_array_from_modifiers (type, optional, error);
8320 } else {
8321 char *type_name = mono_type_get_full_name (member_class);
8322 mono_error_set_not_supported (error, "Custom modifiers on a ParamInfo with member %s are not supported", type_name);
8323 g_free (type_name);
8324 return NULL_HANDLE_ARRAY;
8327 image = m_class_get_image (method->klass);
8328 sig = mono_method_signature_internal (method);
8329 if (pos == -1)
8330 type = sig->ret;
8331 else
8332 type = sig->params [pos];
8334 return type_array_from_modifiers (type, optional, error);
8337 static MonoType*
8338 get_property_type (MonoProperty *prop)
8340 MonoMethodSignature *sig;
8341 if (prop->get) {
8342 sig = mono_method_signature_internal (prop->get);
8343 return sig->ret;
8344 } else if (prop->set) {
8345 sig = mono_method_signature_internal (prop->set);
8346 return sig->params [sig->param_count - 1];
8348 return NULL;
8351 MonoArrayHandle
8352 ves_icall_RuntimePropertyInfo_GetTypeModifiers (MonoReflectionPropertyHandle property, MonoBoolean optional, MonoError *error)
8354 error_init (error);
8355 MonoProperty *prop = MONO_HANDLE_GETVAL (property, property);
8356 MonoType *type = get_property_type (prop);
8358 if (!type)
8359 return NULL_HANDLE_ARRAY;
8360 return type_array_from_modifiers (type, optional, error);
8364 *Construct a MonoType suited to be used to decode a constant blob object.
8366 * @type is the target type which will be constructed
8367 * @blob_type is the blob type, for example, that comes from the constant table
8368 * @real_type is the expected constructed type.
8370 static void
8371 mono_type_from_blob_type (MonoType *type, MonoTypeEnum blob_type, MonoType *real_type)
8373 type->type = blob_type;
8374 type->data.klass = NULL;
8375 if (blob_type == MONO_TYPE_CLASS)
8376 type->data.klass = mono_defaults.object_class;
8377 else if (real_type->type == MONO_TYPE_VALUETYPE && m_class_is_enumtype (real_type->data.klass)) {
8378 /* For enums, we need to use the base type */
8379 type->type = MONO_TYPE_VALUETYPE;
8380 type->data.klass = mono_class_from_mono_type_internal (real_type);
8381 } else
8382 type->data.klass = mono_class_from_mono_type_internal (real_type);
8385 MonoObjectHandle
8386 ves_icall_property_info_get_default_value (MonoReflectionPropertyHandle property_handle, MonoError* error)
8388 MonoReflectionProperty* property = MONO_HANDLE_RAW (property_handle);
8390 MonoType blob_type;
8391 MonoProperty *prop = property->property;
8392 MonoType *type = get_property_type (prop);
8393 MonoDomain *domain = mono_object_domain (property);
8394 MonoTypeEnum def_type;
8395 const char *def_value;
8397 mono_class_init_internal (prop->parent);
8399 if (!(prop->attrs & PROPERTY_ATTRIBUTE_HAS_DEFAULT)) {
8400 mono_error_set_invalid_operation (error, NULL);
8401 return NULL_HANDLE;
8404 def_value = mono_class_get_property_default_value (prop, &def_type);
8406 mono_type_from_blob_type (&blob_type, def_type, type);
8408 return mono_get_object_from_blob (domain, &blob_type, def_value, MONO_HANDLE_NEW (MonoString, NULL), error);
8411 MonoBoolean
8412 ves_icall_MonoCustomAttrs_IsDefinedInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoError *error)
8414 MonoClass *attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
8416 mono_class_init_checked (attr_class, error);
8417 return_val_if_nok (error, FALSE);
8419 MonoCustomAttrInfo *cinfo = mono_reflection_get_custom_attrs_info_checked (obj, error);
8420 return_val_if_nok (error, FALSE);
8422 if (!cinfo)
8423 return FALSE;
8424 gboolean found = mono_custom_attrs_has_attr (cinfo, attr_class);
8425 if (!cinfo->cached)
8426 mono_custom_attrs_free (cinfo);
8427 return found;
8430 MonoArrayHandle
8431 ves_icall_MonoCustomAttrs_GetCustomAttributesInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoBoolean pseudoattrs, MonoError *error)
8433 MonoClass *attr_class;
8434 if (MONO_HANDLE_IS_NULL (attr_type))
8435 attr_class = NULL;
8436 else
8437 attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
8439 if (attr_class) {
8440 mono_class_init_checked (attr_class, error);
8441 return_val_if_nok (error, NULL_HANDLE_ARRAY);
8444 return mono_reflection_get_custom_attrs_by_type_handle (obj, attr_class, error);
8447 MonoArrayHandle
8448 ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObjectHandle obj, MonoError *error)
8450 return mono_reflection_get_custom_attrs_data_checked (obj, error);
8454 MonoStringHandle
8455 ves_icall_Mono_Runtime_GetDisplayName (MonoError *error)
8457 char *info;
8458 MonoStringHandle display_name;
8460 error_init (error);
8461 info = mono_get_runtime_callbacks ()->get_runtime_build_info ();
8462 display_name = mono_string_new_handle (mono_domain_get (), info, error);
8463 g_free (info);
8464 return display_name;
8467 #ifndef HOST_WIN32
8468 static gint32
8469 mono_icall_wait_for_input_idle (gpointer handle, gint32 milliseconds)
8471 return WAIT_TIMEOUT;
8473 #endif /* !HOST_WIN32 */
8475 gint32
8476 ves_icall_Microsoft_Win32_NativeMethods_WaitForInputIdle (gpointer handle, gint32 milliseconds)
8478 return mono_icall_wait_for_input_idle (handle, milliseconds);
8481 gint32
8482 ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcessId (void)
8484 return mono_process_current_pid ();
8487 MonoBoolean
8488 ves_icall_Mono_TlsProviderFactory_IsBtlsSupported (void)
8490 #if HAVE_BTLS
8491 return TRUE;
8492 #else
8493 return FALSE;
8494 #endif
8497 #ifndef DISABLE_COM
8500 ves_icall_System_Runtime_InteropServices_Marshal_GetHRForException_WinRT(MonoExceptionHandle ex, MonoError *error)
8502 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetHRForException_WinRT internal call is not implemented.");
8503 return 0;
8506 MonoObjectHandle
8507 ves_icall_System_Runtime_InteropServices_Marshal_GetNativeActivationFactory(MonoObjectHandle type, MonoError *error)
8509 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetNativeActivationFactory internal call is not implemented.");
8510 return NULL_HANDLE;
8513 void*
8514 ves_icall_System_Runtime_InteropServices_Marshal_GetRawIUnknownForComObjectNoAddRef(MonoObjectHandle obj, MonoError *error)
8516 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetRawIUnknownForComObjectNoAddRef internal call is not implemented.");
8517 return NULL;
8520 MonoObjectHandle
8521 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo(MonoError *error)
8523 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.GetRestrictedErrorInfo internal call is not implemented.");
8524 return NULL_HANDLE;
8527 MonoBoolean
8528 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoOriginateLanguageException (int ierr, MonoStringHandle message, void* languageException, MonoError *error)
8530 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoOriginateLanguageException internal call is not implemented.");
8531 return FALSE;
8534 void
8535 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoReportUnhandledError (MonoObjectHandle oerr, MonoError *error)
8537 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoReportUnhandledError internal call is not implemented.");
8541 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsCreateString(MonoStringHandle sourceString, int length, void** hstring, MonoError *error)
8543 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsCreateString internal call is not implemented.");
8544 return 0;
8548 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsDeleteString(void* hstring, MonoError *error)
8550 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsDeleteString internal call is not implemented.");
8551 return 0;
8554 mono_unichar2*
8555 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsGetStringRawBuffer(void* hstring, unsigned* length, MonoError *error)
8557 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsGetStringRawBuffer internal call is not implemented.");
8558 return NULL;
8561 #endif
8564 void
8565 ves_icall_System_IO_LogcatTextWriter_Log (const char *appname, gint32 level, const char *message)
8567 g_log (appname, (GLogLevelFlags)level, "%s", message);
8571 static const MonoIcallTableCallbacks *icall_table;
8572 static mono_mutex_t icall_mutex;
8573 static GHashTable *icall_hash = NULL;
8575 typedef struct _MonoIcallHashTableValue {
8576 gconstpointer method;
8577 guint32 flags;
8578 } MonoIcallHashTableValue;
8580 void
8581 mono_install_icall_table_callbacks (const MonoIcallTableCallbacks *cb)
8583 g_assert (cb->version == MONO_ICALL_TABLE_CALLBACKS_VERSION);
8584 icall_table = cb;
8587 void
8588 mono_icall_init (void)
8590 #ifndef DISABLE_ICALL_TABLES
8591 mono_icall_table_init ();
8592 #endif
8593 icall_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
8594 mono_os_mutex_init (&icall_mutex);
8597 static void
8598 mono_icall_lock (void)
8600 mono_locks_os_acquire (&icall_mutex, IcallLock);
8603 static void
8604 mono_icall_unlock (void)
8606 mono_locks_os_release (&icall_mutex, IcallLock);
8609 void
8610 mono_icall_cleanup (void)
8612 g_hash_table_destroy (icall_hash);
8613 mono_os_mutex_destroy (&icall_mutex);
8616 static void
8617 add_internal_call_with_flags (const char *name, gconstpointer method, guint32 flags)
8619 char *key = g_strdup (name);
8620 MonoIcallHashTableValue *value = g_new (MonoIcallHashTableValue, 1);
8621 if (key && value) {
8622 value->method = method;
8623 value->flags = flags;
8625 mono_icall_lock ();
8626 g_hash_table_insert (icall_hash, key, (gpointer)value);
8627 mono_icall_unlock ();
8632 * mono_dangerous_add_internal_call_coop:
8633 * \param name method specification to surface to the managed world
8634 * \param method pointer to a C method to invoke when the method is called
8636 * Similar to \c mono_dangerous_add_raw_internal_call.
8639 void
8640 mono_dangerous_add_internal_call_coop (const char *name, gconstpointer method)
8642 add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_COOPERATIVE);
8646 * mono_dangerous_add_internal_call_no_wrapper:
8647 * \param name method specification to surface to the managed world
8648 * \param method pointer to a C method to invoke when the method is called
8650 * Similar to \c mono_dangerous_add_raw_internal_call but with more requirements for correct
8651 * operation.
8653 * The \p method must NOT:
8655 * Run for an unbounded amount of time without calling the mono runtime.
8656 * Additionally, the method must switch to GC Safe mode to perform all blocking
8657 * operations: performing blocking I/O, taking locks, etc. The method can't throw or raise
8658 * exceptions or call other methods that will throw or raise exceptions since the runtime won't
8659 * be able to detect exeptions and unwinder won't be able to correctly find last managed frame in callstack.
8660 * This registration method is for icalls that needs very low overhead and follow all rules in their implementation.
8663 void
8664 mono_dangerous_add_internal_call_no_wrapper (const char *name, gconstpointer method)
8666 add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_NO_WRAPPER);
8670 * mono_add_internal_call:
8671 * \param name method specification to surface to the managed world
8672 * \param method pointer to a C method to invoke when the method is called
8674 * This method surfaces the C function pointed by \p method as a method
8675 * that has been surfaced in managed code with the method specified in
8676 * \p name as an internal call.
8678 * Internal calls are surfaced to all app domains loaded and they are
8679 * accessibly by a type with the specified name.
8681 * You must provide a fully qualified type name, that is namespaces
8682 * and type name, followed by a colon and the method name, with an
8683 * optional signature to bind.
8685 * For example, the following are all valid declarations:
8687 * \c MyApp.Services.ScriptService:Accelerate
8689 * \c MyApp.Services.ScriptService:Slowdown(int,bool)
8691 * You use method parameters in cases where there might be more than
8692 * one surface method to managed code. That way you can register different
8693 * internal calls for different method overloads.
8695 * The internal calls are invoked with no marshalling. This means that .NET
8696 * types like \c System.String are exposed as \c MonoString* parameters. This is
8697 * different than the way that strings are surfaced in P/Invoke.
8699 * For more information on how the parameters are marshalled, see the
8700 * <a href="http://www.mono-project.com/docs/advanced/embedding/">Mono Embedding</a>
8701 * page.
8703 * See the <a href="mono-api-methods.html#method-desc">Method Description</a>
8704 * reference for more information on the format of method descriptions.
8706 void
8707 mono_add_internal_call (const char *name, gconstpointer method)
8709 add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_FOREIGN);
8713 * mono_dangerous_add_raw_internal_call:
8714 * \param name method specification to surface to the managed world
8715 * \param method pointer to a C method to invoke when the method is called
8717 * Similar to \c mono_add_internal_call but with more requirements for correct
8718 * operation.
8720 * A thread running a dangerous raw internal call will avoid a thread state
8721 * transition on entry and exit, but it must take responsiblity for cooperating
8722 * with the Mono runtime.
8724 * The \p method must NOT:
8726 * Run for an unbounded amount of time without calling the mono runtime.
8727 * Additionally, the method must switch to GC Safe mode to perform all blocking
8728 * operations: performing blocking I/O, taking locks, etc.
8731 void
8732 mono_dangerous_add_raw_internal_call (const char *name, gconstpointer method)
8734 add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_COOPERATIVE);
8738 * mono_add_internal_call_with_flags:
8739 * \param name method specification to surface to the managed world
8740 * \param method pointer to a C method to invoke when the method is called
8741 * \param cooperative if \c TRUE, run icall in GC Unsafe (cooperatively suspended) mode,
8742 * otherwise GC Safe (blocking)
8744 * Like \c mono_add_internal_call, but if \p cooperative is \c TRUE the added
8745 * icall promises that it will use the coopertive API to inform the runtime
8746 * when it is running blocking operations, that it will not run for unbounded
8747 * amounts of time without safepointing, and that it will not hold managed
8748 * object references across suspend safepoints.
8750 * If \p cooperative is \c FALSE, run the icall in GC Safe mode - the icall may
8751 * block. The icall must obey the GC Safe rules, e.g. it must not touch
8752 * unpinned managed memory.
8755 void
8756 mono_add_internal_call_with_flags (const char *name, gconstpointer method, gboolean cooperative)
8758 add_internal_call_with_flags (name, method, cooperative ? MONO_ICALL_FLAGS_COOPERATIVE : MONO_ICALL_FLAGS_FOREIGN);
8761 void
8762 mono_add_internal_call_internal (const char *name, gconstpointer method)
8764 add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_COOPERATIVE);
8768 * we should probably export this as an helper (handle nested types).
8769 * Returns the number of chars written in buf.
8771 static int
8772 concat_class_name (char *buf, int bufsize, MonoClass *klass)
8774 int nspacelen, cnamelen;
8775 nspacelen = strlen (m_class_get_name_space (klass));
8776 cnamelen = strlen (m_class_get_name (klass));
8777 if (nspacelen + cnamelen + 2 > bufsize)
8778 return 0;
8779 if (nspacelen) {
8780 memcpy (buf, m_class_get_name_space (klass), nspacelen);
8781 buf [nspacelen ++] = '.';
8783 memcpy (buf + nspacelen, m_class_get_name (klass), cnamelen);
8784 buf [nspacelen + cnamelen] = 0;
8785 return nspacelen + cnamelen;
8788 static void
8789 no_icall_table (void)
8791 g_assert_not_reached ();
8794 gboolean
8795 mono_is_missing_icall_addr (gconstpointer addr)
8797 return addr == NULL || addr == no_icall_table;
8801 * Returns either NULL or no_icall_table for missing icalls.
8803 gconstpointer
8804 mono_lookup_internal_call_full_with_flags (MonoMethod *method, gboolean warn_on_missing, guint32 *flags)
8806 char *sigstart = NULL;
8807 char *tmpsig = NULL;
8808 char mname [2048];
8809 char *classname = NULL;
8810 int typelen = 0, mlen, siglen;
8811 gconstpointer res = NULL;
8812 gboolean locked = FALSE;
8814 g_assert (method != NULL);
8816 if (method->is_inflated)
8817 method = ((MonoMethodInflated *) method)->declaring;
8819 if (m_class_get_nested_in (method->klass)) {
8820 int pos = concat_class_name (mname, sizeof (mname)-2, m_class_get_nested_in (method->klass));
8821 if (!pos)
8822 goto exit;
8824 mname [pos++] = '/';
8825 mname [pos] = 0;
8827 typelen = concat_class_name (mname+pos, sizeof (mname)-pos-1, method->klass);
8828 if (!typelen)
8829 goto exit;
8831 typelen += pos;
8832 } else {
8833 typelen = concat_class_name (mname, sizeof (mname), method->klass);
8834 if (!typelen)
8835 goto exit;
8838 classname = g_strdup (mname);
8840 mname [typelen] = ':';
8841 mname [typelen + 1] = ':';
8843 mlen = strlen (method->name);
8844 memcpy (mname + typelen + 2, method->name, mlen);
8845 sigstart = mname + typelen + 2 + mlen;
8846 *sigstart = 0;
8848 tmpsig = mono_signature_get_desc (mono_method_signature_internal (method), TRUE);
8849 siglen = strlen (tmpsig);
8850 if (typelen + mlen + siglen + 6 > sizeof (mname))
8851 goto exit;
8853 sigstart [0] = '(';
8854 memcpy (sigstart + 1, tmpsig, siglen);
8855 sigstart [siglen + 1] = ')';
8856 sigstart [siglen + 2] = 0;
8858 /* mono_marshal_get_native_wrapper () depends on this */
8859 if (method->klass == mono_defaults.string_class && !strcmp (method->name, ".ctor")) {
8860 res = (gconstpointer)ves_icall_System_String_ctor_RedirectToCreateString;
8861 goto exit;
8864 mono_icall_lock ();
8865 locked = TRUE;
8867 res = g_hash_table_lookup (icall_hash, mname);
8868 if (res) {
8869 MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
8870 if (flags)
8871 *flags = value->flags;
8872 res = value->method;
8873 goto exit;
8876 /* try without signature */
8877 *sigstart = 0;
8878 res = g_hash_table_lookup (icall_hash, mname);
8879 if (res) {
8880 MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
8881 if (flags)
8882 *flags = value->flags;
8883 res = value->method;
8884 goto exit;
8887 if (!icall_table) {
8888 /* Fail only when the result is actually used */
8889 res = (gconstpointer)no_icall_table;
8890 goto exit;
8891 } else {
8892 gboolean uses_handles = FALSE;
8893 g_assert (icall_table->lookup);
8894 res = icall_table->lookup (method, classname, sigstart - mlen, sigstart, &uses_handles);
8895 if (res && flags && uses_handles)
8896 *flags = *flags | MONO_ICALL_FLAGS_USES_HANDLES;
8897 mono_icall_unlock ();
8898 locked = FALSE;
8900 if (res)
8901 goto exit;
8903 if (warn_on_missing) {
8904 g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", mname);
8905 g_print ("\nYour mono runtime and class libraries are out of sync.\n");
8906 g_print ("The out of sync library is: %s\n", m_class_get_image (method->klass)->name);
8907 g_print ("\nWhen you update one from git you need to update, compile and install\nthe other too.\n");
8908 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");
8909 g_print ("If you see other errors or faults after this message they are probably related\n");
8910 g_print ("and you need to fix your mono install first.\n");
8913 res = NULL;
8916 exit:
8917 if (locked)
8918 mono_icall_unlock ();
8919 g_free (classname);
8920 g_free (tmpsig);
8921 return res;
8925 * mono_lookup_internal_call_full:
8926 * \param method the method to look up
8927 * \param uses_handles out argument if method needs handles around managed objects.
8928 * \returns a pointer to the icall code for the given method. If
8929 * \p uses_handles is not NULL, it will be set to TRUE if the method
8930 * needs managed objects wrapped using the infrastructure in handle.h
8932 * If the method is not found, warns and returns NULL.
8934 gconstpointer
8935 mono_lookup_internal_call_full (MonoMethod *method, gboolean warn_on_missing, mono_bool *uses_handles, mono_bool *foreign)
8937 if (uses_handles)
8938 *uses_handles = FALSE;
8939 if (foreign)
8940 *foreign = FALSE;
8942 guint32 flags = MONO_ICALL_FLAGS_NONE;
8943 gconstpointer addr = mono_lookup_internal_call_full_with_flags (method, warn_on_missing, &flags);
8945 if (uses_handles && (flags & MONO_ICALL_FLAGS_USES_HANDLES))
8946 *uses_handles = TRUE;
8947 if (foreign && (flags & MONO_ICALL_FLAGS_FOREIGN))
8948 *foreign = TRUE;
8949 return addr;
8953 * mono_lookup_internal_call:
8955 gpointer
8956 mono_lookup_internal_call (MonoMethod *method)
8958 return (gpointer)mono_lookup_internal_call_full (method, TRUE, NULL, NULL);
8962 * mono_lookup_icall_symbol:
8964 * Given the icall METHOD, returns its C symbol.
8966 const char*
8967 mono_lookup_icall_symbol (MonoMethod *m)
8969 if (!icall_table)
8970 return NULL;
8972 g_assert (icall_table->lookup_icall_symbol);
8973 gpointer func;
8974 func = (gpointer)mono_lookup_internal_call_full (m, FALSE, NULL, NULL);
8975 if (!func)
8976 return NULL;
8977 return icall_table->lookup_icall_symbol (func);
8980 #if defined(TARGET_WIN32) && defined(TARGET_X86)
8982 * Under windows, the default pinvoke calling convention is STDCALL but
8983 * we need CDECL.
8985 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION MONO_CALL_C
8986 #else
8987 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION 0
8988 #endif
8990 // Storage for these enums is pointer-sized as it gets replaced with MonoType*.
8992 // mono_create_icall_signatures depends on this order. Handle with care.
8993 typedef enum ICallSigType {
8994 ICALL_SIG_TYPE_bool = 0x00,
8995 ICALL_SIG_TYPE_boolean = ICALL_SIG_TYPE_bool,
8996 ICALL_SIG_TYPE_double = 0x01,
8997 ICALL_SIG_TYPE_float = 0x02,
8998 ICALL_SIG_TYPE_int = 0x03,
8999 ICALL_SIG_TYPE_int16 = 0x04,
9000 ICALL_SIG_TYPE_int32 = ICALL_SIG_TYPE_int,
9001 ICALL_SIG_TYPE_int8 = 0x05,
9002 ICALL_SIG_TYPE_long = 0x06,
9003 ICALL_SIG_TYPE_obj = 0x07,
9004 ICALL_SIG_TYPE_object = ICALL_SIG_TYPE_obj,
9005 ICALL_SIG_TYPE_ptr = 0x08,
9006 ICALL_SIG_TYPE_ptrref = 0x09,
9007 ICALL_SIG_TYPE_string = 0x0A,
9008 ICALL_SIG_TYPE_uint16 = 0x0B,
9009 ICALL_SIG_TYPE_uint32 = 0x0C,
9010 ICALL_SIG_TYPE_uint8 = 0x0D,
9011 ICALL_SIG_TYPE_ulong = 0x0E,
9012 ICALL_SIG_TYPE_void = 0x0F,
9013 ICALL_SIG_TYPE_sizet = 0x10
9014 } ICallSigType;
9016 #define ICALL_SIG_TYPES_1(a) ICALL_SIG_TYPE_ ## a,
9017 #define ICALL_SIG_TYPES_2(a, b) ICALL_SIG_TYPES_1 (a ) ICALL_SIG_TYPES_1 (b)
9018 #define ICALL_SIG_TYPES_3(a, b, c) ICALL_SIG_TYPES_2 (a, b ) ICALL_SIG_TYPES_1 (c)
9019 #define ICALL_SIG_TYPES_4(a, b, c, d) ICALL_SIG_TYPES_3 (a, b, c ) ICALL_SIG_TYPES_1 (d)
9020 #define ICALL_SIG_TYPES_5(a, b, c, d, e) ICALL_SIG_TYPES_4 (a, b, c, d ) ICALL_SIG_TYPES_1 (e)
9021 #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)
9022 #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)
9023 #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)
9025 #define ICALL_SIG_TYPES(n, types) ICALL_SIG_TYPES_ ## n types
9027 // A scheme to make these const would be nice.
9028 static struct {
9029 #define ICALL_SIG(n, xtypes) \
9030 struct { \
9031 MonoMethodSignature sig; \
9032 gsize types [n]; \
9033 } ICALL_SIG_NAME (n, xtypes);
9034 ICALL_SIGS
9035 MonoMethodSignature end; // terminal zeroed element
9036 } mono_icall_signatures = {
9037 #undef ICALL_SIG
9038 #define ICALL_SIG(n, types) { { \
9039 0, /* ret */ \
9040 n, /* param_count */ \
9041 -1, /* sentinelpos */ \
9042 0, /* generic_param_count */ \
9043 MONO_ICALL_SIGNATURE_CALL_CONVENTION, \
9044 0, /* hasthis */ \
9045 0, /* explicit_this */ \
9046 1, /* pinvoke */ \
9047 0, /* is_inflated */ \
9048 0, /* has_type_parameters */ \
9049 }, /* possible gap here, depending on MONO_ZERO_LEN_ARRAY */ \
9050 { ICALL_SIG_TYPES (n, types) } }, /* params and ret */
9051 ICALL_SIGS
9054 #undef ICALL_SIG
9055 #define ICALL_SIG(n, types) MonoMethodSignature * const ICALL_SIG_NAME (n, types) = &mono_icall_signatures.ICALL_SIG_NAME (n, types).sig;
9056 ICALL_SIGS
9057 #undef ICALL_SIG
9059 void
9060 mono_create_icall_signatures (void)
9062 // Fixup the mostly statically initialized icall signatures.
9063 // x = m_class_get_byval_arg (x)
9064 // Initialize ret with params [0] and params [i] with params [i + 1].
9065 // ptrref is special
9067 // FIXME This is a bit obscure.
9069 typedef MonoMethodSignature G_MAY_ALIAS MonoMethodSignature_a;
9070 typedef gsize G_MAY_ALIAS gsize_a;
9072 MonoType * const lookup [ ] = {
9073 m_class_get_byval_arg (mono_defaults.boolean_class), // ICALL_SIG_TYPE_bool
9074 m_class_get_byval_arg (mono_defaults.double_class), // ICALL_SIG_TYPE_double
9075 m_class_get_byval_arg (mono_defaults.single_class), // ICALL_SIG_TYPE_float
9076 m_class_get_byval_arg (mono_defaults.int32_class), // ICALL_SIG_TYPE_int
9077 m_class_get_byval_arg (mono_defaults.int16_class), // ICALL_SIG_TYPE_int16
9078 m_class_get_byval_arg (mono_defaults.sbyte_class), // ICALL_SIG_TYPE_int8
9079 m_class_get_byval_arg (mono_defaults.int64_class), // ICALL_SIG_TYPE_long
9080 m_class_get_byval_arg (mono_defaults.object_class), // ICALL_SIG_TYPE_obj
9081 m_class_get_byval_arg (mono_defaults.int_class), // ICALL_SIG_TYPE_ptr
9082 mono_class_get_byref_type (mono_defaults.int_class), // ICALL_SIG_TYPE_ptrref
9083 m_class_get_byval_arg (mono_defaults.string_class), // ICALL_SIG_TYPE_string
9084 m_class_get_byval_arg (mono_defaults.uint16_class), // ICALL_SIG_TYPE_uint16
9085 m_class_get_byval_arg (mono_defaults.uint32_class), // ICALL_SIG_TYPE_uint32
9086 m_class_get_byval_arg (mono_defaults.byte_class), // ICALL_SIG_TYPE_uint8
9087 m_class_get_byval_arg (mono_defaults.uint64_class), // ICALL_SIG_TYPE_ulong
9088 m_class_get_byval_arg (mono_defaults.void_class), // ICALL_SIG_TYPE_void
9089 m_class_get_byval_arg (mono_defaults.int_class), // ICALL_SIG_TYPE_sizet
9092 MonoMethodSignature_a *sig = (MonoMethodSignature*)&mono_icall_signatures;
9093 int n;
9094 while ((n = sig->param_count)) {
9095 --sig->param_count; // remove ret
9096 gsize_a *types = (gsize_a*)(sig + 1);
9097 for (int i = 0; i < n; ++i) {
9098 gsize index = *types++;
9099 g_assert (index < G_N_ELEMENTS (lookup));
9100 // Casts on next line are attempt to follow strict aliasing rules,
9101 // to ensure reading from *types precedes writing
9102 // to params [].
9103 *(gsize*)(i ? &sig->params [i - 1] : &sig->ret) = (gsize)lookup [index];
9105 sig = (MonoMethodSignature*)types;
9109 void
9110 mono_register_jit_icall_info (MonoJitICallInfo *info, gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean avoid_wrapper, const char *c_symbol)
9112 // Duplicate initialization is allowed and racy, assuming it is equivalent.
9114 info->name = name;
9115 info->func = func;
9116 info->sig = sig;
9117 info->c_symbol = c_symbol;
9119 // Fill in wrapper ahead of time, to just be func, to avoid
9120 // later initializing it to anything else. So therefore, no wrapper.
9121 if (avoid_wrapper) {
9122 info->wrapper = func;
9123 } else {
9124 // Leave it alone in case of a race.
9129 ves_icall_System_GC_GetCollectionCount (int generation)
9131 return mono_gc_collection_count (generation);
9135 ves_icall_System_GC_GetGeneration (MonoObjectHandle object, MonoError *error)
9137 return mono_gc_get_generation (MONO_HANDLE_RAW (object));
9141 ves_icall_System_GC_GetMaxGeneration (void)
9143 return mono_gc_max_generation ();
9146 gint64
9147 ves_icall_System_GC_GetAllocatedBytesForCurrentThread (void)
9149 return mono_gc_get_allocated_bytes_for_current_thread ();
9153 void
9154 ves_icall_System_GC_RecordPressure (gint64 value)
9156 mono_gc_add_memory_pressure (value);
9159 gint64
9160 ves_icall_System_Diagnostics_Stopwatch_GetTimestamp (void)
9162 return mono_100ns_ticks ();
9165 gint64
9166 ves_icall_System_Threading_Timer_GetTimeMonotonic (void)
9168 return mono_100ns_ticks ();
9171 gint64
9172 ves_icall_System_DateTime_GetSystemTimeAsFileTime (void)
9174 return mono_100ns_datetime ();
9178 ves_icall_System_Threading_Thread_SystemMaxStackSize (void)
9180 return mono_thread_info_get_system_max_stack_size ();
9183 MonoBoolean
9184 ves_icall_System_Threading_Thread_YieldInternal (void)
9186 mono_threads_platform_yield ();
9187 return TRUE;
9190 gint32
9191 ves_icall_System_Environment_get_ProcessorCount (void)
9193 return mono_cpu_limit ();
9196 #if defined(ENABLE_MONODROID)
9198 G_EXTERN_C gpointer CreateNLSocket (void);
9199 G_EXTERN_C gint32 ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size);
9200 G_EXTERN_C gpointer CloseNLSocket (gpointer sock);
9202 gpointer
9203 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CreateNLSocket (void)
9205 return CreateNLSocket ();
9208 gint32
9209 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
9211 return ReadEvents (sock, buffer, count, size);
9214 gpointer
9215 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CloseNLSocket (gpointer sock)
9217 return CloseNLSocket (sock);
9220 #endif
9222 // Generate wrappers.
9224 #define ICALL_TYPE(id,name,first) /* nothing */
9225 #define ICALL(id,name,func) /* nothing */
9226 #define NOHANDLES(inner) /* nothing */
9228 #define MONO_HANDLE_REGISTER_ICALL(func, ret, nargs, argtypes) MONO_HANDLE_REGISTER_ICALL_IMPLEMENT (func, ret, nargs, argtypes)
9230 // Some native functions are exposed via multiple managed names.
9231 // Producing a wrapper for these results in duplicate wrappers with the same names,
9232 // which fails to compile. Do not produce such duplicate wrappers. Alternatively,
9233 // a one line native function with a different name that calls the main one could be used.
9234 // i.e. the wrapper would also have a different name.
9235 #define HANDLES_REUSE_WRAPPER(...) /* nothing */
9237 #define HANDLES(id, name, func, ret, nargs, argtypes) \
9238 MONO_HANDLE_DECLARE (id, name, func, ret, nargs, argtypes); \
9239 MONO_HANDLE_IMPLEMENT (id, name, func, ret, nargs, argtypes)
9241 #include "metadata/icall-def.h"
9243 #undef HANDLES
9244 #undef HANDLES_REUSE_WRAPPER
9245 #undef ICALL_TYPE
9246 #undef ICALL
9247 #undef NOHANDLES
9248 #undef MONO_HANDLE_REGISTER_ICALL