1 /**
2  * \file
3  * System.Type icalls and related reflection queries.
4  *
5  * Author:
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10  * Copyright 2011 Rodrigo Kumpera
11  * Copyright 2016 Microsoft
12  *
13  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14  */
15 #include <config.h>
16 #include "mono/utils/mono-membar.h"
17 #include "mono/metadata/reflection-internals.h"
18 #include "mono/metadata/tabledefs.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include <mono/metadata/profiler-private.h>
21 #include "mono/metadata/class-internals.h"
22 #include "mono/metadata/gc-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/opcodes.h"
25 #include "mono/metadata/assembly.h"
26 #include "mono/metadata/object-internals.h"
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/marshal.h>
29 #include <mono/metadata/security-manager.h>
30 #include <mono/metadata/reflection-cache.h>
31 #include <mono/metadata/sre-internals.h>
32 #include <stdio.h>
33 #include <glib.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include "image.h"
39 #include "cil-coff.h"
40 #include "mono-endian.h"
41 #include <mono/metadata/gc-internals.h>
42 #include <mono/metadata/mempool-internals.h>
43 #include <mono/metadata/security-core-clr.h>
44 #include <mono/metadata/debug-helpers.h>
45 #include <mono/metadata/verify-internals.h>
46 #include <mono/metadata/mono-ptr-array.h>
47 #include <mono/utils/mono-string.h>
48 #include <mono/utils/mono-error-internals.h>
49 #include <mono/utils/checked-build.h>
50 #include <mono/utils/mono-counters.h>
51 
52 static void get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types);
53 static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error);
54 
55 /* Class lazy loading functions */
56 static GENERATE_GET_CLASS_WITH_CACHE (mono_assembly, "System.Reflection", "MonoAssembly")
57 static GENERATE_GET_CLASS_WITH_CACHE (mono_module, "System.Reflection", "MonoModule")
58 static GENERATE_GET_CLASS_WITH_CACHE (mono_method, "System.Reflection", "MonoMethod");
59 static GENERATE_GET_CLASS_WITH_CACHE (mono_cmethod, "System.Reflection", "MonoCMethod");
60 static GENERATE_GET_CLASS_WITH_CACHE (mono_field, "System.Reflection", "MonoField");
61 static GENERATE_GET_CLASS_WITH_CACHE (mono_event, "System.Reflection", "MonoEvent");
62 static GENERATE_GET_CLASS_WITH_CACHE (mono_property, "System.Reflection", "MonoProperty");
63 static GENERATE_GET_CLASS_WITH_CACHE (mono_parameter_info, "System.Reflection", "MonoParameterInfo");
64 static GENERATE_GET_CLASS_WITH_CACHE (missing, "System.Reflection", "Missing");
65 static GENERATE_GET_CLASS_WITH_CACHE (method_body, "System.Reflection", "MethodBody");
66 static GENERATE_GET_CLASS_WITH_CACHE (local_variable_info, "System.Reflection", "LocalVariableInfo");
67 static GENERATE_GET_CLASS_WITH_CACHE (exception_handling_clause, "System.Reflection", "ExceptionHandlingClause");
68 static GENERATE_GET_CLASS_WITH_CACHE (type_builder, "System.Reflection.Emit", "TypeBuilder");
69 static GENERATE_GET_CLASS_WITH_CACHE (dbnull, "System", "DBNull");
70 
71 
72 static int class_ref_info_handle_count;
73 
74 void
mono_reflection_init(void)75 mono_reflection_init (void)
76 {
77 	mono_reflection_emit_init ();
78 
79 	mono_counters_register ("MonoClass::ref_info_handle count",
80 							MONO_COUNTER_METADATA | MONO_COUNTER_INT, &class_ref_info_handle_count);
81 
82 }
83 
84 /*
85  * mono_class_get_ref_info:
86  *
87  *   Return the type builder/generic param builder corresponding to KLASS, if it exists.
88  */
89 MonoObjectHandle
mono_class_get_ref_info(MonoClass * klass)90 mono_class_get_ref_info (MonoClass *klass)
91 {
92 	MONO_REQ_GC_UNSAFE_MODE;
93 	guint32 ref_info_handle = mono_class_get_ref_info_handle (klass);
94 
95 	if (ref_info_handle == 0)
96 		return MONO_HANDLE_NEW (MonoObject, NULL);
97 	else
98 		return mono_gchandle_get_target_handle (ref_info_handle);
99 }
100 
101 gboolean
mono_class_has_ref_info(MonoClass * klass)102 mono_class_has_ref_info (MonoClass *klass)
103 {
104 	MONO_REQ_GC_UNSAFE_MODE;
105 	return 0 != mono_class_get_ref_info_handle (klass);
106 }
107 
108 MonoObject*
mono_class_get_ref_info_raw(MonoClass * klass)109 mono_class_get_ref_info_raw (MonoClass *klass)
110 {
111 	/* FIXME callers of mono_class_get_ref_info_raw should use handles */
112 	MONO_REQ_GC_UNSAFE_MODE;
113 	guint32 ref_info_handle = mono_class_get_ref_info_handle (klass);
114 
115 	if (ref_info_handle == 0)
116 		return NULL;
117 	else
118 		return mono_gchandle_get_target (ref_info_handle);
119 }
120 
121 void
mono_class_set_ref_info(MonoClass * klass,MonoObjectHandle obj)122 mono_class_set_ref_info (MonoClass *klass, MonoObjectHandle obj)
123 {
124 	MONO_REQ_GC_UNSAFE_MODE;
125 
126 	guint32 candidate = mono_gchandle_from_handle (obj, FALSE);
127 	guint32 handle = mono_class_set_ref_info_handle (klass, candidate);
128 	++class_ref_info_handle_count;
129 
130 	if (handle != candidate)
131 		mono_gchandle_free (candidate);
132 }
133 
134 void
mono_class_free_ref_info(MonoClass * klass)135 mono_class_free_ref_info (MonoClass *klass)
136 {
137 	MONO_REQ_GC_NEUTRAL_MODE;
138 	guint32 handle = mono_class_get_ref_info_handle (klass);
139 
140 	if (handle) {
141 		mono_gchandle_free (handle);
142 		mono_class_set_ref_info_handle (klass, 0);
143 	}
144 }
145 
146 /**
147  * mono_custom_attrs_free:
148  */
149 void
mono_custom_attrs_free(MonoCustomAttrInfo * ainfo)150 mono_custom_attrs_free (MonoCustomAttrInfo *ainfo)
151 {
152 	MONO_REQ_GC_NEUTRAL_MODE;
153 
154 	if (ainfo && !ainfo->cached)
155 		g_free (ainfo);
156 }
157 
158 gboolean
reflected_equal(gconstpointer a,gconstpointer b)159 reflected_equal (gconstpointer a, gconstpointer b)
160 {
161 	const ReflectedEntry *ea = (const ReflectedEntry *)a;
162 	const ReflectedEntry *eb = (const ReflectedEntry *)b;
163 
164 	return (ea->item == eb->item) && (ea->refclass == eb->refclass);
165 }
166 
167 guint
reflected_hash(gconstpointer a)168 reflected_hash (gconstpointer a) {
169 	const ReflectedEntry *ea = (const ReflectedEntry *)a;
170 	/* Combine hashes for item and refclass. Identical to boost's hash_combine */
171 	guint seed = mono_aligned_addr_hash (ea->item) + 0x9e3779b9;
172 	seed ^= mono_aligned_addr_hash (ea->refclass) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
173 	return seed;
174 }
175 
176 static void
clear_cached_object(MonoDomain * domain,gpointer o,MonoClass * klass)177 clear_cached_object (MonoDomain *domain, gpointer o, MonoClass *klass)
178 {
179 	mono_domain_lock (domain);
180 	if (domain->refobject_hash) {
181         ReflectedEntry pe;
182 		gpointer orig_pe, orig_value;
183 
184 		pe.item = o;
185 		pe.refclass = klass;
186 
187 		if (mono_conc_g_hash_table_lookup_extended (domain->refobject_hash, &pe, &orig_pe, &orig_value)) {
188 			mono_conc_g_hash_table_remove (domain->refobject_hash, &pe);
189 			free_reflected_entry (orig_pe);
190 		}
191 	}
192 	mono_domain_unlock (domain);
193 }
194 
195 static void
cleanup_refobject_hash(gpointer key,gpointer value,gpointer user_data)196 cleanup_refobject_hash (gpointer key, gpointer value, gpointer user_data)
197 {
198 	free_reflected_entry (key);
199 }
200 
201 void
mono_reflection_cleanup_domain(MonoDomain * domain)202 mono_reflection_cleanup_domain (MonoDomain *domain)
203 {
204 	if (domain->refobject_hash) {
205 		mono_conc_g_hash_table_foreach (domain->refobject_hash, cleanup_refobject_hash, NULL);
206 		mono_conc_g_hash_table_destroy (domain->refobject_hash);
207 		domain->refobject_hash = NULL;
208 	}
209 }
210 
211 /**
212  * mono_assembly_get_object:
213  * \param domain an app domain
214  * \param assembly an assembly
215  * \returns a \c System.Reflection.Assembly object representing the \c MonoAssembly \p assembly.
216  */
217 MonoReflectionAssembly*
mono_assembly_get_object(MonoDomain * domain,MonoAssembly * assembly)218 mono_assembly_get_object (MonoDomain *domain, MonoAssembly *assembly)
219 {
220 	HANDLE_FUNCTION_ENTER ();
221 	MonoError error;
222 	MonoReflectionAssemblyHandle result = mono_assembly_get_object_handle (domain, assembly, &error);
223 	mono_error_cleanup (&error); /* FIXME new API that doesn't swallow the error */
224 	HANDLE_FUNCTION_RETURN_OBJ (result);
225 }
226 
227 static MonoReflectionAssemblyHandle
assembly_object_construct(MonoDomain * domain,MonoClass * unused_klass,MonoAssembly * assembly,gpointer user_data,MonoError * error)228 assembly_object_construct (MonoDomain *domain, MonoClass *unused_klass, MonoAssembly *assembly, gpointer user_data, MonoError *error)
229 {
230 	error_init (error);
231 	MonoReflectionAssemblyHandle res = MONO_HANDLE_NEW (MonoReflectionAssembly, mono_object_new_checked (domain, mono_class_get_mono_assembly_class (), error));
232 	return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE));
233 	MONO_HANDLE_SETVAL (res, assembly, MonoAssembly*, assembly);
234 	return res;
235 }
236 
237 /*
238  * mono_assembly_get_object_handle:
239  * @domain: an app domain
240  * @assembly: an assembly
241  *
242  * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
243  */
244 MonoReflectionAssemblyHandle
mono_assembly_get_object_handle(MonoDomain * domain,MonoAssembly * assembly,MonoError * error)245 mono_assembly_get_object_handle (MonoDomain *domain, MonoAssembly *assembly, MonoError *error)
246 {
247 	error_init (error);
248 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionAssemblyHandle, assembly, NULL, assembly_object_construct, NULL);
249 }
250 
251 /**
252  * mono_module_get_object:
253  */
254 MonoReflectionModule*
mono_module_get_object(MonoDomain * domain,MonoImage * image)255 mono_module_get_object   (MonoDomain *domain, MonoImage *image)
256 {
257 	HANDLE_FUNCTION_ENTER ();
258 	MonoError error;
259 	MonoReflectionModuleHandle result = mono_module_get_object_handle (domain, image, &error);
260 	mono_error_cleanup (&error);
261 	HANDLE_FUNCTION_RETURN_OBJ (result);
262 }
263 
264 static MonoReflectionModuleHandle
module_object_construct(MonoDomain * domain,MonoClass * unused_klass,MonoImage * image,gpointer user_data,MonoError * error)265 module_object_construct (MonoDomain *domain, MonoClass *unused_klass, MonoImage *image, gpointer user_data, MonoError *error)
266 {
267 	char* basename;
268 
269 	error_init (error);
270 	MonoReflectionModuleHandle res = MONO_HANDLE_NEW (MonoReflectionModule, mono_object_new_checked (domain, mono_class_get_mono_module_class (), error));
271 	goto_if_nok (error, fail);
272 
273 	MONO_HANDLE_SETVAL (res, image, MonoImage *, image);
274 	MonoReflectionAssemblyHandle assm_obj = mono_assembly_get_object_handle (domain, image->assembly, error);
275 	goto_if_nok (error, fail);
276 	MONO_HANDLE_SET (res, assembly, assm_obj);
277 
278 	MONO_HANDLE_SET (res, fqname, mono_string_new_handle (domain, image->name, error));
279 	goto_if_nok (error, fail);
280 	basename = g_path_get_basename (image->name);
281 	MONO_HANDLE_SET (res, name, mono_string_new_handle (domain, basename, error));
282 	goto_if_nok (error, fail);
283 	MONO_HANDLE_SET (res, scopename, mono_string_new_handle (domain, image->module_name, error));
284 	goto_if_nok (error, fail);
285 
286 	g_free (basename);
287 
288 	guint32 token = 0;
289 	if (image->assembly->image == image) {
290 		token  = mono_metadata_make_token (MONO_TABLE_MODULE, 1);
291 	} else {
292 		int i;
293 		if (image->assembly->image->modules) {
294 			for (i = 0; i < image->assembly->image->module_count; i++) {
295 				if (image->assembly->image->modules [i] == image)
296 					token = mono_metadata_make_token (MONO_TABLE_MODULEREF, i + 1);
297 			}
298 			g_assert (token != 0);
299 		}
300 	}
301 	MONO_HANDLE_SETVAL (res, token, guint32, token);
302 
303 	return res;
304 fail:
305 	return MONO_HANDLE_CAST (MonoReflectionModule, NULL_HANDLE);
306 }
307 
308 MonoReflectionModuleHandle
mono_module_get_object_handle(MonoDomain * domain,MonoImage * image,MonoError * error)309 mono_module_get_object_handle (MonoDomain *domain, MonoImage *image, MonoError *error)
310 {
311 	error_init (error);
312 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionModuleHandle, image, NULL, module_object_construct, NULL);
313 }
314 
315 /**
316  * mono_module_file_get_object:
317  */
318 MonoReflectionModule*
mono_module_file_get_object(MonoDomain * domain,MonoImage * image,int table_index)319 mono_module_file_get_object (MonoDomain *domain, MonoImage *image, int table_index)
320 {
321 	HANDLE_FUNCTION_ENTER ();
322 	MonoError error;
323 	MonoReflectionModuleHandle result = mono_module_file_get_object_handle (domain, image, table_index, &error);
324 	mono_error_cleanup (&error);
325 	HANDLE_FUNCTION_RETURN_OBJ (result);
326 }
327 
328 MonoReflectionModuleHandle
mono_module_file_get_object_handle(MonoDomain * domain,MonoImage * image,int table_index,MonoError * error)329 mono_module_file_get_object_handle (MonoDomain *domain, MonoImage *image, int table_index, MonoError *error)
330 {
331 	MonoTableInfo *table;
332 	guint32 cols [MONO_FILE_SIZE];
333 	const char *name;
334 	guint32 i, name_idx;
335 	const char *val;
336 
337 	error_init (error);
338 
339 	MonoReflectionModuleHandle res = MONO_HANDLE_NEW (MonoReflectionModule, mono_object_new_checked (domain, mono_class_get_mono_module_class (), error));
340 	goto_if_nok (error, fail);
341 
342 	table = &image->tables [MONO_TABLE_FILE];
343 	g_assert (table_index < table->rows);
344 	mono_metadata_decode_row (table, table_index, cols, MONO_FILE_SIZE);
345 
346 	MONO_HANDLE_SETVAL (res, image, MonoImage*, NULL);
347 	MonoReflectionAssemblyHandle assm_obj = mono_assembly_get_object_handle (domain, image->assembly, error);
348 	goto_if_nok (error, fail);
349 	MONO_HANDLE_SET (res, assembly, assm_obj);
350 	name = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
351 
352 	/* Check whenever the row has a corresponding row in the moduleref table */
353 	table = &image->tables [MONO_TABLE_MODULEREF];
354 	for (i = 0; i < table->rows; ++i) {
355 		name_idx = mono_metadata_decode_row_col (table, i, MONO_MODULEREF_NAME);
356 		val = mono_metadata_string_heap (image, name_idx);
357 		if (strcmp (val, name) == 0)
358 			MONO_HANDLE_SETVAL (res, image, MonoImage*, image->modules [i]);
359 	}
360 
361 	MONO_HANDLE_SET (res, fqname, mono_string_new_handle (domain, name, error));
362 	goto_if_nok (error, fail);
363 	MONO_HANDLE_SET (res, name, mono_string_new_handle (domain, name, error));
364 	goto_if_nok (error, fail);
365 	MONO_HANDLE_SET (res, scopename, mono_string_new_handle (domain, name, error));
366 	goto_if_nok (error, fail);
367 	MONO_HANDLE_SETVAL (res, is_resource, MonoBoolean, cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA);
368 	MONO_HANDLE_SETVAL (res, token, guint32, mono_metadata_make_token (MONO_TABLE_FILE, table_index + 1));
369 
370 	return res;
371 fail:
372 	return MONO_HANDLE_CAST (MonoReflectionModule, NULL_HANDLE);
373 }
374 
375 static MonoType*
mono_type_normalize(MonoType * type)376 mono_type_normalize (MonoType *type)
377 {
378 	int i;
379 	MonoGenericClass *gclass;
380 	MonoGenericInst *ginst;
381 	MonoClass *gtd;
382 	MonoGenericContainer *gcontainer;
383 	MonoType **argv = NULL;
384 	gboolean is_denorm_gtd = TRUE, requires_rebind = FALSE;
385 
386 	if (type->type != MONO_TYPE_GENERICINST)
387 		return type;
388 
389 	gclass = type->data.generic_class;
390 	ginst = gclass->context.class_inst;
391 	if (!ginst->is_open)
392 		return type;
393 
394 	gtd = gclass->container_class;
395 	gcontainer = mono_class_get_generic_container (gtd);
396 	argv = g_newa (MonoType*, ginst->type_argc);
397 
398 	for (i = 0; i < ginst->type_argc; ++i) {
399 		MonoType *t = ginst->type_argv [i], *norm;
400 		if (t->type != MONO_TYPE_VAR || t->data.generic_param->num != i || t->data.generic_param->owner != gcontainer)
401 			is_denorm_gtd = FALSE;
402 		norm = mono_type_normalize (t);
403 		argv [i] = norm;
404 		if (norm != t)
405 			requires_rebind = TRUE;
406 	}
407 
408 	if (is_denorm_gtd)
409 		return type->byref == gtd->byval_arg.byref ? &gtd->byval_arg : &gtd->this_arg;
410 
411 	if (requires_rebind) {
412 		MonoClass *klass = mono_class_bind_generic_parameters (gtd, ginst->type_argc, argv, gclass->is_dynamic);
413 		return type->byref == klass->byval_arg.byref ? &klass->byval_arg : &klass->this_arg;
414 	}
415 
416 	return type;
417 }
418 
419 /**
420  * mono_type_get_object:
421  * \param domain an app domain
422  * \param type a type
423  * \returns A \c System.MonoType object representing the type \p type.
424  */
425 MonoReflectionType*
mono_type_get_object(MonoDomain * domain,MonoType * type)426 mono_type_get_object (MonoDomain *domain, MonoType *type)
427 {
428 	MonoError error;
429 	MonoReflectionType *ret = mono_type_get_object_checked (domain, type, &error);
430 	mono_error_cleanup (&error);
431 
432 	return ret;
433 }
434 
435 MonoReflectionType*
mono_type_get_object_checked(MonoDomain * domain,MonoType * type,MonoError * error)436 mono_type_get_object_checked (MonoDomain *domain, MonoType *type, MonoError *error)
437 {
438 	MonoType *norm_type;
439 	MonoReflectionType *res;
440 	MonoClass *klass;
441 
442 	error_init (error);
443 
444 	g_assert (type != NULL);
445 	klass = mono_class_from_mono_type (type);
446 
447 	/*we must avoid using @type as it might have come
448 	 * from a mono_metadata_type_dup and the caller
449 	 * expects that is can be freed.
450 	 * Using the right type from
451 	 */
452 	type = klass->byval_arg.byref == type->byref ? &klass->byval_arg : &klass->this_arg;
453 
454 	/* void is very common */
455 	if (type->type == MONO_TYPE_VOID && domain->typeof_void)
456 		return (MonoReflectionType*)domain->typeof_void;
457 
458 	/*
459 	 * If the vtable of the given class was already created, we can use
460 	 * the MonoType from there and avoid all locking and hash table lookups.
461 	 *
462 	 * We cannot do this for TypeBuilders as mono_reflection_create_runtime_class expects
463 	 * that the resulting object is different.
464 	 */
465 	if (type == &klass->byval_arg && !image_is_dynamic (klass->image)) {
466 		MonoVTable *vtable = mono_class_try_get_vtable (domain, klass);
467 		if (vtable && vtable->type)
468 			return (MonoReflectionType *)vtable->type;
469 	}
470 
471 	mono_loader_lock (); /*FIXME mono_class_init and mono_class_vtable acquire it*/
472 	mono_domain_lock (domain);
473 	if (!domain->type_hash)
474 		domain->type_hash = mono_g_hash_table_new_type ((GHashFunc)mono_metadata_type_hash,
475 				(GCompareFunc)mono_metadata_type_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain reflection types table");
476 	if ((res = (MonoReflectionType *)mono_g_hash_table_lookup (domain->type_hash, type))) {
477 		mono_domain_unlock (domain);
478 		mono_loader_unlock ();
479 		return res;
480 	}
481 
482 	/*Types must be normalized so a generic instance of the GTD get's the same inner type.
483 	 * For example in: Foo<A,B>; Bar<A> : Foo<A, Bar<A>>
484 	 * The second Bar will be encoded a generic instance of Bar with <A> as parameter.
485 	 * On all other places, Bar<A> will be encoded as the GTD itself. This is an implementation
486 	 * artifact of how generics are encoded and should be transparent to managed code so we
487 	 * need to weed out this diference when retrieving managed System.Type objects.
488 	 */
489 	norm_type = mono_type_normalize (type);
490 	if (norm_type != type) {
491 		res = mono_type_get_object_checked (domain, norm_type, error);
492 		if (!mono_error_ok (error))
493 			return NULL;
494 		mono_g_hash_table_insert (domain->type_hash, type, res);
495 		mono_domain_unlock (domain);
496 		mono_loader_unlock ();
497 		return res;
498 	}
499 
500 	if ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->is_dynamic && !type->data.generic_class->container_class->wastypebuilder) {
501 		/* This can happen if a TypeBuilder for a generic class K<T,U>
502 		 * had reflection_create_generic_class) called on it, but not
503 		 * ves_icall_TypeBuilder_create_runtime_class.  This can happen
504 		 * if the K`2 is refernced from a generic instantiation
505 		 * (e.g. K<int,string>) that appears as type argument
506 		 * (e.g. Dict<string,K<int,string>>), field (e.g. K<int,string>
507 		 * Foo) or method signature, parent class or any of the above
508 		 * in a nested class of some other TypeBuilder.  Such an
509 		 * occurrence caused mono_reflection_type_get_handle to be
510 		 * called on the sre generic instance (K<int,string>) which
511 		 * required the container_class for the generic class K`2 to be
512 		 * set up, but the remainder of class construction for K`2 has
513 		 * not been done. */
514 		char * full_name = mono_type_get_full_name (klass);
515 		/* I would have expected ReflectionTypeLoadException, but evidently .NET throws TLE in this case. */
516 		mono_error_set_type_load_class (error, klass, "TypeBuilder.CreateType() not called for generic class %s", full_name);
517 		g_free (full_name);
518 		mono_domain_unlock (domain);
519 		mono_loader_unlock ();
520 		return NULL;
521 	}
522 
523 	if (mono_class_has_ref_info (klass) && !klass->wastypebuilder && !type->byref) {
524 		mono_domain_unlock (domain);
525 		mono_loader_unlock ();
526 		return (MonoReflectionType *)mono_class_get_ref_info_raw (klass); /* FIXME use handles */
527 	}
528 	/* This is stored in vtables/JITted code so it has to be pinned */
529 	res = (MonoReflectionType *)mono_object_new_pinned (domain, mono_defaults.runtimetype_class, error);
530 	if (!mono_error_ok (error))
531 		return NULL;
532 
533 	res->type = type;
534 	mono_g_hash_table_insert (domain->type_hash, type, res);
535 
536 	if (type->type == MONO_TYPE_VOID)
537 		domain->typeof_void = (MonoObject*)res;
538 
539 	mono_domain_unlock (domain);
540 	mono_loader_unlock ();
541 	return res;
542 }
543 
544 MonoReflectionTypeHandle
mono_type_get_object_handle(MonoDomain * domain,MonoType * type,MonoError * error)545 mono_type_get_object_handle (MonoDomain *domain, MonoType *type, MonoError *error)
546 {
547 	/* NOTE: We happen to know that mono_type_get_object_checked returns
548 	 * pinned objects, so we can just wrap its return value in a handle for
549 	 * uniformity.  If it ever starts returning unpinned, objects, this
550 	 * implementation would need to change!
551 	 */
552 	return MONO_HANDLE_NEW (MonoReflectionType, mono_type_get_object_checked (domain, type, error));
553 }
554 
555 /**
556  * mono_method_get_object:
557  * \param domain an app domain
558  * \param method a method
559  * \param refclass the reflected type (can be NULL)
560  * \returns A \c System.Reflection.MonoMethod object representing the method \p method.
561  */
562 MonoReflectionMethod*
mono_method_get_object(MonoDomain * domain,MonoMethod * method,MonoClass * refclass)563 mono_method_get_object (MonoDomain *domain, MonoMethod *method, MonoClass *refclass)
564 {
565 	HANDLE_FUNCTION_ENTER ();
566 	MonoError error;
567 	MonoReflectionMethodHandle ret = mono_method_get_object_handle (domain, method, refclass, &error);
568 	mono_error_cleanup (&error);
569 	HANDLE_FUNCTION_RETURN_OBJ (ret);
570 }
571 
572 static MonoReflectionMethodHandle
method_object_construct(MonoDomain * domain,MonoClass * refclass,MonoMethod * method,gpointer user_data,MonoError * error)573 method_object_construct (MonoDomain *domain, MonoClass *refclass, MonoMethod *method, gpointer user_data, MonoError *error)
574 {
575 	error_init (error);
576 	g_assert (refclass != NULL);
577 	/*
578 	 * We use the same C representation for methods and constructors, but the type
579 	 * name in C# is different.
580 	 */
581 	MonoClass *klass;
582 
583 	error_init (error);
584 
585 	if (*method->name == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)) {
586 		klass = mono_class_get_mono_cmethod_class ();
587 	}
588 	else {
589 		klass = mono_class_get_mono_method_class ();
590 	}
591 	MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, mono_object_new_checked (domain, klass, error));
592 	goto_if_nok (error, fail);
593 	MONO_HANDLE_SETVAL (ret, method, MonoMethod*, method);
594 
595 	MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &refclass->byval_arg, error);
596 	goto_if_nok (error, fail);
597 
598 	MONO_HANDLE_SET (ret, reftype, rt);
599 
600 	return ret;
601 
602 fail:
603 	return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
604 }
605 
606 /*
607  * mono_method_get_object_handle:
608  * @domain: an app domain
609  * @method: a method
610  * @refclass: the reflected type (can be NULL)
611  * @error: set on error.
612  *
613  * Return an System.Reflection.MonoMethod object representing the method @method.
614  * Returns NULL and sets @error on error.
615  */
616 MonoReflectionMethodHandle
mono_method_get_object_handle(MonoDomain * domain,MonoMethod * method,MonoClass * refclass,MonoError * error)617 mono_method_get_object_handle (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
618 {
619 	error_init (error);
620 	if (!refclass)
621 		refclass = method->klass;
622 
623 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionMethodHandle, method, refclass, method_object_construct, NULL);
624 }
625 /*
626  * mono_method_get_object_checked:
627  * @domain: an app domain
628  * @method: a method
629  * @refclass: the reflected type (can be NULL)
630  * @error: set on error.
631  *
632  * Return an System.Reflection.MonoMethod object representing the method @method.
633  * Returns NULL and sets @error on error.
634  */
635 MonoReflectionMethod*
mono_method_get_object_checked(MonoDomain * domain,MonoMethod * method,MonoClass * refclass,MonoError * error)636 mono_method_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
637 {
638 	HANDLE_FUNCTION_ENTER ();
639 	MonoReflectionMethodHandle result = mono_method_get_object_handle (domain, method, refclass, error);
640 	HANDLE_FUNCTION_RETURN_OBJ (result);
641 }
642 
643 /*
644  * mono_method_clear_object:
645  *
646  *   Clear the cached reflection objects for the dynamic method METHOD.
647  */
648 void
mono_method_clear_object(MonoDomain * domain,MonoMethod * method)649 mono_method_clear_object (MonoDomain *domain, MonoMethod *method)
650 {
651 	MonoClass *klass;
652 	g_assert (method_is_dynamic (method));
653 
654 	klass = method->klass;
655 	while (klass) {
656 		clear_cached_object (domain, method, klass);
657 		klass = klass->parent;
658 	}
659 	/* Added by mono_param_get_objects () */
660 	clear_cached_object (domain, &(method->signature), NULL);
661 	klass = method->klass;
662 	while (klass) {
663 		clear_cached_object (domain, &(method->signature), klass);
664 		klass = klass->parent;
665 	}
666 }
667 
668 /**
669  * mono_field_get_object:
670  * \param domain an app domain
671  * \param klass a type
672  * \param field a field
673  * \returns A \c System.Reflection.MonoField object representing the field \p field
674  * in class \p klass.
675  */
676 MonoReflectionField*
mono_field_get_object(MonoDomain * domain,MonoClass * klass,MonoClassField * field)677 mono_field_get_object (MonoDomain *domain, MonoClass *klass, MonoClassField *field)
678 {
679 	HANDLE_FUNCTION_ENTER ();
680 	MonoError error;
681 	MonoReflectionFieldHandle result = mono_field_get_object_handle (domain, klass, field, &error);
682 	mono_error_cleanup (&error);
683 	HANDLE_FUNCTION_RETURN_OBJ (result);
684 }
685 
686 static MonoReflectionFieldHandle
field_object_construct(MonoDomain * domain,MonoClass * klass,MonoClassField * field,gpointer user_data,MonoError * error)687 field_object_construct (MonoDomain *domain, MonoClass *klass, MonoClassField *field, gpointer user_data, MonoError *error)
688 {
689 	error_init (error);
690 
691 	MonoReflectionFieldHandle res = MONO_HANDLE_NEW (MonoReflectionField, mono_object_new_checked (domain, mono_class_get_mono_field_class (), error));
692 	goto_if_nok (error, fail);
693 	MONO_HANDLE_SETVAL (res, klass, MonoClass *, klass);
694 	MONO_HANDLE_SETVAL (res, field, MonoClassField *, field);
695 	MonoStringHandle name = mono_string_new_handle (domain, mono_field_get_name (field), error);
696 	goto_if_nok (error, fail);
697 	MONO_HANDLE_SET (res, name, name);
698 
699 	if (field->type) {
700 		MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, field->type, error);
701 		goto_if_nok (error, fail);
702 
703 		MONO_HANDLE_SET (res, type, rt);
704 	}
705 	MONO_HANDLE_SETVAL (res, attrs, guint32, mono_field_get_flags (field));
706 	return res;
707 fail:
708 	return MONO_HANDLE_CAST (MonoReflectionField, NULL_HANDLE);
709 }
710 
711 /*
712  * mono_field_get_object_handle:
713  * @domain: an app domain
714  * @klass: a type
715  * @field: a field
716  * @error: set on error
717  *
718  * Return an System.Reflection.MonoField object representing the field @field
719  * in class @klass. On error, returns NULL and sets @error.
720  */
721 MonoReflectionFieldHandle
mono_field_get_object_handle(MonoDomain * domain,MonoClass * klass,MonoClassField * field,MonoError * error)722 mono_field_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
723 {
724 	error_init (error);
725 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionFieldHandle, field, klass, field_object_construct, NULL);
726 }
727 
728 
729 /*
730  * mono_field_get_object_checked:
731  * @domain: an app domain
732  * @klass: a type
733  * @field: a field
734  * @error: set on error
735  *
736  * Return an System.Reflection.MonoField object representing the field @field
737  * in class @klass. On error, returns NULL and sets @error.
738  */
739 MonoReflectionField*
mono_field_get_object_checked(MonoDomain * domain,MonoClass * klass,MonoClassField * field,MonoError * error)740 mono_field_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
741 {
742 	HANDLE_FUNCTION_ENTER ();
743 	MonoReflectionFieldHandle result = mono_field_get_object_handle (domain, klass, field, error);
744 	HANDLE_FUNCTION_RETURN_OBJ (result);
745 }
746 
747 /*
748  * mono_property_get_object:
749  * @domain: an app domain
750  * @klass: a type
751  * @property: a property
752  *
753  * Return an System.Reflection.MonoProperty object representing the property @property
754  * in class @klass.
755  */
756 MonoReflectionProperty*
mono_property_get_object(MonoDomain * domain,MonoClass * klass,MonoProperty * property)757 mono_property_get_object (MonoDomain *domain, MonoClass *klass, MonoProperty *property)
758 {
759 	HANDLE_FUNCTION_ENTER ();
760 	MonoError error;
761 	MonoReflectionPropertyHandle result = mono_property_get_object_handle (domain, klass, property, &error);
762 	mono_error_cleanup (&error);
763 	HANDLE_FUNCTION_RETURN_OBJ (result);
764 }
765 
766 static MonoReflectionPropertyHandle
property_object_construct(MonoDomain * domain,MonoClass * klass,MonoProperty * property,gpointer user_data,MonoError * error)767 property_object_construct (MonoDomain *domain, MonoClass *klass, MonoProperty *property, gpointer user_data, MonoError *error)
768 {
769 	error_init (error);
770 
771 	MonoReflectionPropertyHandle res = MONO_HANDLE_NEW (MonoReflectionProperty, mono_object_new_checked (domain, mono_class_get_mono_property_class (), error));
772 	goto_if_nok (error, fail);
773 	MONO_HANDLE_SETVAL (res, klass, MonoClass *, klass);
774 	MONO_HANDLE_SETVAL (res, property, MonoProperty *, property);
775 	return res;
776 fail:
777 	return MONO_HANDLE_CAST (MonoReflectionProperty, NULL_HANDLE);
778 }
779 
780 /**
781  * mono_property_get_object_handle:
782  * \param domain an app domain
783  * \param klass a type
784  * \param property a property
785  * \param error set on error
786  *
787  * \returns A \c System.Reflection.MonoProperty object representing the property \p property
788  * in class \p klass.  On error returns NULL and sets \p error.
789  */
790 MonoReflectionPropertyHandle
mono_property_get_object_handle(MonoDomain * domain,MonoClass * klass,MonoProperty * property,MonoError * error)791 mono_property_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
792 {
793 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionPropertyHandle, property, klass, property_object_construct, NULL);
794 }
795 
796 /**
797  * mono_property_get_object:
798  * \param domain an app domain
799  * \param klass a type
800  * \param property a property
801  * \param error set on error
802  * \returns a \c System.Reflection.MonoProperty object representing the property \p property
803  * in class \p klass.  On error returns NULL and sets \p error.
804  */
805 MonoReflectionProperty*
mono_property_get_object_checked(MonoDomain * domain,MonoClass * klass,MonoProperty * property,MonoError * error)806 mono_property_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
807 {
808 	HANDLE_FUNCTION_ENTER ();
809 	MonoReflectionPropertyHandle res = mono_property_get_object_handle (domain, klass, property, error);
810 	HANDLE_FUNCTION_RETURN_OBJ (res);
811 }
812 
813 /**
814  * mono_event_get_object:
815  * \param domain an app domain
816  * \param klass a type
817  * \param event a event
818  * \returns A \c System.Reflection.MonoEvent object representing the event \p event
819  * in class \p klass.
820  */
821 MonoReflectionEvent*
mono_event_get_object(MonoDomain * domain,MonoClass * klass,MonoEvent * event)822 mono_event_get_object (MonoDomain *domain, MonoClass *klass, MonoEvent *event)
823 {
824 	HANDLE_FUNCTION_ENTER ();
825 	MonoError error;
826 	MonoReflectionEventHandle result = mono_event_get_object_handle (domain, klass, event, &error);
827 	mono_error_cleanup (&error);
828 	HANDLE_FUNCTION_RETURN_OBJ (result);
829 }
830 
831 static MonoReflectionEventHandle
event_object_construct(MonoDomain * domain,MonoClass * klass,MonoEvent * event,gpointer user_data,MonoError * error)832 event_object_construct (MonoDomain *domain, MonoClass *klass, MonoEvent *event, gpointer user_data, MonoError *error)
833 {
834 
835 	error_init (error);
836 	MonoReflectionMonoEventHandle mono_event = MONO_HANDLE_NEW (MonoReflectionMonoEvent, mono_object_new_checked (domain, mono_class_get_mono_event_class (), error));
837 	if (!is_ok (error))
838 		return MONO_HANDLE_CAST (MonoReflectionEvent, NULL_HANDLE);
839 	MONO_HANDLE_SETVAL (mono_event, klass, MonoClass* , klass);
840 	MONO_HANDLE_SETVAL (mono_event, event, MonoEvent* , event);
841 	return MONO_HANDLE_CAST (MonoReflectionEvent, mono_event);
842 }
843 
844 /**
845  * mono_event_get_object_handle:
846  * \param domain an app domain
847  * \param klass a type
848  * \param event a event
849  * \param error set on error
850  * \returns a \c System.Reflection.MonoEvent object representing the event \p event
851  * in class \p klass. On failure sets \p error and returns NULL
852  */
853 MonoReflectionEventHandle
mono_event_get_object_handle(MonoDomain * domain,MonoClass * klass,MonoEvent * event,MonoError * error)854 mono_event_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoEvent *event, MonoError *error)
855 {
856 	error_init (error);
857 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionEventHandle, event, klass, event_object_construct, NULL);
858 }
859 
860 
861 /**
862  * mono_get_reflection_missing_object:
863  * \param domain Domain where the object lives
864  *
865  * \returns the \c System.Reflection.Missing.Value singleton object
866  * (of type \c System.Reflection.Missing).
867  *
868  * Used as the value for \c ParameterInfo.DefaultValue when Optional
869  * is present
870  */
871 static MonoObjectHandle
mono_get_reflection_missing_object(MonoDomain * domain)872 mono_get_reflection_missing_object (MonoDomain *domain)
873 {
874 	MonoError error;
875 	static MonoClassField *missing_value_field = NULL;
876 
877 	if (!missing_value_field) {
878 		MonoClass *missing_klass;
879 		missing_klass = mono_class_get_missing_class ();
880 		mono_class_init (missing_klass);
881 		missing_value_field = mono_class_get_field_from_name (missing_klass, "Value");
882 		g_assert (missing_value_field);
883 	}
884 	/* FIXME change mono_field_get_value_object_checked to return a handle */
885 	MonoObjectHandle obj = MONO_HANDLE_NEW (MonoObject, mono_field_get_value_object_checked (domain, missing_value_field, NULL, &error));
886 	mono_error_assert_ok (&error);
887 	return obj;
888 }
889 
890 static MonoObjectHandle
get_dbnull_object(MonoDomain * domain,MonoError * error)891 get_dbnull_object (MonoDomain *domain, MonoError *error)
892 {
893 	static MonoClassField *dbnull_value_field = NULL;
894 
895 	error_init (error);
896 
897 	if (!dbnull_value_field) {
898 		MonoClass *dbnull_klass;
899 		dbnull_klass = mono_class_get_dbnull_class ();
900 		dbnull_value_field = mono_class_get_field_from_name (dbnull_klass, "Value");
901 		g_assert (dbnull_value_field);
902 	}
903 	/* FIXME change mono_field_get_value_object_checked to return a handle */
904 	MonoObjectHandle obj = MONO_HANDLE_NEW (MonoObject, mono_field_get_value_object_checked (domain, dbnull_value_field, NULL, error));
905 	return obj;
906 }
907 
908 static MonoObjectHandle
get_dbnull(MonoDomain * domain,MonoObjectHandle dbnull,MonoError * error)909 get_dbnull (MonoDomain *domain, MonoObjectHandle dbnull, MonoError *error)
910 {
911 	error_init (error);
912 	if (MONO_HANDLE_IS_NULL (dbnull))
913 		MONO_HANDLE_ASSIGN (dbnull, get_dbnull_object (domain, error));
914 	return dbnull;
915 }
916 
917 static MonoObjectHandle
get_reflection_missing(MonoDomain * domain,MonoObjectHandleOut reflection_missing)918 get_reflection_missing (MonoDomain *domain, MonoObjectHandleOut reflection_missing)
919 {
920 	if (MONO_HANDLE_IS_NULL (reflection_missing))
921 		MONO_HANDLE_ASSIGN (reflection_missing, mono_get_reflection_missing_object (domain));
922 	return reflection_missing;
923 }
924 
925 static gboolean
add_parameter_object_to_array(MonoDomain * domain,MonoMethod * method,MonoObjectHandle member,int idx,const char * name,MonoType * sig_param,guint32 blob_type_enum,const char * blob,MonoMarshalSpec * mspec,MonoObjectHandle missing,MonoObjectHandle dbnull,MonoArrayHandle dest,MonoError * error)926 add_parameter_object_to_array (MonoDomain *domain, MonoMethod *method, MonoObjectHandle member, int idx, const char *name, MonoType *sig_param, guint32 blob_type_enum, const char *blob, MonoMarshalSpec *mspec, MonoObjectHandle missing, MonoObjectHandle dbnull, MonoArrayHandle dest,  MonoError *error)
927 {
928 	HANDLE_FUNCTION_ENTER ();
929 	error_init (error);
930 	MonoReflectionParameterHandle param = MONO_HANDLE_NEW (MonoReflectionParameter, mono_object_new_checked (domain, mono_class_get_mono_parameter_info_class (), error));
931 	goto_if_nok (error, leave);
932 
933 	MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, sig_param, error);
934 	goto_if_nok (error, leave);
935 
936 	MONO_HANDLE_SET (param, ClassImpl, rt);
937 
938 	MONO_HANDLE_SET (param, MemberImpl, member);
939 
940 	MonoStringHandle name_str = mono_string_new_handle (domain, name, error);
941 	goto_if_nok (error, leave);
942 
943 	MONO_HANDLE_SET (param, NameImpl, name_str);
944 
945 	MONO_HANDLE_SETVAL (param, PositionImpl, gint32, idx);
946 
947 	MONO_HANDLE_SETVAL (param, AttrsImpl, guint32, sig_param->attrs);
948 
949 	if (!(sig_param->attrs & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
950 		if (sig_param->attrs & PARAM_ATTRIBUTE_OPTIONAL)
951 			MONO_HANDLE_SET (param, DefaultValueImpl, get_reflection_missing (domain, missing));
952 		else
953 			MONO_HANDLE_SET (param, DefaultValueImpl, get_dbnull (domain, dbnull, error));
954 		goto_if_nok (error, leave);
955 	} else {
956 
957 		MonoType blob_type;
958 
959 		blob_type.type = (MonoTypeEnum)blob_type_enum;
960 		blob_type.data.klass = NULL;
961 		if (blob_type_enum == MONO_TYPE_CLASS)
962 			blob_type.data.klass = mono_defaults.object_class;
963 		else if ((sig_param->type == MONO_TYPE_VALUETYPE) && sig_param->data.klass->enumtype) {
964 			/* For enums, types [i] contains the base type */
965 
966 			blob_type.type = MONO_TYPE_VALUETYPE;
967 			blob_type.data.klass = mono_class_from_mono_type (sig_param);
968 		} else
969 			blob_type.data.klass = mono_class_from_mono_type (&blob_type);
970 
971 		MonoObjectHandle default_val_obj = MONO_HANDLE_NEW (MonoObject, mono_get_object_from_blob (domain, &blob_type, blob, error)); /* FIXME make mono_get_object_from_blob return a handle */
972 		goto_if_nok (error, leave);
973 		MONO_HANDLE_SET (param, DefaultValueImpl, default_val_obj);
974 
975 		/* Type in the Constant table is MONO_TYPE_CLASS for nulls */
976 		if (blob_type_enum != MONO_TYPE_CLASS && MONO_HANDLE_IS_NULL(default_val_obj)) {
977 			if (sig_param->attrs & PARAM_ATTRIBUTE_OPTIONAL)
978 				MONO_HANDLE_SET (param, DefaultValueImpl, get_reflection_missing (domain, missing));
979 			else
980 				MONO_HANDLE_SET (param, DefaultValueImpl, get_dbnull (domain, dbnull, error));
981 			goto_if_nok (error, leave);
982 		}
983 	}
984 
985 	if (mspec) {
986 		MonoReflectionMarshalAsAttributeHandle mobj = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspec, error);
987 		goto_if_nok (error, leave);
988 		MONO_HANDLE_SET (param, MarshalAsImpl, mobj);
989 	}
990 
991 	MONO_HANDLE_ARRAY_SETREF (dest, idx, param);
992 
993 leave:
994 	HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
995 }
996 
997 static MonoArrayHandle
param_objects_construct(MonoDomain * domain,MonoClass * refclass,MonoMethodSignature ** addr_of_sig,gpointer user_data,MonoError * error)998 param_objects_construct (MonoDomain *domain, MonoClass *refclass, MonoMethodSignature **addr_of_sig, gpointer user_data, MonoError *error)
999 {
1000 	MonoMethod *method = (MonoMethod*)user_data;
1001 	MonoMethodSignature *sig = *addr_of_sig; /* see note in mono_param_get_objects_internal */
1002 
1003 	MonoArrayHandle res = MONO_HANDLE_NEW (MonoArray, NULL);
1004 	char **names = NULL, **blobs = NULL;
1005 	guint32 *types = NULL;
1006 	MonoMarshalSpec **mspecs = NULL;
1007 	int i;
1008 
1009 	error_init (error);
1010 
1011 	MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, refclass, error);
1012 	goto_if_nok (error, leave);
1013 	names = g_new (char *, sig->param_count);
1014 	mono_method_get_param_names (method, (const char **) names);
1015 
1016 	mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
1017 	mono_method_get_marshal_info (method, mspecs);
1018 
1019 	res = mono_array_new_handle (domain, mono_class_get_mono_parameter_info_class (), sig->param_count, error);
1020 	if (!res)
1021 		goto leave;
1022 
1023 	gboolean any_default_value = FALSE;
1024 	for (i = 0; i < sig->param_count; ++i) {
1025 		if ((sig->params [i]->attrs & PARAM_ATTRIBUTE_HAS_DEFAULT) != 0) {
1026 			any_default_value = TRUE;
1027 			break;
1028 		}
1029 	}
1030 	if (any_default_value) {
1031 		blobs = g_new0 (char *, sig->param_count);
1032 		types = g_new0 (guint32, sig->param_count);
1033 		get_default_param_value_blobs (method, blobs, types);
1034 	}
1035 
1036 	/* Handles missing and dbnull are assigned in add_parameter_object_to_array when needed */
1037 	MonoObjectHandle missing = MONO_HANDLE_NEW (MonoObject, NULL);
1038 	MonoObjectHandle dbnull = MONO_HANDLE_NEW (MonoObject, NULL);
1039 	for (i = 0; i < sig->param_count; ++i) {
1040 		if (!add_parameter_object_to_array (domain, method, MONO_HANDLE_CAST(MonoObject, member), i, names[i], sig->params[i], types ? types[i] : 0, blobs ? blobs[i] : NULL, mspecs [i + 1], missing, dbnull, res, error))
1041 			goto leave;
1042 	}
1043 
1044 leave:
1045 	g_free (names);
1046 	g_free (blobs);
1047 	g_free (types);
1048 
1049 	if (sig && mspecs) {
1050 		for (i = sig->param_count; i >= 0; i--) {
1051 			if (mspecs [i])
1052 				mono_metadata_free_marshal_spec (mspecs [i]);
1053 		}
1054 	}
1055 	g_free (mspecs);
1056 
1057 	if (!is_ok (error))
1058 		return NULL;
1059 
1060 	return res;
1061 }
1062 
1063 /*
1064  * mono_param_get_objects:
1065  * @domain: an app domain
1066  * @method: a method
1067  *
1068  * Return an System.Reflection.ParameterInfo array object representing the parameters
1069  * in the method @method.
1070  */
1071 MonoArrayHandle
mono_param_get_objects_internal(MonoDomain * domain,MonoMethod * method,MonoClass * refclass,MonoError * error)1072 mono_param_get_objects_internal (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
1073 {
1074 	error_init (error);
1075 
1076 	/* side-effect: sets method->signature non-NULL on success */
1077 	MonoMethodSignature *sig = mono_method_signature_checked (method, error);
1078 	goto_if_nok (error, fail);
1079 
1080 	if (!sig->param_count) {
1081 		MonoArrayHandle res = mono_array_new_handle (domain, mono_class_get_mono_parameter_info_class (), 0, error);
1082 		goto_if_nok (error, fail);
1083 
1084 		return res;
1085 	}
1086 
1087 	/* Note: the cache is based on the address of the signature into the method
1088 	 * since we already cache MethodInfos with the method as keys.
1089 	 */
1090 	return CHECK_OR_CONSTRUCT_HANDLE (MonoArrayHandle, &method->signature, refclass, param_objects_construct, method);
1091 fail:
1092 	return MONO_HANDLE_NEW (MonoArray, NULL_HANDLE);
1093 }
1094 
1095 /**
1096  * mono_param_get_objects:
1097  */
1098 MonoArray*
mono_param_get_objects(MonoDomain * domain,MonoMethod * method)1099 mono_param_get_objects (MonoDomain *domain, MonoMethod *method)
1100 {
1101 	HANDLE_FUNCTION_ENTER ();
1102 	MonoError error;
1103 	MonoArrayHandle result = mono_param_get_objects_internal (domain, method, NULL, &error);
1104 	mono_error_assert_ok (&error);
1105 	HANDLE_FUNCTION_RETURN_OBJ (result);
1106 }
1107 
1108 static gboolean
add_local_var_info_to_array(MonoDomain * domain,MonoMethodHeader * header,int idx,MonoArrayHandle dest,MonoError * error)1109 add_local_var_info_to_array (MonoDomain *domain, MonoMethodHeader *header, int idx, MonoArrayHandle dest, MonoError *error)
1110 {
1111 	HANDLE_FUNCTION_ENTER ();
1112 	error_init (error);
1113 	MonoReflectionLocalVariableInfoHandle info = MONO_HANDLE_NEW (MonoReflectionLocalVariableInfo, mono_object_new_checked (domain, mono_class_get_local_variable_info_class (), error));
1114 	goto_if_nok (error, leave);
1115 
1116 	MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, header->locals [idx], error);
1117 	goto_if_nok (error, leave);
1118 
1119 	MONO_HANDLE_SET (info, local_type, rt);
1120 
1121 	MONO_HANDLE_SETVAL (info, is_pinned, MonoBoolean, header->locals [idx]->pinned);
1122 	MONO_HANDLE_SETVAL (info, local_index, guint16, idx);
1123 
1124 	MONO_HANDLE_ARRAY_SETREF (dest, idx, info);
1125 
1126 leave:
1127 	HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1128 }
1129 
1130 static gboolean
add_exception_handling_clause_to_array(MonoDomain * domain,MonoMethodHeader * header,int idx,MonoArrayHandle dest,MonoError * error)1131 add_exception_handling_clause_to_array (MonoDomain *domain, MonoMethodHeader *header, int idx, MonoArrayHandle dest, MonoError *error)
1132 {
1133 	HANDLE_FUNCTION_ENTER ();
1134 	error_init (error);
1135 	MonoReflectionExceptionHandlingClauseHandle info = MONO_HANDLE_NEW (MonoReflectionExceptionHandlingClause, mono_object_new_checked (domain, mono_class_get_exception_handling_clause_class (), error));
1136 	goto_if_nok (error, leave);
1137 	MonoExceptionClause *clause = &header->clauses [idx];
1138 
1139 	MONO_HANDLE_SETVAL (info, flags, gint32, clause->flags);
1140 	MONO_HANDLE_SETVAL (info, try_offset, gint32, clause->try_offset);
1141 	MONO_HANDLE_SETVAL (info, try_length, gint32, clause->try_len);
1142 	MONO_HANDLE_SETVAL (info, handler_offset, gint32, clause->handler_offset);
1143 	MONO_HANDLE_SETVAL (info, handler_length, gint32, clause->handler_len);
1144 	if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1145 		MONO_HANDLE_SETVAL (info, filter_offset, gint32, clause->data.filter_offset);
1146 	else if (clause->data.catch_class) {
1147 		MonoReflectionTypeHandle rt = mono_type_get_object_handle (mono_domain_get (), &clause->data.catch_class->byval_arg, error);
1148 		goto_if_nok (error, leave);
1149 
1150 		MONO_HANDLE_SET (info, catch_type, rt);
1151 	}
1152 
1153 	MONO_HANDLE_ARRAY_SETREF (dest, idx, info);
1154 leave:
1155 	HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1156 }
1157 
1158 /**
1159  * mono_method_body_get_object:
1160  * \param domain an app domain
1161  * \param method a method
1162  * \return A \c System.Reflection.MethodBody object representing the method \p method.
1163  */
1164 MonoReflectionMethodBody*
mono_method_body_get_object(MonoDomain * domain,MonoMethod * method)1165 mono_method_body_get_object (MonoDomain *domain, MonoMethod *method)
1166 {
1167 	HANDLE_FUNCTION_ENTER ();
1168 	MonoError error;
1169 	MonoReflectionMethodBodyHandle result = mono_method_body_get_object_handle (domain, method, &error);
1170 	mono_error_cleanup (&error);
1171 	HANDLE_FUNCTION_RETURN_OBJ (result);
1172 }
1173 
1174 /* WARNING: This method can return NULL on sucess */
1175 static MonoReflectionMethodBodyHandle
method_body_object_construct(MonoDomain * domain,MonoClass * unused_class,MonoMethod * method,gpointer user_data,MonoError * error)1176 method_body_object_construct (MonoDomain *domain, MonoClass *unused_class, MonoMethod *method, gpointer user_data, MonoError *error)
1177 {
1178 	MonoMethodHeader *header = NULL;
1179 	MonoImage *image;
1180 	guint32 method_rva, local_var_sig_token;
1181 	char *ptr;
1182 	unsigned char format, flags;
1183 	int i;
1184 
1185 	error_init (error);
1186 
1187 	/* for compatibility with .net */
1188 	if (method_is_dynamic (method)) {
1189 		mono_error_set_generic_error (error, "System", "InvalidOperationException", "");
1190 		goto fail;
1191 	}
1192 
1193 	if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1194 		(method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
1195 	    (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1196 		(method->klass->image->raw_data && method->klass->image->raw_data [1] != 'Z') ||
1197 	    (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME))
1198 		return MONO_HANDLE_CAST (MonoReflectionMethodBody, NULL_HANDLE);
1199 
1200 	image = method->klass->image;
1201 	header = mono_method_get_header_checked (method, error);
1202 	goto_if_nok (error, fail);
1203 
1204 	if (!image_is_dynamic (image)) {
1205 		/* Obtain local vars signature token */
1206 		method_rva = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD], mono_metadata_token_index (method->token) - 1, MONO_METHOD_RVA);
1207 		ptr = mono_image_rva_map (image, method_rva);
1208 		flags = *(const unsigned char *) ptr;
1209 		format = flags & METHOD_HEADER_FORMAT_MASK;
1210 		switch (format){
1211 		case METHOD_HEADER_TINY_FORMAT:
1212 			local_var_sig_token = 0;
1213 			break;
1214 		case METHOD_HEADER_FAT_FORMAT:
1215 			ptr += 2;
1216 			ptr += 2;
1217 			ptr += 4;
1218 			local_var_sig_token = read32 (ptr);
1219 			break;
1220 		default:
1221 			g_assert_not_reached ();
1222 		}
1223 	} else
1224 		local_var_sig_token = 0; //FIXME
1225 
1226 	MonoReflectionMethodBodyHandle ret = MONO_HANDLE_NEW (MonoReflectionMethodBody, mono_object_new_checked (domain, mono_class_get_method_body_class (), error));
1227 	goto_if_nok (error, fail);
1228 
1229 	MONO_HANDLE_SETVAL (ret, init_locals, MonoBoolean, header->init_locals);
1230 	MONO_HANDLE_SETVAL (ret, max_stack, guint32, header->max_stack);
1231 	MONO_HANDLE_SETVAL (ret, local_var_sig_token, guint32, local_var_sig_token);
1232 	MonoArrayHandle il_arr = mono_array_new_handle (domain, mono_defaults.byte_class, header->code_size, error);
1233 	goto_if_nok (error, fail);
1234 	MONO_HANDLE_SET (ret, il, il_arr);
1235 	uint32_t il_gchandle;
1236 	guint8* il_data = MONO_ARRAY_HANDLE_PIN (il_arr, guint8, 0, &il_gchandle);
1237 	memcpy (il_data, header->code, header->code_size);
1238 	mono_gchandle_free (il_gchandle);
1239 
1240 	/* Locals */
1241 	MonoArrayHandle locals_arr = mono_array_new_handle (domain, mono_class_get_local_variable_info_class (), header->num_locals, error);
1242 	goto_if_nok (error, fail);
1243 	MONO_HANDLE_SET (ret, locals, locals_arr);
1244 	for (i = 0; i < header->num_locals; ++i) {
1245 		if (!add_local_var_info_to_array (domain, header, i, locals_arr, error))
1246 			goto fail;
1247 	}
1248 
1249 	/* Exceptions */
1250 	MonoArrayHandle exn_clauses = mono_array_new_handle (domain, mono_class_get_exception_handling_clause_class (), header->num_clauses, error);
1251 	goto_if_nok (error, fail);
1252 	MONO_HANDLE_SET (ret, clauses, exn_clauses);
1253 	for (i = 0; i < header->num_clauses; ++i) {
1254 		if (!add_exception_handling_clause_to_array (domain, header, i, exn_clauses, error))
1255 			goto fail;
1256 	}
1257 
1258 	mono_metadata_free_mh (header);
1259 	return ret;
1260 fail:
1261 	if (header)
1262 		mono_metadata_free_mh (header);
1263 	return NULL;
1264 }
1265 
1266 /**
1267  * mono_method_body_get_object_handle:
1268  * \param domain an app domain
1269  * \param method a method
1270  * \param error set on error
1271  * \returns a \c System.Reflection.MethodBody object representing the
1272  * method \p method.  On failure, returns NULL and sets \p error.
1273  */
1274 MonoReflectionMethodBodyHandle
mono_method_body_get_object_handle(MonoDomain * domain,MonoMethod * method,MonoError * error)1275 mono_method_body_get_object_handle (MonoDomain *domain, MonoMethod *method, MonoError *error)
1276 {
1277 	error_init (error);
1278 	return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionMethodBodyHandle, method, NULL, method_body_object_construct, NULL);
1279 }
1280 
1281 
1282 /**
1283  * mono_get_dbnull_object:
1284  * \param domain Domain where the object lives
1285  * Used as the value for \c ParameterInfo.DefaultValue
1286  * \returns the \c System.DBNull.Value singleton object
1287  */
1288 MonoObject *
mono_get_dbnull_object(MonoDomain * domain)1289 mono_get_dbnull_object (MonoDomain *domain)
1290 {
1291 	HANDLE_FUNCTION_ENTER ();
1292 	MonoError error;
1293 	MonoObjectHandle obj = get_dbnull_object (domain, &error);
1294 	mono_error_assert_ok (&error);
1295 	HANDLE_FUNCTION_RETURN_OBJ (obj);
1296 }
1297 
1298 static void
get_default_param_value_blobs(MonoMethod * method,char ** blobs,guint32 * types)1299 get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types)
1300 {
1301 	guint32 param_index, i, lastp, crow = 0;
1302 	guint32 param_cols [MONO_PARAM_SIZE], const_cols [MONO_CONSTANT_SIZE];
1303 	gint32 idx;
1304 
1305 	MonoClass *klass = method->klass;
1306 	MonoImage *image = klass->image;
1307 	MonoMethodSignature *methodsig = mono_method_signature (method);
1308 
1309 	MonoTableInfo *constt;
1310 	MonoTableInfo *methodt;
1311 	MonoTableInfo *paramt;
1312 
1313 	if (!methodsig->param_count)
1314 		return;
1315 
1316 	mono_class_init (klass);
1317 
1318 	if (image_is_dynamic (klass->image)) {
1319 		MonoReflectionMethodAux *aux;
1320 		if (method->is_inflated)
1321 			method = ((MonoMethodInflated*)method)->declaring;
1322 		aux = (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1323 		if (aux && aux->param_defaults) {
1324 			memcpy (blobs, &(aux->param_defaults [1]), methodsig->param_count * sizeof (char*));
1325 			memcpy (types, &(aux->param_default_types [1]), methodsig->param_count * sizeof (guint32));
1326 		}
1327 		return;
1328 	}
1329 
1330 	methodt = &klass->image->tables [MONO_TABLE_METHOD];
1331 	paramt = &klass->image->tables [MONO_TABLE_PARAM];
1332 	constt = &image->tables [MONO_TABLE_CONSTANT];
1333 
1334 	idx = mono_method_get_index (method) - 1;
1335 	g_assert (idx != -1);
1336 
1337 	param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1338 	if (idx + 1 < methodt->rows)
1339 		lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1340 	else
1341 		lastp = paramt->rows + 1;
1342 
1343 	for (i = param_index; i < lastp; ++i) {
1344 		guint32 paramseq;
1345 
1346 		mono_metadata_decode_row (paramt, i - 1, param_cols, MONO_PARAM_SIZE);
1347 		paramseq = param_cols [MONO_PARAM_SEQUENCE];
1348 
1349 		if (!(param_cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT))
1350 			continue;
1351 
1352 		crow = mono_metadata_get_constant_index (image, MONO_TOKEN_PARAM_DEF | i, crow + 1);
1353 		if (!crow) {
1354 			continue;
1355 		}
1356 
1357 		mono_metadata_decode_row (constt, crow - 1, const_cols, MONO_CONSTANT_SIZE);
1358 		blobs [paramseq - 1] = (char *)mono_metadata_blob_heap (image, const_cols [MONO_CONSTANT_VALUE]);
1359 		types [paramseq - 1] = const_cols [MONO_CONSTANT_TYPE];
1360 	}
1361 
1362 	return;
1363 }
1364 
1365 MonoObject *
mono_get_object_from_blob(MonoDomain * domain,MonoType * type,const char * blob,MonoError * error)1366 mono_get_object_from_blob (MonoDomain *domain, MonoType *type, const char *blob, MonoError *error)
1367 {
1368 	void *retval;
1369 	MonoClass *klass;
1370 	MonoObject *object;
1371 	MonoType *basetype = type;
1372 
1373 	error_init (error);
1374 
1375 	if (!blob)
1376 		return NULL;
1377 
1378 	klass = mono_class_from_mono_type (type);
1379 	if (klass->valuetype) {
1380 		object = mono_object_new_checked (domain, klass, error);
1381 		return_val_if_nok (error, NULL);
1382 		retval = ((gchar *) object + sizeof (MonoObject));
1383 		if (klass->enumtype)
1384 			basetype = mono_class_enum_basetype (klass);
1385 	} else {
1386 		retval = &object;
1387 	}
1388 
1389 	if (!mono_get_constant_value_from_blob (domain, basetype->type,  blob, retval, error))
1390 		return object;
1391 	else
1392 		return NULL;
1393 }
1394 
1395 static int
assembly_name_to_aname(MonoAssemblyName * assembly,char * p)1396 assembly_name_to_aname (MonoAssemblyName *assembly, char *p) {
1397 	int found_sep;
1398 	char *s;
1399 	gboolean quoted = FALSE;
1400 
1401 	memset (assembly, 0, sizeof (MonoAssemblyName));
1402 	assembly->culture = "";
1403 	memset (assembly->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1404 
1405 	if (*p == '"') {
1406 		quoted = TRUE;
1407 		p++;
1408 	}
1409 	assembly->name = p;
1410 	while (*p && (isalnum (*p) || *p == '.' || *p == '-' || *p == '_' || *p == '$' || *p == '@' || g_ascii_isspace (*p)))
1411 		p++;
1412 	if (quoted) {
1413 		if (*p != '"')
1414 			return 1;
1415 		*p = 0;
1416 		p++;
1417 	}
1418 	if (*p != ',')
1419 		return 1;
1420 	*p = 0;
1421 	/* Remove trailing whitespace */
1422 	s = p - 1;
1423 	while (*s && g_ascii_isspace (*s))
1424 		*s-- = 0;
1425 	p ++;
1426 	while (g_ascii_isspace (*p))
1427 		p++;
1428 	while (*p) {
1429 		if (*p == 'V' && g_ascii_strncasecmp (p, "Version=", 8) == 0) {
1430 			p += 8;
1431 			assembly->major = strtoul (p, &s, 10);
1432 			if (s == p || *s != '.')
1433 				return 1;
1434 			p = ++s;
1435 			assembly->minor = strtoul (p, &s, 10);
1436 			if (s == p || *s != '.')
1437 				return 1;
1438 			p = ++s;
1439 			assembly->build = strtoul (p, &s, 10);
1440 			if (s == p || *s != '.')
1441 				return 1;
1442 			p = ++s;
1443 			assembly->revision = strtoul (p, &s, 10);
1444 			if (s == p)
1445 				return 1;
1446 			p = s;
1447 		} else if (*p == 'C' && g_ascii_strncasecmp (p, "Culture=", 8) == 0) {
1448 			p += 8;
1449 			if (g_ascii_strncasecmp (p, "neutral", 7) == 0) {
1450 				assembly->culture = "";
1451 				p += 7;
1452 			} else {
1453 				assembly->culture = p;
1454 				while (*p && *p != ',') {
1455 					p++;
1456 				}
1457 			}
1458 		} else if (*p == 'P' && g_ascii_strncasecmp (p, "PublicKeyToken=", 15) == 0) {
1459 			p += 15;
1460 			if (strncmp (p, "null", 4) == 0) {
1461 				p += 4;
1462 			} else {
1463 				int len;
1464 				gchar *start = p;
1465 				while (*p && *p != ',') {
1466 					p++;
1467 				}
1468 				len = (p - start + 1);
1469 				if (len > MONO_PUBLIC_KEY_TOKEN_LENGTH)
1470 					len = MONO_PUBLIC_KEY_TOKEN_LENGTH;
1471 				g_strlcpy ((char*)assembly->public_key_token, start, len);
1472 			}
1473 		} else {
1474 			while (*p && *p != ',')
1475 				p++;
1476 		}
1477 		found_sep = 0;
1478 		while (g_ascii_isspace (*p) || *p == ',') {
1479 			*p++ = 0;
1480 			found_sep = 1;
1481 			continue;
1482 		}
1483 		/* failed */
1484 		if (!found_sep)
1485 			return 1;
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 /*
1492  * mono_reflection_parse_type:
1493  * @name: type name
1494  *
1495  * Parse a type name as accepted by the GetType () method and output the info
1496  * extracted in the info structure.
1497  * the name param will be mangled, so, make a copy before passing it to this function.
1498  * The fields in info will be valid until the memory pointed to by name is valid.
1499  *
1500  * See also mono_type_get_name () below.
1501  *
1502  * Returns: 0 on parse error.
1503  */
1504 static int
_mono_reflection_parse_type(char * name,char ** endptr,gboolean is_recursed,MonoTypeNameParse * info)1505 _mono_reflection_parse_type (char *name, char **endptr, gboolean is_recursed,
1506 			     MonoTypeNameParse *info)
1507 {
1508 	char *start, *p, *w, *last_point, *startn;
1509 	int in_modifiers = 0;
1510 	int isbyref = 0, rank = 0, isptr = 0;
1511 
1512 	start = p = w = name;
1513 
1514 	memset (info, 0, sizeof (MonoTypeNameParse));
1515 
1516 	/* last_point separates the namespace from the name */
1517 	last_point = NULL;
1518 	/* Skips spaces */
1519 	while (*p == ' ') p++, start++, w++, name++;
1520 
1521 	while (*p) {
1522 		switch (*p) {
1523 		case '+':
1524 			*p = 0; /* NULL terminate the name */
1525 			startn = p + 1;
1526 			info->nested = g_list_append (info->nested, startn);
1527 			/* we have parsed the nesting namespace + name */
1528 			if (info->name)
1529 				break;
1530 			if (last_point) {
1531 				info->name_space = start;
1532 				*last_point = 0;
1533 				info->name = last_point + 1;
1534 			} else {
1535 				info->name_space = (char *)"";
1536 				info->name = start;
1537 			}
1538 			break;
1539 		case '.':
1540 			last_point = p;
1541 			break;
1542 		case '\\':
1543 			++p;
1544 			break;
1545 		case '&':
1546 		case '*':
1547 		case '[':
1548 		case ',':
1549 		case ']':
1550 			in_modifiers = 1;
1551 			break;
1552 		default:
1553 			break;
1554 		}
1555 		if (in_modifiers)
1556 			break;
1557 		// *w++ = *p++;
1558 		p++;
1559 	}
1560 
1561 	if (!info->name) {
1562 		if (last_point) {
1563 			info->name_space = start;
1564 			*last_point = 0;
1565 			info->name = last_point + 1;
1566 		} else {
1567 			info->name_space = (char *)"";
1568 			info->name = start;
1569 		}
1570 	}
1571 	while (*p) {
1572 		switch (*p) {
1573 		case '&':
1574 			if (isbyref) /* only one level allowed by the spec */
1575 				return 0;
1576 			isbyref = 1;
1577 			isptr = 0;
1578 			info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (0));
1579 			*p++ = 0;
1580 			break;
1581 		case '*':
1582 			if (isbyref) /* pointer to ref not okay */
1583 				return 0;
1584 			info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-1));
1585 			isptr = 1;
1586 			*p++ = 0;
1587 			break;
1588 		case '[':
1589 			if (isbyref) /* array of ref and generic ref are not okay */
1590 				return 0;
1591 			//Decide if it's an array of a generic argument list
1592 			*p++ = 0;
1593 
1594 			if (!*p) //XXX test
1595 				return 0;
1596 			if (*p  == ',' || *p == '*' || *p == ']') { //array
1597 				isptr = 0;
1598 				rank = 1;
1599 				while (*p) {
1600 					if (*p == ']')
1601 						break;
1602 					if (*p == ',')
1603 						rank++;
1604 					else if (*p == '*') /* '*' means unknown lower bound */
1605 						info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-2));
1606 					else
1607 						return 0;
1608 					++p;
1609 				}
1610 				if (*p++ != ']')
1611 					return 0;
1612 				info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (rank));
1613 			} else {
1614 				if (rank || isptr) /* generic args after array spec or ptr*/ //XXX test
1615 					return 0;
1616 				isptr = 0;
1617 				info->type_arguments = g_ptr_array_new ();
1618 				while (*p) {
1619 					MonoTypeNameParse *subinfo = g_new0 (MonoTypeNameParse, 1);
1620 					gboolean fqname = FALSE;
1621 
1622 					g_ptr_array_add (info->type_arguments, subinfo);
1623 
1624 					while (*p == ' ') p++;
1625 					if (*p == '[') {
1626 						p++;
1627 						fqname = TRUE;
1628 					}
1629 
1630 					if (!_mono_reflection_parse_type (p, &p, TRUE, subinfo))
1631 						return 0;
1632 
1633 					/*MS is lenient on [] delimited parameters that aren't fqn - and F# uses them.*/
1634 					if (fqname && (*p != ']')) {
1635 						char *aname;
1636 
1637 						if (*p != ',')
1638 							return 0;
1639 						*p++ = 0;
1640 
1641 						aname = p;
1642 						while (*p && (*p != ']'))
1643 							p++;
1644 
1645 						if (*p != ']')
1646 							return 0;
1647 
1648 						*p++ = 0;
1649 						while (*aname) {
1650 							if (g_ascii_isspace (*aname)) {
1651 								++aname;
1652 								continue;
1653 							}
1654 							break;
1655 						}
1656 						if (!*aname ||
1657 						    !assembly_name_to_aname (&subinfo->assembly, aname))
1658 							return 0;
1659 					} else if (fqname && (*p == ']')) {
1660 						*p++ = 0;
1661 					}
1662 					if (*p == ']') {
1663 						*p++ = 0;
1664 						break;
1665 					} else if (!*p) {
1666 						return 0;
1667 					}
1668 					*p++ = 0;
1669 				}
1670 			}
1671 			break;
1672 		case ']':
1673 			if (is_recursed)
1674 				goto end;
1675 			return 0;
1676 		case ',':
1677 			if (is_recursed)
1678 				goto end;
1679 			*p++ = 0;
1680 			while (*p) {
1681 				if (g_ascii_isspace (*p)) {
1682 					++p;
1683 					continue;
1684 				}
1685 				break;
1686 			}
1687 			if (!*p)
1688 				return 0; /* missing assembly name */
1689 			if (!assembly_name_to_aname (&info->assembly, p))
1690 				return 0;
1691 			break;
1692 		default:
1693 			return 0;
1694 		}
1695 		if (info->assembly.name)
1696 			break;
1697 	}
1698 	// *w = 0; /* terminate class name */
1699  end:
1700 	if (!info->name || !*info->name)
1701 		return 0;
1702 	if (endptr)
1703 		*endptr = p;
1704 	/* add other consistency checks */
1705 	return 1;
1706 }
1707 
1708 
1709 /**
1710  * mono_identifier_unescape_type_name_chars:
1711  * \param identifier the display name of a mono type
1712  *
1713  * \returns The name in internal form, that is without escaping backslashes.
1714  *
1715  * The string is modified in place!
1716  */
1717 char*
mono_identifier_unescape_type_name_chars(char * identifier)1718 mono_identifier_unescape_type_name_chars(char* identifier)
1719 {
1720 	char *w, *r;
1721 	if (!identifier)
1722 		return NULL;
1723 	for (w = r = identifier; *r != 0; r++)
1724 	{
1725 		char c = *r;
1726 		if (c == '\\') {
1727 			r++;
1728 			if (*r == 0)
1729 				break;
1730 			c = *r;
1731 		}
1732 		*w = c;
1733 		w++;
1734 	}
1735 	if (w != r)
1736 		*w = 0;
1737 	return identifier;
1738 }
1739 
1740 void
1741 mono_identifier_unescape_info (MonoTypeNameParse* info);
1742 
1743 static void
unescape_each_type_argument(void * data,void * user_data)1744 unescape_each_type_argument(void* data, void* user_data)
1745 {
1746 	MonoTypeNameParse* info = (MonoTypeNameParse*)data;
1747 	mono_identifier_unescape_info (info);
1748 }
1749 
1750 static void
unescape_each_nested_name(void * data,void * user_data)1751 unescape_each_nested_name (void* data, void* user_data)
1752 {
1753 	char* nested_name = (char*) data;
1754 	mono_identifier_unescape_type_name_chars(nested_name);
1755 }
1756 
1757 /**
1758  * mono_identifier_unescape_info:
1759  *
1760  * \param info a parsed display form of an (optionally assembly qualified) full type name.
1761  *
1762  * Destructively updates the info by unescaping the identifiers that
1763  * comprise the type namespace, name, nested types (if any) and
1764  * generic type arguments (if any).
1765  *
1766  * The resulting info has the names in internal form.
1767  *
1768  */
1769 void
mono_identifier_unescape_info(MonoTypeNameParse * info)1770 mono_identifier_unescape_info (MonoTypeNameParse *info)
1771 {
1772 	if (!info)
1773 		return;
1774 	mono_identifier_unescape_type_name_chars(info->name_space);
1775 	mono_identifier_unescape_type_name_chars(info->name);
1776 	// but don't escape info->assembly
1777 	if (info->type_arguments)
1778 		g_ptr_array_foreach(info->type_arguments, &unescape_each_type_argument, NULL);
1779 	if (info->nested)
1780 		g_list_foreach(info->nested, &unescape_each_nested_name, NULL);
1781 }
1782 
1783 /**
1784  * mono_reflection_parse_type:
1785  */
1786 int
mono_reflection_parse_type(char * name,MonoTypeNameParse * info)1787 mono_reflection_parse_type (char *name, MonoTypeNameParse *info)
1788 {
1789 	MonoError error;
1790 	gboolean result = mono_reflection_parse_type_checked (name, info, &error);
1791 	mono_error_cleanup (&error);
1792 	return result ? 1 : 0;
1793 }
1794 
1795 /**
1796  * mono_reflection_parse_type_checked:
1797  * \param name the string to parse
1798  * \param info the parsed name components
1799  * \param error set on error
1800  *
1801  * Parse the given \p name and write the results to \p info, setting \p error
1802  * on error.  The string \p name is modified in place and \p info points into
1803  * its memory and into allocated memory.
1804  *
1805  * \returns TRUE if parsing succeeded, otherwise returns FALSE and sets \p error.
1806  *
1807  */
1808 gboolean
mono_reflection_parse_type_checked(char * name,MonoTypeNameParse * info,MonoError * error)1809 mono_reflection_parse_type_checked (char *name, MonoTypeNameParse *info, MonoError *error)
1810 {
1811 	error_init (error);
1812 	int ok = _mono_reflection_parse_type (name, NULL, FALSE, info);
1813 	if (ok) {
1814 		mono_identifier_unescape_info (info);
1815 	} else {
1816 		mono_error_set_argument (error, "typeName", "failed parse: %s", name);
1817 	}
1818 	return (ok != 0);
1819 }
1820 
1821 static MonoType*
_mono_reflection_get_type_from_info(MonoTypeNameParse * info,MonoImage * image,gboolean ignorecase,MonoError * error)1822 _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, MonoError *error)
1823 {
1824 	gboolean type_resolve = FALSE;
1825 	MonoType *type;
1826 	MonoImage *rootimage = image;
1827 
1828 	error_init (error);
1829 
1830 	if (info->assembly.name) {
1831 		MonoAssembly *assembly = mono_assembly_loaded (&info->assembly);
1832 		if (!assembly && image && image->assembly && mono_assembly_names_equal (&info->assembly, &image->assembly->aname))
1833 			/*
1834 			 * This could happen in the AOT compiler case when the search hook is not
1835 			 * installed.
1836 			 */
1837 			assembly = image->assembly;
1838 		if (!assembly) {
1839 			/* then we must load the assembly ourselve - see #60439 */
1840 			assembly = mono_assembly_load (&info->assembly, image->assembly->basedir, NULL);
1841 			if (!assembly)
1842 				return NULL;
1843 		}
1844 		image = assembly->image;
1845 	} else if (!image) {
1846 		image = mono_defaults.corlib;
1847 	}
1848 
1849 	type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1850 	if (type == NULL && !info->assembly.name && image != mono_defaults.corlib) {
1851 		/* ignore the error and try again */
1852 		mono_error_cleanup (error);
1853 		error_init (error);
1854 		image = mono_defaults.corlib;
1855 		type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1856 	}
1857 
1858 	return type;
1859 }
1860 
1861 /**
1862  * mono_reflection_get_type_internal:
1863  *
1864  * Returns: may return NULL on success, sets error on failure.
1865  */
1866 static MonoType*
mono_reflection_get_type_internal(MonoImage * rootimage,MonoImage * image,MonoTypeNameParse * info,gboolean ignorecase,MonoError * error)1867 mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1868 {
1869 	HANDLE_FUNCTION_ENTER ();
1870 	MonoClass *klass;
1871 	GList *mod;
1872 	int modval;
1873 	gboolean bounded = FALSE;
1874 	MonoType* type = NULL;
1875 
1876 	error_init (error);
1877 	if (!image)
1878 		image = mono_defaults.corlib;
1879 
1880 	if (!rootimage)
1881 		rootimage = mono_defaults.corlib;
1882 
1883 	if (ignorecase)
1884 		klass = mono_class_from_name_case_checked (image, info->name_space, info->name, error);
1885 	else
1886 		klass = mono_class_from_name_checked (image, info->name_space, info->name, error);
1887 
1888 	if (!klass)
1889 		goto leave;
1890 
1891 	for (mod = info->nested; mod; mod = mod->next) {
1892 		gpointer iter = NULL;
1893 		MonoClass *parent;
1894 
1895 		parent = klass;
1896 		mono_class_init (parent);
1897 
1898 		while ((klass = mono_class_get_nested_types (parent, &iter))) {
1899 			char *lastp;
1900 			char *nested_name, *nested_nspace;
1901 			gboolean match = TRUE;
1902 
1903 			lastp = strrchr ((const char *)mod->data, '.');
1904 			if (lastp) {
1905 				/* Nested classes can have namespaces */
1906 				int nspace_len;
1907 
1908 				nested_name = g_strdup (lastp + 1);
1909 				nspace_len = lastp - (char*)mod->data;
1910 				nested_nspace = (char *)g_malloc (nspace_len + 1);
1911 				memcpy (nested_nspace, mod->data, nspace_len);
1912 				nested_nspace [nspace_len] = '\0';
1913 
1914 			} else {
1915 				nested_name = (char *)mod->data;
1916 				nested_nspace = NULL;
1917 			}
1918 
1919 			if (nested_nspace) {
1920 				if (ignorecase) {
1921 					if (!(klass->name_space && mono_utf8_strcasecmp (klass->name_space, nested_nspace) == 0))
1922 						match = FALSE;
1923 				} else {
1924 					if (!(klass->name_space && strcmp (klass->name_space, nested_nspace) == 0))
1925 						match = FALSE;
1926 				}
1927 			}
1928 			if (match) {
1929 				if (ignorecase) {
1930 					if (mono_utf8_strcasecmp (klass->name, nested_name) != 0)
1931 						match = FALSE;
1932 				} else {
1933 					if (strcmp (klass->name, nested_name) != 0)
1934 						match = FALSE;
1935 				}
1936 			}
1937 			if (lastp) {
1938 				g_free (nested_name);
1939 				g_free (nested_nspace);
1940 			}
1941 			if (match)
1942 				break;
1943 		}
1944 
1945 		if (!klass)
1946 			break;
1947 	}
1948 	if (!klass)
1949 		goto leave;
1950 
1951 	if (info->type_arguments) {
1952 		MonoType **type_args = g_new0 (MonoType *, info->type_arguments->len);
1953 		MonoReflectionTypeHandle the_type;
1954 		MonoType *instance;
1955 		int i;
1956 
1957 		for (i = 0; i < info->type_arguments->len; i++) {
1958 			MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
1959 
1960 			type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, error);
1961 			if (!type_args [i]) {
1962 				g_free (type_args);
1963 				goto leave;
1964 			}
1965 		}
1966 
1967 		the_type = mono_type_get_object_handle (mono_domain_get (), &klass->byval_arg, error);
1968 		if (!is_ok (error) || MONO_HANDLE_IS_NULL (the_type))
1969 			goto leave;
1970 
1971 		instance = mono_reflection_bind_generic_parameters (
1972 			the_type, info->type_arguments->len, type_args, error);
1973 
1974 		g_free (type_args);
1975 		if (!instance)
1976 			goto leave;
1977 
1978 		klass = mono_class_from_mono_type (instance);
1979 	}
1980 
1981 	for (mod = info->modifiers; mod; mod = mod->next) {
1982 		modval = GPOINTER_TO_UINT (mod->data);
1983 		if (!modval) { /* byref: must be last modifier */
1984 			type = &klass->this_arg;
1985 			goto leave;
1986 		} else if (modval == -1) {
1987 			klass = mono_ptr_class_get (&klass->byval_arg);
1988 		} else if (modval == -2) {
1989 			bounded = TRUE;
1990 		} else { /* array rank */
1991 			klass = mono_bounded_array_class_get (klass, modval, bounded);
1992 		}
1993 	}
1994 
1995 	type = &klass->byval_arg;
1996 
1997 leave:
1998 	HANDLE_FUNCTION_RETURN_VAL (type);
1999 }
2000 
2001 /**
2002  * mono_reflection_get_type:
2003  * \param image a metadata context
2004  * \param info type description structure
2005  * \param ignorecase flag for case-insensitive string compares
2006  * \param type_resolve whenever type resolve was already tried
2007  *
2008  * Build a MonoType from the type description in \p info.
2009  *
2010  */
2011 MonoType*
mono_reflection_get_type(MonoImage * image,MonoTypeNameParse * info,gboolean ignorecase,gboolean * type_resolve)2012 mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve) {
2013 	MonoError error;
2014 	MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, type_resolve, &error);
2015 	mono_error_cleanup (&error);
2016 	return result;
2017 }
2018 
2019 /**
2020  * mono_reflection_get_type_checked:
2021  * \param rootimage the image of the currently active managed caller
2022  * \param image a metadata context
2023  * \param info type description structure
2024  * \param ignorecase flag for case-insensitive string compares
2025  * \param type_resolve whenever type resolve was already tried
2026  * \param error set on error.
2027  * Build a \c MonoType from the type description in \p info. On failure returns NULL and sets \p error.
2028  */
2029 MonoType*
mono_reflection_get_type_checked(MonoImage * rootimage,MonoImage * image,MonoTypeNameParse * info,gboolean ignorecase,gboolean * type_resolve,MonoError * error)2030 mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) {
2031 	error_init (error);
2032 	return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, type_resolve, error);
2033 }
2034 
2035 
2036 static MonoType*
module_builder_array_get_type(MonoArrayHandle module_builders,int i,MonoImage * rootimage,MonoTypeNameParse * info,gboolean ignorecase,MonoError * error)2037 module_builder_array_get_type (MonoArrayHandle module_builders, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2038 {
2039 	HANDLE_FUNCTION_ENTER ();
2040 	error_init (error);
2041 	MonoType *type = NULL;
2042 	MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
2043 	MONO_HANDLE_ARRAY_GETREF (mb, module_builders, i);
2044 	MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image);
2045 	type = mono_reflection_get_type_internal (rootimage, &dynamic_image->image, info, ignorecase, error);
2046 	HANDLE_FUNCTION_RETURN_VAL (type);
2047 }
2048 
2049 static MonoType*
module_array_get_type(MonoArrayHandle modules,int i,MonoImage * rootimage,MonoTypeNameParse * info,gboolean ignorecase,MonoError * error)2050 module_array_get_type (MonoArrayHandle modules, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2051 {
2052 	HANDLE_FUNCTION_ENTER ();
2053 	error_init (error);
2054 	MonoType *type = NULL;
2055 	MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
2056 	MONO_HANDLE_ARRAY_GETREF (mod, modules, i);
2057 	MonoImage *image = MONO_HANDLE_GETVAL (mod, image);
2058 	type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
2059 	HANDLE_FUNCTION_RETURN_VAL (type);
2060 }
2061 
2062 static MonoType*
mono_reflection_get_type_internal_dynamic(MonoImage * rootimage,MonoAssembly * assembly,MonoTypeNameParse * info,gboolean ignorecase,MonoError * error)2063 mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2064 {
2065 	HANDLE_FUNCTION_ENTER ();
2066 	MonoType *type = NULL;
2067 	int i;
2068 
2069 	error_init (error);
2070 	g_assert (assembly_is_dynamic (assembly));
2071 	MonoReflectionAssemblyBuilderHandle abuilder = MONO_HANDLE_CAST (MonoReflectionAssemblyBuilder, mono_assembly_get_object_handle (((MonoDynamicAssembly*)assembly)->domain, assembly, error));
2072 	goto_if_nok (error, leave);
2073 
2074 	/* Enumerate all modules */
2075 
2076 	MonoArrayHandle modules = MONO_HANDLE_NEW (MonoArray, NULL);
2077 	MONO_HANDLE_GET (modules, abuilder, modules);
2078 	if (!MONO_HANDLE_IS_NULL (modules)) {
2079 		int n = mono_array_handle_length (modules);
2080 		for (i = 0; i < n; ++i) {
2081 			type = module_builder_array_get_type (modules, i, rootimage, info, ignorecase, error);
2082 			if (type)
2083 				break;
2084 			goto_if_nok (error, leave);
2085 		}
2086 	}
2087 
2088 	MonoArrayHandle loaded_modules = MONO_HANDLE_NEW (MonoArray, NULL);
2089 	MONO_HANDLE_GET (loaded_modules, abuilder, loaded_modules);
2090 	if (!type && !MONO_HANDLE_IS_NULL(loaded_modules)) {
2091 		int n = mono_array_handle_length (loaded_modules);
2092 		for (i = 0; i < n; ++i) {
2093 			type = module_array_get_type (loaded_modules, i, rootimage, info, ignorecase, error);
2094 			if (type)
2095 				break;
2096 			goto_if_nok (error, leave);
2097 		}
2098 	}
2099 
2100 leave:
2101 	HANDLE_FUNCTION_RETURN_VAL (type);
2102 }
2103 
2104 MonoType*
mono_reflection_get_type_with_rootimage(MonoImage * rootimage,MonoImage * image,MonoTypeNameParse * info,gboolean ignorecase,gboolean * type_resolve,MonoError * error)2105 mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error)
2106 {
2107 	MonoType *type;
2108 	MonoReflectionAssembly *assembly;
2109 	GString *fullName;
2110 	GList *mod;
2111 
2112 	error_init (error);
2113 
2114 	if (image && image_is_dynamic (image))
2115 		type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, error);
2116 	else {
2117 		type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
2118 	}
2119 	return_val_if_nok (error, NULL);
2120 
2121 	if (type)
2122 		return type;
2123 	if (!mono_domain_has_type_resolve (mono_domain_get ()))
2124 		return NULL;
2125 
2126 	if (type_resolve) {
2127 		if (*type_resolve)
2128 			return NULL;
2129 		else
2130 			*type_resolve = TRUE;
2131 	}
2132 
2133 	/* Reconstruct the type name */
2134 	fullName = g_string_new ("");
2135 	if (info->name_space && (info->name_space [0] != '\0'))
2136 		g_string_printf (fullName, "%s.%s", info->name_space, info->name);
2137 	else
2138 		g_string_printf (fullName, "%s", info->name);
2139 	for (mod = info->nested; mod; mod = mod->next)
2140 		g_string_append_printf (fullName, "+%s", (char*)mod->data);
2141 
2142 	assembly = mono_domain_try_type_resolve_checked ( mono_domain_get (), fullName->str, NULL, error);
2143 	if (!is_ok (error)) {
2144 		g_string_free (fullName, TRUE);
2145 		return NULL;
2146 	}
2147 
2148 	if (assembly) {
2149 		if (assembly_is_dynamic (assembly->assembly))
2150 			type = mono_reflection_get_type_internal_dynamic (rootimage, assembly->assembly,
2151 									  info, ignorecase, error);
2152 		else
2153 			type = mono_reflection_get_type_internal (rootimage, assembly->assembly->image,
2154 								  info, ignorecase, error);
2155 	}
2156 	g_string_free (fullName, TRUE);
2157 	return_val_if_nok (error, NULL);
2158 	return type;
2159 }
2160 
2161 /**
2162  * mono_reflection_free_type_info:
2163  */
2164 void
mono_reflection_free_type_info(MonoTypeNameParse * info)2165 mono_reflection_free_type_info (MonoTypeNameParse *info)
2166 {
2167 	g_list_free (info->modifiers);
2168 	g_list_free (info->nested);
2169 
2170 	if (info->type_arguments) {
2171 		int i;
2172 
2173 		for (i = 0; i < info->type_arguments->len; i++) {
2174 			MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
2175 
2176 			mono_reflection_free_type_info (subinfo);
2177 			/*We free the subinfo since it is allocated by _mono_reflection_parse_type*/
2178 			g_free (subinfo);
2179 		}
2180 
2181 		g_ptr_array_free (info->type_arguments, TRUE);
2182 	}
2183 }
2184 
2185 /**
2186  * mono_reflection_type_from_name:
2187  * \param name type name.
2188  * \param image a metadata context (can be NULL).
2189  *
2190  * Retrieves a \c MonoType from its \p name. If the name is not fully qualified,
2191  * it defaults to get the type from \p image or, if \p image is NULL or loading
2192  * from it fails, uses corlib.
2193  *
2194  */
2195 MonoType*
mono_reflection_type_from_name(char * name,MonoImage * image)2196 mono_reflection_type_from_name (char *name, MonoImage *image)
2197 {
2198 	MonoError error;
2199 	MonoType  *result = mono_reflection_type_from_name_checked (name, image, &error);
2200 	mono_error_cleanup (&error);
2201 	return result;
2202 }
2203 
2204 /**
2205  * mono_reflection_type_from_name_checked:
2206  * \param name type name.
2207  * \param image a metadata context (can be NULL).
2208  * \param error set on errror.
2209  * Retrieves a MonoType from its \p name. If the name is not fully qualified,
2210  * it defaults to get the type from \p image or, if \p image is NULL or loading
2211  * from it fails, uses corlib.  On failure returns NULL and sets \p error.
2212  */
2213 MonoType*
mono_reflection_type_from_name_checked(char * name,MonoImage * image,MonoError * error)2214 mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError *error)
2215 {
2216 	MonoType *type = NULL;
2217 	MonoTypeNameParse info;
2218 	char *tmp;
2219 
2220 	error_init (error);
2221 	/* Make a copy since parse_type modifies its argument */
2222 	tmp = g_strdup (name);
2223 
2224 	/*g_print ("requested type %s\n", str);*/
2225 	MonoError parse_error;
2226 	if (!mono_reflection_parse_type_checked (tmp, &info, &parse_error)) {
2227 		mono_error_cleanup (&parse_error);
2228 		goto leave;
2229 	}
2230 	type = _mono_reflection_get_type_from_info (&info, image, FALSE, error);
2231 leave:
2232 	g_free (tmp);
2233 	mono_reflection_free_type_info (&info);
2234 	return type;
2235 }
2236 
2237 /**
2238  * mono_reflection_get_token:
2239  * \returns the metadata token of \p obj which should be an object
2240  * representing a metadata element.
2241  */
2242 guint32
mono_reflection_get_token(MonoObject * obj_raw)2243 mono_reflection_get_token (MonoObject *obj_raw)
2244 {
2245 	HANDLE_FUNCTION_ENTER ();
2246 	MONO_HANDLE_DCL (MonoObject, obj);
2247 	MonoError error;
2248 	guint32 result = mono_reflection_get_token_checked (obj, &error);
2249 	mono_error_assert_ok (&error);
2250 	HANDLE_FUNCTION_RETURN_VAL (result);
2251 }
2252 
2253 /**
2254  * mono_reflection_get_token_checked:
2255  * \param obj the object
2256  * \param error set on error
2257  * \returns the metadata token of \p obj which should be an object
2258  * representing a metadata element.  On failure sets \p error.
2259  */
2260 guint32
mono_reflection_get_token_checked(MonoObjectHandle obj,MonoError * error)2261 mono_reflection_get_token_checked (MonoObjectHandle obj, MonoError *error)
2262 {
2263 	guint32 token = 0;
2264 
2265 	error_init (error);
2266 
2267 	MonoClass *klass = mono_handle_class (obj);
2268 
2269 	if (strcmp (klass->name, "MethodBuilder") == 0) {
2270 		MonoReflectionMethodBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionMethodBuilder, obj);
2271 
2272 		token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF;
2273 	} else if (strcmp (klass->name, "ConstructorBuilder") == 0) {
2274 		MonoReflectionCtorBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionCtorBuilder, obj);
2275 
2276 		token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF;
2277 	} else if (strcmp (klass->name, "FieldBuilder") == 0) {
2278 		MonoReflectionFieldBuilderHandle fb = MONO_HANDLE_CAST (MonoReflectionFieldBuilder, obj);
2279 
2280 		token = MONO_HANDLE_GETVAL (fb, table_idx) | MONO_TOKEN_FIELD_DEF;
2281 	} else if (strcmp (klass->name, "TypeBuilder") == 0) {
2282 		MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, obj);
2283 		token = MONO_HANDLE_GETVAL (tb, table_idx) | MONO_TOKEN_TYPE_DEF;
2284 	} else if (strcmp (klass->name, "RuntimeType") == 0) {
2285 		MonoType *type = mono_reflection_type_handle_mono_type (MONO_HANDLE_CAST (MonoReflectionType, obj), error);
2286 		return_val_if_nok (error, 0);
2287 		MonoClass *mc = mono_class_from_mono_type (type);
2288 		if (!mono_class_init (mc)) {
2289 			mono_error_set_for_class_failure (error, mc);
2290 			return 0;
2291 		}
2292 
2293 		token = mc->type_token;
2294 	} else if (strcmp (klass->name, "MonoCMethod") == 0 ||
2295 			   strcmp (klass->name, "MonoMethod") == 0) {
2296 		MonoReflectionMethodHandle m = MONO_HANDLE_CAST (MonoReflectionMethod, obj);
2297 		MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
2298 		if (method->is_inflated) {
2299 			MonoMethodInflated *inflated = (MonoMethodInflated *) method;
2300 			return inflated->declaring->token;
2301 		} else {
2302 			token = method->token;
2303 		}
2304 	} else if (strcmp (klass->name, "MonoField") == 0) {
2305 		MonoReflectionFieldHandle f = MONO_HANDLE_CAST (MonoReflectionField, obj);
2306 
2307 		token = mono_class_get_field_token (MONO_HANDLE_GETVAL (f, field));
2308 	} else if (strcmp (klass->name, "MonoProperty") == 0) {
2309 		MonoReflectionPropertyHandle p = MONO_HANDLE_CAST (MonoReflectionProperty, obj);
2310 
2311 		token = mono_class_get_property_token (MONO_HANDLE_GETVAL (p, property));
2312 	} else if (strcmp (klass->name, "MonoEvent") == 0) {
2313 		MonoReflectionMonoEventHandle p = MONO_HANDLE_CAST (MonoReflectionMonoEvent, obj);
2314 
2315 		token = mono_class_get_event_token (MONO_HANDLE_GETVAL (p, event));
2316 	} else if (strcmp (klass->name, "ParameterInfo") == 0 || strcmp (klass->name, "MonoParameterInfo") == 0) {
2317 		MonoReflectionParameterHandle p = MONO_HANDLE_CAST (MonoReflectionParameter, obj);
2318 		MonoObjectHandle member_impl = MONO_HANDLE_NEW (MonoObject, NULL);
2319 		MONO_HANDLE_GET (member_impl, p, MemberImpl);
2320 		MonoClass *member_class = mono_handle_class (member_impl);
2321 		g_assert (mono_class_is_reflection_method_or_constructor (member_class));
2322 		MonoMethod *method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member_impl), method);
2323 
2324 		token = mono_method_get_param_token (method, MONO_HANDLE_GETVAL (p, PositionImpl));
2325 	} else if (strcmp (klass->name, "Module") == 0 || strcmp (klass->name, "MonoModule") == 0 || strcmp (klass->name, "ModuleBuilder") == 0) {
2326 		MonoReflectionModuleHandle m = MONO_HANDLE_CAST (MonoReflectionModule, obj);
2327 
2328 		token = MONO_HANDLE_GETVAL (m, token);
2329 	} else if (strcmp (klass->name, "Assembly") == 0 || strcmp (klass->name, "MonoAssembly") == 0) {
2330 		token = mono_metadata_make_token (MONO_TABLE_ASSEMBLY, 1);
2331 	} else {
2332 		mono_error_set_not_implemented (error, "MetadataToken is not supported for type '%s.%s'",
2333 						klass->name_space, klass->name);
2334 		return 0;
2335 	}
2336 
2337 	return token;
2338 }
2339 
2340 
2341 gboolean
mono_reflection_is_usertype(MonoReflectionTypeHandle ref)2342 mono_reflection_is_usertype (MonoReflectionTypeHandle ref)
2343 {
2344 	MonoClass *klass = mono_handle_class (ref);
2345 	return klass->image != mono_defaults.corlib || strcmp ("TypeDelegator", klass->name) == 0;
2346 }
2347 
2348 /**
2349  * mono_reflection_bind_generic_parameters:
2350  * \param type a managed type object (which should be some kind of generic (instance? definition?))
2351  * \param type_args the number of type arguments to bind
2352  * \param types array of type arguments
2353  * \param error set on error
2354  * Given a managed type object for a generic type instance, binds each of its arguments to the specified types.
2355  * \returns the \c MonoType* for the resulting type instantiation.  On failure returns NULL and sets \p error.
2356  */
2357 MonoType*
mono_reflection_bind_generic_parameters(MonoReflectionTypeHandle reftype,int type_argc,MonoType ** types,MonoError * error)2358 mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int type_argc, MonoType **types, MonoError *error)
2359 {
2360 	gboolean is_dynamic = FALSE;
2361 	MonoClass *geninst;
2362 
2363 	error_init (error);
2364 
2365 	mono_loader_lock ();
2366 
2367 	MonoClass *klass = mono_handle_class (reftype);
2368 	if (mono_is_sre_type_builder (klass)) {
2369 		is_dynamic = TRUE;
2370 	} else if (mono_is_sre_generic_instance (klass)) {
2371 		/* Does this ever make sense?  what does instantiating a generic instance even mean? */
2372 		g_assert_not_reached ();
2373 		MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype);
2374 		MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type);
2375 
2376 		if (mono_is_sre_type_builder (mono_handle_class (gtd)))
2377 			is_dynamic = TRUE;
2378 	}
2379 
2380 	MonoType *t = mono_reflection_type_handle_mono_type (reftype, error);
2381 	if (!is_ok (error)) {
2382 		mono_loader_unlock ();
2383 		return NULL;
2384 	}
2385 
2386 	klass = mono_class_from_mono_type (t);
2387 	if (!mono_class_is_gtd (klass)) {
2388 		mono_loader_unlock ();
2389 		mono_error_set_type_load_class (error, klass, "Cannot bind generic parameters of a non-generic type");
2390 		return NULL;
2391 	}
2392 
2393 	guint gtd_type_argc = mono_class_get_generic_container (klass)->type_argc;
2394 	if (gtd_type_argc != type_argc) {
2395 		mono_loader_unlock ();
2396 		mono_error_set_argument (error, "types", "The generic type definition needs %d type arguments, but was instantiated with %d ", gtd_type_argc, type_argc);
2397 		return NULL;
2398 	}
2399 
2400 
2401 	if (klass->wastypebuilder)
2402 		is_dynamic = TRUE;
2403 
2404 	mono_loader_unlock ();
2405 
2406 	geninst = mono_class_bind_generic_parameters (klass, type_argc, types, is_dynamic);
2407 
2408 	return &geninst->byval_arg;
2409 }
2410 
2411 MonoClass*
mono_class_bind_generic_parameters(MonoClass * klass,int type_argc,MonoType ** types,gboolean is_dynamic)2412 mono_class_bind_generic_parameters (MonoClass *klass, int type_argc, MonoType **types, gboolean is_dynamic)
2413 {
2414 	MonoGenericClass *gclass;
2415 	MonoGenericInst *inst;
2416 
2417 	g_assert (mono_class_is_gtd (klass));
2418 
2419 	inst = mono_metadata_get_generic_inst (type_argc, types);
2420 	gclass = mono_metadata_lookup_generic_class (klass, inst, is_dynamic);
2421 
2422 	return mono_generic_class_get_class (gclass);
2423 }
2424 
2425 static MonoGenericInst*
generic_inst_from_type_array_handle(MonoArrayHandle types,MonoError * error)2426 generic_inst_from_type_array_handle (MonoArrayHandle types, MonoError *error)
2427 {
2428 	HANDLE_FUNCTION_ENTER ();
2429 	error_init (error);
2430 	MonoGenericInst *ginst = NULL;
2431 	int count = mono_array_handle_length (types);
2432 	MonoType **type_argv = g_new0 (MonoType *, count);
2433 	MonoReflectionTypeHandle garg = MONO_HANDLE_NEW (MonoReflectionType, NULL);
2434 	for (int i = 0; i < count; i++) {
2435 		MONO_HANDLE_ARRAY_GETREF (garg, types, i);
2436 		type_argv [i] = mono_reflection_type_handle_mono_type (garg, error);
2437 		goto_if_nok (error, leave);
2438 
2439 	}
2440 	ginst = mono_metadata_get_generic_inst (count, type_argv);
2441 leave:
2442 	g_free (type_argv);
2443 	HANDLE_FUNCTION_RETURN_VAL (ginst);
2444 }
2445 
2446 static MonoMethod*
reflection_bind_generic_method_parameters(MonoMethod * method,MonoArrayHandle types,MonoError * error)2447 reflection_bind_generic_method_parameters (MonoMethod *method, MonoArrayHandle types, MonoError *error)
2448 {
2449 	MonoClass *klass;
2450 	MonoMethod *inflated;
2451 	MonoGenericContext tmp_context;
2452 
2453 	error_init (error);
2454 
2455 	klass = method->klass;
2456 
2457 	if (method->is_inflated)
2458 		method = ((MonoMethodInflated *) method)->declaring;
2459 
2460 	int count = mono_method_signature (method)->generic_param_count;
2461 	if (count != mono_array_handle_length (types)) {
2462 		mono_error_set_argument (error, "typeArguments", "Incorrect number of generic arguments");
2463 		return NULL;
2464 	}
2465 
2466 	MonoGenericInst *ginst = generic_inst_from_type_array_handle (types, error);
2467 	return_val_if_nok (error, NULL);
2468 
2469 	tmp_context.class_inst = mono_class_is_ginst (klass) ? mono_class_get_generic_class (klass)->context.class_inst : NULL;
2470 	tmp_context.method_inst = ginst;
2471 
2472 	inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, error);
2473 	mono_error_assert_ok (error);
2474 
2475 	if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
2476 		mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
2477 		return NULL;
2478 	}
2479 
2480 	return inflated;
2481 }
2482 
2483 MonoReflectionMethodHandle
ves_icall_MonoMethod_MakeGenericMethod_impl(MonoReflectionMethodHandle rmethod,MonoArrayHandle types,MonoError * error)2484 ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethodHandle rmethod, MonoArrayHandle types, MonoError *error)
2485 {
2486 	error_init (error);
2487 	g_assert (0 != strcmp (mono_handle_class (rmethod)->name, "MethodBuilder"));
2488 
2489 	MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
2490 	MonoMethod *imethod = reflection_bind_generic_method_parameters (method, types, error);
2491 	return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
2492 
2493 	/*FIXME but I think this is no longer necessary*/
2494 	if (image_is_dynamic (method->klass->image)) {
2495 		MonoDynamicImage *image = (MonoDynamicImage*)method->klass->image;
2496 		/*
2497 		 * This table maps metadata structures representing inflated methods/fields
2498 		 * to the reflection objects representing their generic definitions.
2499 		 */
2500 		mono_image_lock ((MonoImage*)image);
2501 		mono_g_hash_table_insert (image->generic_def_objects, imethod, MONO_HANDLE_RAW (rmethod));
2502 		mono_image_unlock ((MonoImage*)image);
2503 	}
2504 
2505 	return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (rmethod), imethod, NULL, error);
2506 }
2507 
2508 
2509 /* SECURITY_ACTION_* are defined in mono/metadata/tabledefs.h */
2510 const static guint32 declsec_flags_map[] = {
2511 	0x00000000,					/* empty */
2512 	MONO_DECLSEC_FLAG_REQUEST,			/* SECURITY_ACTION_REQUEST			(x01) */
2513 	MONO_DECLSEC_FLAG_DEMAND,			/* SECURITY_ACTION_DEMAND			(x02) */
2514 	MONO_DECLSEC_FLAG_ASSERT,			/* SECURITY_ACTION_ASSERT			(x03) */
2515 	MONO_DECLSEC_FLAG_DENY,				/* SECURITY_ACTION_DENY				(x04) */
2516 	MONO_DECLSEC_FLAG_PERMITONLY,			/* SECURITY_ACTION_PERMITONLY			(x05) */
2517 	MONO_DECLSEC_FLAG_LINKDEMAND,			/* SECURITY_ACTION_LINKDEMAND			(x06) */
2518 	MONO_DECLSEC_FLAG_INHERITANCEDEMAND,		/* SECURITY_ACTION_INHERITANCEDEMAND		(x07) */
2519 	MONO_DECLSEC_FLAG_REQUEST_MINIMUM,		/* SECURITY_ACTION_REQUEST_MINIMUM		(x08) */
2520 	MONO_DECLSEC_FLAG_REQUEST_OPTIONAL,		/* SECURITY_ACTION_REQUEST_OPTIONAL		(x09) */
2521 	MONO_DECLSEC_FLAG_REQUEST_REFUSE,		/* SECURITY_ACTION_REQUEST_REFUSE		(x0A) */
2522 	MONO_DECLSEC_FLAG_PREJIT_GRANT,			/* SECURITY_ACTION_PREJIT_GRANT			(x0B) */
2523 	MONO_DECLSEC_FLAG_PREJIT_DENY,			/* SECURITY_ACTION_PREJIT_DENY			(x0C) */
2524 	MONO_DECLSEC_FLAG_NONCAS_DEMAND,		/* SECURITY_ACTION_NONCAS_DEMAND		(x0D) */
2525 	MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND,		/* SECURITY_ACTION_NONCAS_LINKDEMAND		(x0E) */
2526 	MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND,	/* SECURITY_ACTION_NONCAS_INHERITANCEDEMAND	(x0F) */
2527 	MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE,		/* SECURITY_ACTION_LINKDEMAND_CHOICE		(x10) */
2528 	MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE,	/* SECURITY_ACTION_INHERITANCEDEMAND_CHOICE	(x11) */
2529 	MONO_DECLSEC_FLAG_DEMAND_CHOICE,		/* SECURITY_ACTION_DEMAND_CHOICE		(x12) */
2530 };
2531 
2532 /*
2533  * Returns flags that includes all available security action associated to the handle.
2534  * @token: metadata token (either for a class or a method)
2535  * @image: image where resides the metadata.
2536  */
2537 static guint32
mono_declsec_get_flags(MonoImage * image,guint32 token)2538 mono_declsec_get_flags (MonoImage *image, guint32 token)
2539 {
2540 	int index = mono_metadata_declsec_from_index (image, token);
2541 	MonoTableInfo *t = &image->tables [MONO_TABLE_DECLSECURITY];
2542 	guint32 result = 0;
2543 	guint32 action;
2544 	int i;
2545 
2546 	/* HasSecurity can be present for other, not specially encoded, attributes,
2547 	   e.g. SuppressUnmanagedCodeSecurityAttribute */
2548 	if (index < 0)
2549 		return 0;
2550 
2551 	for (i = index; i < t->rows; i++) {
2552 		guint32 cols [MONO_DECL_SECURITY_SIZE];
2553 
2554 		mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2555 		if (cols [MONO_DECL_SECURITY_PARENT] != token)
2556 			break;
2557 
2558 		action = cols [MONO_DECL_SECURITY_ACTION];
2559 		if ((action >= MONO_DECLSEC_ACTION_MIN) && (action <= MONO_DECLSEC_ACTION_MAX)) {
2560 			result |= declsec_flags_map [action];
2561 		} else {
2562 			g_assert_not_reached ();
2563 		}
2564 	}
2565 	return result;
2566 }
2567 
2568 /**
2569  * mono_declsec_flags_from_method:
2570  * \param method The method for which we want the declarative security flags.
2571  * Get the security actions (in the form of flags) associated with the specified method.
2572  * To keep \c MonoMethod size down we do not cache the declarative security flags
2573  * (except for the stack modifiers which are kept in the MonoJitInfo structure)
2574  * \returns the declarative security flags for the method (only).
2575  */
2576 guint32
mono_declsec_flags_from_method(MonoMethod * method)2577 mono_declsec_flags_from_method (MonoMethod *method)
2578 {
2579 	if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2580 		/* FIXME: No cache (for the moment) */
2581 		guint32 idx = mono_method_get_index (method);
2582 		idx <<= MONO_HAS_DECL_SECURITY_BITS;
2583 		idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2584 		return mono_declsec_get_flags (method->klass->image, idx);
2585 	}
2586 	return 0;
2587 }
2588 
2589 /**
2590  * mono_declsec_flags_from_class:
2591  * \param klass The class for which we want the declarative security flags.
2592  * Get the security actions (in the form of flags) associated with the specified class.
2593  * We cache the flags inside the \c MonoClass structure as this will get
2594  * called very often (at least for each method).
2595  * \returns the declarative security flags for the class.
2596  */
2597 guint32
mono_declsec_flags_from_class(MonoClass * klass)2598 mono_declsec_flags_from_class (MonoClass *klass)
2599 {
2600 	if (mono_class_get_flags (klass) & TYPE_ATTRIBUTE_HAS_SECURITY) {
2601 		guint32 flags = mono_class_get_declsec_flags (klass);
2602 
2603 		if (!flags) {
2604 			guint32 idx;
2605 
2606 			idx = mono_metadata_token_index (klass->type_token);
2607 			idx <<= MONO_HAS_DECL_SECURITY_BITS;
2608 			idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2609 			flags = mono_declsec_get_flags (klass->image, idx);
2610 			/* we cache the flags on classes */
2611 			mono_class_set_declsec_flags (klass, flags);
2612 		}
2613 		return flags;
2614 	}
2615 	return 0;
2616 }
2617 
2618 /**
2619  * mono_declsec_flags_from_assembly:
2620  * \param assembly The assembly for which we want the declarative security flags.
2621  * Get the security actions (in the form of flags) associated with the specified assembly.
2622  * \returns the declarative security flags for the assembly.
2623  */
2624 guint32
mono_declsec_flags_from_assembly(MonoAssembly * assembly)2625 mono_declsec_flags_from_assembly (MonoAssembly *assembly)
2626 {
2627 	guint32 idx = 1; /* there is only one assembly */
2628 	idx <<= MONO_HAS_DECL_SECURITY_BITS;
2629 	idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2630 	return mono_declsec_get_flags (assembly->image, idx);
2631 }
2632 
2633 
2634 /*
2635  * Fill actions for the specific index (which may either be an encoded class token or
2636  * an encoded method token) from the metadata image.
2637  * Returns TRUE if some actions requiring code generation are present, FALSE otherwise.
2638  */
2639 static MonoBoolean
fill_actions_from_index(MonoImage * image,guint32 token,MonoDeclSecurityActions * actions,guint32 id_std,guint32 id_noncas,guint32 id_choice)2640 fill_actions_from_index (MonoImage *image, guint32 token, MonoDeclSecurityActions* actions,
2641 	guint32 id_std, guint32 id_noncas, guint32 id_choice)
2642 {
2643 	MonoBoolean result = FALSE;
2644 	MonoTableInfo *t;
2645 	guint32 cols [MONO_DECL_SECURITY_SIZE];
2646 	int index = mono_metadata_declsec_from_index (image, token);
2647 	int i;
2648 
2649 	t  = &image->tables [MONO_TABLE_DECLSECURITY];
2650 	for (i = index; i < t->rows; i++) {
2651 		mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2652 
2653 		if (cols [MONO_DECL_SECURITY_PARENT] != token)
2654 			return result;
2655 
2656 		/* if present only replace (class) permissions with method permissions */
2657 		/* if empty accept either class or method permissions */
2658 		if (cols [MONO_DECL_SECURITY_ACTION] == id_std) {
2659 			if (!actions->demand.blob) {
2660 				const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2661 				actions->demand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2662 				actions->demand.blob = (char*) (blob + 2);
2663 				actions->demand.size = mono_metadata_decode_blob_size (blob, &blob);
2664 				result = TRUE;
2665 			}
2666 		} else if (cols [MONO_DECL_SECURITY_ACTION] == id_noncas) {
2667 			if (!actions->noncasdemand.blob) {
2668 				const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2669 				actions->noncasdemand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2670 				actions->noncasdemand.blob = (char*) (blob + 2);
2671 				actions->noncasdemand.size = mono_metadata_decode_blob_size (blob, &blob);
2672 				result = TRUE;
2673 			}
2674 		} else if (cols [MONO_DECL_SECURITY_ACTION] == id_choice) {
2675 			if (!actions->demandchoice.blob) {
2676 				const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2677 				actions->demandchoice.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2678 				actions->demandchoice.blob = (char*) (blob + 2);
2679 				actions->demandchoice.size = mono_metadata_decode_blob_size (blob, &blob);
2680 				result = TRUE;
2681 			}
2682 		}
2683 	}
2684 
2685 	return result;
2686 }
2687 
2688 static MonoBoolean
mono_declsec_get_class_demands_params(MonoClass * klass,MonoDeclSecurityActions * demands,guint32 id_std,guint32 id_noncas,guint32 id_choice)2689 mono_declsec_get_class_demands_params (MonoClass *klass, MonoDeclSecurityActions* demands,
2690 	guint32 id_std, guint32 id_noncas, guint32 id_choice)
2691 {
2692 	guint32 idx = mono_metadata_token_index (klass->type_token);
2693 	idx <<= MONO_HAS_DECL_SECURITY_BITS;
2694 	idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2695 	return fill_actions_from_index (klass->image, idx, demands, id_std, id_noncas, id_choice);
2696 }
2697 
2698 static MonoBoolean
mono_declsec_get_method_demands_params(MonoMethod * method,MonoDeclSecurityActions * demands,guint32 id_std,guint32 id_noncas,guint32 id_choice)2699 mono_declsec_get_method_demands_params (MonoMethod *method, MonoDeclSecurityActions* demands,
2700 	guint32 id_std, guint32 id_noncas, guint32 id_choice)
2701 {
2702 	guint32 idx = mono_method_get_index (method);
2703 	idx <<= MONO_HAS_DECL_SECURITY_BITS;
2704 	idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2705 	return fill_actions_from_index (method->klass->image, idx, demands, id_std, id_noncas, id_choice);
2706 }
2707 
2708 /**
2709  * mono_declsec_get_demands:
2710  * Collect all actions (that requires to generate code in mini) assigned for
2711  * the specified method.
2712  * Don't use the content of actions if the function return FALSE.
2713  */
2714 MonoBoolean
mono_declsec_get_demands(MonoMethod * method,MonoDeclSecurityActions * demands)2715 mono_declsec_get_demands (MonoMethod *method, MonoDeclSecurityActions* demands)
2716 {
2717 	guint32 mask = MONO_DECLSEC_FLAG_DEMAND | MONO_DECLSEC_FLAG_NONCAS_DEMAND |
2718 		MONO_DECLSEC_FLAG_DEMAND_CHOICE;
2719 	MonoBoolean result = FALSE;
2720 	guint32 flags;
2721 
2722 	/* quick exit if no declarative security is present in the metadata */
2723 	if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2724 		return FALSE;
2725 
2726 	/* we want the original as the wrapper is "free" of the security informations */
2727 	if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2728 		method = mono_marshal_method_from_wrapper (method);
2729 		if (!method)
2730 			return FALSE;
2731 	}
2732 
2733 	/* First we look for method-level attributes */
2734 	if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2735 		mono_class_init (method->klass);
2736 		memset (demands, 0, sizeof (MonoDeclSecurityActions));
2737 
2738 		result = mono_declsec_get_method_demands_params (method, demands,
2739 			SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2740 	}
2741 
2742 	/* Here we use (or create) the class declarative cache to look for demands */
2743 	flags = mono_declsec_flags_from_class (method->klass);
2744 	if (flags & mask) {
2745 		if (!result) {
2746 			mono_class_init (method->klass);
2747 			memset (demands, 0, sizeof (MonoDeclSecurityActions));
2748 		}
2749 		result |= mono_declsec_get_class_demands_params (method->klass, demands,
2750 			SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2751 	}
2752 
2753 	/* The boolean return value is used as a shortcut in case nothing needs to
2754 	   be generated (e.g. LinkDemand[Choice] and InheritanceDemand[Choice]) */
2755 	return result;
2756 }
2757 
2758 
2759 /**
2760  * mono_declsec_get_linkdemands:
2761  * Collect all Link actions: \c LinkDemand, \c NonCasLinkDemand and \c LinkDemandChoice (2.0).
2762  * Don't use the content of actions if the function return FALSE.
2763  */
2764 MonoBoolean
mono_declsec_get_linkdemands(MonoMethod * method,MonoDeclSecurityActions * klass,MonoDeclSecurityActions * cmethod)2765 mono_declsec_get_linkdemands (MonoMethod *method, MonoDeclSecurityActions* klass, MonoDeclSecurityActions *cmethod)
2766 {
2767 	MonoBoolean result = FALSE;
2768 	guint32 flags;
2769 
2770 	/* quick exit if no declarative security is present in the metadata */
2771 	if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2772 		return FALSE;
2773 
2774 	/* we want the original as the wrapper is "free" of the security informations */
2775 	if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2776 		method = mono_marshal_method_from_wrapper (method);
2777 		if (!method)
2778 			return FALSE;
2779 	}
2780 
2781 	/* results are independant - zeroize both */
2782 	memset (cmethod, 0, sizeof (MonoDeclSecurityActions));
2783 	memset (klass, 0, sizeof (MonoDeclSecurityActions));
2784 
2785 	/* First we look for method-level attributes */
2786 	if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2787 		mono_class_init (method->klass);
2788 
2789 		result = mono_declsec_get_method_demands_params (method, cmethod,
2790 			SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2791 	}
2792 
2793 	/* Here we use (or create) the class declarative cache to look for demands */
2794 	flags = mono_declsec_flags_from_class (method->klass);
2795 	if (flags & (MONO_DECLSEC_FLAG_LINKDEMAND | MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND | MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE)) {
2796 		mono_class_init (method->klass);
2797 
2798 		result |= mono_declsec_get_class_demands_params (method->klass, klass,
2799 			SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2800 	}
2801 
2802 	return result;
2803 }
2804 
2805 /**
2806  * mono_declsec_get_inheritdemands_class:
2807  * \param klass	The inherited class - this is the class that provides the security check (attributes)
2808  * \param demands
2809  * Collect all Inherit actions - \c InheritanceDemand, \c NonCasInheritanceDemand and \c InheritanceDemandChoice (2.0).
2810  * Don't use the content of actions if the function return FALSE.
2811  * \returns TRUE if inheritance demands (any kind) are present, FALSE otherwise.
2812  */
2813 MonoBoolean
mono_declsec_get_inheritdemands_class(MonoClass * klass,MonoDeclSecurityActions * demands)2814 mono_declsec_get_inheritdemands_class (MonoClass *klass, MonoDeclSecurityActions* demands)
2815 {
2816 	MonoBoolean result = FALSE;
2817 	guint32 flags;
2818 
2819 	/* quick exit if no declarative security is present in the metadata */
2820 	if (!klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2821 		return FALSE;
2822 
2823 	/* Here we use (or create) the class declarative cache to look for demands */
2824 	flags = mono_declsec_flags_from_class (klass);
2825 	if (flags & (MONO_DECLSEC_FLAG_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE)) {
2826 		mono_class_init (klass);
2827 		memset (demands, 0, sizeof (MonoDeclSecurityActions));
2828 
2829 		result |= mono_declsec_get_class_demands_params (klass, demands,
2830 			SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2831 	}
2832 
2833 	return result;
2834 }
2835 
2836 /**
2837  * mono_declsec_get_inheritdemands_method:
2838  * Collect all Inherit actions: \c InheritanceDemand, \c NonCasInheritanceDemand and \c InheritanceDemandChoice (2.0).
2839  * Don't use the content of actions if the function return FALSE.
2840  */
2841 MonoBoolean
mono_declsec_get_inheritdemands_method(MonoMethod * method,MonoDeclSecurityActions * demands)2842 mono_declsec_get_inheritdemands_method (MonoMethod *method, MonoDeclSecurityActions* demands)
2843 {
2844 	/* quick exit if no declarative security is present in the metadata */
2845 	if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2846 		return FALSE;
2847 
2848 	/* we want the original as the wrapper is "free" of the security informations */
2849 	if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2850 		method = mono_marshal_method_from_wrapper (method);
2851 		if (!method)
2852 			return FALSE;
2853 	}
2854 
2855 	if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2856 		mono_class_init (method->klass);
2857 		memset (demands, 0, sizeof (MonoDeclSecurityActions));
2858 
2859 		return mono_declsec_get_method_demands_params (method, demands,
2860 			SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2861 	}
2862 	return FALSE;
2863 }
2864 
2865 
2866 static MonoBoolean
get_declsec_action(MonoImage * image,guint32 token,guint32 action,MonoDeclSecurityEntry * entry)2867 get_declsec_action (MonoImage *image, guint32 token, guint32 action, MonoDeclSecurityEntry *entry)
2868 {
2869 	guint32 cols [MONO_DECL_SECURITY_SIZE];
2870 	MonoTableInfo *t;
2871 	int i;
2872 
2873 	int index = mono_metadata_declsec_from_index (image, token);
2874 	if (index == -1)
2875 		return FALSE;
2876 
2877 	t =  &image->tables [MONO_TABLE_DECLSECURITY];
2878 	for (i = index; i < t->rows; i++) {
2879 		mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2880 
2881 		/* shortcut - index are ordered */
2882 		if (token != cols [MONO_DECL_SECURITY_PARENT])
2883 			return FALSE;
2884 
2885 		if (cols [MONO_DECL_SECURITY_ACTION] == action) {
2886 			const char *metadata = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2887 			entry->blob = (char*) (metadata + 2);
2888 			entry->size = mono_metadata_decode_blob_size (metadata, &metadata);
2889 			return TRUE;
2890 		}
2891 	}
2892 
2893 	return FALSE;
2894 }
2895 
2896 MonoBoolean
mono_declsec_get_method_action(MonoMethod * method,guint32 action,MonoDeclSecurityEntry * entry)2897 mono_declsec_get_method_action (MonoMethod *method, guint32 action, MonoDeclSecurityEntry *entry)
2898 {
2899 	if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2900 		guint32 idx = mono_method_get_index (method);
2901 		idx <<= MONO_HAS_DECL_SECURITY_BITS;
2902 		idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2903 		return get_declsec_action (method->klass->image, idx, action, entry);
2904 	}
2905 	return FALSE;
2906 }
2907 
2908 /**
2909  * mono_declsec_get_class_action:
2910  */
2911 MonoBoolean
mono_declsec_get_class_action(MonoClass * klass,guint32 action,MonoDeclSecurityEntry * entry)2912 mono_declsec_get_class_action (MonoClass *klass, guint32 action, MonoDeclSecurityEntry *entry)
2913 {
2914 	/* use cache */
2915 	guint32 flags = mono_declsec_flags_from_class (klass);
2916 	if (declsec_flags_map [action] & flags) {
2917 		guint32 idx = mono_metadata_token_index (klass->type_token);
2918 		idx <<= MONO_HAS_DECL_SECURITY_BITS;
2919 		idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2920 		return get_declsec_action (klass->image, idx, action, entry);
2921 	}
2922 	return FALSE;
2923 }
2924 
2925 /**
2926  * mono_declsec_get_assembly_action:
2927  */
2928 MonoBoolean
mono_declsec_get_assembly_action(MonoAssembly * assembly,guint32 action,MonoDeclSecurityEntry * entry)2929 mono_declsec_get_assembly_action (MonoAssembly *assembly, guint32 action, MonoDeclSecurityEntry *entry)
2930 {
2931 	guint32 idx = 1; /* there is only one assembly */
2932 	idx <<= MONO_HAS_DECL_SECURITY_BITS;
2933 	idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2934 
2935 	return get_declsec_action (assembly->image, idx, action, entry);
2936 }
2937 
2938 gboolean
mono_reflection_call_is_assignable_to(MonoClass * klass,MonoClass * oklass,MonoError * error)2939 mono_reflection_call_is_assignable_to (MonoClass *klass, MonoClass *oklass, MonoError *error)
2940 {
2941 	MonoObject *res, *exc;
2942 	void *params [1];
2943 	static MonoMethod *method = NULL;
2944 
2945 	error_init (error);
2946 
2947 	if (method == NULL) {
2948 		method = mono_class_get_method_from_name (mono_class_get_type_builder_class (), "IsAssignableTo", 1);
2949 		g_assert (method);
2950 	}
2951 
2952 	/*
2953 	 * The result of mono_type_get_object_checked () might be a System.MonoType but we
2954 	 * need a TypeBuilder so use mono_class_get_ref_info (klass).
2955 	 */
2956 	g_assert (mono_class_has_ref_info (klass));
2957 	g_assert (!strcmp (mono_object_class (mono_class_get_ref_info_raw (klass))->name, "TypeBuilder")); /* FIXME use handles */
2958 
2959 	params [0] = mono_type_get_object_checked (mono_domain_get (), &oklass->byval_arg, error);
2960 	return_val_if_nok (error, FALSE);
2961 
2962 	MonoError inner_error;
2963 	res = mono_runtime_try_invoke (method, mono_class_get_ref_info_raw (klass), params, &exc, &inner_error); /* FIXME use handles */
2964 
2965 	if (exc || !is_ok (&inner_error)) {
2966 		mono_error_cleanup (&inner_error);
2967 		return FALSE;
2968 	} else
2969 		return *(MonoBoolean*)mono_object_unbox (res);
2970 }
2971 
2972 /**
2973  * mono_reflection_type_get_type:
2974  * \param reftype the \c System.Type object
2975  * \returns the \c MonoType* associated with the C# \c System.Type object \p reftype.
2976  */
2977 MonoType*
mono_reflection_type_get_type(MonoReflectionType * reftype)2978 mono_reflection_type_get_type (MonoReflectionType *reftype)
2979 {
2980 	g_assert (reftype);
2981 
2982 	MonoError error;
2983 	MonoType *result = mono_reflection_type_get_handle (reftype, &error);
2984 	mono_error_assert_ok (&error);
2985 	return result;
2986 }
2987 
2988 /**
2989  * mono_reflection_assembly_get_assembly:
2990  * \param refassembly the \c System.Reflection.Assembly object
2991  * \returns the \c MonoAssembly* associated with the C# \c System.Reflection.Assembly object \p refassembly.
2992  */
2993 MonoAssembly*
mono_reflection_assembly_get_assembly(MonoReflectionAssembly * refassembly)2994 mono_reflection_assembly_get_assembly (MonoReflectionAssembly *refassembly)
2995 {
2996 	g_assert (refassembly);
2997 
2998 	return refassembly->assembly;
2999 }
3000 
3001 /**
3002  * mono_class_from_mono_type_handle:
3003  * \param reftype the \c System.Type handle
3004  * \returns the \c MonoClass* corresponding to the given type.
3005  */
3006 MonoClass*
mono_class_from_mono_type_handle(MonoReflectionTypeHandle reftype)3007 mono_class_from_mono_type_handle (MonoReflectionTypeHandle reftype)
3008 {
3009 	return mono_class_from_mono_type (MONO_HANDLE_RAW (reftype)->type);
3010 }
3011