Apply changes from https://github.com/dotnet/runtime/commit/eb1756e97d23df13bc6fe798e...
[mono-project.git] / mono / metadata / icall.c
blob2d70ba3d1ed9ad39c85b7dcb6d96578fc27ca5ad
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/385b4d4296f9c5cb82363565aa210a1a37f92d90/src/coreclr/src/vm/threads.cpp#L6344.
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);
204 #ifdef ENABLE_NETCORE
205 if (m_class_is_native_pointer (element_class)) {
206 mono_error_set_not_supported (error, NULL);
207 return NULL_HANDLE;
209 #endif
211 if (m_class_is_valuetype (element_class)) {
212 gsize element_size = mono_array_element_size (array_class);
213 gpointer element_address = mono_array_addr_with_size_fast (MONO_HANDLE_RAW (array), element_size, (gsize)pos);
214 return mono_value_box_handle (MONO_HANDLE_DOMAIN (array), element_class, element_address, error);
216 MonoObjectHandle result = mono_new_null ();
217 mono_handle_array_getref (result, array, pos);
218 return result;
221 MonoObjectHandle
222 ves_icall_System_Array_GetValue (MonoArrayHandle arr, MonoArrayHandle indices, MonoError *error)
224 MONO_CHECK_ARG_NULL_HANDLE (indices, NULL_HANDLE);
226 MonoClass * const indices_class = mono_handle_class (indices);
227 MonoClass * const array_class = mono_handle_class (arr);
229 g_assert (m_class_get_rank (indices_class) == 1);
231 if (MONO_HANDLE_GETVAL (indices, bounds) || MONO_HANDLE_GETVAL (indices, max_length) != m_class_get_rank (array_class)) {
232 mono_error_set_argument (error, NULL, NULL);
233 return NULL_HANDLE;
236 gint32 index = 0;
238 if (!MONO_HANDLE_GETVAL (arr, bounds)) {
239 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, 0);
240 if (index < 0 || index >= MONO_HANDLE_GETVAL (arr, max_length)) {
241 mono_error_set_index_out_of_range (error);
242 return NULL_HANDLE;
245 return ves_icall_System_Array_GetValueImpl (arr, index, error);
248 for (gint32 i = 0; i < m_class_get_rank (array_class); i++) {
249 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, i);
250 if ((index < MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound)) ||
251 (index >= (mono_array_lower_bound_t)MONO_HANDLE_GETVAL (arr, bounds [i].length) + MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound))) {
252 mono_error_set_index_out_of_range (error);
253 return NULL_HANDLE;
257 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, 0);
258 gint32 pos = index - MONO_HANDLE_GETVAL (arr, bounds [0].lower_bound);
259 for (gint32 i = 1; i < m_class_get_rank (array_class); i++) {
260 MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, i);
261 pos = pos * MONO_HANDLE_GETVAL (arr, bounds [i].length) + index -
262 MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound);
265 return ves_icall_System_Array_GetValueImpl (arr, pos, error);
268 void
269 ves_icall_System_Array_SetValueImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
271 array_set_value_impl (arr, value, pos, FALSE, TRUE, error);
274 static inline void
275 set_invalid_cast (MonoError *error, MonoClass *src_class, MonoClass *dst_class)
277 mono_get_runtime_callbacks ()->set_cast_details (src_class, dst_class);
278 mono_error_set_invalid_cast (error);
281 #if ENABLE_NETCORE
282 void
283 ves_icall_System_Array_SetValueRelaxedImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
285 array_set_value_impl (arr, value, pos, FALSE, FALSE, error);
288 // Copied from CoreCLR: https://github.com/dotnet/coreclr/blob/d3e39bc2f81e3dbf9e4b96347f62b49d8700336c/src/vm/invokeutil.cpp#L33
289 #define PT_Primitive 0x01000000
291 static const guint32 primitive_conversions [] = {
292 0x00, // MONO_TYPE_END
293 0x00, // MONO_TYPE_VOID
294 PT_Primitive | 0x0004, // MONO_TYPE_BOOLEAN
295 PT_Primitive | 0x3F88, // MONO_TYPE_CHAR (W = U2, CHAR, I4, U4, I8, U8, R4, R8)
296 PT_Primitive | 0x3550, // MONO_TYPE_I1 (W = I1, I2, I4, I8, R4, R8)
297 PT_Primitive | 0x3FE8, // MONO_TYPE_U1 (W = CHAR, U1, I2, U2, I4, U4, I8, U8, R4, R8)
298 PT_Primitive | 0x3540, // MONO_TYPE_I2 (W = I2, I4, I8, R4, R8)
299 PT_Primitive | 0x3F88, // MONO_TYPE_U2 (W = U2, CHAR, I4, U4, I8, U8, R4, R8)
300 PT_Primitive | 0x3500, // MONO_TYPE_I4 (W = I4, I8, R4, R8)
301 PT_Primitive | 0x3E00, // MONO_TYPE_U4 (W = U4, I8, R4, R8)
302 PT_Primitive | 0x3400, // MONO_TYPE_I8 (W = I8, R4, R8)
303 PT_Primitive | 0x3800, // MONO_TYPE_U8 (W = U8, R4, R8)
304 PT_Primitive | 0x3000, // MONO_TYPE_R4 (W = R4, R8)
305 PT_Primitive | 0x2000, // MONO_TYPE_R8 (W = R8)
308 // Copied from CoreCLR: https://github.com/dotnet/coreclr/blob/030a3ea9b8dbeae89c90d34441d4d9a1cf4a7de6/src/vm/invokeutil.h#L176
309 static
310 gboolean can_primitive_widen (MonoTypeEnum src_type, MonoTypeEnum dest_type)
312 if (dest_type > MONO_TYPE_R8 || src_type > MONO_TYPE_R8) {
313 return (MONO_TYPE_I == dest_type && MONO_TYPE_I == src_type) || (MONO_TYPE_U == dest_type && MONO_TYPE_U == src_type);
315 return ((1 << dest_type) & primitive_conversions [src_type]) != 0;
318 // Copied from CoreCLR: https://github.com/dotnet/coreclr/blob/eafa8648ebee92de1380278b15cd5c2b6ef11218/src/vm/array.cpp#L1406
319 static MonoTypeEnum
320 get_normalized_integral_array_element_type (MonoTypeEnum elementType)
322 // Array Primitive types such as E_T_I4 and E_T_U4 are interchangeable
323 // Enums with interchangeable underlying types are interchangable
324 // BOOL is NOT interchangeable with I1/U1, neither CHAR -- with I2/U2
326 switch (elementType) {
327 case MONO_TYPE_U1:
328 case MONO_TYPE_U2:
329 case MONO_TYPE_U4:
330 case MONO_TYPE_U8:
331 case MONO_TYPE_U:
332 return (MonoTypeEnum) (elementType - 1); // normalize to signed type
335 return elementType;
338 MonoBoolean
339 ves_icall_System_Array_CanChangePrimitive (MonoReflectionType *volatile* ref_src_type_handle, MonoReflectionType *volatile* ref_dst_type_handle, MonoBoolean reliable)
341 MonoReflectionType* const ref_src_type = *ref_src_type_handle;
342 MonoReflectionType* const ref_dst_type = *ref_dst_type_handle;
344 MonoType *src_type = ref_src_type->type;
345 MonoType *dst_type = ref_dst_type->type;
347 g_assert (mono_type_is_primitive (src_type));
348 g_assert (mono_type_is_primitive (dst_type));
350 MonoTypeEnum normalized_src_type = get_normalized_integral_array_element_type (src_type->type);
351 MonoTypeEnum normalized_dst_type = get_normalized_integral_array_element_type (dst_type->type);
353 // Allow conversions like int <-> uint
354 if (normalized_src_type == normalized_dst_type) {
355 return TRUE;
358 // Widening is not allowed if reliable is true.
359 if (reliable) {
360 return FALSE;
363 // NOTE we don't use normalized types here so int -> ulong will be false
364 // see https://github.com/dotnet/coreclr/pull/25209#issuecomment-505952295
365 return can_primitive_widen (src_type->type, dst_type->type);
367 #endif
369 static void
370 array_set_value_impl (MonoArrayHandle arr_handle, MonoObjectHandle value_handle, guint32 pos, gboolean strict_enums, gboolean strict_signs, MonoError *error)
372 MonoClass *ac, *vc, *ec;
373 gint32 esize, vsize;
374 gpointer *ea = NULL, *va = NULL;
376 guint64 u64 = 0;
377 gint64 i64 = 0;
378 gdouble r64 = 0;
379 gboolean castOk = FALSE;
380 gboolean et_isenum = FALSE;
381 gboolean vt_isenum = FALSE;
383 error_init (error);
385 if (!MONO_HANDLE_IS_NULL (value_handle))
386 vc = mono_handle_class (value_handle);
387 else
388 vc = NULL;
390 ac = mono_handle_class (arr_handle);
391 ec = m_class_get_element_class (ac);
392 esize = mono_array_element_size (ac);
394 if (mono_class_is_nullable (ec)) {
395 #ifdef ENABLE_NETCORE
396 if (vc && m_class_is_primitive (vc) && vc != m_class_get_nullable_elem_class (ec)) {
397 // T -> Nullable<T> T must be exact
398 set_invalid_cast (error, vc, ec);
399 goto leave;
401 #endif
402 MONO_ENTER_NO_SAFEPOINTS;
403 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
404 if (!MONO_HANDLE_IS_NULL (value_handle))
405 va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
406 mono_nullable_init_unboxed ((guint8*)ea, va, ec);
407 MONO_EXIT_NO_SAFEPOINTS;
408 goto leave;
411 if (MONO_HANDLE_IS_NULL (value_handle)) {
412 MONO_ENTER_NO_SAFEPOINTS;
413 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
414 mono_gc_bzero_atomic (ea, esize);
415 MONO_EXIT_NO_SAFEPOINTS;
416 goto leave;
419 #ifdef ENABLE_NETCORE
420 #define WIDENING_MSG NULL
421 #define WIDENING_ARG NULL
422 #else
423 #define WIDENING_MSG "not a widening conversion"
424 #define WIDENING_ARG "value"
425 #endif
427 #define NO_WIDENING_CONVERSION G_STMT_START{ \
428 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
429 break; \
430 }G_STMT_END
432 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{ \
433 if (esize < vsize + (extra)) { \
434 mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
435 break; \
437 }G_STMT_END
439 #define INVALID_CAST G_STMT_START{ \
440 mono_get_runtime_callbacks ()->set_cast_details (vc, ec); \
441 mono_error_set_invalid_cast (error); \
442 break; \
443 }G_STMT_END
445 MonoTypeEnum et;
446 et = m_class_get_byval_arg (ec)->type;
447 MonoTypeEnum vt;
448 vt = m_class_get_byval_arg (vc)->type;
450 /* Check element (destination) type. */
451 switch (et) {
452 case MONO_TYPE_STRING:
453 switch (vt) {
454 case MONO_TYPE_STRING:
455 break;
456 default:
457 INVALID_CAST;
459 break;
460 case MONO_TYPE_BOOLEAN:
461 switch (vt) {
462 case MONO_TYPE_BOOLEAN:
463 break;
464 case MONO_TYPE_CHAR:
465 case MONO_TYPE_U1:
466 case MONO_TYPE_U2:
467 case MONO_TYPE_U4:
468 case MONO_TYPE_U8:
469 case MONO_TYPE_I1:
470 case MONO_TYPE_I2:
471 case MONO_TYPE_I4:
472 case MONO_TYPE_I8:
473 case MONO_TYPE_R4:
474 case MONO_TYPE_R8:
475 NO_WIDENING_CONVERSION;
476 break;
477 default:
478 INVALID_CAST;
480 break;
481 default:
482 break;
484 if (!is_ok (error))
485 goto leave;
487 castOk = mono_object_handle_isinst_mbyref_raw (value_handle, ec, error);
488 if (!is_ok (error))
489 goto leave;
491 if (!m_class_is_valuetype (ec)) {
492 if (!castOk)
493 INVALID_CAST;
494 if (is_ok (error))
495 MONO_HANDLE_ARRAY_SETREF (arr_handle, pos, value_handle);
496 goto leave;
499 if (castOk) {
500 MONO_ENTER_NO_SAFEPOINTS;
501 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
502 va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
503 if (m_class_has_references (ec))
504 mono_value_copy_internal (ea, va, ec);
505 else
506 mono_gc_memmove_atomic (ea, va, esize);
507 MONO_EXIT_NO_SAFEPOINTS;
509 goto leave;
512 if (!m_class_is_valuetype (vc))
513 INVALID_CAST;
515 if (!is_ok (error))
516 goto leave;
518 vsize = mono_class_value_size (vc, NULL);
520 et_isenum = et == MONO_TYPE_VALUETYPE && m_class_is_enumtype (m_class_get_byval_arg (ec)->data.klass);
521 vt_isenum = vt == MONO_TYPE_VALUETYPE && m_class_is_enumtype (m_class_get_byval_arg (vc)->data.klass);
523 #if ENABLE_NETCORE
524 if (strict_enums && et_isenum && !vt_isenum) {
525 INVALID_CAST;
526 goto leave;
528 #endif
530 if (et_isenum)
531 et = mono_class_enum_basetype_internal (m_class_get_byval_arg (ec)->data.klass)->type;
533 if (vt_isenum)
534 vt = mono_class_enum_basetype_internal (m_class_get_byval_arg (vc)->data.klass)->type;
536 #if ENABLE_NETCORE
537 // Treat MONO_TYPE_U/I as MONO_TYPE_U8/I8/U4/I4
538 #if SIZEOF_VOID_P == 8
539 vt = vt == MONO_TYPE_U ? MONO_TYPE_U8 : (vt == MONO_TYPE_I ? MONO_TYPE_I8 : vt);
540 et = et == MONO_TYPE_U ? MONO_TYPE_U8 : (et == MONO_TYPE_I ? MONO_TYPE_I8 : et);
541 #else
542 vt = vt == MONO_TYPE_U ? MONO_TYPE_U4 : (vt == MONO_TYPE_I ? MONO_TYPE_I4 : vt);
543 et = et == MONO_TYPE_U ? MONO_TYPE_U4 : (et == MONO_TYPE_I ? MONO_TYPE_I4 : et);
544 #endif
545 #endif
547 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
548 switch (vt) { \
549 case MONO_TYPE_U1: \
550 case MONO_TYPE_U2: \
551 case MONO_TYPE_U4: \
552 case MONO_TYPE_U8: \
553 case MONO_TYPE_CHAR: \
554 CHECK_WIDENING_CONVERSION(0); \
555 *(etype *) ea = (etype) u64; \
556 break; \
557 /* You can't assign a signed value to an unsigned array. */ \
558 case MONO_TYPE_I1: \
559 case MONO_TYPE_I2: \
560 case MONO_TYPE_I4: \
561 case MONO_TYPE_I8: \
562 if (!strict_signs) { \
563 CHECK_WIDENING_CONVERSION(0); \
564 *(etype *) ea = (etype) i64; \
565 break; \
567 /* You can't assign a floating point number to an integer array. */ \
568 case MONO_TYPE_R4: \
569 case MONO_TYPE_R8: \
570 NO_WIDENING_CONVERSION; \
571 break; \
572 default: \
573 INVALID_CAST; \
574 break; \
576 }G_STMT_END
578 #define ASSIGN_SIGNED(etype) G_STMT_START{\
579 switch (vt) { \
580 case MONO_TYPE_I1: \
581 case MONO_TYPE_I2: \
582 case MONO_TYPE_I4: \
583 case MONO_TYPE_I8: \
584 CHECK_WIDENING_CONVERSION(0); \
585 *(etype *) ea = (etype) i64; \
586 break; \
587 /* You can assign an unsigned value to a signed array if the array's */ \
588 /* element size is larger than the value size. */ \
589 case MONO_TYPE_U1: \
590 case MONO_TYPE_U2: \
591 case MONO_TYPE_U4: \
592 case MONO_TYPE_U8: \
593 case MONO_TYPE_CHAR: \
594 CHECK_WIDENING_CONVERSION(strict_signs ? 1 : 0); \
595 *(etype *) ea = (etype) u64; \
596 break; \
597 /* You can't assign a floating point number to an integer array. */ \
598 case MONO_TYPE_R4: \
599 case MONO_TYPE_R8: \
600 NO_WIDENING_CONVERSION; \
601 break; \
602 default: \
603 INVALID_CAST; \
604 break; \
606 }G_STMT_END
608 #define ASSIGN_REAL(etype) G_STMT_START{\
609 switch (vt) { \
610 case MONO_TYPE_R4: \
611 case MONO_TYPE_R8: \
612 CHECK_WIDENING_CONVERSION(0); \
613 *(etype *) ea = (etype) r64; \
614 break; \
615 /* All integer values fit into a floating point array, so we don't */ \
616 /* need to CHECK_WIDENING_CONVERSION here. */ \
617 case MONO_TYPE_I1: \
618 case MONO_TYPE_I2: \
619 case MONO_TYPE_I4: \
620 case MONO_TYPE_I8: \
621 *(etype *) ea = (etype) i64; \
622 break; \
623 case MONO_TYPE_U1: \
624 case MONO_TYPE_U2: \
625 case MONO_TYPE_U4: \
626 case MONO_TYPE_U8: \
627 case MONO_TYPE_CHAR: \
628 *(etype *) ea = (etype) u64; \
629 break; \
630 default: \
631 INVALID_CAST; \
632 break; \
634 }G_STMT_END
636 MONO_ENTER_NO_SAFEPOINTS;
637 g_assert (!MONO_HANDLE_IS_NULL (value_handle));
638 g_assert (m_class_is_valuetype (vc));
639 va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
640 ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
642 switch (vt) {
643 case MONO_TYPE_U1:
644 u64 = *(guint8 *) va;
645 break;
646 case MONO_TYPE_U2:
647 u64 = *(guint16 *) va;
648 break;
649 case MONO_TYPE_U4:
650 u64 = *(guint32 *) va;
651 break;
652 case MONO_TYPE_U8:
653 u64 = *(guint64 *) va;
654 break;
655 case MONO_TYPE_I1:
656 i64 = *(gint8 *) va;
657 break;
658 case MONO_TYPE_I2:
659 i64 = *(gint16 *) va;
660 break;
661 case MONO_TYPE_I4:
662 i64 = *(gint32 *) va;
663 break;
664 case MONO_TYPE_I8:
665 i64 = *(gint64 *) va;
666 break;
667 case MONO_TYPE_R4:
668 r64 = *(gfloat *) va;
669 break;
670 case MONO_TYPE_R8:
671 r64 = *(gdouble *) va;
672 break;
673 case MONO_TYPE_CHAR:
674 u64 = *(guint16 *) va;
675 break;
676 case MONO_TYPE_BOOLEAN:
677 /* Boolean is only compatible with itself. */
678 switch (et) {
679 case MONO_TYPE_CHAR:
680 case MONO_TYPE_U1:
681 case MONO_TYPE_U2:
682 case MONO_TYPE_U4:
683 case MONO_TYPE_U8:
684 case MONO_TYPE_I1:
685 case MONO_TYPE_I2:
686 case MONO_TYPE_I4:
687 case MONO_TYPE_I8:
688 case MONO_TYPE_R4:
689 case MONO_TYPE_R8:
690 NO_WIDENING_CONVERSION;
691 break;
692 default:
693 INVALID_CAST;
695 break;
696 default:
697 break;
699 /* If we can't do a direct copy, let's try a widening conversion. */
701 if (is_ok (error)) {
702 switch (et) {
703 case MONO_TYPE_CHAR:
704 ASSIGN_UNSIGNED (guint16);
705 break;
706 case MONO_TYPE_U1:
707 ASSIGN_UNSIGNED (guint8);
708 break;
709 case MONO_TYPE_U2:
710 ASSIGN_UNSIGNED (guint16);
711 break;
712 case MONO_TYPE_U4:
713 ASSIGN_UNSIGNED (guint32);
714 break;
715 case MONO_TYPE_U8:
716 ASSIGN_UNSIGNED (guint64);
717 break;
718 case MONO_TYPE_I1:
719 ASSIGN_SIGNED (gint8);
720 break;
721 case MONO_TYPE_I2:
722 ASSIGN_SIGNED (gint16);
723 break;
724 case MONO_TYPE_I4:
725 ASSIGN_SIGNED (gint32);
726 break;
727 case MONO_TYPE_I8:
728 ASSIGN_SIGNED (gint64);
729 break;
730 case MONO_TYPE_R4:
731 ASSIGN_REAL (gfloat);
732 break;
733 case MONO_TYPE_R8:
734 ASSIGN_REAL (gdouble);
735 break;
736 default:
737 INVALID_CAST;
741 MONO_EXIT_NO_SAFEPOINTS;
743 #undef INVALID_CAST
744 #undef NO_WIDENING_CONVERSION
745 #undef CHECK_WIDENING_CONVERSION
746 #undef ASSIGN_UNSIGNED
747 #undef ASSIGN_SIGNED
748 #undef ASSIGN_REAL
750 leave:
751 return;
754 void
755 ves_icall_System_Array_SetValue (MonoArrayHandle arr, MonoObjectHandle value,
756 MonoArrayHandle idxs, MonoError *error)
758 icallarray_print ("%s\n", __func__);
760 MonoArrayBounds dim;
761 MonoClass *ac, *ic;
762 gint32 idx;
763 gint32 i, pos;
765 error_init (error);
767 if (MONO_HANDLE_IS_NULL (idxs)) {
768 #ifdef ENABLE_NETCORE
769 mono_error_set_argument_null (error, "indices", "");
770 #else
771 mono_error_set_argument_null (error, "idxs", "");
772 #endif
773 return;
776 ic = mono_handle_class (idxs);
777 ac = mono_handle_class (arr);
779 g_assert (m_class_get_rank (ic) == 1);
780 if (mono_handle_array_has_bounds (idxs) || MONO_HANDLE_GETVAL (idxs, max_length) != m_class_get_rank (ac)) {
781 #ifdef ENABLE_NETCORE
782 mono_error_set_argument (error, NULL, "");
783 #else
784 mono_error_set_argument (error, "idxs", "");
785 #endif
786 return;
789 if (!mono_handle_array_has_bounds (arr)) {
790 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
791 if (idx < 0 || idx >= MONO_HANDLE_GETVAL (arr, max_length)) {
792 mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
793 return;
796 array_set_value_impl (arr, value, idx, TRUE, TRUE, error);
797 return;
800 gint32 ac_rank = m_class_get_rank (ac);
801 for (i = 0; i < ac_rank; i++) {
802 mono_handle_array_get_bounds_dim (arr, i, &dim);
803 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
804 if ((idx < dim.lower_bound) ||
805 (idx >= (mono_array_lower_bound_t)dim.length + dim.lower_bound)) {
806 mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
807 return;
811 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
812 mono_handle_array_get_bounds_dim (arr, 0, &dim);
813 pos = idx - dim.lower_bound;
814 for (i = 1; i < ac_rank; i++) {
815 mono_handle_array_get_bounds_dim (arr, i, &dim);
816 MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
817 pos = pos * dim.length + idx - dim.lower_bound;
820 array_set_value_impl (arr, value, pos, TRUE, TRUE, error);
823 #ifdef ENABLE_NETCORE
825 void
826 ves_icall_System_Array_InternalCreate (MonoArray *volatile* result, MonoType* type, gint32 rank, gint32* pLengths, gint32* pLowerBounds)
828 ERROR_DECL (error);
830 MonoClass* klass = mono_class_from_mono_type_internal (type);
831 if (!mono_class_init_checked (klass, error))
832 goto exit;
834 if (m_class_get_byval_arg (m_class_get_element_class (klass))->type == MONO_TYPE_VOID) {
835 mono_error_set_not_supported (error, "Arrays of System.Void are not supported.");
836 goto exit;
839 if (type->byref || m_class_is_byreflike (klass)) {
840 mono_error_set_not_supported (error, NULL);
841 goto exit;
844 MonoGenericClass *gklass;
845 gklass = mono_class_try_get_generic_class (klass);
846 if (is_generic_parameter (type) || mono_class_is_gtd (klass) || (gklass && gklass->context.class_inst->is_open)) {
847 mono_error_set_not_supported (error, NULL);
848 goto exit;
851 /* vectors are not the same as one dimensional arrays with non-zero bounds */
852 gboolean bounded;
853 bounded = pLowerBounds != NULL && rank == 1 && pLowerBounds [0] != 0;
855 MonoClass* aklass;
856 aklass = mono_class_create_bounded_array (klass, rank, bounded);
858 uintptr_t aklass_rank;
859 aklass_rank = m_class_get_rank (aklass);
861 uintptr_t* sizes;
862 sizes = g_newa (uintptr_t, aklass_rank * 2);
864 intptr_t* lower_bounds;
865 lower_bounds = (intptr_t*)(sizes + aklass_rank);
867 // Copy lengths and lower_bounds from gint32 to [u]intptr_t.
868 for (uintptr_t i = 0; i < aklass_rank; ++i) {
869 if (pLowerBounds != NULL) {
870 lower_bounds [i] = pLowerBounds [i];
871 if ((gint64) pLowerBounds [i] + (gint64) pLengths [i] > G_MAXINT32) {
872 mono_error_set_argument_out_of_range (error, NULL, "Length + bound must not exceed Int32.MaxValue.");
873 goto exit;
875 } else {
876 lower_bounds [i] = 0;
878 sizes [i] = pLengths [i];
881 *result = mono_array_new_full_checked (mono_domain_get (), aklass, sizes, lower_bounds, error);
883 exit:
884 mono_error_set_pending_exception (error);
887 #endif
889 #ifndef ENABLE_NETCORE
890 MonoArrayHandle
891 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionTypeHandle type, MonoArrayHandle lengths, MonoArrayHandle bounds, MonoError *error)
893 // FIXME? fixed could be used for lengths, bounds.
895 icallarray_print ("%s type:%p length:%p bounds:%p\n", __func__, type, lengths, bounds);
897 MONO_CHECK_ARG_NULL_HANDLE (type, NULL_HANDLE_ARRAY);
898 MONO_CHECK_ARG_NULL_HANDLE (lengths, NULL_HANDLE_ARRAY);
900 MONO_CHECK_ARG (lengths, mono_array_handle_length (lengths) > 0, NULL_HANDLE_ARRAY);
901 if (!MONO_HANDLE_IS_NULL (bounds))
902 MONO_CHECK_ARG (bounds, mono_array_handle_length (lengths) == mono_array_handle_length (bounds), NULL_HANDLE_ARRAY);
904 for (uintptr_t i = 0; i < mono_array_handle_length (lengths); ++i) {
905 gint32 length = 0;
906 MONO_HANDLE_ARRAY_GETVAL (length, lengths, gint32, i);
907 if (length < 0) {
908 mono_error_set_argument_out_of_range (error, NULL, "MonoArgumentException:NULL");
909 return NULL_HANDLE_ARRAY;
913 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
914 if (!mono_class_init_checked (klass, error))
915 return NULL_HANDLE_ARRAY;
917 if (m_class_get_byval_arg (m_class_get_element_class (klass))->type == MONO_TYPE_VOID) {
918 mono_error_set_not_supported (error, "Arrays of System.Void are not supported.");
919 return NULL_HANDLE_ARRAY;
922 /* vectors are not the same as one dimensional arrays with non-zero bounds */
923 gboolean bounded = FALSE;
924 if (!MONO_HANDLE_IS_NULL (bounds) && mono_array_handle_length (bounds) == 1) {
925 gint32 bound0 = 0;
926 MONO_HANDLE_ARRAY_GETVAL (bound0, bounds, gint32, 0);
927 bounded = bound0 != 0;
930 MonoClass * const aklass = mono_class_create_bounded_array (klass, mono_array_handle_length (lengths), bounded);
931 uintptr_t const aklass_rank = m_class_get_rank (aklass);
932 uintptr_t * const sizes = g_newa (uintptr_t, aklass_rank);
933 intptr_t * const lower_bounds = g_newa (intptr_t, aklass_rank);
935 // Copy lengths and lower_bounds from gint32 to [u]intptr_t.
937 for (uintptr_t i = 0; i < aklass_rank; ++i) {
938 MONO_HANDLE_ARRAY_GETVAL (sizes [i], lengths, gint32, i);
939 if (!MONO_HANDLE_IS_NULL (bounds))
940 MONO_HANDLE_ARRAY_GETVAL (lower_bounds [i], bounds, gint32, i);
941 else
942 lower_bounds [i] = 0;
945 return mono_array_new_full_handle (MONO_HANDLE_DOMAIN (type), aklass, sizes, lower_bounds, error);
948 gint32
949 ves_icall_System_Array_GetRank (MonoObjectHandle arr, MonoError *error)
951 gint32 const result = m_class_get_rank (mono_handle_class (arr));
953 icallarray_print ("%s arr:%p res:%d\n", __func__, MONO_HANDLE_RAW (arr), result);
955 return result;
957 #endif
959 #ifdef ENABLE_NETCORE
960 gint32
961 ves_icall_System_Array_GetCorElementTypeOfElementType (MonoArrayHandle arr, MonoError *error)
963 MonoType *type = mono_type_get_underlying_type (m_class_get_byval_arg (m_class_get_element_class (mono_handle_class (arr))));
964 return type->type;
967 gint32
968 ves_icall_System_Array_IsValueOfElementType (MonoArrayHandle arr, MonoObjectHandle obj, MonoError *error)
970 return m_class_get_element_class (mono_handle_class (arr)) == mono_handle_class (obj);
972 #endif
974 static mono_array_size_t
975 mono_array_get_length (MonoArrayHandle arr, gint32 dimension, MonoError *error)
977 if (dimension < 0 || dimension >= m_class_get_rank (mono_handle_class (arr))) {
978 mono_error_set_index_out_of_range (error);
979 return 0;
982 return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].length)
983 : MONO_HANDLE_GETVAL (arr, max_length);
986 gint32
987 ves_icall_System_Array_GetLength (MonoArrayHandle arr, gint32 dimension, MonoError *error)
989 icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
991 mono_array_size_t const length = mono_array_get_length (arr, dimension, error);
992 if (length > G_MAXINT32) {
993 mono_error_set_overflow (error);
994 return 0;
996 return (gint32)length;
999 #ifndef ENABLE_NETCORE
1000 gint64
1001 ves_icall_System_Array_GetLongLength (MonoArrayHandle arr, gint32 dimension, MonoError *error)
1003 icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
1005 return (gint64)mono_array_get_length (arr, dimension, error);
1007 #endif
1009 gint32
1010 ves_icall_System_Array_GetLowerBound (MonoArrayHandle arr, gint32 dimension, MonoError *error)
1012 icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
1014 if (dimension < 0 || dimension >= m_class_get_rank (mono_handle_class (arr))) {
1015 mono_error_set_index_out_of_range (error);
1016 return 0;
1019 return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].lower_bound)
1020 : 0;
1023 #ifndef ENABLE_NETCORE
1024 void
1025 ves_icall_System_Array_ClearInternal (MonoArrayHandle arr, int idx, int length, MonoError *error)
1027 icallarray_print ("%s arr:%p idx:%d len:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)idx, (int)length);
1029 int sz = mono_array_element_size (mono_handle_class (arr));
1030 mono_gc_bzero_atomic (mono_array_addr_with_size_fast (MONO_HANDLE_RAW (arr), sz, idx), length * sz);
1032 #endif
1034 MonoBoolean
1035 ves_icall_System_Array_FastCopy (MonoArrayHandle source, int source_idx, MonoArrayHandle dest, int dest_idx, int length, MonoError *error)
1037 MonoVTable * const src_vtable = MONO_HANDLE_GETVAL (source, obj.vtable);
1038 MonoVTable * const dest_vtable = MONO_HANDLE_GETVAL (dest, obj.vtable);
1040 if (src_vtable->rank != dest_vtable->rank)
1041 return FALSE;
1043 MonoArrayBounds *source_bounds = MONO_HANDLE_GETVAL (source, bounds);
1044 MonoArrayBounds *dest_bounds = MONO_HANDLE_GETVAL (dest, bounds);
1046 for (int i = 0; i < src_vtable->rank; i++) {
1047 if ((source_bounds && source_bounds [i].lower_bound > 0) ||
1048 (dest_bounds && dest_bounds [i].lower_bound > 0))
1049 return FALSE;
1052 /* there's no integer overflow since mono_array_length_internal returns an unsigned integer */
1053 if ((dest_idx + length > mono_array_handle_length (dest)) ||
1054 (source_idx + length > mono_array_handle_length (source)))
1055 return FALSE;
1057 MonoClass * const src_class = m_class_get_element_class (src_vtable->klass);
1058 MonoClass * const dest_class = m_class_get_element_class (dest_vtable->klass);
1061 * Handle common cases.
1064 /* Case1: object[] -> valuetype[] (ArrayList::ToArray)
1065 We fallback to managed here since we need to typecheck each boxed valuetype before storing them in the dest array.
1067 if (src_class == mono_defaults.object_class && m_class_is_valuetype (dest_class))
1068 return FALSE;
1070 /* Check if we're copying a char[] <==> (u)short[] */
1071 if (src_class != dest_class) {
1072 if (m_class_is_valuetype (dest_class) || m_class_is_enumtype (dest_class) ||
1073 m_class_is_valuetype (src_class) || m_class_is_valuetype (src_class))
1074 return FALSE;
1076 /* 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. */
1077 if (!mono_class_is_subclass_of_internal (src_class, dest_class, FALSE))
1078 return FALSE;
1080 if (m_class_is_native_pointer (src_class) || m_class_is_native_pointer (dest_class))
1081 return FALSE;
1084 if (m_class_is_valuetype (dest_class)) {
1085 gsize const element_size = mono_array_element_size (MONO_HANDLE_GETVAL (source, obj.vtable->klass));
1087 MONO_ENTER_NO_SAFEPOINTS; // gchandle would also work here, is slow, breaks profiler tests.
1089 gconstpointer const source_addr =
1090 mono_array_addr_with_size_fast (MONO_HANDLE_RAW (source), element_size, source_idx);
1091 if (m_class_has_references (dest_class)) {
1092 mono_value_copy_array_handle (dest, dest_idx, source_addr, length);
1093 } else {
1094 gpointer const dest_addr =
1095 mono_array_addr_with_size_fast (MONO_HANDLE_RAW (dest), element_size, dest_idx);
1096 mono_gc_memmove_atomic (dest_addr, source_addr, element_size * length);
1099 MONO_EXIT_NO_SAFEPOINTS;
1100 } else {
1101 mono_array_handle_memcpy_refs (dest, dest_idx, source, source_idx, length);
1104 return TRUE;
1107 void
1108 ves_icall_System_Array_GetGenericValue_icall (MonoArray **arr, guint32 pos, gpointer value)
1110 icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__, *arr, pos, value);
1112 MONO_REQ_GC_UNSAFE_MODE; // because of gpointer value
1114 MonoClass * const ac = mono_object_class (*arr);
1115 gsize const esize = mono_array_element_size (ac);
1116 gconstpointer * const ea = (gconstpointer*)((char*)(*arr)->vector + (pos * esize));
1118 mono_gc_memmove_atomic (value, ea, esize);
1121 void
1122 ves_icall_System_Array_SetGenericValue_icall (MonoArray **arr, guint32 pos, gpointer value)
1124 icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__, *arr, pos, value);
1126 MONO_REQ_GC_UNSAFE_MODE; // because of gpointer value
1128 MonoClass * const ac = mono_object_class (*arr);
1129 MonoClass * const ec = m_class_get_element_class (ac);
1131 gsize const esize = mono_array_element_size (ac);
1132 gpointer * const ea = (gpointer*)((char*)(*arr)->vector + (pos * esize));
1134 if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (ec))) {
1135 g_assert (esize == sizeof (gpointer));
1136 mono_gc_wbarrier_generic_store_internal (ea, *(MonoObject **)value);
1137 } else {
1138 g_assert (m_class_is_inited (ec));
1139 g_assert (esize == mono_class_value_size (ec, NULL));
1140 if (m_class_has_references (ec))
1141 mono_gc_wbarrier_value_copy_internal (ea, value, 1, ec);
1142 else
1143 mono_gc_memmove_atomic (ea, value, esize);
1147 void
1148 #if ENABLE_NETCORE
1149 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, size_t byte_count)
1150 #else
1151 ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, guint byte_count)
1152 #endif
1154 mono_gc_memmove_atomic (destination, source, byte_count);
1157 #if ENABLE_NETCORE
1158 void
1159 ves_icall_System_Buffer_BulkMoveWithWriteBarrier (guint8 *destination, guint8 *source, size_t len, MonoType *type)
1161 if (MONO_TYPE_IS_REFERENCE (type))
1162 mono_gc_wbarrier_arrayref_copy_internal (destination, source, (guint)len);
1163 else
1164 mono_gc_wbarrier_value_copy_internal (destination, source, (guint)len, mono_class_from_mono_type_internal (type));
1166 #else
1167 void
1168 ves_icall_System_Runtime_RuntimeImports_Memmove_wbarrier (guint8 *destination, guint8 *source, guint len, MonoType *type)
1170 if (MONO_TYPE_IS_REFERENCE (type))
1171 mono_gc_wbarrier_arrayref_copy_internal (destination, source, len);
1172 else
1173 mono_gc_wbarrier_value_copy_internal (destination, source, len, mono_class_from_mono_type_internal (type));
1175 #endif
1177 void
1178 #if ENABLE_NETCORE
1179 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, size_t byte_length)
1180 #else
1181 ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, guint byte_length)
1182 #endif
1184 memset (p, 0, byte_length);
1187 void
1188 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArrayHandle array, MonoClassField *field_handle, MonoError *error)
1190 MonoClass *klass = mono_handle_class (array);
1191 guint32 size = mono_array_element_size (klass);
1192 MonoType *type = mono_type_get_underlying_type (m_class_get_byval_arg (m_class_get_element_class (klass)));
1193 int align;
1194 const char *field_data;
1196 if (MONO_TYPE_IS_REFERENCE (type) || type->type == MONO_TYPE_VALUETYPE) {
1197 mono_error_set_argument (error, "array", "Cannot initialize array of non-primitive type");
1198 return;
1201 MonoType *field_type = mono_field_get_type_checked (field_handle, error);
1202 if (!field_type)
1203 return;
1205 if (!(field_type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)) {
1206 mono_error_set_argument_format (error, "field_handle", "Field '%s' doesn't have an RVA", mono_field_get_name (field_handle));
1207 return;
1210 size *= MONO_HANDLE_GETVAL(array, max_length);
1211 field_data = mono_field_get_data (field_handle);
1213 if (size > mono_type_size (field_handle->type, &align)) {
1214 mono_error_set_argument (error, "field_handle", "Field not large enough to fill array");
1215 return;
1218 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
1219 #define SWAP(n) { \
1220 guint ## n *data = (guint ## n *) mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0); \
1221 guint ## n *src = (guint ## n *) field_data; \
1222 int i, \
1223 nEnt = (size / sizeof(guint ## n)); \
1225 for (i = 0; i < nEnt; i++) { \
1226 data[i] = read ## n (&src[i]); \
1230 /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
1232 switch (type->type) {
1233 case MONO_TYPE_CHAR:
1234 case MONO_TYPE_I2:
1235 case MONO_TYPE_U2:
1236 SWAP (16);
1237 break;
1238 case MONO_TYPE_I4:
1239 case MONO_TYPE_U4:
1240 case MONO_TYPE_R4:
1241 SWAP (32);
1242 break;
1243 case MONO_TYPE_I8:
1244 case MONO_TYPE_U8:
1245 case MONO_TYPE_R8:
1246 SWAP (64);
1247 break;
1248 default:
1249 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
1250 break;
1252 #else
1253 memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
1254 #endif
1257 gint
1258 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
1260 return offsetof (MonoString, chars);
1263 MonoObjectHandle
1264 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObjectHandle obj, MonoError *error)
1266 if (MONO_HANDLE_IS_NULL (obj) || !m_class_is_valuetype (mono_handle_class (obj)))
1267 return obj;
1269 return mono_object_clone_handle (obj, error);
1272 void
1273 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle, MonoError *error)
1275 MonoClass *klass;
1276 MonoVTable *vtable;
1278 MONO_CHECK_ARG_NULL (handle,);
1280 klass = mono_class_from_mono_type_internal (handle);
1281 MONO_CHECK_ARG (handle, klass,);
1283 if (mono_class_is_gtd (klass))
1284 return;
1286 vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
1287 return_if_nok (error);
1289 /* This will call the type constructor */
1290 mono_runtime_class_init_full (vtable, error);
1293 void
1294 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (MonoImage *image, MonoError *error)
1296 mono_image_check_for_module_cctor (image);
1297 if (!image->has_module_cctor)
1298 return;
1300 MonoClass *module_klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | 1, error);
1301 return_if_nok (error);
1303 MonoVTable * vtable = mono_class_vtable_checked (mono_domain_get (), module_klass, error);
1304 return_if_nok (error);
1306 mono_runtime_class_init_full (vtable, error);
1309 #ifdef ENABLE_NETCORE
1310 MonoBoolean
1311 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
1313 MonoThreadInfo *thread = mono_thread_info_current ();
1314 void *current = &thread;
1316 // Stack upper/lower bound should have been calculated and set as part of register_thread.
1317 // If not, we are optimistic and assume there is enough room.
1318 if (!thread->stack_start_limit || !thread->stack_end)
1319 return TRUE;
1321 // Stack start limit is stack lower bound. Make sure there is enough room left.
1322 void *limit = ((uint8_t *)thread->stack_start_limit) + ALIGN_TO (MONO_STACK_OVERFLOW_GUARD_SIZE + MONO_MIN_EXECUTION_STACK_SIZE, ((gssize)mono_pagesize ()));
1324 if (current < limit)
1325 return FALSE;
1327 return TRUE;
1329 #else
1330 MonoBoolean
1331 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
1333 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
1334 // It does not work on win32
1335 #elif defined(TARGET_ANDROID) || defined(__linux__)
1336 // No need for now
1337 #else
1338 guint8 *stack_addr;
1339 guint8 *current;
1340 size_t stack_size;
1341 int min_size;
1342 MonoInternalThread *thread;
1344 mono_thread_info_get_stack_bounds (&stack_addr, &stack_size);
1345 /* if we have no info we are optimistic and assume there is enough room */
1346 if (!stack_addr)
1347 return TRUE;
1349 thread = mono_thread_internal_current ();
1350 // .net seems to check that at least 50% of stack is available
1351 min_size = thread->stack_size / 2;
1353 // TODO: It's not always set
1354 if (!min_size)
1355 return TRUE;
1357 current = (guint8 *)&stack_addr;
1358 if (current > stack_addr) {
1359 if ((current - stack_addr) < min_size)
1360 return FALSE;
1361 } else {
1362 if (current - (stack_addr - stack_size) < min_size)
1363 return FALSE;
1365 #endif
1366 return TRUE;
1368 #endif
1370 #ifdef ENABLE_NETCORE
1371 MonoObjectHandle
1372 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetUninitializedObjectInternal (MonoType *handle, MonoError *error)
1374 MonoClass *klass;
1376 g_assert (handle);
1378 klass = mono_class_from_mono_type_internal (handle);
1379 if (m_class_is_string (klass)) {
1380 mono_error_set_argument (error, NULL, NULL);
1381 return NULL_HANDLE;
1384 if (m_class_is_abstract (klass) || m_class_is_interface (klass) || m_class_is_gtd (klass)) {
1385 mono_error_set_member_access (error, NULL, NULL);
1386 return NULL_HANDLE;
1389 if (m_class_is_byreflike (klass)) {
1390 mono_error_set_not_supported (error, NULL, NULL);
1391 return NULL_HANDLE;
1394 if (m_class_is_nullable (klass))
1395 return mono_object_new_handle (mono_domain_get (), m_class_get_nullable_elem_class (klass), error);
1396 else
1397 return mono_object_new_handle (mono_domain_get (), klass, error);
1400 void
1401 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_PrepareMethod (MonoMethod *method, gpointer inst_types, int n_inst_types, MonoError *error)
1403 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT) {
1404 mono_error_set_argument (error, NULL, NULL);
1405 return;
1408 MonoGenericContainer *container = NULL;
1409 if (method->is_generic)
1410 container = mono_method_get_generic_container (method);
1411 else if (m_class_is_gtd (method->klass))
1412 container = mono_class_get_generic_container (method->klass);
1413 if (container) {
1414 int nparams = container->type_argc + (container->parent ? container->parent->type_argc : 0);
1415 if (nparams != n_inst_types) {
1416 mono_error_set_argument (error, NULL, NULL);
1417 return;
1421 // FIXME: Implement
1423 #endif
1425 MonoObjectHandle
1426 ves_icall_System_Object_MemberwiseClone (MonoObjectHandle this_obj, MonoError *error)
1428 return mono_object_clone_handle (this_obj, error);
1431 gint32
1432 ves_icall_System_ValueType_InternalGetHashCode (MonoObjectHandle this_obj, MonoArrayHandleOut fields, MonoError *error)
1434 MonoClass *klass;
1435 MonoClassField **unhandled = NULL;
1436 int count = 0;
1437 gint32 result = (int)(gsize)mono_defaults.int32_class;
1438 MonoClassField* field;
1439 gpointer iter;
1441 klass = mono_handle_class (this_obj);
1443 if (mono_class_num_fields (klass) == 0)
1444 return result;
1447 * Compute the starting value of the hashcode for fields of primitive
1448 * types, and return the remaining fields in an array to the managed side.
1449 * This way, we can avoid costly reflection operations in managed code.
1451 iter = NULL;
1452 while ((field = mono_class_get_fields_internal (klass, &iter))) {
1453 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1454 continue;
1455 if (mono_field_is_deleted (field))
1456 continue;
1457 gpointer addr = (guint8*)MONO_HANDLE_RAW (this_obj) + field->offset;
1458 /* FIXME: Add more types */
1459 switch (field->type->type) {
1460 case MONO_TYPE_I4:
1461 result ^= *(gint32*)addr;
1462 break;
1463 case MONO_TYPE_PTR:
1464 result ^= mono_aligned_addr_hash (*(gpointer*)addr);
1465 break;
1466 case MONO_TYPE_STRING: {
1467 MonoString *s;
1468 s = *(MonoString**)addr;
1469 if (s != NULL)
1470 result ^= mono_string_hash_internal (s);
1471 break;
1473 default:
1474 if (!unhandled)
1475 unhandled = g_newa (MonoClassField*, mono_class_num_fields (klass));
1476 unhandled [count ++] = field;
1480 if (unhandled) {
1481 MonoArrayHandle fields_arr = mono_array_new_handle (mono_domain_get (), mono_defaults.object_class, count, error);
1482 return_val_if_nok (error, 0);
1483 MONO_HANDLE_ASSIGN (fields, fields_arr);
1484 MonoObjectHandle h = MONO_HANDLE_NEW (MonoObject, NULL);
1485 for (int i = 0; i < count; ++i) {
1486 MonoObject *o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (this_obj), error);
1487 return_val_if_nok (error, 0);
1488 MONO_HANDLE_ASSIGN_RAW (h, o);
1489 mono_array_handle_setref (fields_arr, i, h);
1491 } else {
1492 MONO_HANDLE_ASSIGN (fields, NULL_HANDLE);
1494 return result;
1497 MonoBoolean
1498 ves_icall_System_ValueType_Equals (MonoObjectHandle this_obj, MonoObjectHandle that, MonoArrayHandleOut fields, MonoError *error)
1500 MonoClass *klass;
1501 MonoClassField **unhandled = NULL;
1502 MonoClassField* field;
1503 gpointer iter;
1504 int count = 0;
1506 MONO_CHECK_ARG_NULL_HANDLE (that, FALSE);
1508 MONO_HANDLE_ASSIGN (fields, NULL_HANDLE);
1510 if (mono_handle_vtable (this_obj) != mono_handle_vtable (that))
1511 return FALSE;
1513 klass = mono_handle_class (this_obj);
1515 if (m_class_is_enumtype (klass) && mono_class_enum_basetype_internal (klass) && mono_class_enum_basetype_internal (klass)->type == MONO_TYPE_I4)
1516 return *(gint32*)mono_handle_get_data_unsafe (this_obj) == *(gint32*)mono_handle_get_data_unsafe (that);
1519 * Do the comparison for fields of primitive type and return a result if
1520 * possible. Otherwise, return the remaining fields in an array to the
1521 * managed side. This way, we can avoid costly reflection operations in
1522 * managed code.
1524 iter = NULL;
1525 while ((field = mono_class_get_fields_internal (klass, &iter))) {
1526 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1527 continue;
1528 if (mono_field_is_deleted (field))
1529 continue;
1530 guint8 *this_field = (guint8 *)MONO_HANDLE_RAW (this_obj) + field->offset;
1531 guint8 *that_field = (guint8 *)MONO_HANDLE_RAW (that) + field->offset;
1533 #define UNALIGNED_COMPARE(type) \
1534 do { \
1535 type left, right; \
1536 memcpy (&left, this_field, sizeof (type)); \
1537 memcpy (&right, that_field, sizeof (type)); \
1538 if (left != right) \
1539 return FALSE; \
1540 } while (0)
1542 /* FIXME: Add more types */
1543 switch (field->type->type) {
1544 case MONO_TYPE_U1:
1545 case MONO_TYPE_I1:
1546 case MONO_TYPE_BOOLEAN:
1547 if (*this_field != *that_field)
1548 return FALSE;
1549 break;
1550 case MONO_TYPE_U2:
1551 case MONO_TYPE_I2:
1552 case MONO_TYPE_CHAR:
1553 #ifdef NO_UNALIGNED_ACCESS
1554 if (G_UNLIKELY ((intptr_t) this_field & 1 || (intptr_t) that_field & 1))
1555 UNALIGNED_COMPARE (gint16);
1556 else
1557 #endif
1558 if (*(gint16 *) this_field != *(gint16 *) that_field)
1559 return FALSE;
1560 break;
1561 case MONO_TYPE_U4:
1562 case MONO_TYPE_I4:
1563 #ifdef NO_UNALIGNED_ACCESS
1564 if (G_UNLIKELY ((intptr_t) this_field & 3 || (intptr_t) that_field & 3))
1565 UNALIGNED_COMPARE (gint32);
1566 else
1567 #endif
1568 if (*(gint32 *) this_field != *(gint32 *) that_field)
1569 return FALSE;
1570 break;
1571 case MONO_TYPE_U8:
1572 case MONO_TYPE_I8:
1573 #ifdef NO_UNALIGNED_ACCESS
1574 if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
1575 UNALIGNED_COMPARE (gint64);
1576 else
1577 #endif
1578 if (*(gint64 *) this_field != *(gint64 *) that_field)
1579 return FALSE;
1580 break;
1582 case MONO_TYPE_R4: {
1583 float d1, d2;
1584 #ifdef NO_UNALIGNED_ACCESS
1585 memcpy (&d1, this_field, sizeof (float));
1586 memcpy (&d2, that_field, sizeof (float));
1587 #else
1588 d1 = *(float *) this_field;
1589 d2 = *(float *) that_field;
1590 #endif
1591 if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
1592 return FALSE;
1593 break;
1595 case MONO_TYPE_R8: {
1596 double d1, d2;
1597 #ifdef NO_UNALIGNED_ACCESS
1598 memcpy (&d1, this_field, sizeof (double));
1599 memcpy (&d2, that_field, sizeof (double));
1600 #else
1601 d1 = *(double *) this_field;
1602 d2 = *(double *) that_field;
1603 #endif
1604 if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
1605 return FALSE;
1606 break;
1608 case MONO_TYPE_PTR:
1609 #ifdef NO_UNALIGNED_ACCESS
1610 if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
1611 UNALIGNED_COMPARE (gpointer);
1612 else
1613 #endif
1614 if (*(gpointer *) this_field != *(gpointer *) that_field)
1615 return FALSE;
1616 break;
1617 case MONO_TYPE_STRING: {
1618 MonoString *s1, *s2;
1619 guint32 s1len, s2len;
1620 s1 = *(MonoString**)this_field;
1621 s2 = *(MonoString**)that_field;
1622 if (s1 == s2)
1623 break;
1624 if ((s1 == NULL) || (s2 == NULL))
1625 return FALSE;
1626 s1len = mono_string_length_internal (s1);
1627 s2len = mono_string_length_internal (s2);
1628 if (s1len != s2len)
1629 return FALSE;
1631 if (memcmp (mono_string_chars_internal (s1), mono_string_chars_internal (s2), s1len * sizeof (gunichar2)) != 0)
1632 return FALSE;
1633 break;
1635 default:
1636 if (!unhandled)
1637 unhandled = g_newa (MonoClassField*, mono_class_num_fields (klass));
1638 unhandled [count ++] = field;
1641 #undef UNALIGNED_COMPARE
1643 if (m_class_is_enumtype (klass))
1644 /* enums only have one non-static field */
1645 break;
1648 if (unhandled) {
1649 MonoArrayHandle fields_arr = mono_array_new_handle (mono_domain_get (), mono_defaults.object_class, count * 2, error);
1650 return_val_if_nok (error, 0);
1651 MONO_HANDLE_ASSIGN (fields, fields_arr);
1652 MonoObjectHandle h = MONO_HANDLE_NEW (MonoObject, NULL);
1653 for (int i = 0; i < count; ++i) {
1654 MonoObject *o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (this_obj), error);
1655 return_val_if_nok (error, FALSE);
1656 MONO_HANDLE_ASSIGN_RAW (h, o);
1657 mono_array_handle_setref (fields_arr, i * 2, h);
1659 o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (that), error);
1660 return_val_if_nok (error, FALSE);
1661 MONO_HANDLE_ASSIGN_RAW (h, o);
1662 mono_array_handle_setref (fields_arr, (i * 2) + 1, h);
1664 return FALSE;
1665 } else {
1666 return TRUE;
1670 #ifndef ENABLE_NETCORE
1671 MonoReflectionTypeHandle
1672 ves_icall_System_Object_GetType (MonoObjectHandle obj, MonoError *error)
1674 MonoDomain *domain = MONO_HANDLE_DOMAIN (obj);
1675 MonoClass *klass = mono_handle_class (obj);
1676 #ifndef DISABLE_REMOTING
1677 if (mono_class_is_transparent_proxy (klass)) {
1678 MonoTransparentProxyHandle proxy_obj = MONO_HANDLE_CAST (MonoTransparentProxy, obj);
1679 MonoRemoteClass *remote_class = MONO_HANDLE_GETVAL (proxy_obj, remote_class);
1680 /* If it's a transparent proxy for an interface, return the
1681 * interface type, not the unhelpful proxy_class class (which
1682 * is just MarshalByRefObject). */
1683 MonoType *proxy_type =
1684 mono_remote_class_is_interface_proxy (remote_class) ?
1685 m_class_get_byval_arg (remote_class->interfaces[0]) :
1686 m_class_get_byval_arg (remote_class->proxy_class);
1687 return mono_type_get_object_handle (domain, proxy_type, error);
1688 } else
1689 #endif
1690 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
1692 #endif
1694 static gboolean
1695 get_executing (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1697 MonoMethod **dest = (MonoMethod **)data;
1699 /* skip unmanaged frames */
1700 if (!managed)
1701 return FALSE;
1703 if (!(*dest)) {
1704 if (!strcmp (m_class_get_name_space (m->klass), "System.Reflection"))
1705 return FALSE;
1706 *dest = m;
1707 return TRUE;
1709 return FALSE;
1712 static gboolean
1713 in_corlib_name_space (MonoClass *klass, const char *name_space)
1715 return m_class_get_image (klass) == mono_defaults.corlib &&
1716 !strcmp (m_class_get_name_space (klass), name_space);
1719 static gboolean
1720 get_caller_no_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1722 MonoMethod **dest = (MonoMethod **)data;
1724 /* skip unmanaged frames */
1725 if (!managed)
1726 return FALSE;
1728 if (m->wrapper_type != MONO_WRAPPER_NONE)
1729 return FALSE;
1731 if (m == *dest) {
1732 *dest = NULL;
1733 return FALSE;
1736 if (in_corlib_name_space (m->klass, "System.Reflection"))
1737 return FALSE;
1739 if (!(*dest)) {
1740 *dest = m;
1741 return TRUE;
1743 return FALSE;
1746 static gboolean
1747 get_caller_no_system_or_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
1749 MonoMethod **dest = (MonoMethod **)data;
1751 /* skip unmanaged frames */
1752 if (!managed)
1753 return FALSE;
1755 if (m->wrapper_type != MONO_WRAPPER_NONE)
1756 return FALSE;
1758 if (m == *dest) {
1759 *dest = NULL;
1760 return FALSE;
1763 if (in_corlib_name_space (m->klass, "System.Reflection") || in_corlib_name_space (m->klass, "System"))
1764 return FALSE;
1766 if (!(*dest)) {
1767 *dest = m;
1768 return TRUE;
1770 return FALSE;
1774 * mono_runtime_get_caller_no_system_or_reflection:
1776 * Walk the stack of the current thread and find the first managed method that
1777 * is not in the mscorlib System or System.Reflection namespace. This skips
1778 * unmanaged callers and wrapper methods.
1780 * \returns a pointer to the \c MonoMethod or NULL if we walked past all the
1781 * callers.
1783 MonoMethod*
1784 mono_runtime_get_caller_no_system_or_reflection (void)
1786 MonoMethod *dest = NULL;
1787 mono_stack_walk_no_il (get_caller_no_system_or_reflection, &dest);
1788 return dest;
1792 * mono_runtime_get_caller_from_stack_mark:
1794 * Walk the stack and return the assembly of the method referenced
1795 * by the stack mark STACK_MARK.
1797 MonoAssembly*
1798 mono_runtime_get_caller_from_stack_mark (MonoStackCrawlMark *stack_mark)
1800 // FIXME: Use the stack mark
1801 MonoMethod *dest = NULL;
1802 mono_stack_walk_no_il (get_caller_no_system_or_reflection, &dest);
1803 if (dest)
1804 return m_class_get_image (dest->klass)->assembly;
1805 else
1806 return NULL;
1809 static MonoReflectionTypeHandle
1810 type_from_parsed_name (MonoTypeNameParse *info, MonoStackCrawlMark *stack_mark, MonoBoolean ignoreCase, MonoAssembly **caller_assembly, MonoError *error)
1812 MonoMethod *m;
1813 MonoType *type = NULL;
1814 MonoAssembly *assembly = NULL;
1815 gboolean type_resolve = FALSE;
1816 MonoImage *rootimage = NULL;
1817 MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
1819 error_init (error);
1822 * We must compute the calling assembly as type loading must happen under a metadata context.
1823 * For example. The main assembly is a.exe and Type.GetType is called from dir/b.dll. Without
1824 * the metadata context (basedir currently) set to dir/b.dll we won't be able to load a dir/c.dll.
1826 m = mono_method_get_last_managed ();
1827 if (m && m_class_get_image (m->klass) != mono_defaults.corlib) {
1828 /* Happens with inlining */
1829 assembly = m_class_get_image (m->klass)->assembly;
1830 } else {
1831 assembly = mono_runtime_get_caller_from_stack_mark (stack_mark);
1834 if (assembly) {
1835 type_resolve = TRUE;
1836 rootimage = assembly->image;
1837 } else {
1838 // FIXME: once wasm can use stack marks, consider turning all this into an assert
1839 g_warning (G_STRLOC);
1842 *caller_assembly = assembly;
1844 if (info->assembly.name) {
1845 MonoAssemblyByNameRequest req;
1846 mono_assembly_request_prepare_byname (&req, MONO_ASMCTX_DEFAULT, alc);
1847 req.requesting_assembly = assembly;
1848 req.basedir = assembly ? assembly->basedir : NULL;
1849 assembly = mono_assembly_request_byname (&info->assembly, &req, NULL);
1852 if (assembly) {
1853 /* When loading from the current assembly, AppDomain.TypeResolve will not be called yet */
1854 type = mono_reflection_get_type_checked (alc, rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error);
1855 goto_if_nok (error, fail);
1858 // XXXX - aleksey -
1859 // Say we're looking for System.Generic.Dict<int, Local>
1860 // we FAIL the get type above, because S.G.Dict isn't in assembly->image. So we drop down here.
1861 // but then we FAIL AGAIN because now we pass null as the image and the rootimage and everything
1862 // is messed up when we go to construct the Local as the type arg...
1864 // By contrast, if we started with Mine<System.Generic.Dict<int, Local>> we'd go in with assembly->image
1865 // as the root and then even the detour into generics would still not cause issues when we went to load Local.
1866 if (!info->assembly.name && !type) {
1867 /* try mscorlib */
1868 type = mono_reflection_get_type_checked (alc, rootimage, NULL, info, ignoreCase, TRUE, &type_resolve, error);
1869 goto_if_nok (error, fail);
1871 if (assembly && !type && type_resolve) {
1872 type_resolve = FALSE; /* This will invoke TypeResolve if not done in the first 'if' */
1873 type = mono_reflection_get_type_checked (alc, rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error);
1874 goto_if_nok (error, fail);
1877 if (!type)
1878 goto fail;
1880 return mono_type_get_object_handle (mono_domain_get (), type, error);
1881 fail:
1882 return MONO_HANDLE_NEW (MonoReflectionType, NULL);
1885 MonoReflectionTypeHandle
1886 ves_icall_System_RuntimeTypeHandle_internal_from_name (MonoStringHandle name,
1887 MonoStackCrawlMark *stack_mark,
1888 MonoReflectionAssemblyHandle callerAssembly,
1889 MonoBoolean throwOnError,
1890 MonoBoolean ignoreCase,
1891 MonoBoolean reflectionOnly,
1892 MonoError *error)
1894 MonoTypeNameParse info;
1895 gboolean free_info = FALSE;
1896 MonoAssembly *caller_assembly;
1897 MonoReflectionTypeHandle type = MONO_HANDLE_NEW (MonoReflectionType, NULL);
1899 /* The callerAssembly argument is unused for now */
1901 char *str = mono_string_handle_to_utf8 (name, error);
1902 goto_if_nok (error, leave);
1904 free_info = TRUE;
1905 if (!mono_reflection_parse_type_checked (str, &info, error))
1906 goto leave;
1908 /* mono_reflection_parse_type() mangles the string */
1910 MONO_HANDLE_ASSIGN (type, type_from_parsed_name (&info, (MonoStackCrawlMark*)stack_mark, ignoreCase, &caller_assembly, error));
1912 goto_if_nok (error, leave);
1914 if (MONO_HANDLE_IS_NULL (type)) {
1915 if (throwOnError) {
1916 char *tname = info.name_space ? g_strdup_printf ("%s.%s", info.name_space, info.name) : g_strdup (info.name);
1917 char *aname;
1918 if (info.assembly.name)
1919 aname = mono_stringify_assembly_name (&info.assembly);
1920 else if (caller_assembly)
1921 aname = mono_stringify_assembly_name (mono_assembly_get_name_internal (caller_assembly));
1922 else
1923 aname = g_strdup ("");
1924 mono_error_set_type_load_name (error, tname, aname, "");
1926 goto leave;
1929 leave:
1930 if (free_info)
1931 mono_reflection_free_type_info (&info);
1932 g_free (str);
1933 if (!is_ok (error)) {
1934 if (!throwOnError) {
1935 mono_error_cleanup (error);
1936 error_init (error);
1938 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
1939 } else
1940 return type;
1944 MonoReflectionTypeHandle
1945 ves_icall_System_Type_internal_from_handle (MonoType *handle, MonoError *error)
1947 MonoDomain *domain = mono_domain_get ();
1949 return mono_type_get_object_handle (domain, handle, error);
1952 MonoType*
1953 ves_icall_Mono_RuntimeClassHandle_GetTypeFromClass (MonoClass *klass)
1955 return m_class_get_byval_arg (klass);
1958 void
1959 ves_icall_Mono_RuntimeGPtrArrayHandle_GPtrArrayFree (GPtrArray *ptr_array)
1961 g_ptr_array_free (ptr_array, TRUE);
1964 void
1965 ves_icall_Mono_SafeStringMarshal_GFree (void *c_str)
1967 g_free (c_str);
1970 char*
1971 ves_icall_Mono_SafeStringMarshal_StringToUtf8 (MonoString *volatile* s)
1973 ERROR_DECL (error);
1974 char *result = mono_string_to_utf8_checked_internal (*s, error);
1975 mono_error_set_pending_exception (error);
1976 return result;
1979 /* System.TypeCode */
1980 typedef enum {
1981 TYPECODE_EMPTY,
1982 TYPECODE_OBJECT,
1983 TYPECODE_DBNULL,
1984 TYPECODE_BOOLEAN,
1985 TYPECODE_CHAR,
1986 TYPECODE_SBYTE,
1987 TYPECODE_BYTE,
1988 TYPECODE_INT16,
1989 TYPECODE_UINT16,
1990 TYPECODE_INT32,
1991 TYPECODE_UINT32,
1992 TYPECODE_INT64,
1993 TYPECODE_UINT64,
1994 TYPECODE_SINGLE,
1995 TYPECODE_DOUBLE,
1996 TYPECODE_DECIMAL,
1997 TYPECODE_DATETIME,
1998 TYPECODE_STRING = 18
1999 } TypeCode;
2001 #ifndef ENABLE_NETCORE
2002 guint32
2003 ves_icall_type_GetTypeCodeInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
2005 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2007 if (type->byref)
2008 return TYPECODE_OBJECT;
2010 handle_enum:
2011 switch (type->type) {
2012 case MONO_TYPE_VOID:
2013 return TYPECODE_OBJECT;
2014 case MONO_TYPE_BOOLEAN:
2015 return TYPECODE_BOOLEAN;
2016 case MONO_TYPE_U1:
2017 return TYPECODE_BYTE;
2018 case MONO_TYPE_I1:
2019 return TYPECODE_SBYTE;
2020 case MONO_TYPE_U2:
2021 return TYPECODE_UINT16;
2022 case MONO_TYPE_I2:
2023 return TYPECODE_INT16;
2024 case MONO_TYPE_CHAR:
2025 return TYPECODE_CHAR;
2026 case MONO_TYPE_PTR:
2027 case MONO_TYPE_U:
2028 case MONO_TYPE_I:
2029 return TYPECODE_OBJECT;
2030 case MONO_TYPE_U4:
2031 return TYPECODE_UINT32;
2032 case MONO_TYPE_I4:
2033 return TYPECODE_INT32;
2034 case MONO_TYPE_U8:
2035 return TYPECODE_UINT64;
2036 case MONO_TYPE_I8:
2037 return TYPECODE_INT64;
2038 case MONO_TYPE_R4:
2039 return TYPECODE_SINGLE;
2040 case MONO_TYPE_R8:
2041 return TYPECODE_DOUBLE;
2042 case MONO_TYPE_VALUETYPE: {
2043 MonoClass *klass = type->data.klass;
2045 if (m_class_is_enumtype (klass)) {
2046 type = mono_class_enum_basetype_internal (klass);
2047 goto handle_enum;
2048 } else if (mono_is_corlib_image (m_class_get_image (klass))) {
2049 if (strcmp (m_class_get_name_space (klass), "System") == 0) {
2050 if (strcmp (m_class_get_name (klass), "Decimal") == 0)
2051 return TYPECODE_DECIMAL;
2052 else if (strcmp (m_class_get_name (klass), "DateTime") == 0)
2053 return TYPECODE_DATETIME;
2056 return TYPECODE_OBJECT;
2058 case MONO_TYPE_STRING:
2059 return TYPECODE_STRING;
2060 case MONO_TYPE_SZARRAY:
2061 case MONO_TYPE_ARRAY:
2062 case MONO_TYPE_OBJECT:
2063 case MONO_TYPE_VAR:
2064 case MONO_TYPE_MVAR:
2065 case MONO_TYPE_TYPEDBYREF:
2066 return TYPECODE_OBJECT;
2067 case MONO_TYPE_CLASS:
2069 MonoClass *klass = type->data.klass;
2070 if (m_class_get_image (klass) == mono_defaults.corlib && strcmp (m_class_get_name_space (klass), "System") == 0) {
2071 if (strcmp (m_class_get_name (klass), "DBNull") == 0)
2072 return TYPECODE_DBNULL;
2075 return TYPECODE_OBJECT;
2076 case MONO_TYPE_GENERICINST:
2077 if (m_class_is_enumtype (type->data.generic_class->container_class)) {
2078 type = mono_class_enum_basetype_internal (type->data.generic_class->container_class);
2079 goto handle_enum;
2081 return TYPECODE_OBJECT;
2082 default:
2083 g_error ("type 0x%02x not handled in GetTypeCode()", type->type);
2085 return 0;
2087 #endif
2089 guint32
2090 ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_c, MonoError *error)
2092 g_assert (!MONO_HANDLE_IS_NULL (ref_type));
2094 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2095 MonoClass *klass = mono_class_from_mono_type_internal (type);
2096 MonoType *ctype = MONO_HANDLE_GETVAL (ref_c, type);
2097 MonoClass *klassc = mono_class_from_mono_type_internal (ctype);
2099 if (type->byref ^ ctype->byref)
2100 return FALSE;
2102 if (type->byref) {
2103 return mono_byref_type_is_assignable_from (type, ctype, FALSE);
2106 gboolean result;
2107 mono_class_is_assignable_from_checked (klass, klassc, &result, error);
2108 return (guint32)result;
2111 MonoBoolean
2112 ves_icall_RuntimeTypeHandle_is_subclass_of (MonoType *childType, MonoType *baseType)
2114 ERROR_DECL (error);
2115 mono_bool result = FALSE;
2116 MonoClass *childClass;
2117 MonoClass *baseClass;
2119 childClass = mono_class_from_mono_type_internal (childType);
2120 baseClass = mono_class_from_mono_type_internal (baseType);
2122 if (G_UNLIKELY (childType->byref)) {
2123 result = !baseType->byref && baseClass == mono_defaults.object_class;
2124 goto done;
2127 if (G_UNLIKELY (baseType->byref)) {
2128 result = FALSE;
2129 goto done;
2132 if (childType == baseType) {
2133 /* .NET IsSubclassOf is not reflexive */
2134 result = FALSE;
2135 goto done;
2138 if (G_UNLIKELY (is_generic_parameter (childType))) {
2139 /* slow path: walk the type hierarchy looking at base types
2140 * until we see baseType. If the current type is not a gparam,
2141 * break out of the loop and use is_subclass_of.
2143 MonoClass *c = mono_generic_param_get_base_type (childClass);
2145 result = FALSE;
2146 while (c != NULL) {
2147 if (c == baseClass) {
2148 result = TRUE;
2149 break;
2151 if (!is_generic_parameter (m_class_get_byval_arg (c))) {
2152 result = mono_class_is_subclass_of_internal (c, baseClass, FALSE);
2153 break;
2154 } else
2155 c = mono_generic_param_get_base_type (c);
2157 } else {
2158 result = mono_class_is_subclass_of_internal (childClass, baseClass, FALSE);
2160 done:
2161 mono_error_set_pending_exception (error);
2162 return result;
2165 guint32
2166 ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type, MonoObjectHandle obj, MonoError *error)
2168 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2169 MonoClass *klass = mono_class_from_mono_type_internal (type);
2170 mono_class_init_checked (klass, error);
2171 return_val_if_nok (error, FALSE);
2172 MonoObjectHandle inst = mono_object_handle_isinst (obj, klass, error);
2173 return_val_if_nok (error, FALSE);
2174 return !MONO_HANDLE_IS_NULL (inst);
2177 guint32
2178 ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionTypeHandle ref_type, MonoError *error)
2180 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2182 #ifdef ENABLE_NETCORE
2183 if (type->byref || type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR)
2184 return TYPE_ATTRIBUTE_NOT_PUBLIC;
2185 #endif
2187 MonoClass *klass = mono_class_from_mono_type_internal (type);
2188 return mono_class_get_flags (klass);
2191 MonoReflectionMarshalAsAttributeHandle
2192 ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionFieldHandle field_h, MonoError *error)
2194 MonoDomain *domain = MONO_HANDLE_DOMAIN (field_h);
2195 MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
2196 MonoClass *klass = field->parent;
2198 MonoGenericClass *gklass = mono_class_try_get_generic_class (klass);
2199 if (mono_class_is_gtd (klass) ||
2200 (gklass && gklass->context.class_inst->is_open))
2201 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
2203 MonoType *ftype = mono_field_get_type_internal (field);
2204 if (ftype && !(ftype->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL))
2205 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
2207 MonoMarshalType *info = mono_marshal_load_type_info (klass);
2209 for (int i = 0; i < info->num_fields; ++i) {
2210 if (info->fields [i].field == field) {
2211 if (!info->fields [i].mspec)
2212 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
2213 else {
2214 return mono_reflection_marshal_as_attribute_from_marshal_spec (domain, klass, info->fields [i].mspec, error);
2219 return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
2222 MonoReflectionFieldHandle
2223 ves_icall_System_Reflection_FieldInfo_internal_from_handle_type (MonoClassField *handle, MonoType *type, MonoError *error)
2225 MonoClass *klass;
2227 g_assert (handle);
2229 if (!type) {
2230 klass = handle->parent;
2231 } else {
2232 klass = mono_class_from_mono_type_internal (type);
2234 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
2236 if (!found)
2237 /* The managed code will throw the exception */
2238 return MONO_HANDLE_CAST (MonoReflectionField, NULL_HANDLE);
2241 return mono_field_get_object_handle (mono_domain_get (), klass, handle, error);
2244 MonoReflectionEventHandle
2245 ves_icall_System_Reflection_EventInfo_internal_from_handle_type (MonoEvent *handle, MonoType *type, MonoError *error)
2247 MonoClass *klass;
2249 g_assert (handle);
2251 if (!type) {
2252 klass = handle->parent;
2253 } else {
2254 klass = mono_class_from_mono_type_internal (type);
2256 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
2257 if (!found)
2258 /* Managed code will throw an exception */
2259 return MONO_HANDLE_CAST (MonoReflectionEvent, NULL_HANDLE);
2262 return mono_event_get_object_handle (mono_domain_get (), klass, handle, error);
2265 MonoReflectionPropertyHandle
2266 ves_icall_System_Reflection_RuntimePropertyInfo_internal_from_handle_type (MonoProperty *handle, MonoType *type, MonoError *error)
2268 MonoClass *klass;
2270 g_assert (handle);
2272 if (!type) {
2273 klass = handle->parent;
2274 } else {
2275 klass = mono_class_from_mono_type_internal (type);
2277 gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
2278 if (!found)
2279 /* Managed code will throw an exception */
2280 return MONO_HANDLE_CAST (MonoReflectionProperty, NULL_HANDLE);
2283 return mono_property_get_object_handle (mono_domain_get (), klass, handle, error);
2286 MonoArrayHandle
2287 ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionFieldHandle field_h, MonoBoolean optional, MonoError *error)
2289 MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
2291 MonoType *type = mono_field_get_type_checked (field, error);
2292 return_val_if_nok (error, NULL_HANDLE_ARRAY);
2294 return type_array_from_modifiers (type, optional, error);
2298 ves_icall_get_method_attributes (MonoMethod *method)
2300 return method->flags;
2303 void
2304 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info, MonoError *error)
2306 MonoDomain *domain = mono_domain_get ();
2308 MonoMethodSignature* sig = mono_method_signature_checked (method, error);
2309 return_if_nok (error);
2311 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (method->klass), error);
2312 return_if_nok (error);
2314 MONO_STRUCT_SETREF_INTERNAL (info, parent, MONO_HANDLE_RAW (rt));
2316 MONO_HANDLE_ASSIGN (rt, mono_type_get_object_handle (domain, sig->ret, error));
2317 return_if_nok (error);
2319 MONO_STRUCT_SETREF_INTERNAL (info, ret, MONO_HANDLE_RAW (rt));
2321 info->attrs = method->flags;
2322 info->implattrs = method->iflags;
2323 guint32 callconv;
2324 if (sig->call_convention == MONO_CALL_DEFAULT)
2325 callconv = sig->sentinelpos >= 0 ? 2 : 1;
2326 else {
2327 if (sig->call_convention == MONO_CALL_VARARG || sig->sentinelpos >= 0)
2328 callconv = 2;
2329 else
2330 callconv = 1;
2332 callconv |= (sig->hasthis << 5) | (sig->explicit_this << 6);
2333 info->callconv = callconv;
2336 MonoArrayHandle
2337 ves_icall_System_Reflection_MonoMethodInfo_get_parameter_info (MonoMethod *method, MonoReflectionMethodHandle member, MonoError *error)
2339 MonoDomain *domain = mono_domain_get ();
2341 MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
2342 MONO_HANDLE_GET (reftype, member, reftype);
2343 MonoClass *klass = NULL;
2344 if (!MONO_HANDLE_IS_NULL (reftype))
2345 klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (reftype, type));
2346 return mono_param_get_objects_internal (domain, method, klass, error);
2349 MonoReflectionMarshalAsAttributeHandle
2350 ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod *method, MonoError *error)
2352 MonoDomain *domain = mono_domain_get ();
2353 MonoReflectionMarshalAsAttributeHandle res = MONO_HANDLE_NEW (MonoReflectionMarshalAsAttribute, NULL);
2355 MonoMarshalSpec **mspecs = g_new (MonoMarshalSpec*, mono_method_signature_internal (method)->param_count + 1);
2356 mono_method_get_marshal_info (method, mspecs);
2358 if (mspecs [0]) {
2359 MONO_HANDLE_ASSIGN (res, mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [0], error));
2360 goto_if_nok (error, leave);
2363 leave:
2364 for (int i = mono_method_signature_internal (method)->param_count; i >= 0; i--)
2365 if (mspecs [i])
2366 mono_metadata_free_marshal_spec (mspecs [i]);
2367 g_free (mspecs);
2369 return res;
2372 gint32
2373 ves_icall_RuntimeFieldInfo_GetFieldOffset (MonoReflectionFieldHandle field, MonoError *error)
2375 MonoClassField *class_field = MONO_HANDLE_GETVAL (field, field);
2376 mono_class_setup_fields (class_field->parent);
2378 return class_field->offset - MONO_ABI_SIZEOF (MonoObject);
2381 MonoReflectionTypeHandle
2382 ves_icall_RuntimeFieldInfo_GetParentType (MonoReflectionFieldHandle field, MonoBoolean declaring, MonoError *error)
2384 MonoDomain *domain = MONO_HANDLE_DOMAIN (field);
2385 MonoClass *parent;
2387 if (declaring) {
2388 MonoClassField *f = MONO_HANDLE_GETVAL (field, field);
2389 parent = f->parent;
2390 } else {
2391 parent = MONO_HANDLE_GETVAL (field, klass);
2394 return mono_type_get_object_handle (domain, m_class_get_byval_arg (parent), error);
2397 MonoObjectHandle
2398 ves_icall_RuntimeFieldInfo_GetValueInternal (MonoReflectionFieldHandle field_handle, MonoObjectHandle obj_handle, MonoError *error)
2400 MonoReflectionField * const field = MONO_HANDLE_RAW (field_handle);
2401 MonoClass *fklass = field->klass;
2402 MonoClassField *cf = field->field;
2404 if (mono_asmctx_get_kind (&m_class_get_image (fklass)->assembly->context) == MONO_ASMCTX_REFONLY) {
2405 mono_error_set_invalid_operation (error,
2406 "It is illegal to get the value on a field on a type loaded using the ReflectionOnly methods.");
2407 return NULL_HANDLE;
2410 if (mono_security_core_clr_enabled () &&
2411 !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
2412 return NULL_HANDLE;
2415 MonoObject * const obj = MONO_HANDLE_RAW (obj_handle);
2416 MonoObject *result;
2418 #ifndef DISABLE_REMOTING
2419 if (G_UNLIKELY (obj != NULL && mono_class_is_transparent_proxy (mono_object_class (obj)))) {
2420 /* We get here if someone used a
2421 * System.Reflection.FieldInfo:GetValue on a
2422 * ContextBoundObject's or cross-domain MarshalByRefObject's
2423 * transparent proxy. */
2424 result = mono_load_remote_field_new_checked (obj, fklass, cf, error);
2425 } else
2426 #endif
2427 result = mono_field_get_value_object_checked (mono_object_domain (field), cf, obj, error);
2429 return MONO_HANDLE_NEW (MonoObject, result);
2432 void
2433 ves_icall_RuntimeFieldInfo_SetValueInternal (MonoReflectionFieldHandle field, MonoObjectHandle obj, MonoObjectHandle value, MonoError *error)
2435 MonoClassField *cf = MONO_HANDLE_GETVAL (field, field);
2437 MonoClass *field_klass = MONO_HANDLE_GETVAL (field, klass);
2438 if (mono_asmctx_get_kind (&m_class_get_image (field_klass)->assembly->context) == MONO_ASMCTX_REFONLY) {
2439 mono_error_set_invalid_operation (error, "It is illegal to set the value on a field on a type loaded using the ReflectionOnly methods.");
2440 return;
2443 if (mono_security_core_clr_enabled () &&
2444 !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
2445 return;
2448 #ifndef DISABLE_REMOTING
2449 if (G_UNLIKELY (!MONO_HANDLE_IS_NULL (obj) && mono_class_is_transparent_proxy (mono_handle_class (obj)))) {
2450 /* We get here if someone used a
2451 * System.Reflection.FieldInfo:SetValue on a
2452 * ContextBoundObject's or cross-domain MarshalByRefObject's
2453 * transparent proxy. */
2454 /* FIXME: use handles for mono_store_remote_field_new_checked */
2455 MonoObject *v = MONO_HANDLE_RAW (value);
2456 MonoObject *o = MONO_HANDLE_RAW (obj);
2457 mono_store_remote_field_new_checked (o, field_klass, cf, v, error);
2458 return;
2460 #endif
2462 MonoType *type = mono_field_get_type_checked (cf, error);
2463 return_if_nok (error);
2465 gboolean isref = FALSE;
2466 MonoGCHandle value_gchandle = 0;
2467 gchar *v = NULL;
2468 if (!type->byref) {
2469 switch (type->type) {
2470 case MONO_TYPE_U1:
2471 case MONO_TYPE_I1:
2472 case MONO_TYPE_BOOLEAN:
2473 case MONO_TYPE_U2:
2474 case MONO_TYPE_I2:
2475 case MONO_TYPE_CHAR:
2476 case MONO_TYPE_U:
2477 case MONO_TYPE_I:
2478 case MONO_TYPE_U4:
2479 case MONO_TYPE_I4:
2480 case MONO_TYPE_R4:
2481 case MONO_TYPE_U8:
2482 case MONO_TYPE_I8:
2483 case MONO_TYPE_R8:
2484 case MONO_TYPE_VALUETYPE:
2485 case MONO_TYPE_PTR:
2486 isref = FALSE;
2487 if (!MONO_HANDLE_IS_NULL (value))
2488 v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
2489 break;
2490 case MONO_TYPE_STRING:
2491 case MONO_TYPE_OBJECT:
2492 case MONO_TYPE_CLASS:
2493 case MONO_TYPE_ARRAY:
2494 case MONO_TYPE_SZARRAY:
2495 /* Do nothing */
2496 isref = TRUE;
2497 break;
2498 case MONO_TYPE_GENERICINST: {
2499 MonoGenericClass *gclass = type->data.generic_class;
2500 g_assert (!gclass->context.class_inst->is_open);
2502 if (mono_class_is_nullable (mono_class_from_mono_type_internal (type))) {
2503 MonoClass *nklass = mono_class_from_mono_type_internal (type);
2506 * Convert the boxed vtype into a Nullable structure.
2507 * This is complicated by the fact that Nullables have
2508 * a variable structure.
2510 MonoObjectHandle nullable = mono_object_new_handle (mono_domain_get (), nklass, error);
2511 return_if_nok (error);
2513 MonoGCHandle nullable_gchandle = 0;
2514 guint8 *nval = (guint8*)mono_object_handle_pin_unbox (nullable, &nullable_gchandle);
2515 mono_nullable_init_from_handle (nval, value, nklass);
2517 isref = FALSE;
2518 value_gchandle = nullable_gchandle;
2519 v = (gchar*)nval;
2521 else {
2522 isref = !m_class_is_valuetype (gclass->container_class);
2523 if (!isref && !MONO_HANDLE_IS_NULL (value)) {
2524 v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
2527 break;
2529 default:
2530 g_error ("type 0x%x not handled in "
2531 "ves_icall_FieldInfo_SetValueInternal", type->type);
2532 return;
2536 /* either value is a reference type, or it's a value type and we pinned
2537 * it and v points to the payload. */
2538 g_assert ((isref && v == NULL && value_gchandle == 0) ||
2539 (!isref && v != NULL && value_gchandle != 0) ||
2540 (!isref && v == NULL && value_gchandle == 0));
2542 if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
2543 MonoVTable *vtable = mono_class_vtable_checked (MONO_HANDLE_DOMAIN (field), cf->parent, error);
2544 goto_if_nok (error, leave);
2546 if (!vtable->initialized) {
2547 if (!mono_runtime_class_init_full (vtable, error))
2548 goto leave;
2550 if (isref)
2551 mono_field_static_set_value_internal (vtable, cf, MONO_HANDLE_RAW (value)); /* FIXME make mono_field_static_set_value work with handles for value */
2552 else
2553 mono_field_static_set_value_internal (vtable, cf, v);
2554 } else {
2556 if (isref)
2557 MONO_HANDLE_SET_FIELD_REF (obj, cf, value);
2558 else
2559 mono_field_set_value_internal (MONO_HANDLE_RAW (obj), cf, v); /* FIXME: make mono_field_set_value take a handle for obj */
2561 leave:
2562 if (value_gchandle)
2563 mono_gchandle_free_internal (value_gchandle);
2566 static MonoObjectHandle
2567 typed_reference_to_object (MonoTypedRef *tref, MonoError *error)
2569 HANDLE_FUNCTION_ENTER ();
2570 MonoObjectHandle result;
2571 if (MONO_TYPE_IS_REFERENCE (tref->type)) {
2572 MonoObject** objp = (MonoObject **)tref->value;
2573 result = MONO_HANDLE_NEW (MonoObject, *objp);
2574 } else if (mono_type_is_pointer (tref->type)) {
2575 /* Boxed as UIntPtr */
2576 result = mono_value_box_handle (mono_domain_get (), mono_get_uintptr_class (), tref->value, error);
2577 } else {
2578 result = mono_value_box_handle (mono_domain_get (), tref->klass, tref->value, error);
2580 HANDLE_FUNCTION_RETURN_REF (MonoObject, result);
2583 MonoObjectHandle
2584 ves_icall_System_RuntimeFieldHandle_GetValueDirect (MonoReflectionFieldHandle field_h, MonoReflectionTypeHandle field_type_h, MonoTypedRef *obj, MonoReflectionTypeHandle context_type_h, MonoError *error)
2586 MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
2587 MonoClass *klass = mono_class_from_mono_type_internal (field->type);
2589 if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (field->parent))) {
2590 mono_error_set_not_implemented (error, "");
2591 return MONO_HANDLE_NEW (MonoObject, NULL);
2592 } else if (MONO_TYPE_IS_REFERENCE (field->type)) {
2593 return MONO_HANDLE_NEW (MonoObject, *(MonoObject**)((guint8*)obj->value + field->offset - sizeof (MonoObject)));
2594 } else {
2595 return mono_value_box_handle (mono_domain_get (), klass, (guint8*)obj->value + field->offset - sizeof (MonoObject), error);
2599 void
2600 ves_icall_System_RuntimeFieldHandle_SetValueDirect (MonoReflectionFieldHandle field_h, MonoReflectionTypeHandle field_type_h, MonoTypedRef *obj, MonoObjectHandle value_h, MonoReflectionTypeHandle context_type_h, MonoError *error)
2602 MonoClassField *f = MONO_HANDLE_GETVAL (field_h, field);
2604 g_assert (obj);
2606 mono_class_setup_fields (f->parent);
2608 if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (f->parent))) {
2609 MonoObjectHandle objHandle = typed_reference_to_object (obj, error);
2610 return_if_nok (error);
2611 ves_icall_RuntimeFieldInfo_SetValueInternal (field_h, objHandle, value_h, error);
2612 } else if (MONO_TYPE_IS_REFERENCE (f->type)) {
2613 mono_copy_value (f->type, (guint8*)obj->value + m_field_get_offset (f) - sizeof (MonoObject), MONO_HANDLE_RAW (value_h), FALSE);
2614 } else {
2615 MonoGCHandle gchandle = NULL;
2616 g_assert (MONO_HANDLE_RAW (value_h));
2617 mono_copy_value (f->type, (guint8*)obj->value + m_field_get_offset (f) - sizeof (MonoObject), mono_object_handle_pin_unbox (value_h, &gchandle), FALSE);
2618 mono_gchandle_free_internal (gchandle);
2622 MonoObjectHandle
2623 ves_icall_RuntimeFieldInfo_GetRawConstantValue (MonoReflectionFieldHandle rfield, MonoError* error)
2625 MonoObjectHandle o_handle = NULL_HANDLE_INIT;
2627 MonoObject *o = NULL;
2628 MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
2629 MonoClass *klass;
2630 MonoDomain *domain = MONO_HANDLE_DOMAIN (rfield);
2631 gchar *v;
2632 MonoTypeEnum def_type;
2633 const char *def_value;
2634 MonoType *t;
2635 MonoStringHandle string_handle = MONO_HANDLE_NEW (MonoString, NULL); // FIXME? Not always needed.
2637 mono_class_init_internal (field->parent);
2639 t = mono_field_get_type_checked (field, error);
2640 goto_if_nok (error, return_null);
2642 if (!(t->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
2643 goto invalid_operation;
2645 if (image_is_dynamic (m_class_get_image (field->parent))) {
2646 MonoClass *klass = field->parent;
2647 int fidx = field - m_class_get_fields (klass);
2648 MonoFieldDefaultValue *def_values = mono_class_get_field_def_values (klass);
2650 g_assert (def_values);
2651 def_type = def_values [fidx].def_type;
2652 def_value = def_values [fidx].data;
2654 if (def_type == MONO_TYPE_END)
2655 goto invalid_operation;
2656 } else {
2657 def_value = mono_class_get_field_default_value (field, &def_type);
2658 /* FIXME, maybe we should try to raise TLE if field->parent is broken */
2659 if (!def_value)
2660 goto invalid_operation;
2663 /*FIXME unify this with reflection.c:mono_get_object_from_blob*/
2664 switch (def_type) {
2665 case MONO_TYPE_U1:
2666 case MONO_TYPE_I1:
2667 case MONO_TYPE_BOOLEAN:
2668 case MONO_TYPE_U2:
2669 case MONO_TYPE_I2:
2670 case MONO_TYPE_CHAR:
2671 case MONO_TYPE_U:
2672 case MONO_TYPE_I:
2673 case MONO_TYPE_U4:
2674 case MONO_TYPE_I4:
2675 case MONO_TYPE_R4:
2676 case MONO_TYPE_U8:
2677 case MONO_TYPE_I8:
2678 case MONO_TYPE_R8: {
2679 MonoType *t;
2681 /* boxed value type */
2682 t = g_new0 (MonoType, 1);
2683 t->type = def_type;
2684 klass = mono_class_from_mono_type_internal (t);
2685 g_free (t);
2686 o = mono_object_new_checked (domain, klass, error);
2687 goto_if_nok (error, return_null);
2688 o_handle = MONO_HANDLE_NEW (MonoObject, o);
2689 v = ((gchar *) o) + sizeof (MonoObject);
2690 (void)mono_get_constant_value_from_blob (domain, def_type, def_value, v, string_handle, error);
2691 goto_if_nok (error, return_null);
2692 break;
2694 case MONO_TYPE_STRING:
2695 case MONO_TYPE_CLASS:
2696 (void)mono_get_constant_value_from_blob (domain, def_type, def_value, &o, string_handle, error);
2697 goto_if_nok (error, return_null);
2698 o_handle = MONO_HANDLE_NEW (MonoObject, o);
2699 break;
2700 default:
2701 g_assert_not_reached ();
2704 goto exit;
2705 invalid_operation:
2706 mono_error_set_invalid_operation (error, NULL);
2707 // fall through
2708 return_null:
2709 o_handle = NULL_HANDLE;
2710 // fall through
2711 exit:
2712 return o_handle;
2715 MonoReflectionTypeHandle
2716 ves_icall_RuntimeFieldInfo_ResolveType (MonoReflectionFieldHandle ref_field, MonoError *error)
2718 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_field);
2719 MonoClassField *field = MONO_HANDLE_GETVAL (ref_field, field);
2720 MonoType *type = mono_field_get_type_checked (field, error);
2721 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
2722 return mono_type_get_object_handle (domain, type, error);
2725 void
2726 ves_icall_RuntimePropertyInfo_get_property_info (MonoReflectionPropertyHandle property, MonoPropertyInfo *info, PInfo req_info, MonoError *error)
2728 MonoDomain *domain = MONO_HANDLE_DOMAIN (property);
2729 const MonoProperty *pproperty = MONO_HANDLE_GETVAL (property, property);
2731 if ((req_info & PInfo_ReflectedType) != 0) {
2732 MonoClass *klass = MONO_HANDLE_GETVAL (property, klass);
2733 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
2734 return_if_nok (error);
2736 MONO_STRUCT_SETREF_INTERNAL (info, parent, MONO_HANDLE_RAW (rt));
2738 if ((req_info & PInfo_DeclaringType) != 0) {
2739 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (pproperty->parent), error);
2740 return_if_nok (error);
2742 MONO_STRUCT_SETREF_INTERNAL (info, declaring_type, MONO_HANDLE_RAW (rt));
2745 if ((req_info & PInfo_Name) != 0) {
2746 MonoStringHandle name = mono_string_new_handle (domain, pproperty->name, error);
2747 return_if_nok (error);
2749 MONO_STRUCT_SETREF_INTERNAL (info, name, MONO_HANDLE_RAW (name));
2752 if ((req_info & PInfo_Attributes) != 0)
2753 info->attrs = pproperty->attrs;
2755 if ((req_info & PInfo_GetMethod) != 0) {
2756 MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
2757 MonoReflectionMethodHandle rm;
2758 if (pproperty->get &&
2759 (((pproperty->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
2760 pproperty->get->klass == property_klass)) {
2761 rm = mono_method_get_object_handle (domain, pproperty->get, property_klass, error);
2762 return_if_nok (error);
2763 } else {
2764 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2767 MONO_STRUCT_SETREF_INTERNAL (info, get, MONO_HANDLE_RAW (rm));
2769 if ((req_info & PInfo_SetMethod) != 0) {
2770 MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
2771 MonoReflectionMethodHandle rm;
2772 if (pproperty->set &&
2773 (((pproperty->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
2774 pproperty->set->klass == property_klass)) {
2775 rm = mono_method_get_object_handle (domain, pproperty->set, property_klass, error);
2776 return_if_nok (error);
2777 } else {
2778 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2781 MONO_STRUCT_SETREF_INTERNAL (info, set, MONO_HANDLE_RAW (rm));
2784 * There may be other methods defined for properties, though, it seems they are not exposed
2785 * in the reflection API
2789 static gboolean
2790 add_event_other_methods_to_array (MonoDomain *domain, MonoMethod *m, MonoArrayHandle dest, int i, MonoError *error)
2792 HANDLE_FUNCTION_ENTER ();
2793 error_init (error);
2794 MonoReflectionMethodHandle rm = mono_method_get_object_handle (domain, m, NULL, error);
2795 goto_if_nok (error, leave);
2796 MONO_HANDLE_ARRAY_SETREF (dest, i, rm);
2797 leave:
2798 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
2801 void
2802 ves_icall_RuntimeEventInfo_get_event_info (MonoReflectionMonoEventHandle ref_event, MonoEventInfo *info, MonoError *error)
2804 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_event);
2806 MonoClass *klass = MONO_HANDLE_GETVAL (ref_event, klass);
2807 MonoEvent *event = MONO_HANDLE_GETVAL (ref_event, event);
2809 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
2810 return_if_nok (error);
2811 MONO_STRUCT_SETREF_INTERNAL (info, reflected_type, MONO_HANDLE_RAW (rt));
2813 rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (event->parent), error);
2814 return_if_nok (error);
2815 MONO_STRUCT_SETREF_INTERNAL (info, declaring_type, MONO_HANDLE_RAW (rt));
2817 MonoStringHandle ev_name = mono_string_new_handle (domain, event->name, error);
2818 return_if_nok (error);
2819 MONO_STRUCT_SETREF_INTERNAL (info, name, MONO_HANDLE_RAW (ev_name));
2821 info->attrs = event->attrs;
2823 MonoReflectionMethodHandle rm;
2824 if (event->add) {
2825 rm = mono_method_get_object_handle (domain, event->add, klass, error);
2826 return_if_nok (error);
2827 } else {
2828 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2831 MONO_STRUCT_SETREF_INTERNAL (info, add_method, MONO_HANDLE_RAW (rm));
2833 if (event->remove) {
2834 rm = mono_method_get_object_handle (domain, event->remove, klass, error);
2835 return_if_nok (error);
2836 } else {
2837 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2840 MONO_STRUCT_SETREF_INTERNAL (info, remove_method, MONO_HANDLE_RAW (rm));
2842 if (event->raise) {
2843 rm = mono_method_get_object_handle (domain, event->raise, klass, error);
2844 return_if_nok (error);
2845 } else {
2846 rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
2849 MONO_STRUCT_SETREF_INTERNAL (info, raise_method, MONO_HANDLE_RAW (rm));
2851 #ifndef MONO_SMALL_CONFIG
2852 if (event->other) {
2853 int i, n = 0;
2854 while (event->other [n])
2855 n++;
2856 MonoArrayHandle info_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, n, error);
2857 return_if_nok (error);
2859 MONO_STRUCT_SETREF_INTERNAL (info, other_methods, MONO_HANDLE_RAW (info_arr));
2861 for (i = 0; i < n; i++)
2862 if (!add_event_other_methods_to_array (domain, event->other [i], info_arr, i, error))
2863 return;
2865 #endif
2868 static void
2869 collect_interfaces (MonoClass *klass, GHashTable *ifaces, MonoError *error)
2871 int i;
2872 MonoClass *ic;
2874 mono_class_setup_interfaces (klass, error);
2875 return_if_nok (error);
2877 int klass_interface_count = m_class_get_interface_count (klass);
2878 MonoClass **klass_interfaces = m_class_get_interfaces (klass);
2879 for (i = 0; i < klass_interface_count; i++) {
2880 ic = klass_interfaces [i];
2881 g_hash_table_insert (ifaces, ic, ic);
2883 collect_interfaces (ic, ifaces, error);
2884 return_if_nok (error);
2888 typedef struct {
2889 MonoArrayHandle iface_array;
2890 MonoGenericContext *context;
2891 MonoError *error;
2892 MonoDomain *domain;
2893 int next_idx;
2894 } FillIfaceArrayData;
2896 static void
2897 fill_iface_array (gpointer key, gpointer value, gpointer user_data)
2899 HANDLE_FUNCTION_ENTER ();
2900 FillIfaceArrayData *data = (FillIfaceArrayData *)user_data;
2901 MonoClass *ic = (MonoClass *)key;
2902 MonoType *ret = m_class_get_byval_arg (ic), *inflated = NULL;
2903 MonoError *error = data->error;
2905 goto_if_nok (error, leave);
2907 if (data->context && mono_class_is_ginst (ic) && mono_class_get_generic_class (ic)->context.class_inst->is_open) {
2908 inflated = ret = mono_class_inflate_generic_type_checked (ret, data->context, error);
2909 goto_if_nok (error, leave);
2912 MonoReflectionTypeHandle rt;
2913 rt = mono_type_get_object_handle (data->domain, ret, error);
2914 goto_if_nok (error, leave);
2916 MONO_HANDLE_ARRAY_SETREF (data->iface_array, data->next_idx, rt);
2917 data->next_idx++;
2919 if (inflated)
2920 mono_metadata_free_type (inflated);
2921 leave:
2922 HANDLE_FUNCTION_RETURN ();
2925 static guint
2926 get_interfaces_hash (gconstpointer v1)
2928 MonoClass *k = (MonoClass*)v1;
2930 return m_class_get_type_token (k);
2933 MonoArrayHandle
2934 ves_icall_RuntimeType_GetInterfaces (MonoReflectionTypeHandle ref_type, MonoError *error)
2936 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
2937 MonoClass *klass = mono_class_from_mono_type_internal (type);
2939 GHashTable *iface_hash = g_hash_table_new (get_interfaces_hash, NULL);
2941 MonoGenericContext *context = NULL;
2942 if (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst->is_open) {
2943 context = mono_class_get_context (klass);
2944 klass = mono_class_get_generic_class (klass)->container_class;
2947 for (MonoClass *parent = klass; parent; parent = m_class_get_parent (parent)) {
2948 mono_class_setup_interfaces (parent, error);
2949 goto_if_nok (error, fail);
2950 collect_interfaces (parent, iface_hash, error);
2951 goto_if_nok (error, fail);
2954 MonoDomain *domain;
2955 domain = MONO_HANDLE_DOMAIN (ref_type);
2957 int len;
2958 len = g_hash_table_size (iface_hash);
2959 if (len == 0) {
2960 g_hash_table_destroy (iface_hash);
2961 if (!domain->empty_types) {
2962 domain->empty_types = mono_array_new_cached (domain, mono_defaults.runtimetype_class, 0, error);
2963 goto_if_nok (error, fail);
2965 return MONO_HANDLE_NEW (MonoArray, domain->empty_types);
2968 FillIfaceArrayData data;
2969 data.iface_array = MONO_HANDLE_NEW (MonoArray, mono_array_new_cached (domain, mono_defaults.runtimetype_class, len, error));
2970 goto_if_nok (error, fail);
2971 data.context = context;
2972 data.error = error;
2973 data.domain = domain;
2974 data.next_idx = 0;
2976 g_hash_table_foreach (iface_hash, fill_iface_array, &data);
2978 goto_if_nok (error, fail);
2980 g_hash_table_destroy (iface_hash);
2981 return data.iface_array;
2983 fail:
2984 g_hash_table_destroy (iface_hash);
2985 return NULL_HANDLE_ARRAY;
2988 static gboolean
2989 set_interface_map_data_method_object (MonoDomain *domain, MonoMethod *method, MonoClass *iclass, int ioffset, MonoClass *klass, MonoArrayHandle targets, MonoArrayHandle methods, int i, MonoError *error)
2991 HANDLE_FUNCTION_ENTER ();
2992 error_init (error);
2993 MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, iclass, error);
2994 goto_if_nok (error, leave);
2996 MONO_HANDLE_ARRAY_SETREF (methods, i, member);
2998 MONO_HANDLE_ASSIGN (member, mono_method_get_object_handle (domain, m_class_get_vtable (klass) [i + ioffset], klass, error));
2999 goto_if_nok (error, leave);
3001 MONO_HANDLE_ARRAY_SETREF (targets, i, member);
3003 leave:
3004 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3007 void
3008 ves_icall_RuntimeType_GetInterfaceMapData (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_iface, MonoArrayHandleOut targets, MonoArrayHandleOut methods, MonoError *error)
3010 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3011 MonoClass *klass = mono_class_from_mono_type_internal (type);
3012 MonoType *iface = MONO_HANDLE_GETVAL (ref_iface, type);
3013 MonoClass *iclass = mono_class_from_mono_type_internal (iface);
3015 mono_class_init_checked (klass, error);
3016 return_if_nok (error);
3017 mono_class_init_checked (iclass, error);
3018 return_if_nok (error);
3020 mono_class_setup_vtable (klass);
3022 gboolean variance_used;
3023 int ioffset = mono_class_interface_offset_with_variance (klass, iclass, &variance_used);
3024 if (ioffset == -1)
3025 return;
3027 int len = mono_class_num_methods (iclass);
3028 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3029 MonoArrayHandle targets_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, len, error);
3030 return_if_nok (error);
3031 MONO_HANDLE_ASSIGN (targets, targets_arr);
3033 MonoArrayHandle methods_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, len, error);
3034 return_if_nok (error);
3035 MONO_HANDLE_ASSIGN (methods, methods_arr);
3037 MonoMethod* method;
3038 int i = 0;
3039 gpointer iter = NULL;
3040 while ((method = mono_class_get_methods (iclass, &iter))) {
3041 if (!set_interface_map_data_method_object (domain, method, iclass, ioffset, klass, targets, methods, i, error))
3042 return;
3043 i ++;
3047 void
3048 ves_icall_RuntimeType_GetPacking (MonoReflectionTypeHandle ref_type, guint32 *packing, guint32 *size, MonoError *error)
3050 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3051 MonoClass *klass = mono_class_from_mono_type_internal (type);
3053 mono_class_init_checked (klass, error);
3054 return_if_nok (error);
3056 if (image_is_dynamic (m_class_get_image (klass))) {
3057 MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, ref_type);
3058 *packing = MONO_HANDLE_GETVAL (tb, packing_size);
3059 *size = MONO_HANDLE_GETVAL (tb, class_size);
3060 } else {
3061 mono_metadata_packing_from_typedef (m_class_get_image (klass), m_class_get_type_token (klass), packing, size);
3065 MonoReflectionTypeHandle
3066 ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
3068 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3069 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3071 if (!type->byref && type->type == MONO_TYPE_SZARRAY) {
3072 return mono_type_get_object_handle (domain, m_class_get_byval_arg (type->data.klass), error);
3075 MonoClass *klass = mono_class_from_mono_type_internal (type);
3076 mono_class_init_checked (klass, error);
3077 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
3079 // GetElementType should only return a type for:
3080 // Array Pointer PassedByRef
3081 if (type->byref)
3082 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
3083 else if (m_class_get_element_class (klass) && MONO_CLASS_IS_ARRAY (klass))
3084 return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_element_class (klass)), error);
3085 else if (m_class_get_element_class (klass) && type->type == MONO_TYPE_PTR)
3086 return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_element_class (klass)), error);
3087 else
3088 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3091 MonoReflectionTypeHandle
3092 ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionTypeHandle ref_type, MonoError *error)
3094 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3095 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3097 if (type->byref)
3098 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3100 MonoClass *klass = mono_class_from_mono_type_internal (type);
3101 if (!m_class_get_parent (klass))
3102 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3104 return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_parent (klass)), error);
3107 guint32
3108 ves_icall_RuntimeTypeHandle_GetCorElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
3110 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3112 if (type->byref)
3113 return MONO_TYPE_BYREF;
3114 else
3115 return (guint32)type->type;
3118 MonoBoolean
3119 ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type, MonoError *error)
3121 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3122 MonoClass *klass;
3124 klass = mono_class_from_mono_type_internal (type);
3125 mono_class_init_internal (klass);
3126 return m_class_has_references (klass);
3129 MonoBoolean
3130 ves_icall_RuntimeTypeHandle_IsByRefLike (MonoReflectionTypeHandle ref_type, MonoError *error)
3132 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3133 /* .NET Core says byref types are not IsByRefLike */
3134 if (type->byref)
3135 return FALSE;
3136 MonoClass *klass = mono_class_from_mono_type_internal (type);
3137 return m_class_is_byreflike (klass);
3140 MonoBoolean
3141 ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionTypeHandle ref_type, MonoError *error)
3143 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3144 MonoClass *klass = mono_class_from_mono_type_internal (type);
3145 mono_class_init_checked (klass, error);
3146 return_val_if_nok (error, FALSE);
3148 return mono_class_is_com_object (klass);
3151 guint32
3152 ves_icall_reflection_get_token (MonoObjectHandle obj, MonoError *error)
3154 error_init (error);
3155 return mono_reflection_get_token_checked (obj, error);
3158 MonoReflectionModuleHandle
3159 ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionTypeHandle type, MonoError *error)
3161 MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
3162 MonoType *t = MONO_HANDLE_GETVAL (type, type);
3163 MonoClass *klass = mono_class_from_mono_type_internal (t);
3164 return mono_module_get_object_handle (domain, m_class_get_image (klass), error);
3167 MonoReflectionAssemblyHandle
3168 ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionTypeHandle type, MonoError *error)
3170 MonoDomain *domain = mono_domain_get ();
3171 MonoType *t = MONO_HANDLE_GETVAL (type, type);
3172 MonoClass *klass = mono_class_from_mono_type_internal (t);
3173 return mono_assembly_get_object_handle (domain, m_class_get_image (klass)->assembly, error);
3176 MonoReflectionTypeHandle
3177 ves_icall_RuntimeType_get_DeclaringType (MonoReflectionTypeHandle ref_type, MonoError *error)
3179 MonoDomain *domain = mono_domain_get ();
3180 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3181 MonoClass *klass;
3183 if (type->byref)
3184 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3185 if (type->type == MONO_TYPE_VAR) {
3186 MonoGenericContainer *param = mono_type_get_generic_param_owner (type);
3187 klass = param ? param->owner.klass : NULL;
3188 } else if (type->type == MONO_TYPE_MVAR) {
3189 MonoGenericContainer *param = mono_type_get_generic_param_owner (type);
3190 klass = param ? param->owner.method->klass : NULL;
3191 } else {
3192 klass = m_class_get_nested_in (mono_class_from_mono_type_internal (type));
3195 if (!klass)
3196 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3198 return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
3201 MonoStringHandle
3202 ves_icall_RuntimeType_get_Name (MonoReflectionTypeHandle reftype, MonoError *error)
3204 MonoDomain *domain = mono_domain_get ();
3205 MonoType *type = MONO_HANDLE_RAW(reftype)->type;
3206 MonoClass *klass = mono_class_from_mono_type_internal (type);
3207 // FIXME: this should be escaped in some scenarios with mono_identifier_escape_type_name_chars
3208 // Determining exactly when to do so is fairly difficult, so for now we don't bother to avoid regressions
3209 const char *klass_name = m_class_get_name (klass);
3211 if (type->byref) {
3212 char *n = g_strdup_printf ("%s&", klass_name);
3213 MonoStringHandle res = mono_string_new_handle (domain, n, error);
3215 g_free (n);
3217 return res;
3218 } else {
3219 return mono_string_new_handle (domain, klass_name, error);
3223 MonoStringHandle
3224 ves_icall_RuntimeType_get_Namespace (MonoReflectionTypeHandle type, MonoError *error)
3226 MonoDomain *domain = mono_domain_get ();
3227 MonoClass *klass = mono_class_from_mono_type_handle (type);
3229 MonoClass *klass_nested_in;
3230 while ((klass_nested_in = m_class_get_nested_in (klass)))
3231 klass = klass_nested_in;
3233 if (m_class_get_name_space (klass) [0] == '\0')
3234 return NULL_HANDLE_STRING;
3236 char *escaped = mono_identifier_escape_type_name_chars (m_class_get_name_space (klass));
3237 MonoStringHandle res = mono_string_new_handle (domain, escaped, error);
3238 g_free (escaped);
3239 return res;
3242 gint32
3243 ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionTypeHandle ref_type, MonoError *error)
3245 error_init (error);
3246 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3248 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
3249 mono_error_set_argument (error, "type", "Type must be an array type");
3250 return 0;
3253 MonoClass *klass = mono_class_from_mono_type_internal (type);
3255 return m_class_get_rank (klass);
3258 static MonoArrayHandle
3259 create_type_array (MonoDomain *domain, MonoBoolean runtimeTypeArray, int count, MonoError *error)
3261 return mono_array_new_handle (domain, runtimeTypeArray ? mono_defaults.runtimetype_class : mono_defaults.systemtype_class, count, error);
3264 static gboolean
3265 set_type_object_in_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int i, MonoError *error)
3267 HANDLE_FUNCTION_ENTER();
3268 error_init (error);
3269 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, type, error);
3270 goto_if_nok (error, leave);
3272 MONO_HANDLE_ARRAY_SETREF (dest, i, rt);
3274 leave:
3275 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3278 MonoArrayHandle
3279 ves_icall_RuntimeType_GetGenericArguments (MonoReflectionTypeHandle ref_type, MonoBoolean runtimeTypeArray, MonoError *error)
3281 error_init (error);
3282 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3284 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3285 MonoClass *klass = mono_class_from_mono_type_internal (type);
3287 MonoArrayHandle res = MONO_HANDLE_NEW (MonoArray, NULL);
3288 if (mono_class_is_gtd (klass)) {
3289 MonoGenericContainer *container = mono_class_get_generic_container (klass);
3290 MONO_HANDLE_ASSIGN (res, create_type_array (domain, runtimeTypeArray, container->type_argc, error));
3291 goto_if_nok (error, leave);
3292 for (int i = 0; i < container->type_argc; ++i) {
3293 MonoClass *pklass = mono_class_create_generic_parameter (mono_generic_container_get_param (container, i));
3295 if (!set_type_object_in_array (domain, m_class_get_byval_arg (pklass), res, i, error))
3296 goto leave;
3299 } else if (mono_class_is_ginst (klass)) {
3300 MonoGenericInst *inst = mono_class_get_generic_class (klass)->context.class_inst;
3301 MONO_HANDLE_ASSIGN (res, create_type_array (domain, runtimeTypeArray, inst->type_argc, error));
3302 goto_if_nok (error, leave);
3303 for (int i = 0; i < inst->type_argc; ++i) {
3304 if (!set_type_object_in_array (domain, inst->type_argv [i], res, i, error))
3305 goto leave;
3309 leave:
3310 return res;
3313 MonoBoolean
3314 ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle ref_type, MonoError *error)
3316 error_init (error);
3318 if (!IS_MONOTYPE (MONO_HANDLE_RAW(ref_type)))
3319 return FALSE;
3321 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3322 if (type->byref)
3323 return FALSE;
3325 MonoClass *klass = mono_class_from_mono_type_internal (type);
3326 return mono_class_is_gtd (klass);
3329 MonoReflectionTypeHandle
3330 ves_icall_RuntimeTypeHandle_GetGenericTypeDefinition_impl (MonoReflectionTypeHandle ref_type, MonoError *error)
3332 error_init (error);
3333 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3335 MonoReflectionTypeHandle ret = MONO_HANDLE_NEW (MonoReflectionType, NULL);
3337 if (type->byref)
3338 goto leave;
3340 MonoClass *klass;
3341 klass = mono_class_from_mono_type_internal (type);
3343 if (mono_class_is_gtd (klass)) {
3344 /* check this one */
3345 MONO_HANDLE_ASSIGN (ret, ref_type);
3346 goto leave;
3348 if (mono_class_is_ginst (klass)) {
3349 MonoClass *generic_class = mono_class_get_generic_class (klass)->container_class;
3351 MonoGCHandle ref_info_handle = mono_class_get_ref_info_handle (generic_class);
3353 if (m_class_was_typebuilder (generic_class) && ref_info_handle) {
3354 MonoObjectHandle tb = mono_gchandle_get_target_handle (ref_info_handle);
3355 g_assert (!MONO_HANDLE_IS_NULL (tb));
3356 MONO_HANDLE_ASSIGN (ret, tb);
3357 } else {
3358 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3359 MONO_HANDLE_ASSIGN (ret, mono_type_get_object_handle (domain, m_class_get_byval_arg (generic_class), error));
3362 leave:
3363 return ret;
3366 MonoReflectionTypeHandle
3367 ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype, MonoArrayHandle type_array, MonoError *error)
3369 error_init (error);
3370 MonoDomain *domain = MONO_HANDLE_DOMAIN (reftype);
3372 g_assert (IS_MONOTYPE_HANDLE (reftype));
3373 MonoType *type = MONO_HANDLE_GETVAL (reftype, type);
3374 mono_class_init_checked (mono_class_from_mono_type_internal (type), error);
3375 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
3377 int count = mono_array_handle_length (type_array);
3378 MonoType **types = g_new0 (MonoType *, count);
3380 MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
3381 for (int i = 0; i < count; i++) {
3382 MONO_HANDLE_ARRAY_GETREF (t, type_array, i);
3383 types [i] = MONO_HANDLE_GETVAL (t, type);
3386 MonoType *geninst = mono_reflection_bind_generic_parameters (reftype, count, types, error);
3387 g_free (types);
3388 if (!geninst) {
3389 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3392 MonoClass *klass = mono_class_from_mono_type_internal (geninst);
3394 /*we might inflate to the GTD*/
3395 if (mono_class_is_ginst (klass) && !mono_verifier_class_is_valid_generic_instantiation (klass)) {
3396 mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
3397 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
3400 return mono_type_get_object_handle (domain, geninst, error);
3403 MonoBoolean
3404 ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type, MonoError *error)
3406 error_init (error);
3407 MonoClass *klass;
3409 if (!IS_MONOTYPE (MONO_HANDLE_RAW (ref_type)))
3410 return FALSE;
3412 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3413 if (type->byref)
3414 return FALSE;
3416 klass = mono_class_from_mono_type_internal (type);
3417 return mono_class_is_ginst (klass) || mono_class_is_gtd (klass);
3420 gint32
3421 ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_type, MonoError *error)
3423 error_init (error);
3424 if (!IS_MONOTYPE_HANDLE (ref_type))
3425 return -1;
3426 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3428 if (is_generic_parameter (type))
3429 return mono_type_get_generic_param_num (type);
3430 return -1;
3433 MonoGenericParamInfo *
3434 ves_icall_RuntimeTypeHandle_GetGenericParameterInfo (MonoReflectionTypeHandle ref_type, MonoError *error)
3436 error_init (error);
3437 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3438 return mono_generic_param_info (type->data.generic_param);
3441 MonoBoolean
3442 ves_icall_RuntimeTypeHandle_IsGenericVariable (MonoReflectionTypeHandle ref_type, MonoError *error)
3444 MonoType *type = MONO_HANDLE_GETVAL(ref_type, type);
3445 return is_generic_parameter (type);
3448 MonoReflectionMethodHandle
3449 ves_icall_RuntimeType_GetCorrespondingInflatedMethod (MonoReflectionTypeHandle ref_type,
3450 MonoReflectionMethodHandle generic,
3451 MonoError *error)
3453 error_init (error);
3454 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
3455 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3456 MonoClass *klass = mono_class_from_mono_type_internal (type);
3458 mono_class_init_checked (klass, error);
3459 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
3461 MonoMethod *generic_method = MONO_HANDLE_GETVAL (generic, method);
3463 MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
3464 MonoMethod *method;
3465 gpointer iter = NULL;
3466 while ((method = mono_class_get_methods (klass, &iter))) {
3467 if (method->token == generic_method->token) {
3468 ret = mono_method_get_object_handle (domain, method, klass, error);
3469 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
3473 return ret;
3476 MonoReflectionMethodHandle
3477 ves_icall_RuntimeType_get_DeclaringMethod (MonoReflectionTypeHandle ref_type, MonoError *error)
3479 error_init (error);
3480 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
3481 MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
3483 if (type->byref || (type->type != MONO_TYPE_MVAR && type->type != MONO_TYPE_VAR)) {
3484 mono_error_set_invalid_operation (error, "DeclaringMethod can only be used on generic arguments");
3485 goto leave;
3487 if (type->type == MONO_TYPE_VAR)
3488 goto leave;
3490 MonoMethod *method;
3491 method = mono_type_get_generic_param_owner (type)->owner.method;
3492 g_assert (method);
3494 MonoDomain *domain;
3495 domain = MONO_HANDLE_DOMAIN (ref_type);
3497 MONO_HANDLE_ASSIGN (ret, mono_method_get_object_handle (domain, method, method->klass, error));
3498 leave:
3499 return ret;
3502 #ifndef ENABLE_NETCORE
3503 MonoBoolean
3504 ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError *error)
3506 error_init (error);
3507 mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsTypeExportedToWindowsRuntime");
3508 return FALSE;
3511 MonoBoolean
3512 ves_icall_System_RuntimeType_IsWindowsRuntimeObjectType (MonoError *error)
3514 error_init (error);
3515 mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsWindowsRuntimeObjectType");
3516 return FALSE;
3518 #endif /* ENABLE_NETCORE */
3520 void
3521 ves_icall_RuntimeMethodInfo_GetPInvoke (MonoReflectionMethodHandle ref_method, int* flags, MonoStringHandleOut entry_point, MonoStringHandleOut dll_name, MonoError *error)
3523 MonoDomain *domain = mono_domain_get ();
3524 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3525 MonoImage *image = m_class_get_image (method->klass);
3526 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
3527 MonoTableInfo *tables = image->tables;
3528 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
3529 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
3530 guint32 im_cols [MONO_IMPLMAP_SIZE];
3531 guint32 scope_token;
3532 const char *import = NULL;
3533 const char *scope = NULL;
3535 error_init (error);
3537 if (image_is_dynamic (image)) {
3538 MonoReflectionMethodAux *method_aux =
3539 (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)image)->method_aux_hash, method);
3540 if (method_aux) {
3541 import = method_aux->dllentry;
3542 scope = method_aux->dll;
3545 if (!import || !scope) {
3546 mono_error_set_argument (error, "method", "System.Refleciton.Emit method with invalid pinvoke information");
3547 return;
3550 else {
3551 if (piinfo->implmap_idx) {
3552 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
3554 piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
3555 import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
3556 scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
3557 scope = mono_metadata_string_heap (image, scope_token);
3561 *flags = piinfo->piflags;
3562 MONO_HANDLE_ASSIGN (entry_point, mono_string_new_handle (domain, import, error));
3563 return_if_nok (error);
3564 MONO_HANDLE_ASSIGN (dll_name, mono_string_new_handle (domain, scope, error));
3567 MonoReflectionMethodHandle
3568 ves_icall_RuntimeMethodInfo_GetGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *error)
3570 error_init (error);
3571 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3573 if (method->is_generic)
3574 return ref_method;
3576 if (!method->is_inflated)
3577 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
3579 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
3581 MonoMethod *result = imethod->declaring;
3582 /* Not a generic method. */
3583 if (!result->is_generic)
3584 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
3586 if (image_is_dynamic (m_class_get_image (method->klass))) {
3587 MonoDynamicImage *image = (MonoDynamicImage*)m_class_get_image (method->klass);
3590 * FIXME: Why is this stuff needed at all ? Why can't the code below work for
3591 * the dynamic case as well ?
3593 mono_image_lock ((MonoImage*)image);
3594 MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, (MonoReflectionMethod*)mono_g_hash_table_lookup (image->generic_def_objects, imethod));
3595 mono_image_unlock ((MonoImage*)image);
3597 if (!MONO_HANDLE_IS_NULL (res))
3598 return res;
3601 if (imethod->context.class_inst) {
3602 MonoClass *klass = ((MonoMethod *) imethod)->klass;
3603 /*Generic methods gets the context of the GTD.*/
3604 if (mono_class_get_context (klass)) {
3605 result = mono_class_inflate_generic_method_full_checked (result, klass, mono_class_get_context (klass), error);
3606 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
3610 return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (ref_method), result, NULL, error);
3613 #ifdef ENABLE_NETCORE
3614 static GENERATE_TRY_GET_CLASS_WITH_CACHE (stream, "System.IO", "Stream")
3615 static int io_stream_begin_read_slot = -1;
3616 static int io_stream_begin_write_slot = -1;
3617 static int io_stream_end_read_slot = -1;
3618 static int io_stream_end_write_slot = -1;
3619 static gboolean io_stream_slots_set = FALSE;
3621 static void
3622 init_io_stream_slots (void)
3624 MonoClass* klass = mono_class_try_get_stream_class ();
3625 mono_class_setup_vtable (klass);
3626 MonoMethod **klass_methods = m_class_get_methods (klass);
3627 if (!klass_methods) {
3628 mono_class_setup_methods (klass);
3629 klass_methods = m_class_get_methods (klass);
3631 int method_count = mono_class_get_method_count (klass);
3632 int methods_found = 0;
3633 for (int i = 0; i < method_count; i++) {
3634 // find slots for Begin(End)Read and Begin(End)Write
3635 MonoMethod* m = klass_methods [i];
3636 if (m->slot == -1)
3637 continue;
3639 if (!strcmp (m->name, "BeginRead")) {
3640 methods_found++;
3641 io_stream_begin_read_slot = m->slot;
3642 } else if (!strcmp (m->name, "BeginWrite")) {
3643 methods_found++;
3644 io_stream_begin_write_slot = m->slot;
3645 } else if (!strcmp (m->name, "EndRead")) {
3646 methods_found++;
3647 io_stream_end_read_slot = m->slot;
3648 } else if (!strcmp (m->name, "EndWrite")) {
3649 methods_found++;
3650 io_stream_end_write_slot = m->slot;
3653 g_assert (methods_found <= 4); // some of them can be linked out
3654 io_stream_slots_set = TRUE;
3657 MonoBoolean
3658 ves_icall_System_IO_Stream_HasOverriddenBeginEndRead (MonoObjectHandle stream, MonoError *error)
3660 MonoClass* curr_klass = MONO_HANDLE_GET_CLASS (stream);
3661 MonoClass* base_klass = mono_class_try_get_stream_class ();
3663 if (!io_stream_slots_set)
3664 init_io_stream_slots ();
3666 // slots can still be -1 and it means Linker removed the methods from the base class (Stream)
3667 // in this case we can safely assume the methods are not overridden
3668 // otherwise - check vtable
3669 MonoMethod **curr_klass_vtable = m_class_get_vtable (curr_klass);
3670 gboolean begin_read_is_overriden = io_stream_begin_read_slot != -1 && curr_klass_vtable [io_stream_begin_read_slot]->klass != base_klass;
3671 gboolean end_read_is_overriden = io_stream_end_read_slot != -1 && curr_klass_vtable [io_stream_end_read_slot]->klass != base_klass;
3673 // return true if BeginRead or EndRead were overriden
3674 return begin_read_is_overriden || end_read_is_overriden;
3677 MonoBoolean
3678 ves_icall_System_IO_Stream_HasOverriddenBeginEndWrite (MonoObjectHandle stream, MonoError *error)
3680 MonoClass* curr_klass = MONO_HANDLE_GETVAL (stream, vtable)->klass;
3681 MonoClass* base_klass = mono_class_try_get_stream_class ();
3683 if (!io_stream_slots_set)
3684 init_io_stream_slots ();
3686 MonoMethod **curr_klass_vtable = m_class_get_vtable (curr_klass);
3687 gboolean begin_write_is_overriden = curr_klass_vtable [io_stream_begin_write_slot]->klass != base_klass;
3688 gboolean end_write_is_overriden = curr_klass_vtable [io_stream_end_write_slot]->klass != base_klass;
3690 // return true if BeginWrite or EndWrite were overriden
3691 return begin_write_is_overriden || end_write_is_overriden;
3693 #endif
3695 MonoBoolean
3696 ves_icall_RuntimeMethodInfo_get_IsGenericMethod (MonoReflectionMethodHandle ref_method, MonoError *erro)
3698 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3699 return mono_method_signature_internal (method)->generic_param_count != 0;
3702 MonoBoolean
3703 ves_icall_RuntimeMethodInfo_get_IsGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *Error)
3705 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3706 return method->is_generic;
3709 static gboolean
3710 set_array_generic_argument_handle_inflated (MonoDomain *domain, MonoGenericInst *inst, int i, MonoArrayHandle arr, MonoError *error)
3712 HANDLE_FUNCTION_ENTER ();
3713 error_init (error);
3714 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, inst->type_argv [i], error);
3715 goto_if_nok (error, leave);
3716 MONO_HANDLE_ARRAY_SETREF (arr, i, rt);
3717 leave:
3718 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3721 static gboolean
3722 set_array_generic_argument_handle_gparam (MonoDomain *domain, MonoGenericContainer *container, int i, MonoArrayHandle arr, MonoError *error)
3724 HANDLE_FUNCTION_ENTER ();
3725 error_init (error);
3726 MonoGenericParam *param = mono_generic_container_get_param (container, i);
3727 MonoClass *pklass = mono_class_create_generic_parameter (param);
3728 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (pklass), error);
3729 goto_if_nok (error, leave);
3730 MONO_HANDLE_ARRAY_SETREF (arr, i, rt);
3731 leave:
3732 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
3735 MonoArrayHandle
3736 ves_icall_RuntimeMethodInfo_GetGenericArguments (MonoReflectionMethodHandle ref_method, MonoError *error)
3738 error_init (error);
3739 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_method);
3740 MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
3742 if (method->is_inflated) {
3743 MonoGenericInst *inst = mono_method_get_context (method)->method_inst;
3745 if (inst) {
3746 int count = inst->type_argc;
3747 MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
3748 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3750 for (int i = 0; i < count; i++) {
3751 if (!set_array_generic_argument_handle_inflated (domain, inst, i, res, error))
3752 break;
3754 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3755 return res;
3759 int count = mono_method_signature_internal (method)->generic_param_count;
3760 MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
3761 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3763 MonoGenericContainer *container = mono_method_get_generic_container (method);
3764 for (int i = 0; i < count; i++) {
3765 if (!set_array_generic_argument_handle_gparam (domain, container, i, res, error))
3766 break;
3768 return_val_if_nok (error, NULL_HANDLE_ARRAY);
3769 return res;
3772 MonoObjectHandle
3773 ves_icall_InternalInvoke (MonoReflectionMethodHandle method_handle, MonoObjectHandle this_arg_handle,
3774 MonoArrayHandle params_handle, MonoExceptionHandleOut exception_out, MonoError *error)
3776 MonoReflectionMethod* const method = MONO_HANDLE_RAW (method_handle);
3777 MonoObject* const this_arg = MONO_HANDLE_RAW (this_arg_handle);
3778 MonoArray* const params = MONO_HANDLE_RAW (params_handle);
3781 * Invoke from reflection is supposed to always be a virtual call (the API
3782 * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
3783 * greater flexibility.
3785 MonoMethod *m = method->method;
3786 MonoMethodSignature* const sig = mono_method_signature_internal (m);
3787 MonoImage *image = NULL;
3788 int pcount = 0;
3789 void *obj = this_arg;
3790 char *this_name = NULL;
3791 char *target_name = NULL;
3792 char *msg = NULL;
3793 MonoObject *result = NULL;
3794 MonoArray *arr = NULL;
3795 MonoException *exception = NULL;
3797 *MONO_HANDLE_REF (exception_out) = NULL;
3799 if (mono_security_core_clr_enabled () &&
3800 !mono_security_core_clr_ensure_reflection_access_method (m, error)) {
3801 goto return_null;
3804 if (!(m->flags & METHOD_ATTRIBUTE_STATIC)) {
3805 if (!mono_class_vtable_checked (mono_object_domain (method), m->klass, error)) {
3806 mono_error_cleanup (error); /* FIXME does this make sense? */
3807 error_init_reuse (error);
3808 exception = mono_class_get_exception_for_failure (m->klass);
3809 goto return_null;
3812 if (this_arg) {
3813 if (!mono_object_isinst_checked (this_arg, m->klass, error)) {
3814 if (!is_ok (error)) {
3815 exception = mono_error_convert_to_exception (error);
3816 goto return_null;
3818 this_name = mono_type_get_full_name (mono_object_class (this_arg));
3819 target_name = mono_type_get_full_name (m->klass);
3820 msg = g_strdup_printf ("Object of type '%s' doesn't match target type '%s'", this_name, target_name);
3821 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", msg);
3822 goto return_null;
3824 m = mono_object_get_virtual_method_internal (this_arg, m);
3825 /* must pass the pointer to the value for valuetype methods */
3826 if (m_class_is_valuetype (m->klass)) {
3827 obj = mono_object_unbox_internal (this_arg);
3828 // FIXMEcoop? Does obj need to be put into a handle?
3830 } else if (strcmp (m->name, ".ctor") && !m->wrapper_type) {
3831 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", "Non-static method requires a target.");
3832 goto return_null;
3836 if ((m->klass != NULL && m_class_is_byreflike (m->klass)) || m_class_is_byreflike (mono_class_from_mono_type_internal (sig->ret))) {
3837 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method with stack pointers via reflection");
3838 goto return_null;
3840 #if ENABLE_NETCORE
3841 if (sig->ret->byref) {
3842 MonoType* ret_byval = m_class_get_byval_arg (mono_class_from_mono_type_internal (sig->ret));
3843 if (ret_byval->type == MONO_TYPE_VOID) {
3844 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "ByRef to void return values are not supported in reflection invocation");
3845 goto return_null;
3847 if (m_class_is_byreflike (mono_class_from_mono_type_internal (ret_byval))) {
3848 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method returning ByRef to ByRefLike type via reflection");
3849 goto return_null;
3852 #else
3853 if (sig->ret->byref) {
3854 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method returning ByRef type via reflection");
3855 goto return_null;
3857 #endif
3859 pcount = params? mono_array_length_internal (params): 0;
3860 if (pcount != sig->param_count) {
3861 exception = mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetParameterCountException");
3862 goto return_null;
3865 if (mono_class_is_abstract (m->klass) && !strcmp (m->name, ".ctor") && !this_arg) {
3866 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", "Cannot invoke constructor of an abstract class.");
3867 goto return_null;
3870 image = m_class_get_image (m->klass);
3871 if (mono_asmctx_get_kind (&image->assembly->context) == MONO_ASMCTX_REFONLY) {
3872 exception = mono_get_exception_invalid_operation ("It is illegal to invoke a method on a type loaded using the ReflectionOnly api.");
3873 goto return_null;
3876 if (image_is_dynamic (image) && !((MonoDynamicImage*)image)->run) {
3877 exception = mono_get_exception_not_supported ("Cannot invoke a method in a dynamic assembly without run access.");
3878 goto return_null;
3881 if (m_class_get_rank (m->klass) && !strcmp (m->name, ".ctor")) {
3882 int i;
3883 pcount = mono_array_length_internal (params);
3884 uintptr_t * const lengths = g_newa (uintptr_t, pcount);
3885 /* Note: the synthetized array .ctors have int32 as argument type */
3886 for (i = 0; i < pcount; ++i)
3887 lengths [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, i) + sizeof (MonoObject));
3889 if (m_class_get_rank (m->klass) == 1 && sig->param_count == 2 && m_class_get_rank (m_class_get_element_class (m->klass))) {
3890 /* This is a ctor for jagged arrays. MS creates an array of arrays. */
3891 arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, NULL, error);
3892 goto_if_nok (error, return_null);
3894 MonoArrayHandle subarray_handle = MONO_HANDLE_NEW (MonoArray, NULL);
3896 for (i = 0; i < mono_array_length_internal (arr); ++i) {
3897 MonoArray *subarray = mono_array_new_full_checked (mono_object_domain (params), m_class_get_element_class (m->klass), &lengths [1], NULL, error);
3898 goto_if_nok (error, return_null);
3899 MONO_HANDLE_ASSIGN_RAW (subarray_handle, subarray); // FIXME? Overkill?
3900 mono_array_setref_fast (arr, i, subarray);
3902 goto exit;
3905 if (m_class_get_rank (m->klass) == pcount) {
3906 /* Only lengths provided. */
3907 arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, NULL, error);
3908 goto_if_nok (error, return_null);
3909 goto exit;
3910 } else {
3911 g_assert (pcount == (m_class_get_rank (m->klass) * 2));
3912 /* The arguments are lower-bound-length pairs */
3913 intptr_t * const lower_bounds = (intptr_t *)g_alloca (sizeof (intptr_t) * pcount);
3915 for (i = 0; i < pcount / 2; ++i) {
3916 lower_bounds [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, (i * 2)) + sizeof (MonoObject));
3917 lengths [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, (i * 2) + 1) + sizeof (MonoObject));
3920 arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, lower_bounds, error);
3921 goto_if_nok (error, return_null);
3922 goto exit;
3925 result = mono_runtime_invoke_array_checked (m, obj, params, error);
3926 goto exit;
3927 return_null:
3928 result = NULL;
3929 arr = NULL;
3930 exit:
3931 if (exception) {
3932 MONO_HANDLE_NEW (MonoException, exception); // FIXME? overkill?
3933 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (exception_out), (MonoObject*)exception);
3935 g_free (target_name);
3936 g_free (this_name);
3937 g_free (msg);
3938 g_assert (!result || !arr); // only one, or neither, should be set
3939 return result ? MONO_HANDLE_NEW (MonoObject, result) : arr ? MONO_HANDLE_NEW (MonoObject, (MonoObject*)arr) : NULL_HANDLE;
3942 #ifndef DISABLE_REMOTING
3943 static void
3944 internal_execute_field_getter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArrayHandleOut outArgs, MonoError *error)
3946 MonoArray *out_args;
3947 MonoClass *k = mono_object_class (this_arg);
3948 MonoString *name;
3949 char *str;
3951 // FIXME Refactor/inline internal_execute_field_setter and internal_execute_field_getter.
3953 /* If this is a proxy, then it must be a CBO */
3954 if (mono_class_is_transparent_proxy (k)) {
3955 MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
3956 this_arg = tp->rp->unwrapped_server;
3957 MONO_HANDLE_NEW (MonoObject, this_arg);
3958 g_assert (this_arg);
3959 k = mono_object_class (this_arg);
3962 name = mono_array_get_internal (params, MonoString *, 1);
3963 MONO_HANDLE_NEW (MonoString, name);
3965 str = mono_string_to_utf8_checked_internal (name, error);
3966 return_if_nok (error);
3968 do {
3969 MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
3970 if (field) {
3971 g_free (str);
3972 MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
3973 MonoObject *result;
3974 if (m_class_is_valuetype (field_klass)) {
3975 result = mono_value_box_checked (domain, field_klass, (char *)this_arg + field->offset, error);
3976 return_if_nok (error);
3977 } else
3978 result = (MonoObject *)*((gpointer *)((char *)this_arg + field->offset));
3980 MONO_HANDLE_NEW (MonoObject, result);
3982 out_args = mono_array_new_checked (domain, mono_defaults.object_class, 1, error);
3983 return_if_nok (error);
3984 MONO_HANDLE_NEW (MonoArray, out_args); // FIXME? overkill?
3985 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*) out_args);
3986 mono_array_setref_internal (out_args, 0, result);
3987 return;
3989 k = m_class_get_parent (k);
3990 } while (k);
3992 g_free (str);
3993 g_assert_not_reached ();
3996 static void
3997 internal_execute_field_setter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArrayHandleOut outArgs, MonoError *error)
3999 MonoArray *out_args;
4000 MonoClass *k = mono_object_class (this_arg);
4001 MonoString *name;
4002 guint32 size;
4003 gint32 align;
4004 char *str;
4006 /* If this is a proxy, then it must be a CBO */
4007 if (mono_class_is_transparent_proxy (k)) {
4008 MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
4009 this_arg = tp->rp->unwrapped_server;
4010 MONO_HANDLE_NEW (MonoObject, this_arg);
4011 g_assert (this_arg);
4012 k = mono_object_class (this_arg);
4015 name = mono_array_get_internal (params, MonoString *, 1);
4016 MONO_HANDLE_NEW (MonoString, name);
4018 str = mono_string_to_utf8_checked_internal (name, error);
4019 return_if_nok (error);
4021 do {
4022 MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
4023 if (field) {
4024 g_free (str);
4025 MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
4026 MonoObject *val = (MonoObject *)mono_array_get_internal (params, gpointer, 2);
4027 MONO_HANDLE_NEW (MonoObject, val);
4029 if (m_class_is_valuetype (field_klass)) {
4030 size = mono_type_size (field->type, &align);
4031 g_assert (size == mono_class_value_size (field_klass, NULL));
4032 mono_gc_wbarrier_value_copy_internal ((char *)this_arg + field->offset, (char*)val + sizeof (MonoObject), 1, field_klass);
4033 } else {
4034 mono_gc_wbarrier_set_field_internal (this_arg, (char*)this_arg + field->offset, val);
4037 out_args = mono_array_new_checked (domain, mono_defaults.object_class, 0, error);
4038 return_if_nok (error);
4039 MONO_HANDLE_NEW (MonoArray, out_args); // FIXME? overkill?
4040 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*) out_args);
4041 return;
4044 k = m_class_get_parent (k);
4045 } while (k);
4047 g_free (str);
4048 g_assert_not_reached ();
4051 MonoObjectHandle
4052 ves_icall_InternalExecute (MonoReflectionMethodHandle method_handle, MonoObjectHandle this_arg_handle, MonoArrayHandle params_handle, MonoArrayHandleOut outArgs, MonoError* error)
4054 MONO_HANDLE_ASSIGN_RAW (outArgs, NULL);
4056 MonoReflectionMethod* const method = MONO_HANDLE_RAW (method_handle);
4057 MonoObject* const this_arg = MONO_HANDLE_RAW (this_arg_handle);
4058 MonoArray* const params = MONO_HANDLE_RAW (params_handle);
4059 MonoObjectHandle null_handle = NULL_HANDLE_INIT;
4061 MonoDomain *domain = mono_object_domain (method);
4062 MonoMethod *m = method->method;
4063 MonoMethodSignature *sig = mono_method_signature_internal (m);
4064 MonoArray *out_args;
4065 MonoObject *result;
4066 int i, j, outarg_count = 0;
4068 if (m->klass == mono_defaults.object_class) {
4070 if (!strcmp (m->name, "FieldGetter")) {
4071 internal_execute_field_getter (domain, this_arg, params, outArgs, error);
4072 return null_handle;
4073 } else if (!strcmp (m->name, "FieldSetter")) {
4074 internal_execute_field_setter (domain, this_arg, params, outArgs, error);
4075 return null_handle;
4079 for (i = 0; i < mono_array_length_internal (params); i++) {
4080 if (sig->params [i]->byref)
4081 outarg_count++;
4084 out_args = mono_array_new_checked (domain, mono_defaults.object_class, outarg_count, error);
4085 return_val_if_nok (error, null_handle);
4086 MONO_HANDLE_NEW (MonoArray, out_args);
4088 /* handle constructors only for objects already allocated */
4089 if (!strcmp (method->method->name, ".ctor"))
4090 g_assert (this_arg);
4092 /* This can be called only on MBR objects, so no need to unbox for valuetypes. */
4093 g_assert (!m_class_is_valuetype (method->method->klass));
4094 result = mono_runtime_invoke_array_checked (method->method, this_arg, params, error);
4095 return_val_if_nok (error, null_handle);
4097 MonoObjectHandle result_handle = MONO_HANDLE_NEW (MonoObject, result);
4098 MonoObjectHandle arg_handle = MONO_HANDLE_NEW (MonoObject, NULL);
4100 for (i = 0, j = 0; i < mono_array_length_internal (params); i++) {
4101 if (sig->params [i]->byref) {
4102 gpointer arg;
4103 arg = mono_array_get_internal (params, gpointer, i);
4104 MONO_HANDLE_ASSIGN_RAW (arg_handle, arg); // FIXME? overkill?
4105 mono_array_setref_internal (out_args, j, arg);
4106 j++;
4110 mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*)out_args);
4112 return result_handle;
4114 #endif
4116 static guint64
4117 read_enum_value (const char *mem, int type)
4119 switch (type) {
4120 case MONO_TYPE_BOOLEAN:
4121 case MONO_TYPE_U1:
4122 return *(guint8*)mem;
4123 case MONO_TYPE_I1:
4124 return *(gint8*)mem;
4125 case MONO_TYPE_CHAR:
4126 case MONO_TYPE_U2:
4127 return read16 (mem);
4128 case MONO_TYPE_I2:
4129 return (gint16) read16 (mem);
4130 case MONO_TYPE_U4:
4131 case MONO_TYPE_R4:
4132 return read32 (mem);
4133 case MONO_TYPE_I4:
4134 return (gint32) read32 (mem);
4135 case MONO_TYPE_U8:
4136 case MONO_TYPE_I8:
4137 case MONO_TYPE_R8:
4138 return read64 (mem);
4139 case MONO_TYPE_U:
4140 case MONO_TYPE_I:
4141 #if SIZEOF_REGISTER == 8
4142 return read64 (mem);
4143 #else
4144 return read32 (mem);
4145 #endif
4146 default:
4147 g_assert_not_reached ();
4149 return 0;
4152 static void
4153 write_enum_value (void *mem, int type, guint64 value)
4155 switch (type) {
4156 case MONO_TYPE_U1:
4157 case MONO_TYPE_I1:
4158 case MONO_TYPE_BOOLEAN: {
4159 guint8 *p = (guint8*)mem;
4160 *p = value;
4161 break;
4163 case MONO_TYPE_U2:
4164 case MONO_TYPE_I2:
4165 case MONO_TYPE_CHAR: {
4166 guint16 *p = (guint16 *)mem;
4167 *p = value;
4168 break;
4170 case MONO_TYPE_U4:
4171 case MONO_TYPE_I4:
4172 case MONO_TYPE_R4: {
4173 guint32 *p = (guint32 *)mem;
4174 *p = value;
4175 break;
4177 case MONO_TYPE_U8:
4178 case MONO_TYPE_I8:
4179 case MONO_TYPE_R8: {
4180 guint64 *p = (guint64 *)mem;
4181 *p = value;
4182 break;
4184 case MONO_TYPE_U:
4185 case MONO_TYPE_I: {
4186 #if SIZEOF_REGISTER == 8
4187 guint64 *p = (guint64 *)mem;
4188 *p = value;
4189 #else
4190 guint32 *p = (guint32 *)mem;
4191 *p = value;
4192 break;
4193 #endif
4194 break;
4196 default:
4197 g_assert_not_reached ();
4199 return;
4202 MonoObjectHandle
4203 ves_icall_System_Enum_ToObject (MonoReflectionTypeHandle enumType, guint64 value, MonoError *error)
4205 MonoDomain *domain;
4206 MonoClass *enumc;
4207 MonoObjectHandle resultHandle;
4208 MonoType *etype;
4210 domain = MONO_HANDLE_DOMAIN (enumType);
4211 enumc = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (enumType, type));
4213 mono_class_init_checked (enumc, error);
4214 goto_if_nok (error, return_null);
4216 etype = mono_class_enum_basetype_internal (enumc);
4218 resultHandle = mono_object_new_handle (domain, enumc, error);
4219 goto_if_nok (error, return_null);
4221 write_enum_value (mono_handle_unbox_unsafe (resultHandle), etype->type, value);
4223 return resultHandle;
4225 return_null:
4226 return MONO_HANDLE_NEW (MonoObject, NULL);
4229 MonoBoolean
4230 ves_icall_System_Enum_InternalHasFlag (MonoObjectHandle a, MonoObjectHandle b, MonoError *error)
4232 int size = mono_class_value_size (mono_handle_class (a), NULL);
4233 guint64 a_val = 0, b_val = 0;
4235 memcpy (&a_val, mono_handle_unbox_unsafe (a), size);
4236 memcpy (&b_val, mono_handle_unbox_unsafe (b), size);
4238 return (a_val & b_val) == b_val;
4241 #ifndef ENABLE_NETCORE
4242 MonoObjectHandle
4243 ves_icall_System_Enum_get_value (MonoObjectHandle ehandle, MonoError *error)
4245 MonoObjectHandle resultHandle;
4246 MonoClass *enumc;
4247 int size;
4249 goto_if (MONO_HANDLE_IS_NULL (ehandle), return_null);
4251 g_assert (m_class_is_enumtype (mono_handle_class (ehandle)));
4253 enumc = mono_class_from_mono_type_internal (mono_class_enum_basetype_internal (mono_handle_class (ehandle)));
4255 resultHandle = mono_object_new_handle (MONO_HANDLE_DOMAIN (ehandle), enumc, error);
4256 goto_if_nok (error, return_null);
4257 size = mono_class_value_size (enumc, NULL);
4259 memcpy (mono_handle_unbox_unsafe (resultHandle), mono_handle_unbox_unsafe (ehandle), size);
4261 return resultHandle;
4262 return_null:
4263 return MONO_HANDLE_NEW (MonoObject, NULL);
4265 #endif
4267 MonoReflectionTypeHandle
4268 ves_icall_System_Enum_get_underlying_type (MonoReflectionTypeHandle type, MonoError *error)
4270 MonoType *etype;
4271 MonoClass *klass;
4273 klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
4274 mono_class_init_checked (klass, error);
4275 goto_if_nok (error, return_null);
4277 etype = mono_class_enum_basetype_internal (klass);
4278 if (!etype) {
4279 mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
4280 goto return_null;
4283 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (type), etype, error);
4285 return_null:
4286 return MONO_HANDLE_NEW (MonoReflectionType, NULL);
4290 ves_icall_System_Enum_InternalGetCorElementType (MonoObjectHandle this_handle, MonoError *error)
4292 MonoClass *klass = MONO_HANDLE_GETVAL (this_handle, vtable)->klass;
4294 return (int)m_class_get_byval_arg (m_class_get_element_class (klass))->type;
4297 #ifndef ENABLE_NETCORE
4299 ves_icall_System_Enum_compare_value_to (MonoObjectHandle enumHandle, MonoObjectHandle otherHandle, MonoError *error)
4301 if (MONO_HANDLE_IS_NULL (otherHandle))
4302 return 1;
4304 if (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass != MONO_HANDLE_GETVAL (otherHandle, vtable)->klass)
4305 return 2;
4307 gpointer tdata = mono_handle_unbox_unsafe (enumHandle);
4308 gpointer odata = mono_handle_unbox_unsafe (otherHandle);
4309 MonoType *basetype = mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass);
4310 g_assert (basetype);
4312 #define COMPARE_ENUM_VALUES(ENUM_TYPE) do { \
4313 ENUM_TYPE me = *((ENUM_TYPE*)tdata); \
4314 ENUM_TYPE other = *((ENUM_TYPE*)odata); \
4315 if (me == other) \
4316 return 0; \
4317 return me > other ? 1 : -1; \
4318 } while (0)
4320 switch (basetype->type) {
4321 case MONO_TYPE_BOOLEAN:
4322 case MONO_TYPE_U1:
4323 COMPARE_ENUM_VALUES (guint8);
4324 case MONO_TYPE_I1:
4325 COMPARE_ENUM_VALUES (gint8);
4326 case MONO_TYPE_CHAR:
4327 case MONO_TYPE_U2:
4328 COMPARE_ENUM_VALUES (guint16);
4329 case MONO_TYPE_I2:
4330 COMPARE_ENUM_VALUES (gint16);
4331 case MONO_TYPE_U4:
4332 COMPARE_ENUM_VALUES (guint32);
4333 case MONO_TYPE_I4:
4334 COMPARE_ENUM_VALUES (gint32);
4335 case MONO_TYPE_R4:
4336 COMPARE_ENUM_VALUES (gfloat);
4337 case MONO_TYPE_U8:
4338 COMPARE_ENUM_VALUES (guint64);
4339 case MONO_TYPE_I8:
4340 COMPARE_ENUM_VALUES (gint64);
4341 case MONO_TYPE_R8:
4342 COMPARE_ENUM_VALUES (gdouble);
4343 case MONO_TYPE_U:
4344 #if SIZEOF_REGISTER == 8
4345 COMPARE_ENUM_VALUES (guint64);
4346 #else
4347 COMPARE_ENUM_VALUES (guint32);
4348 #endif
4349 case MONO_TYPE_I:
4350 #if SIZEOF_REGISTER == 8
4351 COMPARE_ENUM_VALUES (gint64);
4352 #else
4353 COMPARE_ENUM_VALUES (gint32);
4354 #endif
4355 default:
4356 break;
4358 #undef COMPARE_ENUM_VALUES
4359 /* indicates that the enum was of an unsupported underlying type */
4360 return 3;
4362 #endif
4364 #ifndef ENABLE_NETCORE
4366 ves_icall_System_Enum_get_hashcode (MonoObjectHandle enumHandle, MonoError *error)
4368 gpointer data = mono_handle_unbox_unsafe (enumHandle);
4369 MonoType *basetype = mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass);
4370 g_assert (basetype);
4372 switch (basetype->type) {
4373 case MONO_TYPE_I1: {
4374 gint8 value = *((gint8*)data);
4375 return ((int)value ^ (int)value << 8);
4377 case MONO_TYPE_U1:
4378 return *((guint8*)data);
4379 case MONO_TYPE_CHAR:
4380 case MONO_TYPE_U2:
4381 return *((guint16*)data);
4383 case MONO_TYPE_I2: {
4384 gint16 value = *((gint16*)data);
4385 return ((int)(guint16)value | (((int)value) << 16));
4387 case MONO_TYPE_U4:
4388 case MONO_TYPE_R4:
4389 return *((guint32*)data);
4390 case MONO_TYPE_I4:
4391 return *((gint32*)data);
4392 case MONO_TYPE_U8:
4393 case MONO_TYPE_I8:
4394 case MONO_TYPE_R8: {
4395 gint64 value = *((gint64*)data);
4396 return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
4398 case MONO_TYPE_I:
4399 case MONO_TYPE_U: {
4400 #if SIZEOF_REGISTER == 8
4401 gint64 value = *((gint64*)data);
4402 return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
4403 #else
4404 return *((guint32*)data);
4405 #endif
4407 default:
4408 g_error ("Implement type 0x%02x in get_hashcode", basetype->type);
4410 return 0;
4412 #endif
4414 static void
4415 get_enum_field (MonoDomain *domain, MonoArrayHandle names, MonoArrayHandle values, int base_type, MonoClassField *field, guint* j, guint64 *previous_value, gboolean *sorted, MonoError *error)
4417 error_init (error);
4418 HANDLE_FUNCTION_ENTER();
4419 guint64 field_value;
4420 const char *p;
4421 MonoTypeEnum def_type;
4423 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
4424 goto leave;
4425 if (strcmp ("value__", mono_field_get_name (field)) == 0)
4426 goto leave;
4427 if (mono_field_is_deleted (field))
4428 goto leave;
4429 MonoStringHandle name;
4430 name = mono_string_new_handle (domain, mono_field_get_name (field), error);
4431 goto_if_nok (error, leave);
4432 MONO_HANDLE_ARRAY_SETREF (names, *j, name);
4434 p = mono_class_get_field_default_value (field, &def_type);
4435 /* len = */ mono_metadata_decode_blob_size (p, &p);
4437 field_value = read_enum_value (p, base_type);
4438 MONO_HANDLE_ARRAY_SETVAL (values, guint64, *j, field_value);
4440 if (*previous_value > field_value)
4441 *sorted = FALSE;
4443 *previous_value = field_value;
4444 (*j)++;
4445 leave:
4446 HANDLE_FUNCTION_RETURN();
4449 MonoBoolean
4450 ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionTypeHandle type, MonoArrayHandleOut values, MonoArrayHandleOut names, MonoError *error)
4452 MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
4453 MonoClass *enumc = mono_class_from_mono_type_internal (MONO_HANDLE_RAW(type)->type);
4454 guint j = 0, nvalues;
4455 gpointer iter;
4456 MonoClassField *field;
4457 int base_type;
4458 guint64 previous_value = 0;
4459 gboolean sorted = TRUE;
4461 error_init (error);
4462 mono_class_init_checked (enumc, error);
4463 return_val_if_nok (error, FALSE);
4465 if (!m_class_is_enumtype (enumc)) {
4466 #if ENABLE_NETCORE
4467 mono_error_set_argument (error, NULL, "Type provided must be an Enum.");
4468 #else
4469 mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
4470 #endif
4471 return TRUE;
4474 base_type = mono_class_enum_basetype_internal (enumc)->type;
4476 nvalues = mono_class_num_fields (enumc) > 0 ? mono_class_num_fields (enumc) - 1 : 0;
4477 MONO_HANDLE_ASSIGN(names, mono_array_new_handle (domain, mono_defaults.string_class, nvalues, error));
4478 return_val_if_nok (error, FALSE);
4479 MONO_HANDLE_ASSIGN(values, mono_array_new_handle (domain, mono_defaults.uint64_class, nvalues, error));
4480 return_val_if_nok (error, FALSE);
4482 iter = NULL;
4483 while ((field = mono_class_get_fields_internal (enumc, &iter))) {
4484 get_enum_field(domain, names, values, base_type, field, &j, &previous_value, &sorted, error);
4485 if (!is_ok (error))
4486 break;
4488 return_val_if_nok (error, FALSE);
4490 return sorted || base_type == MONO_TYPE_R4 || base_type == MONO_TYPE_R8;
4493 enum {
4494 BFLAGS_IgnoreCase = 1,
4495 BFLAGS_DeclaredOnly = 2,
4496 BFLAGS_Instance = 4,
4497 BFLAGS_Static = 8,
4498 BFLAGS_Public = 0x10,
4499 BFLAGS_NonPublic = 0x20,
4500 BFLAGS_FlattenHierarchy = 0x40,
4501 BFLAGS_InvokeMethod = 0x100,
4502 BFLAGS_CreateInstance = 0x200,
4503 BFLAGS_GetField = 0x400,
4504 BFLAGS_SetField = 0x800,
4505 BFLAGS_GetProperty = 0x1000,
4506 BFLAGS_SetProperty = 0x2000,
4507 BFLAGS_ExactBinding = 0x10000,
4508 BFLAGS_SuppressChangeType = 0x20000,
4509 BFLAGS_OptionalParamBinding = 0x40000
4512 enum {
4513 MLISTTYPE_All = 0,
4514 MLISTTYPE_CaseSensitive = 1,
4515 MLISTTYPE_CaseInsensitive = 2,
4516 MLISTTYPE_HandleToInfo = 3
4519 GPtrArray*
4520 ves_icall_RuntimeType_GetFields_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 bflags, guint32 mlisttype, MonoError *error)
4522 error_init (error);
4523 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4525 if (type->byref) {
4526 return g_ptr_array_new ();
4529 int (*compare_func) (const char *s1, const char *s2) = NULL;
4530 compare_func = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
4532 MonoClass *startklass, *klass;
4533 klass = startklass = mono_class_from_mono_type_internal (type);
4535 GPtrArray *ptr_array = g_ptr_array_sized_new (16);
4537 handle_parent:
4538 if (mono_class_has_failure (klass)) {
4539 mono_error_set_for_class_failure (error, klass);
4540 goto fail;
4543 MonoClassField *field;
4544 gpointer iter;
4545 iter = NULL;
4546 while ((field = mono_class_get_fields_lazy (klass, &iter))) {
4547 guint32 flags = mono_field_get_flags (field);
4548 int match = 0;
4549 if (mono_field_is_deleted_with_flags (field, flags))
4550 continue;
4551 if ((flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
4552 if (bflags & BFLAGS_Public)
4553 match++;
4554 } else if ((klass == startklass) || (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) != FIELD_ATTRIBUTE_PRIVATE) {
4555 if (bflags & BFLAGS_NonPublic) {
4556 match++;
4559 if (!match)
4560 continue;
4561 match = 0;
4562 if (flags & FIELD_ATTRIBUTE_STATIC) {
4563 if (bflags & BFLAGS_Static)
4564 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4565 match++;
4566 } else {
4567 if (bflags & BFLAGS_Instance)
4568 match++;
4571 if (!match)
4572 continue;
4574 if (((mlisttype != MLISTTYPE_All) && (utf8_name != NULL)) && compare_func (mono_field_get_name (field), utf8_name))
4575 continue;
4577 g_ptr_array_add (ptr_array, field);
4579 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
4580 goto handle_parent;
4582 return ptr_array;
4584 fail:
4585 g_ptr_array_free (ptr_array, TRUE);
4586 return NULL;
4589 static gboolean
4590 method_nonpublic (MonoMethod* method, gboolean start_klass)
4592 switch (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) {
4593 case METHOD_ATTRIBUTE_ASSEM:
4594 return (start_klass || mono_defaults.generic_ilist_class);
4595 case METHOD_ATTRIBUTE_PRIVATE:
4596 return start_klass;
4597 case METHOD_ATTRIBUTE_PUBLIC:
4598 return FALSE;
4599 default:
4600 return TRUE;
4604 GPtrArray*
4605 mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bflags, guint32 mlisttype, gboolean allow_ctors, MonoError *error)
4607 GPtrArray *array;
4608 MonoClass *startklass;
4609 MonoMethod *method;
4610 gpointer iter;
4611 int match, nslots;
4612 /*FIXME, use MonoBitSet*/
4613 guint32 method_slots_default [8];
4614 guint32 *method_slots = NULL;
4615 int (*compare_func) (const char *s1, const char *s2) = NULL;
4617 array = g_ptr_array_new ();
4618 startklass = klass;
4619 error_init (error);
4621 compare_func = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
4623 /* An optimization for calls made from Delegate:CreateDelegate () */
4624 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))) {
4625 method = mono_get_delegate_invoke_internal (klass);
4626 g_assert (method);
4628 g_ptr_array_add (array, method);
4629 return array;
4632 mono_class_setup_methods (klass);
4633 mono_class_setup_vtable (klass);
4634 if (mono_class_has_failure (klass))
4635 goto loader_error;
4637 if (is_generic_parameter (m_class_get_byval_arg (klass)))
4638 nslots = mono_class_get_vtable_size (m_class_get_parent (klass));
4639 else
4640 nslots = MONO_CLASS_IS_INTERFACE_INTERNAL (klass) ? mono_class_num_methods (klass) : mono_class_get_vtable_size (klass);
4641 if (nslots >= sizeof (method_slots_default) * 8) {
4642 method_slots = g_new0 (guint32, nslots / 32 + 1);
4643 } else {
4644 method_slots = method_slots_default;
4645 memset (method_slots, 0, sizeof (method_slots_default));
4647 handle_parent:
4648 mono_class_setup_methods (klass);
4649 mono_class_setup_vtable (klass);
4650 if (mono_class_has_failure (klass))
4651 goto loader_error;
4653 iter = NULL;
4654 while ((method = mono_class_get_methods (klass, &iter))) {
4655 match = 0;
4656 if (method->slot != -1) {
4657 g_assert (method->slot < nslots);
4658 if (method_slots [method->slot >> 5] & (1 << (method->slot & 0x1f)))
4659 continue;
4660 if (!(method->flags & METHOD_ATTRIBUTE_NEW_SLOT))
4661 method_slots [method->slot >> 5] |= 1 << (method->slot & 0x1f);
4664 if (!allow_ctors && method->name [0] == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0))
4665 continue;
4666 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
4667 if (bflags & BFLAGS_Public)
4668 match++;
4669 } else if ((bflags & BFLAGS_NonPublic) && method_nonpublic (method, (klass == startklass))) {
4670 match++;
4672 if (!match)
4673 continue;
4674 match = 0;
4675 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4676 if (bflags & BFLAGS_Static)
4677 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4678 match++;
4679 } else {
4680 if (bflags & BFLAGS_Instance)
4681 match++;
4684 if (!match)
4685 continue;
4687 if ((mlisttype != MLISTTYPE_All) && (name != NULL)) {
4688 if (compare_func (name, method->name))
4689 continue;
4692 match = 0;
4693 g_ptr_array_add (array, method);
4695 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
4696 goto handle_parent;
4697 if (method_slots != method_slots_default)
4698 g_free (method_slots);
4700 return array;
4702 loader_error:
4703 if (method_slots != method_slots_default)
4704 g_free (method_slots);
4705 g_ptr_array_free (array, TRUE);
4707 g_assert (mono_class_has_failure (klass));
4708 mono_error_set_for_class_failure (error, klass);
4709 return NULL;
4712 GPtrArray*
4713 ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type, const char *mname, guint32 bflags, guint32 mlisttype, MonoError *error)
4715 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4717 MonoClass *klass = mono_class_from_mono_type_internal (type);
4718 if (type->byref) {
4719 return g_ptr_array_new ();
4722 return mono_class_get_methods_by_name (klass, mname, bflags, mlisttype, FALSE, error);
4725 GPtrArray*
4726 ves_icall_RuntimeType_GetConstructors_native (MonoReflectionTypeHandle ref_type, guint32 bflags, MonoError *error)
4728 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4729 if (type->byref) {
4730 return g_ptr_array_new ();
4733 MonoClass *startklass, *klass;
4734 klass = startklass = mono_class_from_mono_type_internal (type);
4736 mono_class_setup_methods (klass);
4737 if (mono_class_has_failure (klass)) {
4738 mono_error_set_for_class_failure (error, klass);
4739 return NULL;
4743 GPtrArray *res_array = g_ptr_array_sized_new (4); /* FIXME, guestimating */
4745 MonoMethod *method;
4746 gpointer iter = NULL;
4747 while ((method = mono_class_get_methods (klass, &iter))) {
4748 int match = 0;
4749 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
4750 continue;
4751 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
4752 if (bflags & BFLAGS_Public)
4753 match++;
4754 } else {
4755 if (bflags & BFLAGS_NonPublic)
4756 match++;
4758 if (!match)
4759 continue;
4760 match = 0;
4761 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
4762 if (bflags & BFLAGS_Static)
4763 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4764 match++;
4765 } else {
4766 if (bflags & BFLAGS_Instance)
4767 match++;
4770 if (!match)
4771 continue;
4772 g_ptr_array_add (res_array, method);
4775 return res_array;
4778 static guint
4779 property_hash (gconstpointer data)
4781 MonoProperty *prop = (MonoProperty*)data;
4783 return g_str_hash (prop->name);
4786 static gboolean
4787 property_accessor_override (MonoMethod *method1, MonoMethod *method2)
4789 if (method1->slot != -1 && method1->slot == method2->slot)
4790 return TRUE;
4792 if (mono_class_get_generic_type_definition (method1->klass) == mono_class_get_generic_type_definition (method2->klass)) {
4793 if (method1->is_inflated)
4794 method1 = ((MonoMethodInflated*) method1)->declaring;
4795 if (method2->is_inflated)
4796 method2 = ((MonoMethodInflated*) method2)->declaring;
4799 return mono_metadata_signature_equal (mono_method_signature_internal (method1), mono_method_signature_internal (method2));
4802 static gboolean
4803 property_equal (MonoProperty *prop1, MonoProperty *prop2)
4805 // Properties are hide-by-name-and-signature
4806 if (!g_str_equal (prop1->name, prop2->name))
4807 return FALSE;
4809 /* If we see a property in a generic method, we want to
4810 compare the generic signatures, not the inflated signatures
4811 because we might conflate two properties that were
4812 distinct:
4814 class Foo<T,U> {
4815 public T this[T t] { getter { return t; } } // method 1
4816 public U this[U u] { getter { return u; } } // method 2
4819 If we see int Foo<int,int>::Item[int] we need to know if
4820 the indexer came from method 1 or from method 2, and we
4821 shouldn't conflate them. (Bugzilla 36283)
4823 if (prop1->get && prop2->get && !property_accessor_override (prop1->get, prop2->get))
4824 return FALSE;
4826 if (prop1->set && prop2->set && !property_accessor_override (prop1->set, prop2->set))
4827 return FALSE;
4829 return TRUE;
4832 static gboolean
4833 property_accessor_nonpublic (MonoMethod* accessor, gboolean start_klass)
4835 if (!accessor)
4836 return FALSE;
4838 return method_nonpublic (accessor, start_klass);
4841 GPtrArray*
4842 ves_icall_RuntimeType_GetPropertiesByName_native (MonoReflectionTypeHandle ref_type, gchar *propname, guint32 bflags, guint32 mlisttype, MonoError *error)
4844 #if ENABLE_NETCORE
4845 // Fetch non-public properties as well because they can hide public properties with the same name in base classes
4846 bflags |= BFLAGS_NonPublic;
4847 #endif
4848 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4850 if (type->byref) {
4851 return g_ptr_array_new ();
4855 MonoClass *startklass, *klass;
4856 klass = startklass = mono_class_from_mono_type_internal (type);
4858 int (*compare_func) (const char *s1, const char *s2) = (mlisttype == MLISTTYPE_CaseInsensitive) ? mono_utf8_strcasecmp : strcmp;
4860 GPtrArray *res_array = g_ptr_array_sized_new (8); /*This the average for ASP.NET types*/
4862 GHashTable *properties = g_hash_table_new (property_hash, (GEqualFunc)property_equal);
4864 handle_parent:
4865 mono_class_setup_methods (klass);
4866 mono_class_setup_vtable (klass);
4867 if (mono_class_has_failure (klass)) {
4868 mono_error_set_for_class_failure (error, klass);
4869 goto loader_error;
4872 MonoProperty *prop;
4873 gpointer iter;
4874 iter = NULL;
4875 while ((prop = mono_class_get_properties (klass, &iter))) {
4876 int match = 0;
4877 MonoMethod *method = prop->get;
4878 if (!method)
4879 method = prop->set;
4880 guint32 flags = 0;
4881 if (method)
4882 flags = method->flags;
4884 if ((prop->get && ((prop->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)) ||
4885 (prop->set && ((prop->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC))) {
4886 if (bflags & BFLAGS_Public)
4887 match++;
4888 } else if (bflags & BFLAGS_NonPublic) {
4889 if (property_accessor_nonpublic(prop->get, startklass == klass) ||
4890 property_accessor_nonpublic(prop->set, startklass == klass)) {
4891 match++;
4894 if (!match)
4895 continue;
4897 match = 0;
4898 if (flags & METHOD_ATTRIBUTE_STATIC) {
4899 if (bflags & BFLAGS_Static)
4900 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
4901 match++;
4902 } else {
4903 if (bflags & BFLAGS_Instance)
4904 match++;
4907 if (!match)
4908 continue;
4909 match = 0;
4911 if ((mlisttype != MLISTTYPE_All) && (propname != NULL) && compare_func (propname, prop->name))
4912 continue;
4914 if (g_hash_table_lookup (properties, prop))
4915 continue;
4917 g_ptr_array_add (res_array, prop);
4919 g_hash_table_insert (properties, prop, prop);
4921 if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass))) {
4922 #if ENABLE_NETCORE
4923 // BFLAGS_NonPublic should be excluded for base classes
4924 bflags &= ~BFLAGS_NonPublic;
4925 #endif
4926 goto handle_parent;
4929 g_hash_table_destroy (properties);
4931 return res_array;
4934 loader_error:
4935 if (properties)
4936 g_hash_table_destroy (properties);
4937 g_ptr_array_free (res_array, TRUE);
4939 return NULL;
4942 static guint
4943 event_hash (gconstpointer data)
4945 MonoEvent *event = (MonoEvent*)data;
4947 return g_str_hash (event->name);
4950 static gboolean
4951 event_equal (MonoEvent *event1, MonoEvent *event2)
4953 // Events are hide-by-name
4954 return g_str_equal (event1->name, event2->name);
4957 GPtrArray*
4958 ves_icall_RuntimeType_GetEvents_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 mlisttype, MonoError *error)
4960 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
4962 if (type->byref) {
4963 return g_ptr_array_new ();
4966 int (*compare_func) (const char *s1, const char *s2) = (mlisttype == MLISTTYPE_CaseInsensitive) ? mono_utf8_strcasecmp : strcmp;
4968 GPtrArray *res_array = g_ptr_array_sized_new (4);
4970 MonoClass *startklass, *klass;
4971 klass = startklass = mono_class_from_mono_type_internal (type);
4973 GHashTable *events = g_hash_table_new (event_hash, (GEqualFunc)event_equal);
4974 handle_parent:
4975 mono_class_setup_methods (klass);
4976 mono_class_setup_vtable (klass);
4977 if (mono_class_has_failure (klass)) {
4978 mono_error_set_for_class_failure (error, klass);
4979 goto failure;
4982 MonoEvent *event;
4983 gpointer iter;
4984 iter = NULL;
4985 while ((event = mono_class_get_events (klass, &iter))) {
4987 // Remove inherited privates and inherited
4988 // without add/remove/raise methods
4989 if (klass != startklass)
4991 MonoMethod *method = event->add;
4992 if (!method)
4993 method = event->remove;
4994 if (!method)
4995 method = event->raise;
4996 if (!method)
4997 continue;
4998 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PRIVATE)
4999 continue;
5002 if ((mlisttype != MLISTTYPE_All) && (utf8_name != NULL) && compare_func (event->name, utf8_name))
5003 continue;
5005 if (g_hash_table_lookup (events, event))
5006 continue;
5008 g_ptr_array_add (res_array, event);
5010 g_hash_table_insert (events, event, event);
5012 if ((klass = m_class_get_parent (klass)))
5013 goto handle_parent;
5015 g_hash_table_destroy (events);
5017 return res_array;
5019 failure:
5020 if (events != NULL)
5021 g_hash_table_destroy (events);
5023 g_ptr_array_free (res_array, TRUE);
5025 return NULL;
5028 GPtrArray *
5029 ves_icall_RuntimeType_GetNestedTypes_native (MonoReflectionTypeHandle ref_type, char *str, guint32 bflags, guint32 mlisttype, MonoError *error)
5031 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
5033 if (type->byref) {
5034 return g_ptr_array_new ();
5037 int (*compare_func) (const char *s1, const char *s2) = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
5039 MonoClass *klass = mono_class_from_mono_type_internal (type);
5042 * If a nested type is generic, return its generic type definition.
5043 * Note that this means that the return value is essentially the set
5044 * of nested types of the generic type definition of @klass.
5046 * A note in MSDN claims that a generic type definition can have
5047 * nested types that aren't generic. In any case, the container of that
5048 * nested type would be the generic type definition.
5050 if (mono_class_is_ginst (klass))
5051 klass = mono_class_get_generic_class (klass)->container_class;
5053 GPtrArray *res_array = g_ptr_array_new ();
5055 MonoClass *nested;
5056 gpointer iter = NULL;
5057 while ((nested = mono_class_get_nested_types (klass, &iter))) {
5058 int match = 0;
5059 if ((mono_class_get_flags (nested) & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
5060 if (bflags & BFLAGS_Public)
5061 match++;
5062 } else {
5063 if (bflags & BFLAGS_NonPublic)
5064 match++;
5066 if (!match)
5067 continue;
5069 if ((mlisttype != MLISTTYPE_All) && (str != NULL) && compare_func (m_class_get_name (nested), str))
5070 continue;
5072 g_ptr_array_add (res_array, m_class_get_byval_arg (nested));
5075 return res_array;
5078 static MonoType*
5079 get_type_from_module_builder_module (MonoAssemblyLoadContext *alc, MonoArrayHandle modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
5081 HANDLE_FUNCTION_ENTER ();
5082 MonoType *type = NULL;
5083 MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
5084 MONO_HANDLE_ARRAY_GETREF (mb, modules, i);
5085 MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image);
5086 type = mono_reflection_get_type_checked (alc, &dynamic_image->image, &dynamic_image->image, info, ignoreCase, FALSE, type_resolve, error);
5087 HANDLE_FUNCTION_RETURN_VAL (type);
5090 static MonoType*
5091 get_type_from_module_builder_loaded_modules (MonoAssemblyLoadContext *alc, MonoArrayHandle loaded_modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
5093 HANDLE_FUNCTION_ENTER ();
5094 MonoType *type = NULL;
5095 MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
5096 MONO_HANDLE_ARRAY_GETREF (mod, loaded_modules, i);
5097 MonoImage *image = MONO_HANDLE_GETVAL (mod, image);
5098 type = mono_reflection_get_type_checked (alc, image, image, info, ignoreCase, FALSE, type_resolve, error);
5099 HANDLE_FUNCTION_RETURN_VAL (type);
5102 MonoReflectionTypeHandle
5103 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHandle assembly_h, MonoReflectionModuleHandle module, MonoStringHandle name, MonoBoolean throwOnError, MonoBoolean ignoreCase, MonoError *error)
5105 ERROR_DECL (parse_error);
5107 MonoTypeNameParse info;
5108 gboolean type_resolve;
5109 MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
5111 /* On MS.NET, this does not fire a TypeResolve event */
5112 type_resolve = TRUE;
5113 char *str = mono_string_handle_to_utf8 (name, error);
5114 goto_if_nok (error, fail);
5116 /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
5117 if (!mono_reflection_parse_type_checked (str, &info, parse_error)) {
5118 g_free (str);
5119 mono_reflection_free_type_info (&info);
5120 mono_error_cleanup (parse_error);
5121 if (throwOnError) {
5122 #if ENABLE_NETCORE
5123 mono_error_set_argument (error, "typeName@0", "failed to parse the type");
5124 #else
5125 mono_error_set_argument (error, "typeName", "failed to parse the type");
5126 #endif
5127 goto fail;
5129 /*g_print ("failed parse\n");*/
5130 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
5133 if (info.assembly.name) {
5134 g_free (str);
5135 mono_reflection_free_type_info (&info);
5136 if (throwOnError) {
5137 /* 1.0 and 2.0 throw different exceptions */
5138 if (mono_defaults.generic_ilist_class)
5139 mono_error_set_argument (error, NULL, "Type names passed to Assembly.GetType() must not specify an assembly.");
5140 else
5141 mono_error_set_type_load_name (error, g_strdup (""), g_strdup (""), "Type names passed to Assembly.GetType() must not specify an assembly.");
5142 goto fail;
5144 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
5147 MonoType *type;
5148 type = NULL;
5149 if (!MONO_HANDLE_IS_NULL (module)) {
5150 MonoImage *image = MONO_HANDLE_GETVAL (module, image);
5151 if (image) {
5152 type = mono_reflection_get_type_checked (alc, image, image, &info, ignoreCase, FALSE, &type_resolve, error);
5153 if (!is_ok (error)) {
5154 g_free (str);
5155 mono_reflection_free_type_info (&info);
5156 goto fail;
5160 else {
5161 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5162 if (assembly_is_dynamic (assembly)) {
5163 /* Enumerate all modules */
5164 MonoReflectionAssemblyBuilderHandle abuilder = MONO_HANDLE_NEW (MonoReflectionAssemblyBuilder, NULL);
5165 MONO_HANDLE_ASSIGN (abuilder, assembly_h);
5166 int i;
5168 MonoArrayHandle modules = MONO_HANDLE_NEW (MonoArray, NULL);
5169 MONO_HANDLE_GET (modules, abuilder, modules);
5170 if (!MONO_HANDLE_IS_NULL (modules)) {
5171 int n = mono_array_handle_length (modules);
5172 for (i = 0; i < n; ++i) {
5173 type = get_type_from_module_builder_module (alc, modules, i, &info, ignoreCase, &type_resolve, error);
5174 if (!is_ok (error)) {
5175 g_free (str);
5176 mono_reflection_free_type_info (&info);
5177 goto fail;
5179 if (type)
5180 break;
5184 MonoArrayHandle loaded_modules = MONO_HANDLE_NEW (MonoArray, NULL);
5185 MONO_HANDLE_GET (loaded_modules, abuilder, loaded_modules);
5186 if (!type && !MONO_HANDLE_IS_NULL (loaded_modules)) {
5187 int n = mono_array_handle_length (loaded_modules);
5188 for (i = 0; i < n; ++i) {
5189 type = get_type_from_module_builder_loaded_modules (alc, loaded_modules, i, &info, ignoreCase, &type_resolve, error);
5191 if (!is_ok (error)) {
5192 g_free (str);
5193 mono_reflection_free_type_info (&info);
5194 goto fail;
5196 if (type)
5197 break;
5201 else {
5202 type = mono_reflection_get_type_checked (alc, assembly->image, assembly->image, &info, ignoreCase, FALSE, &type_resolve, error);
5203 if (!is_ok (error)) {
5204 g_free (str);
5205 mono_reflection_free_type_info (&info);
5206 goto fail;
5210 g_free (str);
5211 mono_reflection_free_type_info (&info);
5213 if (!type) {
5214 if (throwOnError) {
5215 ERROR_DECL (inner_error);
5216 char *type_name = mono_string_handle_to_utf8 (name, inner_error);
5217 mono_error_assert_ok (inner_error);
5218 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5219 char *assmname = mono_stringify_assembly_name (&assembly->aname);
5220 mono_error_set_type_load_name (error, type_name, assmname, "%s", "");
5221 goto fail;
5224 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
5227 if (type->type == MONO_TYPE_CLASS) {
5228 MonoClass *klass = mono_type_get_class_internal (type);
5230 /* need to report exceptions ? */
5231 if (throwOnError && mono_class_has_failure (klass)) {
5232 /* report SecurityException (or others) that occured when loading the assembly */
5233 mono_error_set_for_class_failure (error, klass);
5234 goto fail;
5238 /* g_print ("got it\n"); */
5239 return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (assembly_h), type, error);
5240 fail:
5241 g_assert (!is_ok (error));
5242 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
5245 static gboolean
5246 replace_shadow_path (MonoDomain *domain, gchar *dirname, gchar **filename)
5248 gchar *content;
5249 gchar *shadow_ini_file;
5250 gsize len;
5252 /* Check for shadow-copied assembly */
5253 if (mono_is_shadow_copy_enabled (domain, dirname)) {
5254 shadow_ini_file = g_build_filename (dirname, "__AssemblyInfo__.ini", (const char*)NULL);
5255 content = NULL;
5256 if (!g_file_get_contents (shadow_ini_file, &content, &len, NULL) ||
5257 !g_file_test (content, G_FILE_TEST_IS_REGULAR)) {
5258 g_free (content);
5259 content = NULL;
5261 g_free (shadow_ini_file);
5262 if (content != NULL) {
5263 g_free (*filename);
5264 *filename = content;
5265 return TRUE;
5268 return FALSE;
5271 MonoStringHandle
5272 ves_icall_System_Reflection_RuntimeAssembly_get_code_base (MonoReflectionAssemblyHandle assembly, MonoBoolean escaped, MonoError *error)
5274 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5275 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
5276 gchar *absolute;
5277 gchar *dirname;
5279 if (g_path_is_absolute (mass->image->name)) {
5280 absolute = g_strdup (mass->image->name);
5281 dirname = g_path_get_dirname (absolute);
5282 } else {
5283 absolute = g_build_filename (mass->basedir, mass->image->name, (const char*)NULL);
5284 dirname = g_strdup (mass->basedir);
5287 replace_shadow_path (domain, dirname, &absolute);
5288 g_free (dirname);
5290 mono_icall_make_platform_path (absolute);
5292 gchar *uri;
5293 if (escaped) {
5294 uri = g_filename_to_uri (absolute, NULL, NULL);
5295 } else {
5296 const gchar *prepend = mono_icall_get_file_path_prefix (absolute);
5297 uri = g_strconcat (prepend, absolute, (const char*)NULL);
5300 g_free (absolute);
5302 MonoStringHandle res;
5303 if (uri) {
5304 res = mono_string_new_handle (domain, uri, error);
5305 g_free (uri);
5306 } else {
5307 res = MONO_HANDLE_NEW (MonoString, NULL);
5309 return res;
5312 #ifndef ENABLE_NETCORE
5313 MonoBoolean
5314 ves_icall_System_Reflection_RuntimeAssembly_get_global_assembly_cache (MonoReflectionAssemblyHandle assembly, MonoError *error)
5316 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly,assembly);
5318 return mass->in_gac;
5321 MonoReflectionAssemblyHandle
5322 ves_icall_System_Reflection_Assembly_load_with_partial_name (MonoStringHandle mname, MonoObjectHandle evidence, MonoError *error)
5324 gchar *name;
5325 MonoImageOpenStatus status;
5326 MonoReflectionAssemblyHandle result = MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
5328 name = mono_string_handle_to_utf8 (mname, error);
5329 goto_if_nok (error, leave);
5330 MonoAssembly *res;
5331 res = mono_assembly_load_with_partial_name_internal (name, mono_domain_default_alc (mono_domain_get ()), &status);
5333 g_free (name);
5335 if (res == NULL)
5336 goto leave;
5337 result = mono_assembly_get_object_handle (mono_domain_get (), res, error);
5338 leave:
5339 return result;
5341 #endif
5343 MonoStringHandle
5344 ves_icall_System_Reflection_RuntimeAssembly_get_location (MonoReflectionAssemblyHandle refassembly, MonoError *error)
5346 MonoDomain *domain = MONO_HANDLE_DOMAIN (refassembly);
5347 MonoAssembly *assembly = MONO_HANDLE_GETVAL (refassembly, assembly);
5348 const char *image_name = m_image_get_filename (assembly->image);
5349 return mono_string_new_handle (domain, image_name != NULL ? image_name : "", error);
5352 #ifndef ENABLE_NETCORE
5353 MonoBoolean
5354 ves_icall_System_Reflection_RuntimeAssembly_get_ReflectionOnly (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5356 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5357 return mono_asmctx_get_kind (&assembly->context) == MONO_ASMCTX_REFONLY;
5359 #endif
5361 MonoStringHandle
5362 ves_icall_System_Reflection_RuntimeAssembly_InternalImageRuntimeVersion (MonoReflectionAssemblyHandle refassembly, MonoError *error)
5364 MonoDomain *domain = MONO_HANDLE_DOMAIN (refassembly);
5365 MonoAssembly *assembly = MONO_HANDLE_GETVAL (refassembly, assembly);
5367 return mono_string_new_handle (domain, assembly->image->version, error);
5370 MonoReflectionMethodHandle
5371 ves_icall_System_Reflection_RuntimeAssembly_get_EntryPoint (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5373 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5374 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5375 MonoMethod *method;
5377 MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
5378 guint32 token = mono_image_get_entry_point (assembly->image);
5380 if (!token)
5381 goto leave;
5382 method = mono_get_method_checked (assembly->image, token, NULL, NULL, error);
5383 goto_if_nok (error, leave);
5385 MONO_HANDLE_ASSIGN (res, mono_method_get_object_handle (domain, method, NULL, error));
5386 leave:
5387 return res;
5390 MonoReflectionModuleHandle
5391 ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssemblyHandle assembly, MonoError *error)
5393 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5394 MonoAssembly *a = MONO_HANDLE_GETVAL (assembly, assembly);
5395 return mono_module_get_object_handle (domain, a->image, error);
5398 static gboolean
5399 add_manifest_resource_name_to_array (MonoDomain *domain, MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle dest, MonoError *error)
5401 HANDLE_FUNCTION_ENTER ();
5402 const char *val = mono_metadata_string_heap (image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
5403 MonoStringHandle str = mono_string_new_handle (domain, val, error);
5404 goto_if_nok (error, leave);
5405 MONO_HANDLE_ARRAY_SETREF (dest, i, str);
5406 leave:
5407 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5410 MonoArrayHandle
5411 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceNames (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5413 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5414 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5415 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
5416 MonoArrayHandle result = mono_array_new_handle (domain, mono_defaults.string_class, table->rows, error);
5417 goto_if_nok (error, fail);
5418 int i;
5420 for (i = 0; i < table->rows; ++i) {
5421 if (!add_manifest_resource_name_to_array (domain, assembly->image, table, i, result, error))
5422 goto fail;
5424 return result;
5425 fail:
5426 return NULL_HANDLE_ARRAY;
5429 #ifndef ENABLE_NETCORE
5430 MonoBoolean
5431 ves_icall_System_Reflection_RuntimeAssembly_GetAotIdInternal (MonoArrayHandle guid_h, MonoError *error)
5433 g_assert (mono_array_handle_length (guid_h) == 16);
5435 guint8 *aotid = mono_runtime_get_aotid_arr ();
5436 if (!aotid) {
5437 return FALSE;
5438 } else {
5439 MONO_ENTER_NO_SAFEPOINTS;
5440 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
5441 memcpy (data, aotid, 16);
5442 MONO_EXIT_NO_SAFEPOINTS;
5443 return TRUE;
5446 #endif
5448 static MonoAssemblyName*
5449 create_referenced_assembly_name (MonoDomain *domain, MonoImage *image, int i, MonoError *error)
5451 MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
5453 mono_assembly_get_assemblyref_checked (image, i, aname, error);
5454 return_val_if_nok (error, NULL);
5455 aname->hash_alg = ASSEMBLY_HASH_SHA1 /* SHA1 (default) */;
5456 /* name and culture are pointers into the image tables, but we need
5457 * real malloc'd strings (so that we can g_free() them later from
5458 * Mono.RuntimeMarshal.FreeAssemblyName) */
5459 aname->name = g_strdup (aname->name);
5460 aname->culture = g_strdup (aname->culture);
5461 /* Don't need the hash value in managed */
5462 aname->hash_value = NULL;
5463 aname->hash_len = 0;
5464 g_assert (aname->public_key == NULL);
5466 /* note: this function doesn't return the codebase on purpose (i.e. it can
5467 be used under partial trust as path information isn't present). */
5468 return aname;
5471 GPtrArray*
5472 ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflectionAssemblyHandle assembly, MonoError *error)
5474 error_init (error);
5475 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
5476 MonoAssembly *ass = MONO_HANDLE_GETVAL(assembly, assembly);
5477 MonoImage *image = ass->image;
5478 int count;
5480 if (image_is_dynamic (ass->image)) {
5481 MonoDynamicTable *t = &(((MonoDynamicImage*) image)->tables [MONO_TABLE_ASSEMBLYREF]);
5482 count = t->rows;
5484 else {
5485 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
5486 count = t->rows;
5489 GPtrArray *result = g_ptr_array_sized_new (count);
5491 for (int i = 0; i < count; i++) {
5492 MonoAssemblyName *aname = create_referenced_assembly_name (domain, image, i, error);
5493 if (!is_ok (error))
5494 break;
5495 g_ptr_array_add (result, aname);
5497 return result;
5500 /* move this in some file in mono/util/ */
5501 static char *
5502 g_concat_dir_and_file (const char *dir, const char *file)
5504 g_return_val_if_fail (dir != NULL, NULL);
5505 g_return_val_if_fail (file != NULL, NULL);
5508 * If the directory name doesn't have a / on the end, we need
5509 * to add one so we get a proper path to the file
5511 if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
5512 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, (const char*)NULL);
5513 else
5514 return g_strconcat (dir, file, (const char*)NULL);
5517 #ifdef ENABLE_NETCORE
5518 static MonoReflectionAssemblyHandle
5519 try_resource_resolve_name (MonoReflectionAssemblyHandle assembly_handle, MonoStringHandle name_handle)
5521 MonoObjectHandle ret;
5523 ERROR_DECL (error);
5525 HANDLE_FUNCTION_ENTER ();
5527 if (mono_runtime_get_no_exec ())
5528 goto return_null;
5530 MONO_STATIC_POINTER_INIT (MonoMethod, resolve_method)
5532 MonoClass *alc_class = mono_class_get_assembly_load_context_class ();
5533 g_assert (alc_class);
5534 resolve_method = mono_class_get_method_from_name_checked (alc_class, "OnResourceResolve", -1, 0, error);
5536 MONO_STATIC_POINTER_INIT_END (MonoMethod, resolve_method)
5538 goto_if_nok (error, return_null);
5540 gpointer args [2];
5541 args [0] = MONO_HANDLE_RAW (assembly_handle);
5542 args [1] = MONO_HANDLE_RAW (name_handle);
5543 ret = mono_runtime_try_invoke_handle (resolve_method, NULL_HANDLE, args, error);
5544 goto_if_nok (error, return_null);
5546 goto exit;
5548 return_null:
5549 ret = NULL_HANDLE;
5551 exit:
5552 HANDLE_FUNCTION_RETURN_REF (MonoReflectionAssembly, MONO_HANDLE_CAST (MonoReflectionAssembly, ret));
5554 #endif
5556 static void *
5557 get_manifest_resource_internal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
5559 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5560 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5561 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
5562 guint32 i;
5563 guint32 cols [MONO_MANIFEST_SIZE];
5564 guint32 impl, file_idx;
5565 const char *val;
5566 MonoImage *module;
5568 char *n = mono_string_handle_to_utf8 (name, error);
5569 return_val_if_nok (error, NULL);
5571 for (i = 0; i < table->rows; ++i) {
5572 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
5573 val = mono_metadata_string_heap (assembly->image, cols [MONO_MANIFEST_NAME]);
5574 if (strcmp (val, n) == 0)
5575 break;
5577 g_free (n);
5578 if (i == table->rows)
5579 return NULL;
5580 /* FIXME */
5581 impl = cols [MONO_MANIFEST_IMPLEMENTATION];
5582 if (impl) {
5584 * this code should only be called after obtaining the
5585 * ResourceInfo and handling the other cases.
5587 g_assert ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE);
5588 file_idx = impl >> MONO_IMPLEMENTATION_BITS;
5590 module = mono_image_load_file_for_image_checked (assembly->image, file_idx, error);
5591 if (!is_ok (error) || !module)
5592 return NULL;
5594 else
5595 module = assembly->image;
5598 MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
5599 return_val_if_nok (error, NULL);
5600 MONO_HANDLE_ASSIGN (ref_module, rm);
5602 return (void*)mono_image_get_resource (module, cols [MONO_MANIFEST_OFFSET], (guint32*)size);
5605 void *
5606 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
5608 gpointer ret = get_manifest_resource_internal (assembly_h, name, size, ref_module, error);
5610 #ifdef ENABLE_NETCORE
5611 if (!ret) {
5612 MonoReflectionAssemblyHandle event_assembly_h = try_resource_resolve_name (assembly_h, name);
5613 if (MONO_HANDLE_BOOL (event_assembly_h))
5614 ret = get_manifest_resource_internal (event_assembly_h, name, size, ref_module, error);
5616 #endif
5618 return ret;
5621 static gboolean
5622 get_manifest_resource_info_internal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info, MonoError *error)
5624 HANDLE_FUNCTION_ENTER ();
5625 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5626 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5627 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
5628 int i;
5629 guint32 cols [MONO_MANIFEST_SIZE];
5630 guint32 file_cols [MONO_FILE_SIZE];
5631 const char *val;
5632 char *n;
5634 gboolean result = FALSE;
5636 n = mono_string_handle_to_utf8 (name, error);
5637 goto_if_nok (error, leave);
5639 for (i = 0; i < table->rows; ++i) {
5640 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
5641 val = mono_metadata_string_heap (assembly->image, cols [MONO_MANIFEST_NAME]);
5642 if (strcmp (val, n) == 0)
5643 break;
5645 g_free (n);
5646 if (i == table->rows)
5647 goto leave;
5649 if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
5650 MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST);
5652 else {
5653 switch (cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) {
5654 case MONO_IMPLEMENTATION_FILE:
5655 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> MONO_IMPLEMENTATION_BITS;
5656 table = &assembly->image->tables [MONO_TABLE_FILE];
5657 mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
5658 val = mono_metadata_string_heap (assembly->image, file_cols [MONO_FILE_NAME]);
5659 MONO_HANDLE_SET (info, filename, mono_string_new_handle (domain, val, error));
5660 if (file_cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA)
5661 MONO_HANDLE_SETVAL (info, location, guint32, 0);
5662 else
5663 MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED);
5664 break;
5666 case MONO_IMPLEMENTATION_ASSEMBLYREF:
5667 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> MONO_IMPLEMENTATION_BITS;
5668 mono_assembly_load_reference (assembly->image, i - 1);
5669 if (assembly->image->references [i - 1] == REFERENCE_MISSING) {
5670 mono_error_set_file_not_found (error, NULL, "Assembly %d referenced from assembly %s not found ", i - 1, assembly->image->name);
5671 goto leave;
5673 MonoReflectionAssemblyHandle assm_obj;
5674 assm_obj = mono_assembly_get_object_handle (mono_domain_get (), assembly->image->references [i - 1], error);
5675 goto_if_nok (error, leave);
5676 MONO_HANDLE_SET (info, assembly, assm_obj);
5678 /* Obtain info recursively */
5679 get_manifest_resource_info_internal (assm_obj, name, info, error);
5680 goto_if_nok (error, leave);
5681 guint32 location;
5682 location = MONO_HANDLE_GETVAL (info, location);
5683 location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
5684 MONO_HANDLE_SETVAL (info, location, guint32, location);
5685 break;
5687 case MONO_IMPLEMENTATION_EXP_TYPE:
5688 g_assert_not_reached ();
5689 break;
5693 result = TRUE;
5694 leave:
5695 HANDLE_FUNCTION_RETURN_VAL (result);
5698 MonoBoolean
5699 ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInfoInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info_h, MonoError *error)
5701 return get_manifest_resource_info_internal (assembly_h, name, info_h, error);
5704 static gboolean
5705 add_filename_to_files_array (MonoDomain *domain, MonoAssembly * assembly, MonoTableInfo *table, int i, MonoArrayHandle dest, int dest_idx, MonoError *error)
5707 HANDLE_FUNCTION_ENTER();
5708 const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
5709 char *n = g_concat_dir_and_file (assembly->basedir, val);
5710 MonoStringHandle str = mono_string_new_handle (domain, n, error);
5711 g_free (n);
5712 goto_if_nok (error, leave);
5713 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, str);
5714 leave:
5715 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5718 MonoObjectHandle
5719 ves_icall_System_Reflection_RuntimeAssembly_GetFilesInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoBoolean resource_modules, MonoError *error)
5721 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
5722 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5723 MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_FILE];
5724 int i, count;
5726 /* check hash if needed */
5727 if (!MONO_HANDLE_IS_NULL(name)) {
5728 char *n = mono_string_handle_to_utf8 (name, error);
5729 goto_if_nok (error, fail);
5731 for (i = 0; i < table->rows; ++i) {
5732 const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
5733 if (strcmp (val, n) == 0) {
5734 g_free (n);
5735 n = g_concat_dir_and_file (assembly->basedir, val);
5736 MonoStringHandle fn = mono_string_new_handle (domain, n, error);
5737 g_free (n);
5738 goto_if_nok (error, fail);
5739 return MONO_HANDLE_CAST (MonoObject, fn);
5742 g_free (n);
5743 return NULL_HANDLE;
5746 count = 0;
5747 for (i = 0; i < table->rows; ++i) {
5748 if (resource_modules || !(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA))
5749 count ++;
5752 MonoArrayHandle result;
5753 result = mono_array_new_handle (domain, mono_defaults.string_class, count, error);
5754 goto_if_nok (error, fail);
5756 count = 0;
5757 for (i = 0; i < table->rows; ++i) {
5758 if (resource_modules || !(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
5759 if (!add_filename_to_files_array (domain, assembly, table, i, result, count, error))
5760 goto fail;
5761 count++;
5764 return MONO_HANDLE_CAST (MonoObject, result);
5765 fail:
5766 return NULL_HANDLE;
5769 static gboolean
5770 add_module_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int *dest_idx, MonoImage* module, MonoError *error)
5772 HANDLE_FUNCTION_ENTER ();
5773 error_init (error);
5774 if (module) {
5775 MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
5776 goto_if_nok (error, leave);
5778 MONO_HANDLE_ARRAY_SETREF (dest, *dest_idx, rm);
5779 ++(*dest_idx);
5782 leave:
5783 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5786 static gboolean
5787 add_file_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int dest_idx, MonoImage *image, MonoTableInfo *table, int table_idx, MonoError *error)
5789 HANDLE_FUNCTION_ENTER ();
5791 guint32 cols [MONO_FILE_SIZE];
5792 mono_metadata_decode_row (table, table_idx, cols, MONO_FILE_SIZE);
5793 if (cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA) {
5794 MonoReflectionModuleHandle rm = mono_module_file_get_object_handle (domain, image, table_idx, error);
5795 goto_if_nok (error, leave);
5796 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rm);
5797 } else {
5798 MonoImage *m = mono_image_load_file_for_image_checked (image, table_idx + 1, error);
5799 goto_if_nok (error, leave);
5800 if (!m) {
5801 const char *filename = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
5802 gboolean refonly = FALSE;
5803 if (image->assembly)
5804 refonly = mono_asmctx_get_kind (&image->assembly->context) == MONO_ASMCTX_REFONLY;
5805 mono_error_set_simple_file_not_found (error, filename, refonly);
5806 goto leave;
5808 MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, m, error);
5809 goto_if_nok (error, leave);
5810 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rm);
5813 leave:
5814 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
5817 MonoArrayHandle
5818 ves_icall_System_Reflection_RuntimeAssembly_GetModulesInternal (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
5820 error_init (error);
5821 MonoDomain *domain = mono_domain_get();
5822 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
5823 MonoClass *klass;
5824 int i, j, file_count = 0;
5825 MonoImage **modules;
5826 guint32 module_count, real_module_count;
5827 MonoTableInfo *table;
5828 MonoImage *image = assembly->image;
5830 g_assert (image != NULL);
5831 g_assert (!assembly_is_dynamic (assembly));
5833 table = &image->tables [MONO_TABLE_FILE];
5834 file_count = table->rows;
5836 modules = image->modules;
5837 module_count = image->module_count;
5839 real_module_count = 0;
5840 for (i = 0; i < module_count; ++i)
5841 if (modules [i])
5842 real_module_count ++;
5844 klass = mono_class_get_module_class ();
5845 MonoArrayHandle res = mono_array_new_handle (domain, klass, 1 + real_module_count + file_count, error);
5846 goto_if_nok (error, fail);
5848 MonoReflectionModuleHandle image_obj;
5849 image_obj = mono_module_get_object_handle (domain, image, error);
5850 goto_if_nok (error, fail);
5852 MONO_HANDLE_ARRAY_SETREF (res, 0, image_obj);
5854 j = 1;
5855 for (i = 0; i < module_count; ++i)
5856 if (!add_module_to_modules_array (domain, res, &j, modules[i], error))
5857 goto fail;
5859 for (i = 0; i < file_count; ++i, ++j) {
5860 if (!add_file_to_modules_array (domain, res, j, image, table, i, error))
5861 goto fail;
5864 return res;
5865 fail:
5866 return NULL_HANDLE_ARRAY;
5869 MonoReflectionMethodHandle
5870 ves_icall_GetCurrentMethod (MonoError *error)
5872 MonoMethod *m = mono_method_get_last_managed ();
5874 if (!m) {
5875 mono_error_set_not_supported (error, "Stack walks are not supported on this platform.");
5876 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5879 while (m->is_inflated)
5880 m = ((MonoMethodInflated*)m)->declaring;
5882 return mono_method_get_object_handle (mono_domain_get (), m, NULL, error);
5885 static MonoMethod*
5886 mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
5888 int offset = -1, i;
5889 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
5890 ERROR_DECL (error);
5891 MonoMethod *result;
5892 MonoMethodInflated *inflated = (MonoMethodInflated*)method;
5893 //method is inflated, we should inflate it on the other class
5894 MonoGenericContext ctx;
5895 ctx.method_inst = inflated->context.method_inst;
5896 ctx.class_inst = inflated->context.class_inst;
5897 if (mono_class_is_ginst (klass))
5898 ctx.class_inst = mono_class_get_generic_class (klass)->context.class_inst;
5899 else if (mono_class_is_gtd (klass))
5900 ctx.class_inst = mono_class_get_generic_container (klass)->context.class_inst;
5901 result = mono_class_inflate_generic_method_full_checked (inflated->declaring, klass, &ctx, error);
5902 g_assert (is_ok (error)); /* FIXME don't swallow the error */
5903 return result;
5906 mono_class_setup_methods (method->klass);
5907 if (mono_class_has_failure (method->klass))
5908 return NULL;
5909 int mcount = mono_class_get_method_count (method->klass);
5910 MonoMethod **method_klass_methods = m_class_get_methods (method->klass);
5911 for (i = 0; i < mcount; ++i) {
5912 if (method_klass_methods [i] == method) {
5913 offset = i;
5914 break;
5917 mono_class_setup_methods (klass);
5918 if (mono_class_has_failure (klass))
5919 return NULL;
5920 g_assert (offset >= 0 && offset < mono_class_get_method_count (klass));
5921 return m_class_get_methods (klass) [offset];
5924 MonoReflectionMethodHandle
5925 ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodFromHandleInternalType_native (MonoMethod *method, MonoType *type, MonoBoolean generic_check, MonoError *error)
5927 MonoClass *klass;
5928 if (type && generic_check) {
5929 klass = mono_class_from_mono_type_internal (type);
5930 if (mono_class_get_generic_type_definition (method->klass) != mono_class_get_generic_type_definition (klass))
5931 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5933 if (method->klass != klass) {
5934 method = mono_method_get_equivalent_method (method, klass);
5935 if (!method)
5936 return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
5938 } else if (type)
5939 klass = mono_class_from_mono_type_internal (type);
5940 else
5941 klass = method->klass;
5942 return mono_method_get_object_handle (mono_domain_get (), method, klass, error);
5945 MonoReflectionMethodBodyHandle
5946 ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodBodyInternal (MonoMethod *method, MonoError *error)
5948 return mono_method_body_get_object_handle (mono_domain_get (), method, error);
5951 #if ENABLE_NETCORE
5952 MonoReflectionAssemblyHandle
5953 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoStackCrawlMark *stack_mark, MonoError *error)
5955 MonoAssembly *assembly;
5956 assembly = mono_runtime_get_caller_from_stack_mark (stack_mark);
5957 g_assert (assembly);
5958 return mono_assembly_get_object_handle (mono_domain_get (), assembly, error);
5960 #else
5961 MonoReflectionAssemblyHandle
5962 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError *error)
5964 MonoMethod *dest = NULL;
5965 mono_stack_walk_no_il (get_executing, &dest);
5966 g_assert (dest);
5967 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
5969 #endif
5971 MonoReflectionAssemblyHandle
5972 ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError *error)
5974 MonoDomain* domain = mono_domain_get ();
5976 if (!domain->entry_assembly)
5977 return MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
5979 return mono_assembly_get_object_handle (domain, domain->entry_assembly, error);
5982 MonoReflectionAssemblyHandle
5983 ves_icall_System_Reflection_Assembly_GetCallingAssembly (MonoError *error)
5985 error_init (error);
5986 MonoMethod *m;
5987 MonoMethod *dest;
5989 dest = NULL;
5990 mono_stack_walk_no_il (get_executing, &dest);
5991 m = dest;
5992 mono_stack_walk_no_il (get_caller_no_reflection, &dest);
5993 if (!dest)
5994 dest = m;
5995 if (!m) {
5996 mono_error_set_not_supported (error, "Stack walks are not supported on this platform.");
5997 return MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
5999 return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
6002 MonoStringHandle
6003 ves_icall_System_RuntimeType_getFullName (MonoReflectionTypeHandle object, MonoBoolean full_name,
6004 MonoBoolean assembly_qualified, MonoError *error)
6006 MonoDomain *domain = mono_object_domain (MONO_HANDLE_RAW (object));
6007 MonoType *type = MONO_HANDLE_RAW (object)->type;
6008 MonoTypeNameFormat format;
6009 MonoStringHandle res;
6010 gchar *name;
6012 if (full_name)
6013 format = assembly_qualified ?
6014 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED :
6015 MONO_TYPE_NAME_FORMAT_FULL_NAME;
6016 else
6017 format = MONO_TYPE_NAME_FORMAT_REFLECTION;
6019 name = mono_type_get_name_full (type, format);
6020 if (!name)
6021 return NULL_HANDLE_STRING;
6023 if (full_name && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
6024 g_free (name);
6025 return NULL_HANDLE_STRING;
6028 res = mono_string_new_handle (domain, name, error);
6029 g_free (name);
6031 return res;
6034 #ifndef ENABLE_NETCORE
6036 ves_icall_RuntimeType_get_core_clr_security_level (MonoReflectionTypeHandle rfield, MonoError *error)
6038 MonoType *type = MONO_HANDLE_GETVAL (rfield, type);
6039 MonoClass *klass = mono_class_from_mono_type_internal (type);
6041 mono_class_init_checked (klass, error);
6042 return_val_if_nok (error, -1);
6043 return mono_security_core_clr_class_level (klass);
6045 #endif
6048 ves_icall_RuntimeFieldInfo_get_core_clr_security_level (MonoReflectionFieldHandle rfield, MonoError *error)
6050 MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
6051 return mono_security_core_clr_field_level (field, TRUE);
6055 ves_icall_RuntimeMethodInfo_get_core_clr_security_level (MonoReflectionMethodHandle rfield, MonoError *error)
6057 MonoMethod *method = MONO_HANDLE_GETVAL (rfield, method);
6058 return mono_security_core_clr_method_level (method, TRUE);
6061 MonoStringHandle
6062 ves_icall_System_Reflection_RuntimeAssembly_get_fullname (MonoReflectionAssemblyHandle assembly, MonoError *error)
6064 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
6065 MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
6066 gchar *name;
6068 name = mono_stringify_assembly_name (&mass->aname);
6069 MonoStringHandle res = mono_string_new_handle (domain, name, error);
6070 g_free (name);
6071 return res;
6074 MonoAssemblyName *
6075 ves_icall_System_Reflection_AssemblyName_GetNativeName (MonoAssembly *mass)
6077 return &mass->aname;
6080 void
6081 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoStringHandle fname, MonoAssemblyName *name, MonoStringHandleOut normalized_codebase, MonoError *error)
6083 char *filename;
6084 MonoImageOpenStatus status = MONO_IMAGE_OK;
6085 char *codebase = NULL;
6086 gboolean res;
6087 MonoImage *image;
6088 char *dirname;
6090 error_init (error);
6092 MonoDomain *domain = MONO_HANDLE_DOMAIN (fname);
6093 filename = mono_string_handle_to_utf8 (fname, error);
6094 return_if_nok (error);
6096 dirname = g_path_get_dirname (filename);
6097 replace_shadow_path (mono_domain_get (), dirname, &filename);
6098 g_free (dirname);
6100 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_ASSEMBLY, "InternalGetAssemblyName (\"%s\")", filename);
6102 MonoAssemblyLoadContext *alc = mono_domain_default_alc (domain);
6103 image = mono_image_open_a_lot (alc, filename, &status, TRUE, FALSE);
6105 if (!image){
6106 if (status == MONO_IMAGE_IMAGE_INVALID)
6107 mono_error_set_bad_image_by_name (error, filename, "Invalid Image: %s", filename);
6108 else
6109 mono_error_set_simple_file_not_found (error, filename, FALSE);
6110 g_free (filename);
6111 return;
6114 res = mono_assembly_fill_assembly_name_full (image, name, TRUE);
6115 if (!res) {
6116 mono_image_close (image);
6117 g_free (filename);
6118 mono_error_set_argument (error, "assemblyFile", "The file does not contain a manifest");
6119 return;
6122 if (filename != NULL && *filename != '\0') {
6123 gchar *result;
6125 codebase = g_strdup (filename);
6127 mono_icall_make_platform_path (codebase);
6129 const gchar *prepend = mono_icall_get_file_path_prefix (codebase);
6131 result = g_strconcat (prepend, codebase, (const char*)NULL);
6132 g_free (codebase);
6133 codebase = result;
6135 MONO_HANDLE_ASSIGN (normalized_codebase, mono_string_new_handle (mono_domain_get (), codebase, error));
6136 g_free (codebase);
6138 mono_image_close (image);
6139 g_free (filename);
6142 #ifndef ENABLE_NETCORE
6143 MonoBoolean
6144 ves_icall_System_Reflection_RuntimeAssembly_LoadPermissions (MonoReflectionAssemblyHandle assembly_h,
6145 char **minimum, guint32 *minLength, char **optional, guint32 *optLength, char **refused, guint32 *refLength, MonoError *error)
6147 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
6148 MonoBoolean result = FALSE;
6149 MonoDeclSecurityEntry entry;
6151 /* SecurityAction.RequestMinimum */
6152 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQMIN, &entry)) {
6153 *minimum = entry.blob;
6154 *minLength = entry.size;
6155 result = TRUE;
6157 /* SecurityAction.RequestOptional */
6158 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQOPT, &entry)) {
6159 *optional = entry.blob;
6160 *optLength = entry.size;
6161 result = TRUE;
6163 /* SecurityAction.RequestRefuse */
6164 if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQREFUSE, &entry)) {
6165 *refused = entry.blob;
6166 *refLength = entry.size;
6167 result = TRUE;
6170 return result;
6172 #endif
6174 static gboolean
6175 mono_module_type_is_visible (MonoTableInfo *tdef, MonoImage *image, int type)
6177 guint32 attrs, visibility;
6178 do {
6179 attrs = mono_metadata_decode_row_col (tdef, type - 1, MONO_TYPEDEF_FLAGS);
6180 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
6181 if (visibility != TYPE_ATTRIBUTE_PUBLIC && visibility != TYPE_ATTRIBUTE_NESTED_PUBLIC)
6182 return FALSE;
6184 } while ((type = mono_metadata_token_index (mono_metadata_nested_in_typedef (image, type))));
6186 return TRUE;
6189 static void
6190 image_get_type (MonoDomain *domain, MonoImage *image, MonoTableInfo *tdef, int table_idx, int count, MonoArrayHandle res, MonoArrayHandle exceptions, MonoBoolean exportedOnly, MonoError *error)
6192 error_init (error);
6193 HANDLE_FUNCTION_ENTER ();
6194 ERROR_DECL (klass_error);
6195 MonoClass *klass = mono_class_get_checked (image, table_idx | MONO_TOKEN_TYPE_DEF, klass_error);
6197 if (klass) {
6198 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
6199 return_if_nok (error);
6201 MONO_HANDLE_ARRAY_SETREF (res, count, rt);
6202 } else {
6203 MonoExceptionHandle ex = mono_error_convert_to_exception_handle (klass_error);
6204 MONO_HANDLE_ARRAY_SETREF (exceptions, count, ex);
6206 HANDLE_FUNCTION_RETURN ();
6209 static MonoArrayHandle
6210 mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArrayHandleOut exceptions, MonoBoolean exportedOnly, MonoError *error)
6212 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
6213 int i, count;
6215 error_init (error);
6217 /* we start the count from 1 because we skip the special type <Module> */
6218 if (exportedOnly) {
6219 count = 0;
6220 for (i = 1; i < tdef->rows; ++i) {
6221 if (mono_module_type_is_visible (tdef, image, i + 1))
6222 count++;
6224 } else {
6225 count = tdef->rows - 1;
6227 MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.runtimetype_class, count, error);
6228 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6229 MONO_HANDLE_ASSIGN (exceptions, mono_array_new_handle (domain, mono_defaults.exception_class, count, error));
6230 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6231 count = 0;
6232 for (i = 1; i < tdef->rows; ++i) {
6233 if (!exportedOnly || mono_module_type_is_visible (tdef, image, i+1)) {
6234 image_get_type (domain, image, tdef, i + 1, count, res, exceptions, exportedOnly, error);
6235 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6236 count++;
6240 return res;
6243 static void
6244 append_module_types (MonoDomain *domain, MonoArrayHandleOut res, MonoArrayHandleOut exceptions, MonoImage *image, MonoBoolean exportedOnly, MonoError *error)
6246 HANDLE_FUNCTION_ENTER ();
6247 error_init (error);
6248 MonoArrayHandle ex2 = MONO_HANDLE_NEW (MonoArray, NULL);
6249 MonoArrayHandle res2 = mono_module_get_types (domain, image, ex2, exportedOnly, error);
6250 goto_if_nok (error, leave);
6252 /* Append the new types to the end of the array */
6253 if (mono_array_handle_length (res2) > 0) {
6254 guint32 len1, len2;
6256 len1 = mono_array_handle_length (res);
6257 len2 = mono_array_handle_length (res2);
6259 MonoArrayHandle res3 = mono_array_new_handle (domain, mono_defaults.runtimetype_class, len1 + len2, error);
6260 goto_if_nok (error, leave);
6262 mono_array_handle_memcpy_refs (res3, 0, res, 0, len1);
6263 mono_array_handle_memcpy_refs (res3, len1, res2, 0, len2);
6264 MONO_HANDLE_ASSIGN (res, res3);
6266 MonoArrayHandle ex3 = mono_array_new_handle (domain, mono_defaults.runtimetype_class, len1 + len2, error);
6267 goto_if_nok (error, leave);
6269 mono_array_handle_memcpy_refs (ex3, 0, exceptions, 0, len1);
6270 mono_array_handle_memcpy_refs (ex3, len1, ex2, 0, len2);
6271 MONO_HANDLE_ASSIGN (exceptions, ex3);
6273 leave:
6274 HANDLE_FUNCTION_RETURN ();
6277 static void
6278 set_class_failure_in_array (MonoArrayHandle exl, int i, MonoClass *klass)
6280 HANDLE_FUNCTION_ENTER ();
6281 ERROR_DECL (unboxed_error);
6282 mono_error_set_for_class_failure (unboxed_error, klass);
6284 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_error_convert_to_exception (unboxed_error));
6285 MONO_HANDLE_ARRAY_SETREF (exl, i, exc);
6286 HANDLE_FUNCTION_RETURN ();
6289 static MonoArrayHandle
6290 assembly_get_types (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
6292 MonoArrayHandle exceptions = MONO_HANDLE_NEW(MonoArray, NULL);
6293 int i;
6295 MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_handle);
6296 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_handle, assembly);
6298 g_assert (!assembly_is_dynamic (assembly));
6299 MonoImage *image = assembly->image;
6300 MonoTableInfo *table = &image->tables [MONO_TABLE_FILE];
6301 MonoArrayHandle res = mono_module_get_types (domain, image, exceptions, exportedOnly, error);
6302 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6304 /* Append data from all modules in the assembly */
6305 for (i = 0; i < table->rows; ++i) {
6306 if (!(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
6307 MonoImage *loaded_image = mono_assembly_load_module_checked (image->assembly, i + 1, error);
6308 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6310 if (loaded_image) {
6311 append_module_types (domain, res, exceptions, loaded_image, exportedOnly, error);
6312 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6317 /* the ReflectionTypeLoadException must have all the types (Types property),
6318 * NULL replacing types which throws an exception. The LoaderException must
6319 * contain all exceptions for NULL items.
6322 int len = mono_array_handle_length (res);
6324 int ex_count = 0;
6325 GList *list = NULL;
6326 MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
6327 for (i = 0; i < len; i++) {
6328 MONO_HANDLE_ARRAY_GETREF (t, res, i);
6330 if (!MONO_HANDLE_IS_NULL (t)) {
6331 MonoClass *klass = mono_type_get_class_internal (MONO_HANDLE_GETVAL (t, type));
6332 if ((klass != NULL) && mono_class_has_failure (klass)) {
6333 /* keep the class in the list */
6334 list = g_list_append (list, klass);
6335 /* and replace Type with NULL */
6336 MONO_HANDLE_ARRAY_SETREF (res, i, NULL_HANDLE);
6338 } else {
6339 ex_count ++;
6343 if (list || ex_count) {
6344 GList *tmp = NULL;
6345 int j, length = g_list_length (list) + ex_count;
6347 MonoArrayHandle exl = mono_array_new_handle (domain, mono_defaults.exception_class, length, error);
6348 if (!is_ok (error)) {
6349 g_list_free (list);
6350 return NULL_HANDLE_ARRAY;
6352 /* Types for which mono_class_get_checked () succeeded */
6353 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, NULL);
6354 for (i = 0, tmp = list; tmp; i++, tmp = tmp->next) {
6355 set_class_failure_in_array (exl, i, (MonoClass*)tmp->data);
6357 /* Types for which it don't */
6358 for (j = 0; j < mono_array_handle_length (exceptions); ++j) {
6359 MONO_HANDLE_ARRAY_GETREF (exc, exceptions, j);
6360 if (!MONO_HANDLE_IS_NULL (exc)) {
6361 g_assert (i < length);
6362 MONO_HANDLE_ARRAY_SETREF (exl, i, exc);
6363 i ++;
6366 g_list_free (list);
6367 list = NULL;
6369 MONO_HANDLE_ASSIGN (exc, mono_get_exception_reflection_type_load_checked (res, exl, error));
6370 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6371 mono_error_set_exception_handle (error, exc);
6372 return NULL_HANDLE_ARRAY;
6375 return res;
6378 #ifndef ENABLE_NETCORE
6379 MonoArrayHandle
6380 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
6382 return assembly_get_types (assembly_handle, exportedOnly, error);
6384 #endif
6386 #if ENABLE_NETCORE
6387 MonoArrayHandle
6388 ves_icall_System_Reflection_RuntimeAssembly_GetExportedTypes (MonoReflectionAssemblyHandle assembly_handle, MonoError *error)
6390 return assembly_get_types (assembly_handle, TRUE, error);
6393 static void
6394 get_top_level_forwarded_type (MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle types, MonoArrayHandle exceptions, int *aindex, int *exception_count)
6396 ERROR_DECL (local_error);
6397 guint32 cols [MONO_EXP_TYPE_SIZE];
6398 MonoClass *klass;
6399 MonoReflectionTypeHandle rt;
6401 mono_metadata_decode_row (table, i, cols, MONO_EXP_TYPE_SIZE);
6402 if (!(cols [MONO_EXP_TYPE_FLAGS] & TYPE_ATTRIBUTE_FORWARDER))
6403 return;
6404 guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
6405 const char *name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
6406 const char *nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
6408 g_assert ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF);
6409 guint32 assembly_idx = impl >> MONO_IMPLEMENTATION_BITS;
6411 mono_assembly_load_reference (image, assembly_idx - 1);
6412 g_assert (image->references [assembly_idx - 1]);
6414 HANDLE_FUNCTION_ENTER ();
6416 if (image->references [assembly_idx - 1] == REFERENCE_MISSING) {
6417 MonoExceptionHandle ex = MONO_HANDLE_NEW (MonoException, mono_get_exception_bad_image_format ("Invalid image"));
6418 MONO_HANDLE_ARRAY_SETREF (types, *aindex, NULL_HANDLE);
6419 MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, ex);
6420 (*exception_count)++; (*aindex)++;
6421 goto exit;
6423 klass = mono_class_from_name_checked (image->references [assembly_idx - 1]->image, nspace, name, local_error);
6424 if (!is_ok (local_error)) {
6425 MonoExceptionHandle ex = mono_error_convert_to_exception_handle (local_error);
6426 MONO_HANDLE_ARRAY_SETREF (types, *aindex, NULL_HANDLE);
6427 MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, ex);
6428 mono_error_cleanup (local_error);
6429 (*exception_count)++; (*aindex)++;
6430 goto exit;
6432 rt = mono_type_get_object_handle (mono_domain_get (), m_class_get_byval_arg (klass), local_error);
6433 if (!is_ok (local_error)) {
6434 MonoExceptionHandle ex = mono_error_convert_to_exception_handle (local_error);
6435 MONO_HANDLE_ARRAY_SETREF (types, *aindex, NULL_HANDLE);
6436 MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, ex);
6437 mono_error_cleanup (local_error);
6438 (*exception_count)++; (*aindex)++;
6439 goto exit;
6441 MONO_HANDLE_ARRAY_SETREF (types, *aindex, rt);
6442 MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, NULL_HANDLE);
6443 (*aindex)++;
6445 exit:
6446 HANDLE_FUNCTION_RETURN ();
6449 MonoArrayHandle
6450 ves_icall_System_Reflection_RuntimeAssembly_GetTopLevelForwardedTypes (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
6452 MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
6453 MonoImage *image = assembly->image;
6454 int count = 0;
6456 g_assert (!assembly_is_dynamic (assembly));
6457 MonoTableInfo *table = &image->tables [MONO_TABLE_EXPORTEDTYPE];
6458 for (int i = 0; i < table->rows; ++i) {
6459 if (mono_metadata_decode_row_col (table, i, MONO_EXP_TYPE_FLAGS) & TYPE_ATTRIBUTE_FORWARDER)
6460 count ++;
6463 MonoArrayHandle types = mono_array_new_handle (mono_domain_get (), mono_defaults.runtimetype_class, count, error);
6464 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6465 MonoArrayHandle exceptions = mono_array_new_handle (mono_domain_get (), mono_defaults.exception_class, count, error);
6466 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6468 int aindex = 0;
6469 int exception_count = 0;
6470 for (int i = 0; i < table->rows; ++i) {
6471 get_top_level_forwarded_type (image, table, i, types, exceptions, &aindex, &exception_count);
6474 if (exception_count > 0) {
6475 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, NULL);
6476 MONO_HANDLE_ASSIGN (exc, mono_get_exception_reflection_type_load_checked (types, exceptions, error));
6477 return_val_if_nok (error, NULL_HANDLE_ARRAY);
6478 mono_error_set_exception_handle (error, exc);
6479 return NULL_HANDLE_ARRAY;
6482 return types;
6484 #endif
6486 void
6487 ves_icall_Mono_RuntimeMarshal_FreeAssemblyName (MonoAssemblyName *aname, MonoBoolean free_struct)
6489 mono_assembly_name_free_internal (aname);
6490 if (free_struct)
6491 g_free (aname);
6494 void
6495 ves_icall_Mono_Runtime_DisableMicrosoftTelemetry (void)
6497 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6498 mono_merp_disable ();
6499 #else
6500 // Icall has platform check in managed too.
6501 g_assert_not_reached ();
6502 #endif
6505 void
6506 ves_icall_Mono_Runtime_AnnotateMicrosoftTelemetry (const char *key, const char *value)
6508 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6509 if (!mono_merp_enabled ())
6510 g_error ("Cannot add attributes to telemetry without enabling subsystem");
6511 mono_merp_add_annotation (key, value);
6512 #else
6513 // Icall has platform check in managed too.
6514 g_assert_not_reached ();
6515 #endif
6518 void
6519 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)
6521 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6522 mono_merp_enable (appBundleID, appSignature, appVersion, merpGUIPath, appPath, configDir);
6524 mono_get_runtime_callbacks ()->install_state_summarizer ();
6525 #else
6526 // Icall has platform check in managed too.
6527 g_assert_not_reached ();
6528 #endif
6531 // Number derived from trials on relevant hardware.
6532 // If it seems large, please confirm it's safe to shrink
6533 // before doing so.
6534 #define MONO_MAX_SUMMARY_LEN_ICALL 500000
6536 MonoStringHandle
6537 ves_icall_Mono_Runtime_ExceptionToState (MonoExceptionHandle exc_handle, guint64 *portable_hash_out, guint64 *unportable_hash_out, MonoError *error)
6539 MonoStringHandle result;
6541 #ifndef DISABLE_CRASH_REPORTING
6542 if (mono_get_eh_callbacks ()->mono_summarize_exception) {
6543 // FIXME: Push handles down into mini/mini-exceptions.c
6544 MonoException *exc = MONO_HANDLE_RAW (exc_handle);
6545 MonoThreadSummary out;
6546 mono_summarize_timeline_start ("ExceptionToState");
6547 mono_summarize_timeline_phase_log (MonoSummarySuspendHandshake);
6548 mono_summarize_timeline_phase_log (MonoSummaryUnmanagedStacks);
6549 mono_get_eh_callbacks ()->mono_summarize_exception (exc, &out);
6550 mono_summarize_timeline_phase_log (MonoSummaryManagedStacks);
6552 *portable_hash_out = (guint64) out.hashes.offset_free_hash;
6553 *unportable_hash_out = (guint64) out.hashes.offset_rich_hash;
6555 MonoStateWriter writer;
6556 char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
6557 mono_state_writer_init (&writer, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
6558 mono_native_state_init (&writer);
6559 mono_summarize_timeline_phase_log (MonoSummaryStateWriter);
6560 gboolean first_thread_added = TRUE;
6561 mono_native_state_add_thread (&writer, &out, NULL, first_thread_added, TRUE);
6562 char *output = mono_native_state_free (&writer, FALSE);
6563 mono_summarize_timeline_phase_log (MonoSummaryStateWriterDone);
6564 result = mono_string_new_handle (mono_domain_get (), output, error);
6565 g_free (output);
6566 g_free (scratch);
6567 return result;
6569 #endif
6571 *portable_hash_out = 0;
6572 *unportable_hash_out = 0;
6573 result = mono_string_new_handle (mono_domain_get (), "", error);
6574 return result;
6577 void
6578 ves_icall_Mono_Runtime_SendMicrosoftTelemetry (const char *payload, guint64 portable_hash, guint64 unportable_hash, MonoError *error)
6580 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
6581 if (!mono_merp_enabled ())
6582 g_error ("Cannot send telemetry without registering parameters first");
6584 pid_t crashed_pid = getpid ();
6586 MonoStackHash hashes;
6587 memset (&hashes, 0, sizeof (MonoStackHash));
6588 hashes.offset_free_hash = portable_hash;
6589 hashes.offset_rich_hash = unportable_hash;
6591 const char *signal = "MANAGED_EXCEPTION";
6593 gboolean success = mono_merp_invoke (crashed_pid, signal, payload, &hashes);
6594 if (!success) {
6595 //g_assert_not_reached ();
6596 mono_error_set_generic_error (error, "System", "Exception", "We were unable to start the Microsoft Error Reporting client.");
6598 #else
6599 // Icall has platform check in managed too.
6600 g_assert_not_reached ();
6601 #endif
6604 void
6605 ves_icall_Mono_Runtime_DumpTelemetry (const char *payload, guint64 portable_hash, guint64 unportable_hash, MonoError *error)
6607 #ifndef DISABLE_CRASH_REPORTING
6608 MonoStackHash hashes;
6609 memset (&hashes, 0, sizeof (MonoStackHash));
6610 hashes.offset_free_hash = portable_hash;
6611 hashes.offset_rich_hash = unportable_hash;
6612 mono_crash_dump (payload, &hashes);
6613 #else
6614 return;
6615 #endif
6618 MonoStringHandle
6619 ves_icall_Mono_Runtime_DumpStateSingle (guint64 *portable_hash, guint64 *unportable_hash, MonoError *error)
6621 MonoStringHandle result;
6623 #ifndef DISABLE_CRASH_REPORTING
6624 MonoStackHash hashes;
6625 memset (&hashes, 0, sizeof (MonoStackHash));
6626 MonoContext *ctx = NULL;
6628 MonoThreadSummary this_thread;
6629 if (!mono_threads_summarize_one (&this_thread, ctx))
6630 return mono_string_new_handle (mono_domain_get (), "", error);
6632 *portable_hash = (guint64) this_thread.hashes.offset_free_hash;
6633 *unportable_hash = (guint64) this_thread.hashes.offset_rich_hash;
6635 MonoStateWriter writer;
6636 char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
6637 mono_state_writer_init (&writer, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
6638 mono_native_state_init (&writer);
6639 gboolean first_thread_added = TRUE;
6640 mono_native_state_add_thread (&writer, &this_thread, NULL, first_thread_added, TRUE);
6641 char *output = mono_native_state_free (&writer, FALSE);
6642 result = mono_string_new_handle (mono_domain_get (), output, error);
6643 g_free (output);
6644 g_free (scratch);
6645 #else
6646 *portable_hash = 0;
6647 *unportable_hash = 0;
6648 result = mono_string_new_handle (mono_domain_get (), "", error);
6649 #endif
6651 return result;
6655 void
6656 ves_icall_Mono_Runtime_RegisterReportingForNativeLib (const char *path_suffix, const char *module_name)
6658 #ifndef DISABLE_CRASH_REPORTING
6659 if (mono_get_eh_callbacks ()->mono_register_native_library)
6660 mono_get_eh_callbacks ()->mono_register_native_library (path_suffix, module_name);
6661 #endif
6664 void
6665 ves_icall_Mono_Runtime_RegisterReportingForAllNativeLibs ()
6667 #ifndef DISABLE_CRASH_REPORTING
6668 if (mono_get_eh_callbacks ()->mono_allow_all_native_libraries)
6669 mono_get_eh_callbacks ()->mono_allow_all_native_libraries ();
6670 #endif
6673 void
6674 ves_icall_Mono_Runtime_EnableCrashReportingLog (const char *directory)
6676 #ifndef DISABLE_CRASH_REPORTING
6677 mono_summarize_set_timeline_dir (directory);
6678 #endif
6682 ves_icall_Mono_Runtime_CheckCrashReportingLog (const char *directory, MonoBoolean clear)
6684 int ret = 0;
6685 #ifndef DISABLE_CRASH_REPORTING
6686 ret = (int) mono_summarize_timeline_read_level (directory, clear != 0);
6687 #endif
6688 return ret;
6691 MonoStringHandle
6692 ves_icall_Mono_Runtime_DumpStateTotal (guint64 *portable_hash, guint64 *unportable_hash, MonoError *error)
6694 MonoStringHandle result;
6696 #ifndef DISABLE_CRASH_REPORTING
6697 char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
6699 char *out;
6700 MonoStackHash hashes;
6701 memset (&hashes, 0, sizeof (MonoStackHash));
6702 MonoContext *ctx = NULL;
6704 while (!mono_dump_start ())
6705 g_usleep (1000); // wait around for other dump to finish
6707 mono_get_runtime_callbacks ()->install_state_summarizer ();
6709 mono_summarize_timeline_start ("DumpStateTotal");
6711 gboolean success = mono_threads_summarize (ctx, &out, &hashes, TRUE, FALSE, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
6712 mono_summarize_timeline_phase_log (MonoSummaryCleanup);
6714 if (!success)
6715 return mono_string_new_handle (mono_domain_get (), "", error);
6717 *portable_hash = (guint64) hashes.offset_free_hash;
6718 *unportable_hash = (guint64) hashes.offset_rich_hash;
6719 result = mono_string_new_handle (mono_domain_get (), out, error);
6721 // out is now a pointer into garbage memory
6722 g_free (scratch);
6724 mono_summarize_timeline_phase_log (MonoSummaryDone);
6726 mono_dump_complete ();
6727 #else
6728 *portable_hash = 0;
6729 *unportable_hash = 0;
6730 result = mono_string_new_handle (mono_domain_get (), "", error);
6731 #endif
6733 return result;
6736 MonoBoolean
6737 ves_icall_System_Reflection_AssemblyName_ParseAssemblyName (const char *name, MonoAssemblyName *aname, MonoBoolean *is_version_defined_arg, MonoBoolean *is_token_defined_arg)
6739 gboolean is_version_defined = FALSE;
6740 gboolean is_token_defined = FALSE;
6741 gboolean result = FALSE;
6743 result = mono_assembly_name_parse_full (name, aname, TRUE, &is_version_defined, &is_token_defined);
6745 *is_version_defined_arg = (MonoBoolean)is_version_defined;
6746 *is_token_defined_arg = (MonoBoolean)is_token_defined;
6748 return result;
6751 MonoReflectionTypeHandle
6752 ves_icall_System_Reflection_RuntimeModule_GetGlobalType (MonoImage *image, MonoError *error)
6754 MonoDomain *domain = mono_domain_get ();
6755 MonoClass *klass;
6757 g_assert (image);
6759 MonoReflectionTypeHandle ret = MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
6761 if (image_is_dynamic (image) && ((MonoDynamicImage*)image)->initial_image)
6762 /* These images do not have a global type */
6763 goto leave;
6765 klass = mono_class_get_checked (image, 1 | MONO_TOKEN_TYPE_DEF, error);
6766 goto_if_nok (error, leave);
6768 ret = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
6769 leave:
6770 return ret;
6773 void
6774 ves_icall_System_Reflection_RuntimeModule_GetGuidInternal (MonoImage *image, MonoArrayHandle guid_h, MonoError *error)
6776 g_assert (mono_array_handle_length (guid_h) == 16);
6778 if (!image->metadata_only) {
6779 g_assert (image->heap_guid.data);
6780 g_assert (image->heap_guid.size >= 16);
6782 MONO_ENTER_NO_SAFEPOINTS;
6783 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
6784 memcpy (data, (guint8*)image->heap_guid.data, 16);
6785 MONO_EXIT_NO_SAFEPOINTS;
6786 } else {
6787 MONO_ENTER_NO_SAFEPOINTS;
6788 guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
6789 memset (data, 0, 16);
6790 MONO_EXIT_NO_SAFEPOINTS;
6794 #ifndef HOST_WIN32
6795 static inline gpointer
6796 mono_icall_module_get_hinstance (MonoImage *image)
6798 return (gpointer) (-1);
6800 #endif /* HOST_WIN32 */
6802 gpointer
6803 ves_icall_System_Reflection_RuntimeModule_GetHINSTANCE (MonoImage *image, MonoError *error)
6805 return mono_icall_module_get_hinstance (image);
6808 void
6809 ves_icall_System_Reflection_RuntimeModule_GetPEKind (MonoImage *image, gint32 *pe_kind, gint32 *machine, MonoError *error)
6811 if (image_is_dynamic (image)) {
6812 MonoDynamicImage *dyn = (MonoDynamicImage*)image;
6813 *pe_kind = dyn->pe_kind;
6814 *machine = dyn->machine;
6816 else {
6817 *pe_kind = (image->image_info->cli_cli_header.ch_flags & 0x3);
6818 *machine = image->image_info->cli_header.coff.coff_machine;
6822 gint32
6823 ves_icall_System_Reflection_RuntimeModule_GetMDStreamVersion (MonoImage *image, MonoError *error)
6825 return (image->md_version_major << 16) | (image->md_version_minor);
6828 MonoArrayHandle
6829 ves_icall_System_Reflection_RuntimeModule_InternalGetTypes (MonoImage *image, MonoError *error)
6831 MonoDomain *domain = mono_domain_get ();
6833 if (!image) {
6834 MonoArrayHandle arr = mono_array_new_handle (domain, mono_defaults.runtimetype_class, 0, error);
6835 return arr;
6836 } else {
6837 MonoArrayHandle exceptions = MONO_HANDLE_NEW (MonoArray, NULL);
6838 MonoArrayHandle res = mono_module_get_types (domain, image, exceptions, FALSE, error);
6839 return_val_if_nok (error, MONO_HANDLE_CAST(MonoArray, NULL_HANDLE));
6841 int n = mono_array_handle_length (exceptions);
6842 MonoExceptionHandle ex = MONO_HANDLE_NEW (MonoException, NULL);
6843 for (int i = 0; i < n; ++i) {
6844 MONO_HANDLE_ARRAY_GETREF(ex, exceptions, i);
6845 if (!MONO_HANDLE_IS_NULL (ex)) {
6846 mono_error_set_exception_handle (error, ex);
6847 return MONO_HANDLE_CAST(MonoArray, NULL_HANDLE);
6850 return res;
6854 static gboolean
6855 mono_memberref_is_method (MonoImage *image, guint32 token)
6857 if (!image_is_dynamic (image)) {
6858 guint32 cols [MONO_MEMBERREF_SIZE];
6859 const char *sig;
6860 const MonoTableInfo *table = &image->tables [MONO_TABLE_MEMBERREF];
6861 int idx = mono_metadata_token_index (token) - 1;
6862 if (idx < 0 || table->rows <= idx) {
6863 return FALSE;
6865 mono_metadata_decode_row (table, idx, cols, MONO_MEMBERREF_SIZE);
6866 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
6867 mono_metadata_decode_blob_size (sig, &sig);
6868 return (*sig != 0x6);
6869 } else {
6870 ERROR_DECL (error);
6871 MonoClass *handle_class;
6873 if (!mono_lookup_dynamic_token_class (image, token, FALSE, &handle_class, NULL, error)) {
6874 mono_error_cleanup (error); /* just probing, ignore error */
6875 return FALSE;
6878 return mono_defaults.methodhandle_class == handle_class;
6882 static MonoGenericInst *
6883 get_generic_inst_from_array_handle (MonoArrayHandle type_args)
6885 int type_argc = mono_array_handle_length (type_args);
6886 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
6888 MonoGenericInst *ginst = (MonoGenericInst *)g_alloca (size);
6889 memset (ginst, 0, MONO_SIZEOF_GENERIC_INST);
6890 ginst->type_argc = type_argc;
6891 for (int i = 0; i < type_argc; i++) {
6892 MONO_HANDLE_ARRAY_GETVAL (ginst->type_argv[i], type_args, MonoType*, i);
6894 ginst->is_open = FALSE;
6895 for (int i = 0; i < type_argc; i++) {
6896 if (mono_class_is_open_constructed_type (ginst->type_argv[i])) {
6897 ginst->is_open = TRUE;
6898 break;
6902 return mono_metadata_get_canonical_generic_inst (ginst);
6905 static void
6906 init_generic_context_from_args_handles (MonoGenericContext *context, MonoArrayHandle type_args, MonoArrayHandle method_args)
6908 if (!MONO_HANDLE_IS_NULL (type_args)) {
6909 context->class_inst = get_generic_inst_from_array_handle (type_args);
6910 } else {
6911 context->class_inst = NULL;
6913 if (!MONO_HANDLE_IS_NULL (method_args)) {
6914 context->method_inst = get_generic_inst_from_array_handle (method_args);
6915 } else {
6916 context->method_inst = NULL;
6921 static MonoType*
6922 module_resolve_type_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6924 HANDLE_FUNCTION_ENTER ();
6925 error_init (error);
6926 MonoType *result = NULL;
6927 MonoClass *klass;
6928 int table = mono_metadata_token_table (token);
6929 int index = mono_metadata_token_index (token);
6930 MonoGenericContext context;
6932 *resolve_error = ResolveTokenError_Other;
6934 /* Validate token */
6935 if ((table != MONO_TABLE_TYPEDEF) && (table != MONO_TABLE_TYPEREF) &&
6936 (table != MONO_TABLE_TYPESPEC)) {
6937 *resolve_error = ResolveTokenError_BadTable;
6938 goto leave;
6941 if (image_is_dynamic (image)) {
6942 if ((table == MONO_TABLE_TYPEDEF) || (table == MONO_TABLE_TYPEREF)) {
6943 ERROR_DECL (inner_error);
6944 klass = (MonoClass *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
6945 mono_error_cleanup (inner_error);
6946 result = klass ? m_class_get_byval_arg (klass) : NULL;
6947 goto leave;
6950 init_generic_context_from_args_handles (&context, type_args, method_args);
6951 ERROR_DECL (inner_error);
6952 klass = (MonoClass *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
6953 mono_error_cleanup (inner_error);
6954 result = klass ? m_class_get_byval_arg (klass) : NULL;
6955 goto leave;
6958 if ((index <= 0) || (index > image->tables [table].rows)) {
6959 *resolve_error = ResolveTokenError_OutOfRange;
6960 goto leave;
6963 init_generic_context_from_args_handles (&context, type_args, method_args);
6964 klass = mono_class_get_checked (image, token, error);
6965 if (klass)
6966 klass = mono_class_inflate_generic_class_checked (klass, &context, error);
6967 goto_if_nok (error, leave);
6969 if (klass)
6970 result = m_class_get_byval_arg (klass);
6971 leave:
6972 HANDLE_FUNCTION_RETURN_VAL (result);
6975 MonoType*
6976 ves_icall_System_Reflection_RuntimeModule_ResolveTypeToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6978 return module_resolve_type_token (image, token, type_args, method_args, resolve_error, error);
6981 static MonoMethod*
6982 module_resolve_method_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
6984 HANDLE_FUNCTION_ENTER ();
6985 error_init (error);
6986 MonoMethod *method = NULL;
6987 int table = mono_metadata_token_table (token);
6988 int index = mono_metadata_token_index (token);
6989 MonoGenericContext context;
6991 *resolve_error = ResolveTokenError_Other;
6993 /* Validate token */
6994 if ((table != MONO_TABLE_METHOD) && (table != MONO_TABLE_METHODSPEC) &&
6995 (table != MONO_TABLE_MEMBERREF)) {
6996 *resolve_error = ResolveTokenError_BadTable;
6997 goto leave;
7000 if (image_is_dynamic (image)) {
7001 if (table == MONO_TABLE_METHOD) {
7002 ERROR_DECL (inner_error);
7003 method = (MonoMethod *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
7004 mono_error_cleanup (inner_error);
7005 goto leave;
7008 if ((table == MONO_TABLE_MEMBERREF) && !(mono_memberref_is_method (image, token))) {
7009 *resolve_error = ResolveTokenError_BadTable;
7010 goto leave;
7013 init_generic_context_from_args_handles (&context, type_args, method_args);
7014 ERROR_DECL (inner_error);
7015 method = (MonoMethod *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
7016 mono_error_cleanup (inner_error);
7017 goto leave;
7020 if ((index <= 0) || (index > image->tables [table].rows)) {
7021 *resolve_error = ResolveTokenError_OutOfRange;
7022 goto leave;
7024 if ((table == MONO_TABLE_MEMBERREF) && (!mono_memberref_is_method (image, token))) {
7025 *resolve_error = ResolveTokenError_BadTable;
7026 goto leave;
7029 init_generic_context_from_args_handles (&context, type_args, method_args);
7030 method = mono_get_method_checked (image, token, NULL, &context, error);
7032 leave:
7033 HANDLE_FUNCTION_RETURN_VAL (method);
7036 MonoMethod*
7037 ves_icall_System_Reflection_RuntimeModule_ResolveMethodToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
7039 return module_resolve_method_token (image, token, type_args, method_args, resolve_error, error);
7042 MonoStringHandle
7043 ves_icall_System_Reflection_RuntimeModule_ResolveStringToken (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
7045 int index = mono_metadata_token_index (token);
7047 *resolve_error = ResolveTokenError_Other;
7049 /* Validate token */
7050 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
7051 *resolve_error = ResolveTokenError_BadTable;
7052 return NULL_HANDLE_STRING;
7055 if (image_is_dynamic (image)) {
7056 ERROR_DECL (ignore_inner_error);
7057 // FIXME ignoring error
7058 // FIXME Push MONO_HANDLE_NEW to lower layers.
7059 MonoStringHandle result = MONO_HANDLE_NEW (MonoString, (MonoString*)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, ignore_inner_error));
7060 mono_error_cleanup (ignore_inner_error);
7061 return result;
7064 if ((index <= 0) || (index >= image->heap_us.size)) {
7065 *resolve_error = ResolveTokenError_OutOfRange;
7066 return NULL_HANDLE_STRING;
7069 /* FIXME: What to do if the index points into the middle of a string ? */
7070 return mono_ldstr_handle (mono_domain_get (), image, index, error);
7073 static MonoClassField*
7074 module_resolve_field_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
7076 HANDLE_FUNCTION_ENTER ();
7077 MonoClass *klass;
7078 int table = mono_metadata_token_table (token);
7079 int index = mono_metadata_token_index (token);
7080 MonoGenericContext context;
7081 MonoClassField *field = NULL;
7083 error_init (error);
7084 *resolve_error = ResolveTokenError_Other;
7086 /* Validate token */
7087 if ((table != MONO_TABLE_FIELD) && (table != MONO_TABLE_MEMBERREF)) {
7088 *resolve_error = ResolveTokenError_BadTable;
7089 goto leave;
7092 if (image_is_dynamic (image)) {
7093 if (table == MONO_TABLE_FIELD) {
7094 ERROR_DECL (inner_error);
7095 field = (MonoClassField *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
7096 mono_error_cleanup (inner_error);
7097 goto leave;
7100 if (mono_memberref_is_method (image, token)) {
7101 *resolve_error = ResolveTokenError_BadTable;
7102 goto leave;
7105 init_generic_context_from_args_handles (&context, type_args, method_args);
7106 ERROR_DECL (inner_error);
7107 field = (MonoClassField *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
7108 mono_error_cleanup (inner_error);
7109 goto leave;
7112 if ((index <= 0) || (index > image->tables [table].rows)) {
7113 *resolve_error = ResolveTokenError_OutOfRange;
7114 goto leave;
7116 if ((table == MONO_TABLE_MEMBERREF) && (mono_memberref_is_method (image, token))) {
7117 *resolve_error = ResolveTokenError_BadTable;
7118 goto leave;
7121 init_generic_context_from_args_handles (&context, type_args, method_args);
7122 field = mono_field_from_token_checked (image, token, &klass, &context, error);
7124 leave:
7125 HANDLE_FUNCTION_RETURN_VAL (field);
7128 MonoClassField*
7129 ves_icall_System_Reflection_RuntimeModule_ResolveFieldToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
7131 return module_resolve_field_token (image, token, type_args, method_args, resolve_error, error);
7134 MonoObjectHandle
7135 ves_icall_System_Reflection_RuntimeModule_ResolveMemberToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *error, MonoError *merror)
7137 int table = mono_metadata_token_table (token);
7139 *error = ResolveTokenError_Other;
7141 switch (table) {
7142 case MONO_TABLE_TYPEDEF:
7143 case MONO_TABLE_TYPEREF:
7144 case MONO_TABLE_TYPESPEC: {
7145 MonoType *t = module_resolve_type_token (image, token, type_args, method_args, error, merror);
7146 if (t) {
7147 return MONO_HANDLE_CAST (MonoObject, mono_type_get_object_handle (mono_domain_get (), t, merror));
7149 else
7150 return NULL_HANDLE;
7152 case MONO_TABLE_METHOD:
7153 case MONO_TABLE_METHODSPEC: {
7154 MonoMethod *m = module_resolve_method_token (image, token, type_args, method_args, error, merror);
7155 if (m) {
7156 return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
7157 } else
7158 return NULL_HANDLE;
7160 case MONO_TABLE_FIELD: {
7161 MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
7162 if (f) {
7163 return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
7165 else
7166 return NULL_HANDLE;
7168 case MONO_TABLE_MEMBERREF:
7169 if (mono_memberref_is_method (image, token)) {
7170 MonoMethod *m = module_resolve_method_token (image, token, type_args, method_args, error, merror);
7171 if (m) {
7172 return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
7173 } else
7174 return NULL_HANDLE;
7176 else {
7177 MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
7178 if (f) {
7179 return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
7181 else
7182 return NULL_HANDLE;
7184 break;
7186 default:
7187 *error = ResolveTokenError_BadTable;
7190 return NULL_HANDLE;
7193 MonoArrayHandle
7194 ves_icall_System_Reflection_RuntimeModule_ResolveSignature (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
7196 int table = mono_metadata_token_table (token);
7197 int idx = mono_metadata_token_index (token);
7198 MonoTableInfo *tables = image->tables;
7199 guint32 sig, len;
7200 const char *ptr;
7202 *resolve_error = ResolveTokenError_OutOfRange;
7204 /* FIXME: Support other tables ? */
7205 if (table != MONO_TABLE_STANDALONESIG)
7206 return NULL_HANDLE_ARRAY;
7208 if (image_is_dynamic (image))
7209 return NULL_HANDLE_ARRAY;
7211 if ((idx == 0) || (idx > tables [MONO_TABLE_STANDALONESIG].rows))
7212 return NULL_HANDLE_ARRAY;
7214 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
7216 ptr = mono_metadata_blob_heap (image, sig);
7217 len = mono_metadata_decode_blob_size (ptr, &ptr);
7219 MonoArrayHandle res = mono_array_new_handle (mono_domain_get (), mono_defaults.byte_class, len, error);
7220 return_val_if_nok (error, NULL_HANDLE_ARRAY);
7222 // FIXME MONO_ENTER_NO_SAFEPOINTS instead of pin/gchandle.
7224 MonoGCHandle h;
7225 gpointer array_base = MONO_ARRAY_HANDLE_PIN (res, guint8, 0, &h);
7226 memcpy (array_base, ptr, len);
7227 mono_gchandle_free_internal (h);
7229 return res;
7232 static void
7233 check_for_invalid_array_type (MonoClass *klass, MonoError *error)
7235 char *name;
7237 error_init (error);
7239 if (m_class_get_byval_arg (klass)->type != MONO_TYPE_TYPEDBYREF)
7240 return;
7242 name = mono_type_get_full_name (klass);
7243 mono_error_set_type_load_name (error, name, g_strdup (""), "");
7246 static void
7247 check_for_invalid_byref_or_pointer_type (MonoClass *klass, MonoError *error)
7249 #ifdef ENABLE_NETCORE
7250 return;
7251 #else
7252 check_for_invalid_array_type (klass, error);
7253 #endif
7256 MonoReflectionTypeHandle
7257 ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int rank, MonoError *error)
7259 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
7261 MonoClass *klass = mono_class_from_mono_type_internal (type);
7262 check_for_invalid_array_type (klass, error);
7263 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
7265 MonoClass *aklass;
7266 if (rank == 0) //single dimension array
7267 aklass = mono_class_create_array (klass, 1);
7268 else
7269 aklass = mono_class_create_bounded_array (klass, rank, TRUE);
7271 if (mono_class_has_failure (aklass)) {
7272 mono_error_set_for_class_failure (error, aklass);
7273 return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
7276 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
7277 return mono_type_get_object_handle (domain, m_class_get_byval_arg (aklass), error);
7280 MonoReflectionTypeHandle
7281 ves_icall_RuntimeType_make_byref_type (MonoReflectionTypeHandle ref_type, MonoError *error)
7283 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
7285 MonoClass *klass = mono_class_from_mono_type_internal (type);
7286 mono_class_init_checked (klass, error);
7287 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
7289 check_for_invalid_byref_or_pointer_type (klass, error);
7290 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
7292 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
7293 return mono_type_get_object_handle (domain, m_class_get_this_arg (klass), error);
7296 MonoReflectionTypeHandle
7297 ves_icall_RuntimeType_MakePointerType (MonoReflectionTypeHandle ref_type, MonoError *error)
7299 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
7300 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
7301 MonoClass *klass = mono_class_from_mono_type_internal (type);
7302 mono_class_init_checked (klass, error);
7303 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
7305 check_for_invalid_byref_or_pointer_type (klass, error);
7306 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
7308 MonoClass *pklass = mono_class_create_ptr (type);
7310 return mono_type_get_object_handle (domain, m_class_get_byval_arg (pklass), error);
7313 MonoObjectHandle
7314 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionTypeHandle ref_type, MonoObjectHandle target,
7315 MonoReflectionMethodHandle info, MonoBoolean throwOnBindFailure, MonoError *error)
7317 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
7318 MonoClass *delegate_class = mono_class_from_mono_type_internal (type);
7319 MonoMethod *method = MONO_HANDLE_GETVAL (info, method);
7320 MonoMethodSignature *sig = mono_method_signature_internal (method);
7322 mono_class_init_checked (delegate_class, error);
7323 return_val_if_nok (error, NULL_HANDLE);
7325 if (!(m_class_get_parent (delegate_class) == mono_defaults.multicastdelegate_class)) {
7326 /* FIXME improve this exception message */
7327 mono_error_set_execution_engine (error, "file %s: line %d (%s): assertion failed: (%s)", __FILE__, __LINE__,
7328 __func__,
7329 "delegate_class->parent == mono_defaults.multicastdelegate_class");
7330 return NULL_HANDLE;
7333 if (mono_security_core_clr_enabled ()) {
7334 ERROR_DECL (security_error);
7335 if (!mono_security_core_clr_ensure_delegate_creation (method, security_error)) {
7336 if (throwOnBindFailure)
7337 mono_error_move (error, security_error);
7338 else
7339 mono_error_cleanup (security_error);
7340 return NULL_HANDLE;
7344 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7345 if (!method->is_inflated) {
7346 mono_error_set_argument (error, "method", " Cannot bind to the target method because its signature differs from that of the delegate type");
7347 return NULL_HANDLE;
7351 MonoObjectHandle delegate = mono_object_new_handle (MONO_HANDLE_DOMAIN (ref_type), delegate_class, error);
7352 return_val_if_nok (error, NULL_HANDLE);
7354 if (!method_is_dynamic (method) && (!MONO_HANDLE_IS_NULL (target) && method->flags & METHOD_ATTRIBUTE_VIRTUAL && method->klass != mono_handle_class (target))) {
7355 method = mono_object_handle_get_virtual_method (target, method, error);
7356 return_val_if_nok (error, NULL_HANDLE);
7359 mono_delegate_ctor (delegate, target, NULL, method, error);
7360 return_val_if_nok (error, NULL_HANDLE);
7361 return delegate;
7364 MonoMulticastDelegateHandle
7365 ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegateHandle delegate, MonoError *error)
7367 MonoClass *klass = mono_handle_class (delegate);
7368 g_assert (mono_class_has_parent (klass, mono_defaults.multicastdelegate_class));
7370 MonoMulticastDelegateHandle ret = MONO_HANDLE_CAST (MonoMulticastDelegate, mono_object_new_handle (MONO_HANDLE_DOMAIN (delegate), klass, error));
7371 return_val_if_nok (error, MONO_HANDLE_CAST (MonoMulticastDelegate, NULL_HANDLE));
7373 MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoDelegate, ret), invoke_impl, gpointer, mono_runtime_create_delegate_trampoline (klass));
7375 return ret;
7378 MonoReflectionMethodHandle
7379 ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegateHandle delegate, MonoError *error)
7381 MonoObjectHandle delegate_target = MONO_HANDLE_NEW_GET (MonoObject, delegate, target);
7382 MonoMethod *m = mono_object_handle_get_virtual_method (delegate_target, MONO_HANDLE_GETVAL (delegate, method), error);
7383 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
7384 return mono_method_get_object_handle (mono_domain_get (), m, m->klass, error);
7387 /* System.Buffer */
7389 static gint32
7390 mono_array_get_byte_length (MonoArrayHandle array)
7392 int length;
7394 MonoClass * const klass = mono_handle_class (array);
7396 // This resembles mono_array_get_length, but adds the loop.
7398 if (mono_handle_array_has_bounds (array)) {
7399 length = 1;
7400 const int klass_rank = m_class_get_rank (klass);
7401 for (int i = 0; i < klass_rank; ++ i)
7402 length *= MONO_HANDLE_GETVAL (array, bounds [i].length);
7403 } else {
7404 length = mono_array_handle_length (array);
7407 switch (m_class_get_byval_arg (m_class_get_element_class (klass))->type) {
7408 case MONO_TYPE_I1:
7409 case MONO_TYPE_U1:
7410 case MONO_TYPE_BOOLEAN:
7411 return length;
7412 case MONO_TYPE_I2:
7413 case MONO_TYPE_U2:
7414 case MONO_TYPE_CHAR:
7415 return length << 1;
7416 case MONO_TYPE_I4:
7417 case MONO_TYPE_U4:
7418 case MONO_TYPE_R4:
7419 return length << 2;
7420 case MONO_TYPE_I:
7421 case MONO_TYPE_U:
7422 return length * sizeof (gpointer);
7423 case MONO_TYPE_I8:
7424 case MONO_TYPE_U8:
7425 case MONO_TYPE_R8:
7426 return length << 3;
7427 default:
7428 return -1;
7432 #ifndef ENABLE_NETCORE
7433 gint32
7434 ves_icall_System_Buffer_ByteLengthInternal (MonoArrayHandle array, MonoError* error)
7436 return mono_array_get_byte_length (array);
7439 void
7440 ves_icall_System_Buffer_MemcpyInternal (gpointer dest, gconstpointer src, gint32 count)
7442 memcpy (dest, src, count);
7445 MonoBoolean
7446 ves_icall_System_Buffer_BlockCopyInternal (MonoArrayHandle src, gint32 src_offset, MonoArrayHandle dest, gint32 dest_offset, gint32 count, MonoError* error)
7448 if (count < 0) {
7449 mono_error_set_argument (error, "count", "is negative");
7450 return FALSE;
7453 /* This is called directly from the class libraries without going through the managed wrapper */
7454 MONO_CHECK_ARG_NULL_HANDLE (src, FALSE);
7455 MONO_CHECK_ARG_NULL_HANDLE (dest, FALSE);
7457 /* watch out for integer overflow */
7458 if ((src_offset > mono_array_get_byte_length (src) - count) || (dest_offset > mono_array_get_byte_length (dest) - count))
7459 return FALSE;
7461 MONO_ENTER_NO_SAFEPOINTS;
7463 guint8 const * const src_buf = (guint8*)MONO_HANDLE_RAW (src)->vector + src_offset;
7464 guint8* const dest_buf = (guint8*)MONO_HANDLE_RAW (dest)->vector + dest_offset;
7466 #if !HOST_WIN32
7468 // Windows memcpy is memmove and checks for overlap anyway, so skip
7469 // the check here that would not help.
7471 if (MONO_HANDLE_RAW (src) != MONO_HANDLE_RAW (dest))
7472 memcpy (dest_buf, src_buf, count);
7473 else
7474 #endif
7475 memmove (dest_buf, src_buf, count); /* Source and dest are the same array */
7477 MONO_EXIT_NO_SAFEPOINTS;
7479 return TRUE;
7481 #endif
7483 #ifndef DISABLE_REMOTING
7484 MonoObjectHandle
7485 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObjectHandle this_obj, MonoStringHandle class_name, MonoError *error)
7487 MonoDomain *domain = MONO_HANDLE_DOMAIN (this_obj);
7488 MonoRealProxyHandle rp = MONO_HANDLE_CAST (MonoRealProxy, this_obj);
7490 MonoObjectHandle res = mono_object_new_handle (domain, mono_defaults.transparent_proxy_class, error);
7491 return_val_if_nok (error, NULL_HANDLE);
7493 MonoTransparentProxyHandle tp = MONO_HANDLE_CAST (MonoTransparentProxy, res);
7495 MONO_HANDLE_SET (tp, rp, rp);
7497 MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
7498 MONO_HANDLE_GET (reftype, rp, class_to_proxy);
7499 MonoType *type = MONO_HANDLE_GETVAL (reftype, type);
7500 MonoClass *klass = mono_class_from_mono_type_internal (type);
7502 // mono_remote_class_vtable cannot handle errors well, so force any loading error to occur early
7503 mono_class_setup_vtable (klass);
7504 if (mono_class_has_failure (klass)) {
7505 mono_error_set_for_class_failure (error, klass);
7506 return NULL_HANDLE;
7509 MonoObjectHandle remoting_obj = mono_object_handle_isinst (this_obj, mono_defaults.iremotingtypeinfo_class, error);
7510 return_val_if_nok (error, NULL_HANDLE);
7511 MONO_HANDLE_SETVAL (tp, custom_type_info, MonoBoolean, !MONO_HANDLE_IS_NULL (remoting_obj));
7513 MonoRemoteClass *remote_class = (MonoRemoteClass*)mono_remote_class (domain, class_name, klass, error);
7514 return_val_if_nok (error, NULL_HANDLE);
7515 MONO_HANDLE_SETVAL (tp, remote_class, MonoRemoteClass*, remote_class);
7517 MONO_HANDLE_SETVAL (res, vtable, MonoVTable*, (MonoVTable*)mono_remote_class_vtable (domain, remote_class, rp, error));
7518 return_val_if_nok (error, NULL_HANDLE);
7519 return res;
7522 MonoReflectionTypeHandle
7523 ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxyHandle tp, MonoError *error)
7525 MonoRemoteClass *remote_class;
7527 g_assert (mono_handle_class (tp) == mono_defaults.transparent_proxy_class);
7528 remote_class = MONO_HANDLE_RAW (tp)->remote_class;
7529 g_assert (remote_class != NULL && remote_class->proxy_class != NULL);
7530 return mono_type_get_object_handle (mono_handle_domain (tp), m_class_get_byval_arg (remote_class->proxy_class), error);
7532 #endif
7534 /* System.Environment */
7536 #ifndef ENABLE_NETCORE
7537 MonoStringHandle
7538 ves_icall_System_Environment_get_UserName (MonoError *error)
7540 /* using glib is more portable */
7541 const gchar *user_name = g_get_user_name ();
7542 if (user_name != NULL)
7543 return mono_string_new_handle (mono_domain_get (), user_name, error);
7544 else
7545 return NULL_HANDLE_STRING;
7548 #ifndef HOST_WIN32
7549 static MonoStringHandle
7550 mono_icall_get_machine_name (MonoError *error)
7552 #if !defined(DISABLE_SOCKETS)
7553 MonoStringHandle result;
7554 char *buf;
7555 int n;
7556 #if defined _SC_HOST_NAME_MAX
7557 n = sysconf (_SC_HOST_NAME_MAX);
7558 if (n == -1)
7559 #endif
7560 n = 512;
7561 buf = (char*)g_malloc (n + 1);
7563 #if defined(HAVE_GETHOSTNAME)
7564 if (gethostname (buf, n) == 0){
7565 buf [n] = 0;
7566 int i;
7567 // try truncating the string at the first dot
7568 for (i = 0; i < n; i++) {
7569 if (buf [i] == '.') {
7570 buf [i] = 0;
7571 break;
7574 result = mono_string_new_handle (mono_domain_get (), buf, error);
7575 } else
7576 #endif
7577 result = MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
7579 g_free (buf);
7581 return result;
7582 #else
7583 return mono_string_new_handle (mono_domain_get (), "mono", error);
7584 #endif
7586 #endif /* !HOST_WIN32 */
7588 MonoStringHandle
7589 ves_icall_System_Environment_get_MachineName (MonoError *error)
7591 return mono_icall_get_machine_name (error);
7594 #ifndef HOST_WIN32
7595 static int
7596 mono_icall_get_platform (void)
7598 #if defined(__MACH__)
7599 /* OSX */
7601 // Notice that the value is hidden from user code, and only exposed
7602 // to mscorlib. This is due to Mono's Unix/MacOS code predating the
7603 // define and making assumptions based on Unix/128/4 values before there
7604 // was a MacOS define. Lots of code would assume that not-Unix meant
7605 // Windows, but in this case, it would be OSX.
7607 return 6;
7608 #else
7609 /* Unix */
7610 return 4;
7611 #endif
7613 #endif /* !HOST_WIN32 */
7616 ves_icall_System_Environment_get_Platform (void)
7618 return mono_icall_get_platform ();
7620 #endif /* !ENABLE_NETCORE */
7622 #ifndef HOST_WIN32
7623 static MonoStringHandle
7624 mono_icall_get_new_line (MonoError *error)
7626 return mono_string_new_handle (mono_domain_get (), "\n", error);
7628 #endif /* !HOST_WIN32 */
7630 #ifndef ENABLE_NETCORE
7631 MonoStringHandle
7632 ves_icall_System_Environment_get_NewLine (MonoError *error)
7634 return mono_icall_get_new_line (error);
7636 #endif
7638 #ifndef HOST_WIN32
7639 static inline MonoBoolean
7640 mono_icall_is_64bit_os (void)
7642 #if SIZEOF_VOID_P == 8
7643 return TRUE;
7644 #else
7645 #if defined(HAVE_SYS_UTSNAME_H)
7646 struct utsname name;
7648 if (uname (&name) >= 0) {
7649 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;
7651 #endif
7652 return FALSE;
7653 #endif
7655 #endif /* !HOST_WIN32 */
7657 MonoBoolean
7658 ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
7660 return mono_icall_is_64bit_os ();
7663 MonoStringHandle
7664 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
7666 gchar *value;
7668 if (utf8_name == NULL)
7669 return NULL_HANDLE_STRING;
7671 value = g_getenv (utf8_name);
7673 if (value == 0)
7674 return NULL_HANDLE_STRING;
7676 MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
7677 g_free (value);
7678 return res;
7682 * There is no standard way to get at environ.
7684 #ifndef _MSC_VER
7685 #ifndef __MINGW32_VERSION
7686 #if defined(__APPLE__)
7687 #if defined (TARGET_OSX)
7688 /* Apple defines this in crt_externs.h but doesn't provide that header for
7689 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
7690 * in fact exist on all implementations (so far)
7692 G_BEGIN_DECLS
7693 gchar ***_NSGetEnviron(void);
7694 G_END_DECLS
7695 #define environ (*_NSGetEnviron())
7696 #else
7697 static char *mono_environ[1] = { NULL };
7698 #define environ mono_environ
7699 #endif /* defined (TARGET_OSX) */
7700 #else
7701 G_BEGIN_DECLS
7702 extern
7703 char **environ;
7704 G_END_DECLS
7705 #endif
7706 #endif
7707 #endif
7709 MonoArrayHandle
7710 ves_icall_System_Environment_GetCommandLineArgs (MonoError *error)
7712 MonoArrayHandle result = mono_runtime_get_main_args_handle (error);
7713 return result;
7716 #ifndef HOST_WIN32
7717 static MonoArrayHandle
7718 mono_icall_get_environment_variable_names (MonoError *error)
7720 MonoArrayHandle names;
7721 MonoDomain *domain;
7722 MonoStringHandle str;
7723 gchar **e, **parts;
7724 int n;
7726 n = 0;
7727 for (e = environ; *e != 0; ++ e)
7728 ++ n;
7730 domain = mono_domain_get ();
7731 names = mono_array_new_handle (domain, mono_defaults.string_class, n, error);
7732 return_val_if_nok (error, NULL_HANDLE_ARRAY);
7734 str = MONO_HANDLE_NEW (MonoString, NULL);
7735 n = 0;
7736 for (e = environ; *e != 0; ++ e) {
7737 parts = g_strsplit (*e, "=", 2);
7738 if (*parts != 0) {
7739 MonoString *s = mono_string_new_checked (domain, *parts, error);
7740 MONO_HANDLE_ASSIGN_RAW (str, s);
7741 if (!is_ok (error)) {
7742 g_strfreev (parts);
7743 return NULL_HANDLE_ARRAY;
7745 mono_array_handle_setref (names, n, str);
7748 g_strfreev (parts);
7750 ++ n;
7753 return names;
7755 #endif /* !HOST_WIN32 */
7757 MonoArrayHandle
7758 ves_icall_System_Environment_GetEnvironmentVariableNames (MonoError *error)
7760 return mono_icall_get_environment_variable_names (error);
7763 #ifndef ENABLE_NETCORE
7764 void
7765 ves_icall_System_Environment_InternalSetEnvironmentVariable (const gunichar2 *name, gint32 name_length,
7766 const gunichar2 *value, gint32 value_length, MonoError *error)
7768 #ifdef HOST_WIN32
7769 if (!value || !value_length || !value [0])
7770 value = NULL;
7772 SetEnvironmentVariableW (name, value);
7773 #else
7774 char *utf8_name = NULL;
7775 char *utf8_value = NULL;
7777 utf8_name = mono_utf16_to_utf8 (name, name_length, error); // FIXME: this should be ascii
7778 goto_if_nok (error, exit);
7780 if (!value || !value_length || !value [0]) {
7781 g_unsetenv (utf8_name);
7782 goto exit;
7785 utf8_value = mono_utf16_to_utf8 (value, value_length, error);
7786 goto_if_nok (error, exit);
7788 g_setenv (utf8_name, utf8_value, TRUE);
7789 exit:
7790 g_free (utf8_name);
7791 g_free (utf8_value);
7792 #endif
7794 #endif
7796 void
7797 ves_icall_System_Environment_Exit (int result)
7799 mono_environment_exitcode_set (result);
7801 if (!mono_runtime_try_shutdown ())
7802 mono_thread_exit ();
7804 #ifndef ENABLE_NETCORE
7805 /* Suspend all managed threads since the runtime is going away */
7806 mono_thread_suspend_all_other_threads ();
7807 #endif
7809 mono_runtime_quit_internal ();
7811 /* we may need to do some cleanup here... */
7812 exit (result);
7815 void
7816 ves_icall_System_Environment_FailFast (MonoStringHandle message, MonoExceptionHandle exception, MonoStringHandle errorSource, MonoError *error)
7818 if (MONO_HANDLE_IS_NULL (message)) {
7819 g_warning ("CLR: Managed code called FailFast without specifying a reason.");
7820 } else {
7821 char *msg = mono_string_handle_to_utf8 (message, error);
7822 g_warning ("CLR: Managed code called FailFast, saying \"%s\"", msg);
7823 #ifndef DISABLE_CRASH_REPORTING
7824 char *old_msg = mono_crash_save_failfast_msg (msg);
7825 g_free (old_msg);
7826 #else
7827 g_free (msg);
7828 #endif
7831 if (!MONO_HANDLE_IS_NULL (exception)) {
7832 mono_print_unhandled_exception_internal ((MonoObject *) MONO_HANDLE_RAW (exception));
7835 // NOTE: While this does trigger WER on Windows it doesn't quite provide all the
7836 // information in the error dump that CoreCLR would. On Windows 7+ we should call
7837 // RaiseFailFastException directly instead of relying on the C runtime doing it
7838 // for us and pass it as much information as possible. On Windows 8+ we can also
7839 // use the __fastfail intrinsic.
7840 abort ();
7843 #ifndef ENABLE_NETCORE
7844 MonoStringHandle
7845 ves_icall_System_Environment_GetGacPath (MonoError *error)
7847 return mono_string_new_handle (mono_domain_get (), mono_assembly_getrootdir (), error);
7849 #endif
7851 #ifndef HOST_WIN32
7852 static inline MonoStringHandle
7853 mono_icall_get_windows_folder_path (int folder, MonoError *error)
7855 error_init (error);
7856 g_warning ("ves_icall_System_Environment_GetWindowsFolderPath should only be called on Windows!");
7857 return mono_string_new_handle (mono_domain_get (), "", error);
7859 #endif /* !HOST_WIN32 */
7861 #ifndef ENABLE_NETCORE
7862 MonoStringHandle
7863 ves_icall_System_Environment_GetWindowsFolderPath (int folder, MonoError *error)
7865 return mono_icall_get_windows_folder_path (folder, error);
7867 #endif
7869 static MonoArrayHandle
7870 mono_icall_get_logical_drives (MonoError *error)
7872 gunichar2 buf [256], *ptr, *dname;
7873 gunichar2 *u16;
7874 guint initial_size = 127, size = 128;
7875 gint ndrives;
7876 MonoArrayHandle result = NULL_HANDLE_ARRAY;
7877 MonoStringHandle drivestr;
7878 MonoDomain *domain = mono_domain_get ();
7879 gint len;
7881 buf [0] = '\0';
7882 ptr = buf;
7884 while (size > initial_size) {
7885 size = (guint) mono_w32file_get_logical_drive (initial_size, ptr, error);
7886 if (!is_ok (error))
7887 goto leave;
7888 if (size > initial_size) {
7889 if (ptr != buf)
7890 g_free (ptr);
7891 ptr = (gunichar2 *)g_malloc0 ((size + 1) * sizeof (gunichar2));
7892 initial_size = size;
7893 size++;
7897 /* Count strings */
7898 dname = ptr;
7899 ndrives = 0;
7900 do {
7901 while (*dname++);
7902 ndrives++;
7903 } while (*dname);
7905 dname = ptr;
7906 result = mono_array_new_handle (domain, mono_defaults.string_class, ndrives, error);
7907 goto_if_nok (error, leave);
7909 drivestr = MONO_HANDLE_NEW (MonoString, NULL);
7910 ndrives = 0;
7911 do {
7912 len = 0;
7913 u16 = dname;
7914 while (*u16) {
7915 u16++; len ++;
7917 MonoString *s = mono_string_new_utf16_checked (domain, dname, len, error);
7918 goto_if_nok (error, leave);
7919 MONO_HANDLE_ASSIGN_RAW (drivestr, s);
7921 mono_array_handle_setref (result, ndrives, drivestr);
7922 ndrives ++;
7923 while (*dname++);
7924 } while (*dname);
7926 leave:
7927 if (ptr != buf)
7928 g_free (ptr);
7930 return result;
7933 #ifndef ENABLE_NETCORE
7934 MonoArrayHandle
7935 ves_icall_System_Environment_GetLogicalDrivesInternal (MonoError *error)
7937 return mono_icall_get_logical_drives (error);
7940 guint32
7941 ves_icall_System_IO_DriveInfo_GetDriveType (const gunichar2 *root_path_name, gint32 root_path_name_length, MonoError *error)
7943 return mono_w32file_get_drive_type (root_path_name, root_path_name_length, error);
7946 MonoStringHandle
7947 ves_icall_System_IO_DriveInfo_GetDriveFormat (const gunichar2 *path, gint32 path_length, MonoError *error)
7949 gunichar2 volume_name [MAX_PATH + 1];
7951 if (mono_w32file_get_file_system_type (path, volume_name, MAX_PATH + 1) == FALSE)
7952 return NULL_HANDLE_STRING;
7953 return mono_string_new_utf16_handle (mono_domain_get (), volume_name, g_utf16_len (volume_name), error);
7956 MonoStringHandle
7957 ves_icall_System_Environment_InternalGetHome (MonoError *error)
7959 const gchar *home_dir = g_get_home_dir ();
7960 if (home_dir != NULL)
7961 return mono_string_new_handle (mono_domain_get (), home_dir, error);
7962 else
7963 return NULL_HANDLE_STRING;
7966 static const char * const encodings [] = {
7967 (char *) 1,
7968 "ascii", "us_ascii", "us", "ansi_x3.4_1968",
7969 "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
7970 "iso_ir_6", "iso646_us", "iso_646.irv:1991",
7971 (char *) 2,
7972 "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
7973 "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
7974 "x_unicode_2_0_utf_7",
7975 (char *) 3,
7976 "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
7977 "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
7978 (char *) 4,
7979 "utf_16", "UTF_16LE", "ucs_2", "unicode",
7980 "iso_10646_ucs2",
7981 (char *) 5,
7982 "unicodefffe", "utf_16be",
7983 (char *) 6,
7984 "iso_8859_1",
7985 (char *) 0
7989 * Returns the internal codepage, if the value of "int_code_page" is
7990 * 1 at entry, and we can not compute a suitable code page number,
7991 * returns the code page as a string
7993 MonoStringHandle
7994 ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page, MonoError *error)
7996 error_init (error);
7997 const char *cset;
7998 const char *p;
7999 char *c;
8000 char *codepage = NULL;
8001 int code;
8002 int want_name = *int_code_page;
8003 int i;
8005 *int_code_page = -1;
8007 g_get_charset (&cset);
8008 c = codepage = g_strdup (cset);
8009 for (c = codepage; *c; c++){
8010 if (isascii (*c) && isalpha (*c))
8011 *c = tolower (*c);
8012 if (*c == '-')
8013 *c = '_';
8015 /* g_print ("charset: %s\n", cset); */
8017 /* handle some common aliases */
8018 p = encodings [0];
8019 code = 0;
8020 for (i = 0; p != 0; ){
8021 if ((gsize) p < 7){
8022 code = (gssize) p;
8023 p = encodings [++i];
8024 continue;
8026 if (strcmp (p, codepage) == 0){
8027 *int_code_page = code;
8028 break;
8030 p = encodings [++i];
8033 if (strstr (codepage, "utf_8") != NULL)
8034 *int_code_page |= 0x10000000;
8035 g_free (codepage);
8037 if (want_name && *int_code_page == -1)
8038 return mono_string_new_handle (mono_domain_get (), cset, error);
8039 return NULL_HANDLE_STRING;
8041 #endif
8043 MonoBoolean
8044 ves_icall_System_Environment_get_HasShutdownStarted (void)
8046 return mono_runtime_is_shutting_down () || mono_domain_is_unloading (mono_domain_get ());
8049 #ifndef HOST_WIN32
8051 #ifndef ENABLE_NETCORE
8052 void
8053 ves_icall_System_Environment_BroadcastSettingChange (MonoError *error)
8056 #endif
8058 #endif
8060 gint32
8061 ves_icall_System_Environment_get_TickCount (void)
8063 /* this will overflow after ~24 days */
8064 return (gint32) (mono_msec_boottime () & 0xffffffff);
8067 #if ENABLE_NETCORE
8068 gint64
8069 ves_icall_System_Environment_get_TickCount64 (void)
8071 return mono_msec_boottime ();
8073 #endif
8075 #ifndef ENABLE_NETCORE
8076 gint32
8077 ves_icall_System_Runtime_Versioning_VersioningHelper_GetRuntimeId (MonoError *error)
8079 return 9;
8081 #endif
8083 #ifndef DISABLE_REMOTING
8084 MonoBoolean
8085 ves_icall_IsTransparentProxy (MonoObjectHandle proxy, MonoError *error)
8087 if (MONO_HANDLE_IS_NULL (proxy))
8088 return 0;
8090 if (mono_class_is_transparent_proxy (mono_handle_class (proxy)))
8091 return 1;
8093 return 0;
8096 MonoReflectionMethodHandle
8097 ves_icall_Remoting_RemotingServices_GetVirtualMethod (
8098 MonoReflectionTypeHandle rtype, MonoReflectionMethodHandle rmethod, MonoError *error)
8100 MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
8102 if (MONO_HANDLE_IS_NULL (rtype)) {
8103 mono_error_set_argument_null (error, "type", "");
8104 return ret;
8106 if (MONO_HANDLE_IS_NULL (rmethod)) {
8107 mono_error_set_argument_null (error, "method", "");
8108 return ret;
8111 MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
8112 MonoType *type = MONO_HANDLE_GETVAL (rtype, type);
8113 MonoClass *klass = mono_class_from_mono_type_internal (type);
8114 mono_class_init_checked (klass, error);
8115 return_val_if_nok (error, ret);
8117 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass))
8118 return ret;
8120 if (method->flags & METHOD_ATTRIBUTE_STATIC)
8121 return ret;
8123 if ((method->flags & METHOD_ATTRIBUTE_FINAL) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
8124 if (klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE))
8125 ret = rmethod;
8126 return ret;
8129 mono_class_setup_vtable (klass);
8130 MonoMethod **vtable = m_class_get_vtable (klass);
8132 MonoMethod *res = NULL;
8133 if (mono_class_is_interface (method->klass)) {
8134 gboolean variance_used = FALSE;
8135 /*MS fails with variant interfaces but it's the right thing to do anyway.*/
8136 int offs = mono_class_interface_offset_with_variance (klass, method->klass, &variance_used);
8137 if (offs >= 0)
8138 res = vtable [offs + method->slot];
8139 } else {
8140 if (!(klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE)))
8141 return ret;
8143 if (method->slot != -1)
8144 res = vtable [method->slot];
8147 if (!res)
8148 return ret;
8150 ret = mono_method_get_object_handle (mono_domain_get (), res, NULL, error);
8151 return ret;
8154 void
8155 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
8157 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
8158 MonoVTable *vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
8159 return_if_nok (error);
8161 mono_vtable_set_is_remote (vtable, enable);
8164 #else /* DISABLE_REMOTING */
8166 #ifndef ENABLE_NETCORE
8167 void
8168 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
8170 g_assert_not_reached ();
8172 #endif
8174 #endif
8176 #ifndef ENABLE_NETCORE
8177 MonoObjectHandle
8178 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionTypeHandle type, MonoError *error)
8180 MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
8181 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
8182 mono_class_init_checked (klass, error);
8183 return_val_if_nok (error, NULL_HANDLE);
8185 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass) || mono_class_is_abstract (klass)) {
8186 mono_error_set_argument (error, "type", "Type cannot be instantiated");
8187 return NULL_HANDLE;
8190 if (m_class_get_rank (klass) >= 1) {
8191 g_assert (m_class_get_rank (klass) == 1);
8192 return MONO_HANDLE_CAST (MonoObject, mono_array_new_handle (domain, m_class_get_element_class (klass), 0, error));
8193 } else {
8194 MonoVTable *vtable = mono_class_vtable_checked (domain, klass, error);
8195 return_val_if_nok (error, NULL_HANDLE);
8197 /* Bypass remoting object creation check */
8198 return MONO_HANDLE_NEW (MonoObject, mono_object_new_alloc_specific_checked (vtable, error));
8202 MonoStringHandle
8203 ves_icall_System_IO_get_temp_path (MonoError *error)
8205 return mono_string_new_handle (mono_domain_get (), g_get_tmp_dir (), error);
8208 #if defined(ENABLE_MONODROID) || defined(ENABLE_MONOTOUCH) || defined(TARGET_WASM)
8210 // FIXME? Names should start "mono"?
8211 G_EXTERN_C gpointer CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data);
8212 G_EXTERN_C gint32 CloseZStream (gpointer stream);
8213 G_EXTERN_C gint32 Flush (gpointer stream);
8214 G_EXTERN_C gint32 ReadZStream (gpointer stream, gpointer buffer, gint32 length);
8215 G_EXTERN_C gint32 WriteZStream (gpointer stream, gpointer buffer, gint32 length);
8217 gpointer
8218 ves_icall_System_IO_Compression_DeflateStreamNative_CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data)
8220 #ifdef MONO_CROSS_COMPILE
8221 return NULL;
8222 #else
8223 return CreateZStream (compress, gzip, feeder, data);
8224 #endif
8227 gint32
8228 ves_icall_System_IO_Compression_DeflateStreamNative_CloseZStream (gpointer stream)
8230 #ifdef MONO_CROSS_COMPILE
8231 return 0;
8232 #else
8233 return CloseZStream (stream);
8234 #endif
8237 gint32
8238 ves_icall_System_IO_Compression_DeflateStreamNative_Flush (gpointer stream)
8240 #ifdef MONO_CROSS_COMPILE
8241 return 0;
8242 #else
8243 return Flush (stream);
8244 #endif
8247 gint32
8248 ves_icall_System_IO_Compression_DeflateStreamNative_ReadZStream (gpointer stream, gpointer buffer, gint32 length)
8250 #ifdef MONO_CROSS_COMPILE
8251 return 0;
8252 #else
8253 return ReadZStream (stream, buffer, length);
8254 #endif
8257 gint32
8258 ves_icall_System_IO_Compression_DeflateStreamNative_WriteZStream (gpointer stream, gpointer buffer, gint32 length)
8260 #ifdef MONO_CROSS_COMPILE
8261 return 0;
8262 #else
8263 return WriteZStream (stream, buffer, length);
8264 #endif
8267 #endif
8269 #if defined(TARGET_WASM)
8270 G_EXTERN_C void mono_timezone_get_local_name (MonoString **result);
8271 void
8272 ves_icall_System_TimeZoneInfo_mono_timezone_get_local_name (MonoString **result)
8274 // MONO_CROSS_COMPILE returns undefined symbol "_mono_timezone_get_local_name"
8275 // The icall offsets will be generated and linked at build time
8276 // This is defined outside the runtime within the webassembly sdk
8277 #ifndef MONO_CROSS_COMPILE
8278 return mono_timezone_get_local_name (result);
8279 #endif
8281 #endif
8283 #endif /* ENABLE_NETCORE */
8285 #ifndef PLATFORM_NO_DRIVEINFO
8286 MonoBoolean
8287 ves_icall_System_IO_DriveInfo_GetDiskFreeSpace (const gunichar2 *path_name, gint32 path_name_length, guint64 *free_bytes_avail,
8288 guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes,
8289 gint32 *error)
8291 g_assert (error);
8292 g_assert (free_bytes_avail);
8293 g_assert (total_number_of_bytes);
8294 g_assert (total_number_of_free_bytes);
8296 // FIXME check for embedded nuls here or managed
8298 *error = ERROR_SUCCESS;
8299 *free_bytes_avail = (guint64)-1;
8300 *total_number_of_bytes = (guint64)-1;
8301 *total_number_of_free_bytes = (guint64)-1;
8303 gboolean result = mono_w32file_get_disk_free_space (path_name, free_bytes_avail, total_number_of_bytes, total_number_of_free_bytes);
8304 if (!result)
8305 *error = mono_w32error_get_last ();
8307 return result;
8309 #endif /* PLATFORM_NO_DRIVEINFO */
8311 gpointer
8312 ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method, MonoError *error)
8314 return mono_compile_method_checked (method, error);
8317 #ifndef ENABLE_NETCORE
8319 MonoStringHandle
8320 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (MonoError *error)
8322 gchar *path;
8324 const char *mono_cfg_dir = mono_get_config_dir ();
8325 if (!mono_cfg_dir)
8326 return mono_string_new_handle (mono_domain_get (), "", error);
8328 path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", mono_get_runtime_info ()->framework_version, "machine.config", (const char*)NULL);
8330 mono_icall_make_platform_path (path);
8332 MonoStringHandle mcpath = mono_string_new_handle (mono_domain_get (), path, error);
8333 g_free (path);
8335 mono_error_assert_ok (error);
8337 return mcpath;
8340 MonoStringHandle
8341 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_app_config (MonoError *error)
8343 const gchar *app_config;
8344 MonoDomain *domain;
8345 gchar *config_file_name, *config_file_path;
8346 gsize len, config_file_path_length, config_ext_length;
8347 gchar *module;
8349 domain = mono_domain_get ();
8350 MonoStringHandle file = MONO_HANDLE_NEW (MonoString, domain->setup->configuration_file);
8351 if (MONO_HANDLE_IS_NULL (file) || MONO_HANDLE_GETVAL (file, length) == 0)
8352 return MONO_HANDLE_CAST (MonoString, mono_new_null ());
8354 // Retrieve config file and remove the extension
8355 config_file_name = mono_string_handle_to_utf8 (file, error);
8356 return_val_if_nok (error, MONO_HANDLE_CAST (MonoString, NULL_HANDLE));
8358 config_file_path = mono_portability_find_file (config_file_name, TRUE);
8359 if (!config_file_path)
8360 config_file_path = config_file_name;
8362 config_file_path_length = strlen (config_file_path);
8363 config_ext_length = strlen (".config");
8364 if (config_file_path_length <= config_ext_length) {
8365 if (config_file_name != config_file_path)
8366 g_free (config_file_name);
8367 return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
8370 len = config_file_path_length - config_ext_length;
8371 module = (gchar *)g_malloc0 (len + 1);
8372 memcpy (module, config_file_path, len);
8373 // Get the config file from the module name
8374 app_config = mono_config_string_for_assembly_file (module);
8375 // Clean-up
8376 g_free (module);
8377 if (config_file_name != config_file_path)
8378 g_free (config_file_name);
8379 g_free (config_file_path);
8381 if (!app_config)
8382 return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
8384 return mono_string_new_handle (mono_domain_get (), app_config, error);
8387 static MonoStringHandle
8388 get_bundled_machine_config (MonoError *error)
8390 const gchar *machine_config;
8392 machine_config = mono_get_machine_config ();
8394 if (!machine_config)
8395 return NULL_HANDLE_STRING;
8397 return mono_string_new_handle (mono_domain_get (), machine_config, error);
8400 MonoStringHandle
8401 ves_icall_System_Environment_get_bundled_machine_config (MonoError *error)
8403 return get_bundled_machine_config (error);
8406 MonoStringHandle
8407 ves_icall_System_Configuration_DefaultConfig_get_bundled_machine_config (MonoError *error)
8409 return get_bundled_machine_config (error);
8412 MonoStringHandle
8413 ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_machine_config (MonoError *error)
8415 return get_bundled_machine_config (error);
8418 MonoStringHandle
8419 ves_icall_System_Web_Util_ICalls_get_machine_install_dir (MonoError *error)
8421 const char *mono_cfg_dir = mono_get_config_dir ();
8422 if (!mono_cfg_dir)
8423 return mono_string_new_handle (mono_domain_get (), "", error);
8425 char *path = g_path_get_dirname (mono_cfg_dir);
8427 mono_icall_make_platform_path (path);
8429 MonoStringHandle ipath = mono_string_new_handle (mono_domain_get (), path, error);
8430 g_free (path);
8432 return ipath;
8435 MonoBoolean
8436 ves_icall_get_resources_ptr (MonoReflectionAssemblyHandle assembly, gpointer *result, gint32 *size, MonoError *error)
8438 MonoPEResourceDataEntry *entry;
8439 MonoImage *image;
8441 if (MONO_HANDLE_IS_NULL (assembly) || !result || !size)
8442 return FALSE;
8444 *result = NULL;
8445 *size = 0;
8446 MonoAssembly *assm = MONO_HANDLE_GETVAL (assembly, assembly);
8447 image = assm->image;
8448 entry = (MonoPEResourceDataEntry *)mono_image_lookup_resource (image, MONO_PE_RESOURCE_ID_ASPNET_STRING, 0, NULL);
8449 if (!entry)
8450 return FALSE;
8452 *result = mono_image_rva_map (image, entry->rde_data_offset);
8453 if (!(*result)) {
8454 g_free (entry);
8455 return FALSE;
8457 *size = entry->rde_size;
8458 g_free (entry);
8459 return TRUE;
8462 #endif /* ENABLE_NETCORE */
8464 MonoBoolean
8465 ves_icall_System_Diagnostics_Debugger_IsAttached_internal (void)
8467 return mono_is_debugger_attached ();
8470 MonoBoolean
8471 ves_icall_System_Diagnostics_Debugger_IsLogging (void)
8473 return mono_get_runtime_callbacks ()->debug_log_is_enabled
8474 && mono_get_runtime_callbacks ()->debug_log_is_enabled ();
8477 void
8478 ves_icall_System_Diagnostics_Debugger_Log (int level, MonoString *volatile* category, MonoString *volatile* message)
8480 if (mono_get_runtime_callbacks ()->debug_log)
8481 mono_get_runtime_callbacks ()->debug_log (level, *category, *message);
8484 #ifndef HOST_WIN32
8485 static inline void
8486 mono_icall_write_windows_debug_string (const gunichar2 *message)
8488 g_warning ("WriteWindowsDebugString called and HOST_WIN32 not defined!\n");
8490 #endif /* !HOST_WIN32 */
8492 #ifndef ENABLE_NETCORE
8493 void
8494 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (const gunichar2 *message, MonoError *error)
8496 mono_icall_write_windows_debug_string (message);
8498 #endif
8500 /* Only used for value types */
8501 MonoObjectHandle
8502 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
8504 MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
8505 MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
8506 MonoClass *klass = mono_class_from_mono_type_internal (type);
8507 (void)klass;
8509 mono_class_init_checked (klass, error);
8510 return_val_if_nok (error, NULL_HANDLE);
8512 if (mono_class_is_nullable (klass))
8513 /* No arguments -> null */
8514 return NULL_HANDLE;
8516 return mono_object_new_handle (domain, klass, error);
8519 MonoReflectionMethodHandle
8520 ves_icall_RuntimeMethodInfo_get_base_method (MonoReflectionMethodHandle m, MonoBoolean definition, MonoError *error)
8522 MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
8524 MonoMethod *base = mono_method_get_base_method (method, definition, error);
8525 return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
8526 if (base == method) {
8527 /* we want to short-circuit and return 'm' here. But we should
8528 return the same method object that
8529 mono_method_get_object_handle, below would return. Since
8530 that call takes NULL for the reftype argument, it will take
8531 base->klass as the reflected type for the MonoMethod. So we
8532 need to check that m also has base->klass as the reflected
8533 type. */
8534 MonoReflectionTypeHandle orig_reftype = MONO_HANDLE_NEW_GET (MonoReflectionType, m, reftype);
8535 MonoClass *orig_klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (orig_reftype, type));
8536 if (base->klass == orig_klass)
8537 return m;
8539 return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
8542 MonoStringHandle
8543 ves_icall_RuntimeMethodInfo_get_name (MonoReflectionMethodHandle m, MonoError *error)
8545 MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
8547 MonoStringHandle s = mono_string_new_handle (MONO_HANDLE_DOMAIN (m), method->name, error);
8548 return_val_if_nok (error, NULL_HANDLE_STRING);
8549 MONO_HANDLE_SET (m, name, s);
8550 return s;
8553 void
8554 ves_icall_System_ArgIterator_Setup (MonoArgIterator *iter, char* argsp, char* start)
8556 iter->sig = *(MonoMethodSignature**)argsp;
8558 g_assert (iter->sig->sentinelpos <= iter->sig->param_count);
8559 g_assert (iter->sig->call_convention == MONO_CALL_VARARG);
8561 iter->next_arg = 0;
8562 /* FIXME: it's not documented what start is exactly... */
8563 if (start) {
8564 iter->args = start;
8565 } else {
8566 iter->args = argsp + sizeof (gpointer);
8568 iter->num_args = iter->sig->param_count - iter->sig->sentinelpos;
8570 /* g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos); */
8573 void
8574 ves_icall_System_ArgIterator_IntGetNextArg (MonoArgIterator *iter, MonoTypedRef *res)
8576 guint32 i, arg_size;
8577 gint32 align;
8579 i = iter->sig->sentinelpos + iter->next_arg;
8581 g_assert (i < iter->sig->param_count);
8583 res->type = iter->sig->params [i];
8584 res->klass = mono_class_from_mono_type_internal (res->type);
8585 arg_size = mono_type_stack_size (res->type, &align);
8586 #if defined(__arm__) || defined(__mips__)
8587 iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
8588 #endif
8589 res->value = iter->args;
8590 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
8591 if (arg_size <= sizeof (gpointer)) {
8592 int dummy;
8593 int padding = arg_size - mono_type_size (res->type, &dummy);
8594 res->value = (guint8*)res->value + padding;
8596 #endif
8597 iter->args = (char*)iter->args + arg_size;
8598 iter->next_arg++;
8600 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res->type->type, arg_size, res->value); */
8603 void
8604 ves_icall_System_ArgIterator_IntGetNextArgWithType (MonoArgIterator *iter, MonoTypedRef *res, MonoType *type)
8606 guint32 i, arg_size;
8607 gint32 align;
8609 i = iter->sig->sentinelpos + iter->next_arg;
8611 g_assert (i < iter->sig->param_count);
8613 while (i < iter->sig->param_count) {
8614 if (!mono_metadata_type_equal (type, iter->sig->params [i]))
8615 continue;
8616 res->type = iter->sig->params [i];
8617 res->klass = mono_class_from_mono_type_internal (res->type);
8618 /* FIXME: endianess issue... */
8619 arg_size = mono_type_stack_size (res->type, &align);
8620 #if defined(__arm__) || defined(__mips__)
8621 iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
8622 #endif
8623 res->value = iter->args;
8624 iter->args = (char*)iter->args + arg_size;
8625 iter->next_arg++;
8626 /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value); */
8627 return;
8629 /* g_print ("arg type 0x%02x not found\n", res.type->type); */
8631 memset (res, 0, sizeof (MonoTypedRef));
8634 MonoType*
8635 ves_icall_System_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
8637 gint i;
8639 i = iter->sig->sentinelpos + iter->next_arg;
8641 g_assert (i < iter->sig->param_count);
8643 return iter->sig->params [i];
8646 MonoObjectHandle
8647 ves_icall_System_TypedReference_ToObject (MonoTypedRef* tref, MonoError *error)
8649 return typed_reference_to_object (tref, error);
8652 void
8653 ves_icall_System_TypedReference_InternalMakeTypedReference (MonoTypedRef *res, MonoObjectHandle target, MonoArrayHandle fields, MonoReflectionTypeHandle last_field, MonoError *error)
8655 MonoType *ftype = NULL;
8656 int i;
8658 memset (res, 0, sizeof (MonoTypedRef));
8660 g_assert (mono_array_handle_length (fields) > 0);
8662 (void)mono_handle_class (target);
8664 int offset = 0;
8665 for (i = 0; i < mono_array_handle_length (fields); ++i) {
8666 MonoClassField *f;
8667 MONO_HANDLE_ARRAY_GETVAL (f, fields, MonoClassField*, i);
8669 g_assert (f);
8671 if (i == 0)
8672 offset = f->offset;
8673 else
8674 offset += f->offset - sizeof (MonoObject);
8675 (void)mono_class_from_mono_type_internal (f->type);
8676 ftype = f->type;
8679 res->type = ftype;
8680 res->klass = mono_class_from_mono_type_internal (ftype);
8681 res->value = (guint8*)MONO_HANDLE_RAW (target) + offset;
8684 static void
8685 prelink_method (MonoMethod *method, MonoError *error)
8687 error_init (error);
8688 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8689 return;
8690 mono_lookup_pinvoke_call_internal (method, error);
8691 /* create the wrapper, too? */
8694 void
8695 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethodHandle method, MonoError *error)
8697 error_init (error);
8699 prelink_method (MONO_HANDLE_GETVAL (method, method), error);
8702 void
8703 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error);
8705 void
8706 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error)
8708 g_assert_not_netcore ();
8710 error_init (error);
8711 MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
8712 MonoMethod* m;
8713 gpointer iter = NULL;
8715 mono_class_init_checked (klass, error);
8716 return_if_nok (error);
8718 while ((m = mono_class_get_methods (klass, &iter))) {
8719 prelink_method (m, error);
8720 return_if_nok (error);
8724 #ifndef ENABLE_NETCORE
8726 * used by System.Runtime.InteropServices.RuntimeInformation.(OS|Process)Architecture;
8727 * which use them in different ways for filling in an enum
8729 MonoStringHandle
8730 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetRuntimeArchitecture (MonoError *error)
8732 return mono_string_new_handle (mono_domain_get (), mono_config_get_cpu (), error);
8736 * used by System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform
8738 MonoStringHandle
8739 ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetOSName (MonoError *error)
8741 return mono_string_new_handle (mono_domain_get (), mono_config_get_os (), error);
8743 #endif /* ENABLE_NETCORE */
8746 ves_icall_Interop_Sys_DoubleToString(double value, char *format, char *buffer, int bufferLength)
8748 #if defined(TARGET_ARM)
8749 /* workaround for faulty vcmp.f64 implementation on some 32bit ARM CPUs */
8750 guint64 bits = *(guint64 *) &value;
8751 if (bits == 0x1) { /* 4.9406564584124654E-324 */
8752 g_assert (!strcmp (format, "%.40e"));
8753 return snprintf (buffer, bufferLength, "%s", "4.9406564584124654417656879286822137236506e-324");
8754 } else if (bits == 0x4) { /* 2E-323 */
8755 g_assert (!strcmp (format, "%.40e"));
8756 return snprintf (buffer, bufferLength, "%s", "1.9762625833649861767062751714728854894602e-323");
8758 #endif
8760 return snprintf(buffer, bufferLength, format, value);
8763 void
8764 ves_icall_System_Runtime_RuntimeImports_ecvt_s(char *buffer, size_t sizeInBytes, double value, int count, int* dec, int* sign)
8766 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
8767 _ecvt_s(buffer, sizeInBytes, value, count, dec, sign);
8768 #endif
8772 /* These parameters are "readonly" in corlib/System/NumberFormatter.cs */
8773 void
8774 ves_icall_System_NumberFormatter_GetFormatterTables (guint64 const **mantissas,
8775 gint32 const **exponents,
8776 gunichar2 const **digitLowerTable,
8777 gunichar2 const **digitUpperTable,
8778 gint64 const **tenPowersList,
8779 gint32 const **decHexDigits)
8781 *mantissas = Formatter_MantissaBitsTable;
8782 *exponents = Formatter_TensExponentTable;
8783 *digitLowerTable = Formatter_DigitLowerTable;
8784 *digitUpperTable = Formatter_DigitUpperTable;
8785 *tenPowersList = Formatter_TenPowersList;
8786 *decHexDigits = Formatter_DecHexDigits;
8789 static gboolean
8790 add_modifier_to_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int dest_idx, MonoError *error)
8792 HANDLE_FUNCTION_ENTER ();
8793 error_init (error);
8794 MonoClass *klass = mono_class_from_mono_type_internal (type);
8796 MonoReflectionTypeHandle rt;
8797 rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
8798 goto_if_nok (error, leave);
8800 MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rt);
8801 leave:
8802 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
8806 * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
8807 * and avoid useless allocations.
8809 static MonoArrayHandle
8810 type_array_from_modifiers (MonoType *type, int optional, MonoError *error)
8812 int i, count = 0;
8813 MonoDomain *domain = mono_domain_get ();
8815 int cmod_count = mono_type_custom_modifier_count (type);
8816 if (cmod_count == 0)
8817 goto fail;
8819 error_init (error);
8820 for (i = 0; i < cmod_count; ++i) {
8821 gboolean required;
8822 (void) mono_type_get_custom_modifier (type, i, &required, error);
8823 goto_if_nok (error, fail);
8824 if ((optional && !required) || (!optional && required))
8825 count++;
8827 if (!count)
8828 goto fail;
8830 MonoArrayHandle res;
8831 res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
8832 goto_if_nok (error, fail);
8833 count = 0;
8834 for (i = 0; i < cmod_count; ++i) {
8835 gboolean required;
8836 MonoType *cmod_type = mono_type_get_custom_modifier (type, i, &required, error);
8837 goto_if_nok (error, fail);
8838 if ((optional && !required) || (!optional && required)) {
8839 if (!add_modifier_to_array (domain, cmod_type, res, count, error))
8840 goto fail;
8841 count++;
8844 return res;
8845 fail:
8846 return MONO_HANDLE_NEW (MonoArray, NULL);
8849 MonoArrayHandle
8850 ves_icall_RuntimeParameterInfo_GetTypeModifiers (MonoReflectionTypeHandle rt, MonoObjectHandle member, int pos, MonoBoolean optional, MonoError *error)
8852 error_init (error);
8853 MonoType *type = MONO_HANDLE_GETVAL (rt, type);
8854 MonoClass *member_class = mono_handle_class (member);
8855 MonoMethod *method = NULL;
8856 MonoImage *image;
8857 MonoMethodSignature *sig;
8859 if (mono_class_is_reflection_method_or_constructor (member_class)) {
8860 method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member), method);
8861 } else if (m_class_get_image (member_class) == mono_defaults.corlib && !strcmp ("RuntimePropertyInfo", m_class_get_name (member_class))) {
8862 MonoProperty *prop = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionProperty, member), property);
8863 if (!(method = prop->get))
8864 method = prop->set;
8865 g_assert (method);
8866 } else if (strcmp (m_class_get_name (member_class), "DynamicMethod") == 0 && strcmp (m_class_get_name_space (member_class), "System.Reflection.Emit") == 0) {
8867 MonoArrayHandle params = MONO_HANDLE_NEW_GET (MonoArray, MONO_HANDLE_CAST (MonoReflectionDynamicMethod, member), parameters);
8868 MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
8869 MONO_HANDLE_ARRAY_GETREF (t, params, pos);
8870 type = mono_reflection_type_handle_mono_type (t, error);
8871 return type_array_from_modifiers (type, optional, error);
8872 } else {
8873 char *type_name = mono_type_get_full_name (member_class);
8874 mono_error_set_not_supported (error, "Custom modifiers on a ParamInfo with member %s are not supported", type_name);
8875 g_free (type_name);
8876 return NULL_HANDLE_ARRAY;
8879 image = m_class_get_image (method->klass);
8880 sig = mono_method_signature_internal (method);
8881 if (pos == -1)
8882 type = sig->ret;
8883 else
8884 type = sig->params [pos];
8886 return type_array_from_modifiers (type, optional, error);
8889 static MonoType*
8890 get_property_type (MonoProperty *prop)
8892 MonoMethodSignature *sig;
8893 if (prop->get) {
8894 sig = mono_method_signature_internal (prop->get);
8895 return sig->ret;
8896 } else if (prop->set) {
8897 sig = mono_method_signature_internal (prop->set);
8898 return sig->params [sig->param_count - 1];
8900 return NULL;
8903 MonoArrayHandle
8904 ves_icall_RuntimePropertyInfo_GetTypeModifiers (MonoReflectionPropertyHandle property, MonoBoolean optional, MonoError *error)
8906 error_init (error);
8907 MonoProperty *prop = MONO_HANDLE_GETVAL (property, property);
8908 MonoType *type = get_property_type (prop);
8910 if (!type)
8911 return NULL_HANDLE_ARRAY;
8912 return type_array_from_modifiers (type, optional, error);
8916 *Construct a MonoType suited to be used to decode a constant blob object.
8918 * @type is the target type which will be constructed
8919 * @blob_type is the blob type, for example, that comes from the constant table
8920 * @real_type is the expected constructed type.
8922 static void
8923 mono_type_from_blob_type (MonoType *type, MonoTypeEnum blob_type, MonoType *real_type)
8925 type->type = blob_type;
8926 type->data.klass = NULL;
8927 if (blob_type == MONO_TYPE_CLASS)
8928 type->data.klass = mono_defaults.object_class;
8929 else if (real_type->type == MONO_TYPE_VALUETYPE && m_class_is_enumtype (real_type->data.klass)) {
8930 /* For enums, we need to use the base type */
8931 type->type = MONO_TYPE_VALUETYPE;
8932 type->data.klass = mono_class_from_mono_type_internal (real_type);
8933 } else
8934 type->data.klass = mono_class_from_mono_type_internal (real_type);
8937 MonoObjectHandle
8938 ves_icall_property_info_get_default_value (MonoReflectionPropertyHandle property_handle, MonoError* error)
8940 MonoReflectionProperty* property = MONO_HANDLE_RAW (property_handle);
8942 MonoType blob_type;
8943 MonoProperty *prop = property->property;
8944 MonoType *type = get_property_type (prop);
8945 MonoDomain *domain = mono_object_domain (property);
8946 MonoTypeEnum def_type;
8947 const char *def_value;
8949 mono_class_init_internal (prop->parent);
8951 if (!(prop->attrs & PROPERTY_ATTRIBUTE_HAS_DEFAULT)) {
8952 mono_error_set_invalid_operation (error, NULL);
8953 return NULL_HANDLE;
8956 def_value = mono_class_get_property_default_value (prop, &def_type);
8958 mono_type_from_blob_type (&blob_type, def_type, type);
8960 return mono_get_object_from_blob (domain, &blob_type, def_value, MONO_HANDLE_NEW (MonoString, NULL), error);
8963 MonoBoolean
8964 ves_icall_MonoCustomAttrs_IsDefinedInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoError *error)
8966 MonoClass *attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
8968 mono_class_init_checked (attr_class, error);
8969 return_val_if_nok (error, FALSE);
8971 MonoCustomAttrInfo *cinfo = mono_reflection_get_custom_attrs_info_checked (obj, error);
8972 return_val_if_nok (error, FALSE);
8974 if (!cinfo)
8975 return FALSE;
8976 gboolean found = mono_custom_attrs_has_attr (cinfo, attr_class);
8977 if (!cinfo->cached)
8978 mono_custom_attrs_free (cinfo);
8979 return found;
8982 MonoArrayHandle
8983 ves_icall_MonoCustomAttrs_GetCustomAttributesInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoBoolean pseudoattrs, MonoError *error)
8985 MonoClass *attr_class;
8986 if (MONO_HANDLE_IS_NULL (attr_type))
8987 attr_class = NULL;
8988 else
8989 attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
8991 if (attr_class) {
8992 mono_class_init_checked (attr_class, error);
8993 return_val_if_nok (error, NULL_HANDLE_ARRAY);
8996 return mono_reflection_get_custom_attrs_by_type_handle (obj, attr_class, error);
8999 MonoArrayHandle
9000 ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObjectHandle obj, MonoError *error)
9002 return mono_reflection_get_custom_attrs_data_checked (obj, error);
9006 MonoStringHandle
9007 ves_icall_Mono_Runtime_GetDisplayName (MonoError *error)
9009 char *info;
9010 MonoStringHandle display_name;
9012 error_init (error);
9013 info = mono_get_runtime_callbacks ()->get_runtime_build_info ();
9014 display_name = mono_string_new_handle (mono_domain_get (), info, error);
9015 g_free (info);
9016 return display_name;
9019 #ifndef HOST_WIN32
9020 static gint32
9021 mono_icall_wait_for_input_idle (gpointer handle, gint32 milliseconds)
9023 return WAIT_TIMEOUT;
9025 #endif /* !HOST_WIN32 */
9027 #ifndef ENABLE_NETCORE
9028 gint32
9029 ves_icall_Microsoft_Win32_NativeMethods_WaitForInputIdle (gpointer handle, gint32 milliseconds)
9031 return mono_icall_wait_for_input_idle (handle, milliseconds);
9034 gint32
9035 ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcessId (void)
9037 return mono_process_current_pid ();
9040 MonoBoolean
9041 ves_icall_Mono_TlsProviderFactory_IsBtlsSupported (void)
9043 #if HAVE_BTLS
9044 return TRUE;
9045 #else
9046 return FALSE;
9047 #endif
9049 #endif /* ENABLE_NETCORE */
9051 #ifndef DISABLE_COM
9054 ves_icall_System_Runtime_InteropServices_Marshal_GetHRForException_WinRT(MonoExceptionHandle ex, MonoError *error)
9056 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetHRForException_WinRT internal call is not implemented.");
9057 return 0;
9060 MonoObjectHandle
9061 ves_icall_System_Runtime_InteropServices_Marshal_GetNativeActivationFactory(MonoObjectHandle type, MonoError *error)
9063 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetNativeActivationFactory internal call is not implemented.");
9064 return NULL_HANDLE;
9067 void*
9068 ves_icall_System_Runtime_InteropServices_Marshal_GetRawIUnknownForComObjectNoAddRef(MonoObjectHandle obj, MonoError *error)
9070 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetRawIUnknownForComObjectNoAddRef internal call is not implemented.");
9071 return NULL;
9074 MonoObjectHandle
9075 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo(MonoError *error)
9077 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.GetRestrictedErrorInfo internal call is not implemented.");
9078 return NULL_HANDLE;
9081 MonoBoolean
9082 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoOriginateLanguageException (int ierr, MonoStringHandle message, void* languageException, MonoError *error)
9084 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoOriginateLanguageException internal call is not implemented.");
9085 return FALSE;
9088 void
9089 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoReportUnhandledError (MonoObjectHandle oerr, MonoError *error)
9091 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoReportUnhandledError internal call is not implemented.");
9095 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsCreateString(MonoStringHandle sourceString, int length, void** hstring, MonoError *error)
9097 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsCreateString internal call is not implemented.");
9098 return 0;
9102 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsDeleteString(void* hstring, MonoError *error)
9104 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsDeleteString internal call is not implemented.");
9105 return 0;
9108 mono_unichar2*
9109 ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsGetStringRawBuffer(void* hstring, unsigned* length, MonoError *error)
9111 mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsGetStringRawBuffer internal call is not implemented.");
9112 return NULL;
9115 #endif
9117 #if !ENABLE_NETCORE
9119 void
9120 ves_icall_System_IO_LogcatTextWriter_Log (const char *appname, gint32 level, const char *message)
9122 g_log (appname, (GLogLevelFlags)level, "%s", message);
9125 #endif
9127 static const MonoIcallTableCallbacks *icall_table;
9128 static mono_mutex_t icall_mutex;
9129 static GHashTable *icall_hash = NULL;
9131 typedef struct _MonoIcallHashTableValue {
9132 gconstpointer method;
9133 guint32 flags;
9134 } MonoIcallHashTableValue;
9136 void
9137 mono_install_icall_table_callbacks (const MonoIcallTableCallbacks *cb)
9139 g_assert (cb->version == MONO_ICALL_TABLE_CALLBACKS_VERSION);
9140 icall_table = cb;
9143 void
9144 mono_icall_init (void)
9146 #ifndef DISABLE_ICALL_TABLES
9147 mono_icall_table_init ();
9148 #endif
9149 icall_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
9150 mono_os_mutex_init (&icall_mutex);
9153 static void
9154 mono_icall_lock (void)
9156 mono_locks_os_acquire (&icall_mutex, IcallLock);
9159 static void
9160 mono_icall_unlock (void)
9162 mono_locks_os_release (&icall_mutex, IcallLock);
9165 void
9166 mono_icall_cleanup (void)
9168 g_hash_table_destroy (icall_hash);
9169 mono_os_mutex_destroy (&icall_mutex);
9172 static void
9173 add_internal_call_with_flags (const char *name, gconstpointer method, guint32 flags)
9175 char *key = g_strdup (name);
9176 MonoIcallHashTableValue *value = g_new (MonoIcallHashTableValue, 1);
9177 if (key && value) {
9178 value->method = method;
9179 value->flags = flags;
9181 mono_icall_lock ();
9182 g_hash_table_insert (icall_hash, key, (gpointer)value);
9183 mono_icall_unlock ();
9188 * mono_add_internal_call:
9189 * \param name method specification to surface to the managed world
9190 * \param method pointer to a C method to invoke when the method is called
9192 * This method surfaces the C function pointed by \p method as a method
9193 * that has been surfaced in managed code with the method specified in
9194 * \p name as an internal call.
9196 * Internal calls are surfaced to all app domains loaded and they are
9197 * accessibly by a type with the specified name.
9199 * You must provide a fully qualified type name, that is namespaces
9200 * and type name, followed by a colon and the method name, with an
9201 * optional signature to bind.
9203 * For example, the following are all valid declarations:
9205 * \c MyApp.Services.ScriptService:Accelerate
9207 * \c MyApp.Services.ScriptService:Slowdown(int,bool)
9209 * You use method parameters in cases where there might be more than
9210 * one surface method to managed code. That way you can register different
9211 * internal calls for different method overloads.
9213 * The internal calls are invoked with no marshalling. This means that .NET
9214 * types like \c System.String are exposed as \c MonoString* parameters. This is
9215 * different than the way that strings are surfaced in P/Invoke.
9217 * For more information on how the parameters are marshalled, see the
9218 * <a href="http://www.mono-project.com/docs/advanced/embedding/">Mono Embedding</a>
9219 * page.
9221 * See the <a href="mono-api-methods.html#method-desc">Method Description</a>
9222 * reference for more information on the format of method descriptions.
9224 void
9225 mono_add_internal_call (const char *name, gconstpointer method)
9227 mono_add_internal_call_with_flags (name, method, FALSE);
9231 * mono_dangerous_add_raw_internal_call:
9232 * \param name method specification to surface to the managed world
9233 * \param method pointer to a C method to invoke when the method is called
9235 * Similar to \c mono_add_internal_call but with more requirements for correct
9236 * operation.
9238 * A thread running a dangerous raw internal call will avoid a thread state
9239 * transition on entry and exit, but it must take responsiblity for cooperating
9240 * with the Mono runtime.
9242 * The \p method must NOT:
9244 * Run for an unbounded amount of time without calling the mono runtime.
9245 * Additionally, the method must switch to GC Safe mode to perform all blocking
9246 * operations: performing blocking I/O, taking locks, etc.
9249 void
9250 mono_dangerous_add_raw_internal_call (const char *name, gconstpointer method)
9252 mono_add_internal_call_with_flags (name, method, TRUE);
9256 * mono_add_internal_call_with_flags:
9257 * \param name method specification to surface to the managed world
9258 * \param method pointer to a C method to invoke when the method is called
9259 * \param cooperative if \c TRUE, run icall in GC Unsafe (cooperatively suspended) mode,
9260 * otherwise GC Safe (blocking)
9262 * Like \c mono_add_internal_call, but if \p cooperative is \c TRUE the added
9263 * icall promises that it will use the coopertive API to inform the runtime
9264 * when it is running blocking operations, that it will not run for unbounded
9265 * amounts of time without safepointing, and that it will not hold managed
9266 * object references across suspend safepoints.
9268 * If \p cooperative is \c FALSE, run the icall in GC Safe mode - the icall may
9269 * block. The icall must obey the GC Safe rules, e.g. it must not touch
9270 * unpinned managed memory.
9273 void
9274 mono_add_internal_call_with_flags (const char *name, gconstpointer method, gboolean cooperative)
9276 add_internal_call_with_flags (name, method, cooperative ? MONO_ICALL_FLAGS_COOPERATIVE : MONO_ICALL_FLAGS_FOREIGN);
9279 void
9280 mono_add_internal_call_internal (const char *name, gconstpointer method)
9282 mono_add_internal_call_with_flags (name, method, TRUE);
9286 * we should probably export this as an helper (handle nested types).
9287 * Returns the number of chars written in buf.
9289 static int
9290 concat_class_name (char *buf, int bufsize, MonoClass *klass)
9292 int nspacelen, cnamelen;
9293 nspacelen = strlen (m_class_get_name_space (klass));
9294 cnamelen = strlen (m_class_get_name (klass));
9295 if (nspacelen + cnamelen + 2 > bufsize)
9296 return 0;
9297 if (nspacelen) {
9298 memcpy (buf, m_class_get_name_space (klass), nspacelen);
9299 buf [nspacelen ++] = '.';
9301 memcpy (buf + nspacelen, m_class_get_name (klass), cnamelen);
9302 buf [nspacelen + cnamelen] = 0;
9303 return nspacelen + cnamelen;
9306 static void
9307 no_icall_table (void)
9309 g_assert_not_reached ();
9312 gboolean
9313 mono_is_missing_icall_addr (gconstpointer addr)
9315 return addr == NULL || addr == no_icall_table;
9319 * Returns either NULL or no_icall_table for missing icalls.
9321 gconstpointer
9322 mono_lookup_internal_call_full_with_flags (MonoMethod *method, gboolean warn_on_missing, guint32 *flags)
9324 char *sigstart = NULL;
9325 char *tmpsig = NULL;
9326 char mname [2048];
9327 char *classname = NULL;
9328 int typelen = 0, mlen, siglen;
9329 gconstpointer res = NULL;
9330 gboolean locked = FALSE;
9332 g_assert (method != NULL);
9334 if (method->is_inflated)
9335 method = ((MonoMethodInflated *) method)->declaring;
9337 if (m_class_get_nested_in (method->klass)) {
9338 int pos = concat_class_name (mname, sizeof (mname)-2, m_class_get_nested_in (method->klass));
9339 if (!pos)
9340 goto exit;
9342 mname [pos++] = '/';
9343 mname [pos] = 0;
9345 typelen = concat_class_name (mname+pos, sizeof (mname)-pos-1, method->klass);
9346 if (!typelen)
9347 goto exit;
9349 typelen += pos;
9350 } else {
9351 typelen = concat_class_name (mname, sizeof (mname), method->klass);
9352 if (!typelen)
9353 goto exit;
9356 classname = g_strdup (mname);
9358 mname [typelen] = ':';
9359 mname [typelen + 1] = ':';
9361 mlen = strlen (method->name);
9362 memcpy (mname + typelen + 2, method->name, mlen);
9363 sigstart = mname + typelen + 2 + mlen;
9364 *sigstart = 0;
9366 tmpsig = mono_signature_get_desc (mono_method_signature_internal (method), TRUE);
9367 siglen = strlen (tmpsig);
9368 if (typelen + mlen + siglen + 6 > sizeof (mname))
9369 goto exit;
9371 sigstart [0] = '(';
9372 memcpy (sigstart + 1, tmpsig, siglen);
9373 sigstart [siglen + 1] = ')';
9374 sigstart [siglen + 2] = 0;
9376 /* mono_marshal_get_native_wrapper () depends on this */
9377 if (method->klass == mono_defaults.string_class && !strcmp (method->name, ".ctor")) {
9378 res = (gconstpointer)ves_icall_System_String_ctor_RedirectToCreateString;
9379 goto exit;
9382 mono_icall_lock ();
9383 locked = TRUE;
9385 res = g_hash_table_lookup (icall_hash, mname);
9386 if (res) {
9387 MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
9388 if (flags)
9389 *flags = value->flags;
9390 res = value->method;
9391 goto exit;
9394 /* try without signature */
9395 *sigstart = 0;
9396 res = g_hash_table_lookup (icall_hash, mname);
9397 if (res) {
9398 MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
9399 if (flags)
9400 *flags = value->flags;
9401 res = value->method;
9402 goto exit;
9405 if (!icall_table) {
9406 /* Fail only when the result is actually used */
9407 res = (gconstpointer)no_icall_table;
9408 goto exit;
9409 } else {
9410 gboolean uses_handles = FALSE;
9411 g_assert (icall_table->lookup);
9412 res = icall_table->lookup (method, classname, sigstart - mlen, sigstart, &uses_handles);
9413 if (res && flags && uses_handles)
9414 *flags = *flags | MONO_ICALL_FLAGS_USES_HANDLES;
9415 mono_icall_unlock ();
9416 locked = FALSE;
9418 if (res)
9419 goto exit;
9421 if (warn_on_missing) {
9422 g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", mname);
9423 g_print ("\nYour mono runtime and class libraries are out of sync.\n");
9424 g_print ("The out of sync library is: %s\n", m_class_get_image (method->klass)->name);
9425 g_print ("\nWhen you update one from git you need to update, compile and install\nthe other too.\n");
9426 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");
9427 g_print ("If you see other errors or faults after this message they are probably related\n");
9428 g_print ("and you need to fix your mono install first.\n");
9431 res = NULL;
9434 exit:
9435 if (locked)
9436 mono_icall_unlock ();
9437 g_free (classname);
9438 g_free (tmpsig);
9439 return res;
9443 * mono_lookup_internal_call_full:
9444 * \param method the method to look up
9445 * \param uses_handles out argument if method needs handles around managed objects.
9446 * \returns a pointer to the icall code for the given method. If
9447 * \p uses_handles is not NULL, it will be set to TRUE if the method
9448 * needs managed objects wrapped using the infrastructure in handle.h
9450 * If the method is not found, warns and returns NULL.
9452 gconstpointer
9453 mono_lookup_internal_call_full (MonoMethod *method, gboolean warn_on_missing, mono_bool *uses_handles, mono_bool *foreign)
9455 if (uses_handles)
9456 *uses_handles = FALSE;
9457 if (foreign)
9458 *foreign = FALSE;
9460 guint32 flags = MONO_ICALL_FLAGS_NONE;
9461 gconstpointer addr = mono_lookup_internal_call_full_with_flags (method, warn_on_missing, &flags);
9463 if (uses_handles && (flags & MONO_ICALL_FLAGS_USES_HANDLES))
9464 *uses_handles = TRUE;
9465 if (foreign && (flags & MONO_ICALL_FLAGS_FOREIGN))
9466 *foreign = TRUE;
9467 return addr;
9471 * mono_lookup_internal_call:
9473 gpointer
9474 mono_lookup_internal_call (MonoMethod *method)
9476 return (gpointer)mono_lookup_internal_call_full (method, TRUE, NULL, NULL);
9480 * mono_lookup_icall_symbol:
9482 * Given the icall METHOD, returns its C symbol.
9484 const char*
9485 mono_lookup_icall_symbol (MonoMethod *m)
9487 if (!icall_table)
9488 return NULL;
9490 g_assert (icall_table->lookup_icall_symbol);
9491 gpointer func;
9492 func = (gpointer)mono_lookup_internal_call_full (m, FALSE, NULL, NULL);
9493 if (!func)
9494 return NULL;
9495 return icall_table->lookup_icall_symbol (func);
9498 #if defined(TARGET_WIN32) && defined(TARGET_X86)
9500 * Under windows, the default pinvoke calling convention is STDCALL but
9501 * we need CDECL.
9503 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION MONO_CALL_C
9504 #else
9505 #define MONO_ICALL_SIGNATURE_CALL_CONVENTION 0
9506 #endif
9508 // Storage for these enums is pointer-sized as it gets replaced with MonoType*.
9510 // mono_create_icall_signatures depends on this order. Handle with care.
9511 typedef enum ICallSigType {
9512 ICALL_SIG_TYPE_bool = 0x00,
9513 ICALL_SIG_TYPE_boolean = ICALL_SIG_TYPE_bool,
9514 ICALL_SIG_TYPE_double = 0x01,
9515 ICALL_SIG_TYPE_float = 0x02,
9516 ICALL_SIG_TYPE_int = 0x03,
9517 ICALL_SIG_TYPE_int16 = 0x04,
9518 ICALL_SIG_TYPE_int32 = ICALL_SIG_TYPE_int,
9519 ICALL_SIG_TYPE_int8 = 0x05,
9520 ICALL_SIG_TYPE_long = 0x06,
9521 ICALL_SIG_TYPE_obj = 0x07,
9522 ICALL_SIG_TYPE_object = ICALL_SIG_TYPE_obj,
9523 ICALL_SIG_TYPE_ptr = 0x08,
9524 ICALL_SIG_TYPE_ptrref = 0x09,
9525 ICALL_SIG_TYPE_string = 0x0A,
9526 ICALL_SIG_TYPE_uint16 = 0x0B,
9527 ICALL_SIG_TYPE_uint32 = 0x0C,
9528 ICALL_SIG_TYPE_uint8 = 0x0D,
9529 ICALL_SIG_TYPE_ulong = 0x0E,
9530 ICALL_SIG_TYPE_void = 0x0F,
9531 ICALL_SIG_TYPE_sizet = 0x10
9532 } ICallSigType;
9534 #define ICALL_SIG_TYPES_1(a) ICALL_SIG_TYPE_ ## a,
9535 #define ICALL_SIG_TYPES_2(a, b) ICALL_SIG_TYPES_1 (a ) ICALL_SIG_TYPES_1 (b)
9536 #define ICALL_SIG_TYPES_3(a, b, c) ICALL_SIG_TYPES_2 (a, b ) ICALL_SIG_TYPES_1 (c)
9537 #define ICALL_SIG_TYPES_4(a, b, c, d) ICALL_SIG_TYPES_3 (a, b, c ) ICALL_SIG_TYPES_1 (d)
9538 #define ICALL_SIG_TYPES_5(a, b, c, d, e) ICALL_SIG_TYPES_4 (a, b, c, d ) ICALL_SIG_TYPES_1 (e)
9539 #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)
9540 #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)
9541 #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)
9543 #define ICALL_SIG_TYPES(n, types) ICALL_SIG_TYPES_ ## n types
9545 // A scheme to make these const would be nice.
9546 static struct {
9547 #define ICALL_SIG(n, xtypes) \
9548 struct { \
9549 MonoMethodSignature sig; \
9550 gsize types [n]; \
9551 } ICALL_SIG_NAME (n, xtypes);
9552 ICALL_SIGS
9553 MonoMethodSignature end; // terminal zeroed element
9554 } mono_icall_signatures = {
9555 #undef ICALL_SIG
9556 #define ICALL_SIG(n, types) { { \
9557 0, /* ret */ \
9558 n, /* param_count */ \
9559 -1, /* sentinelpos */ \
9560 0, /* generic_param_count */ \
9561 MONO_ICALL_SIGNATURE_CALL_CONVENTION, \
9562 0, /* hasthis */ \
9563 0, /* explicit_this */ \
9564 1, /* pinvoke */ \
9565 0, /* is_inflated */ \
9566 0, /* has_type_parameters */ \
9567 }, /* possible gap here, depending on MONO_ZERO_LEN_ARRAY */ \
9568 { ICALL_SIG_TYPES (n, types) } }, /* params and ret */
9569 ICALL_SIGS
9572 #undef ICALL_SIG
9573 #define ICALL_SIG(n, types) MonoMethodSignature * const ICALL_SIG_NAME (n, types) = &mono_icall_signatures.ICALL_SIG_NAME (n, types).sig;
9574 ICALL_SIGS
9575 #undef ICALL_SIG
9577 void
9578 mono_create_icall_signatures (void)
9580 // Fixup the mostly statically initialized icall signatures.
9581 // x = m_class_get_byval_arg (x)
9582 // Initialize ret with params [0] and params [i] with params [i + 1].
9583 // ptrref is special
9585 // FIXME This is a bit obscure.
9587 typedef MonoMethodSignature G_MAY_ALIAS MonoMethodSignature_a;
9588 typedef gsize G_MAY_ALIAS gsize_a;
9590 MonoType * const lookup [ ] = {
9591 m_class_get_byval_arg (mono_defaults.boolean_class), // ICALL_SIG_TYPE_bool
9592 m_class_get_byval_arg (mono_defaults.double_class), // ICALL_SIG_TYPE_double
9593 m_class_get_byval_arg (mono_defaults.single_class), // ICALL_SIG_TYPE_float
9594 m_class_get_byval_arg (mono_defaults.int32_class), // ICALL_SIG_TYPE_int
9595 m_class_get_byval_arg (mono_defaults.int16_class), // ICALL_SIG_TYPE_int16
9596 m_class_get_byval_arg (mono_defaults.sbyte_class), // ICALL_SIG_TYPE_int8
9597 m_class_get_byval_arg (mono_defaults.int64_class), // ICALL_SIG_TYPE_long
9598 m_class_get_byval_arg (mono_defaults.object_class), // ICALL_SIG_TYPE_obj
9599 m_class_get_byval_arg (mono_defaults.int_class), // ICALL_SIG_TYPE_ptr
9600 mono_class_get_byref_type (mono_defaults.int_class), // ICALL_SIG_TYPE_ptrref
9601 m_class_get_byval_arg (mono_defaults.string_class), // ICALL_SIG_TYPE_string
9602 m_class_get_byval_arg (mono_defaults.uint16_class), // ICALL_SIG_TYPE_uint16
9603 m_class_get_byval_arg (mono_defaults.uint32_class), // ICALL_SIG_TYPE_uint32
9604 m_class_get_byval_arg (mono_defaults.byte_class), // ICALL_SIG_TYPE_uint8
9605 m_class_get_byval_arg (mono_defaults.uint64_class), // ICALL_SIG_TYPE_ulong
9606 m_class_get_byval_arg (mono_defaults.void_class), // ICALL_SIG_TYPE_void
9607 m_class_get_byval_arg (mono_defaults.int_class), // ICALL_SIG_TYPE_sizet
9610 MonoMethodSignature_a *sig = (MonoMethodSignature*)&mono_icall_signatures;
9611 int n;
9612 while ((n = sig->param_count)) {
9613 --sig->param_count; // remove ret
9614 gsize_a *types = (gsize_a*)(sig + 1);
9615 for (int i = 0; i < n; ++i) {
9616 gsize index = *types++;
9617 g_assert (index < G_N_ELEMENTS (lookup));
9618 // Casts on next line are attempt to follow strict aliasing rules,
9619 // to ensure reading from *types precedes writing
9620 // to params [].
9621 *(gsize*)(i ? &sig->params [i - 1] : &sig->ret) = (gsize)lookup [index];
9623 sig = (MonoMethodSignature*)types;
9627 void
9628 mono_register_jit_icall_info (MonoJitICallInfo *info, gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean avoid_wrapper, const char *c_symbol)
9630 // Duplicate initialization is allowed and racy, assuming it is equivalent.
9632 info->name = name;
9633 info->func = func;
9634 info->sig = sig;
9635 info->c_symbol = c_symbol;
9637 // Fill in wrapper ahead of time, to just be func, to avoid
9638 // later initializing it to anything else. So therefore, no wrapper.
9639 if (avoid_wrapper) {
9640 info->wrapper = func;
9641 } else {
9642 // Leave it alone in case of a race.
9647 ves_icall_System_GC_GetCollectionCount (int generation)
9649 return mono_gc_collection_count (generation);
9653 ves_icall_System_GC_GetGeneration (MonoObjectHandle object, MonoError *error)
9655 return mono_gc_get_generation (MONO_HANDLE_RAW (object));
9659 ves_icall_System_GC_GetMaxGeneration (void)
9661 return mono_gc_max_generation ();
9664 gint64
9665 ves_icall_System_GC_GetAllocatedBytesForCurrentThread (void)
9667 return mono_gc_get_allocated_bytes_for_current_thread ();
9670 #ifdef ENABLE_NETCORE
9671 guint64
9672 ves_icall_System_GC_GetTotalAllocatedBytes (MonoBoolean precise, MonoError* error)
9674 return mono_gc_get_total_allocated_bytes (precise);
9676 #endif
9678 void
9679 ves_icall_System_GC_RecordPressure (gint64 value)
9681 mono_gc_add_memory_pressure (value);
9684 gint64
9685 ves_icall_System_Diagnostics_Stopwatch_GetTimestamp (void)
9687 return mono_100ns_ticks ();
9690 gint64
9691 ves_icall_System_Threading_Timer_GetTimeMonotonic (void)
9693 return mono_100ns_ticks ();
9696 gint64
9697 ves_icall_System_DateTime_GetSystemTimeAsFileTime (void)
9699 return mono_100ns_datetime ();
9703 ves_icall_System_Threading_Thread_SystemMaxStackSize (void)
9705 return mono_thread_info_get_system_max_stack_size ();
9708 MonoBoolean
9709 ves_icall_System_Threading_Thread_YieldInternal (void)
9711 mono_threads_platform_yield ();
9712 return TRUE;
9715 gint32
9716 ves_icall_System_Environment_get_ProcessorCount (void)
9718 return mono_cpu_count ();
9721 #if !defined(ENABLE_NETCORE)
9722 #if defined(ENABLE_MONODROID)
9724 G_EXTERN_C gpointer CreateNLSocket (void);
9725 G_EXTERN_C gint32 ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size);
9726 G_EXTERN_C gpointer CloseNLSocket (gpointer sock);
9728 gpointer
9729 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CreateNLSocket (void)
9731 return CreateNLSocket ();
9734 gint32
9735 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
9737 return ReadEvents (sock, buffer, count, size);
9740 gpointer
9741 ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CloseNLSocket (gpointer sock)
9743 return CloseNLSocket (sock);
9746 #endif
9747 #endif
9749 // Generate wrappers.
9751 #define ICALL_TYPE(id,name,first) /* nothing */
9752 #define ICALL(id,name,func) /* nothing */
9753 #define NOHANDLES(inner) /* nothing */
9755 #define MONO_HANDLE_REGISTER_ICALL(func, ret, nargs, argtypes) MONO_HANDLE_REGISTER_ICALL_IMPLEMENT (func, ret, nargs, argtypes)
9757 // Some native functions are exposed via multiple managed names.
9758 // Producing a wrapper for these results in duplicate wrappers with the same names,
9759 // which fails to compile. Do not produce such duplicate wrappers. Alternatively,
9760 // a one line native function with a different name that calls the main one could be used.
9761 // i.e. the wrapper would also have a different name.
9762 #define HANDLES_REUSE_WRAPPER(...) /* nothing */
9764 #define HANDLES(id, name, func, ret, nargs, argtypes) \
9765 MONO_HANDLE_DECLARE (id, name, func, ret, nargs, argtypes); \
9766 MONO_HANDLE_IMPLEMENT (id, name, func, ret, nargs, argtypes)
9768 #include "metadata/icall-def.h"
9770 #undef HANDLES
9771 #undef HANDLES_REUSE_WRAPPER
9772 #undef ICALL_TYPE
9773 #undef ICALL
9774 #undef NOHANDLES
9775 #undef MONO_HANDLE_REGISTER_ICALL