[merp] Remove dead code (#20043)
[mono-project.git] / mono / metadata / dynamic-image.c
blob3b149cfe0c3337c2280640620a558d58375a6ccc
1 /**
2 * \file
3 * Images created at runtime.
4 *
5 *
6 * Author:
7 * Paolo Molaro (lupus@ximian.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * Copyright 2011 Rodrigo Kumpera
12 * Copyright 2016 Microsoft
14 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include <config.h>
18 #include <glib.h>
19 #include "mono/metadata/object.h"
20 #include "mono/metadata/dynamic-image-internals.h"
21 #include "mono/metadata/dynamic-stream-internals.h"
22 #include "mono/metadata/gc-internals.h"
23 #include "mono/metadata/metadata-internals.h"
24 #include "mono/metadata/mono-hash-internals.h"
25 #include "mono/metadata/profiler-private.h"
26 #include "mono/metadata/reflection-internals.h"
27 #include "mono/metadata/sre-internals.h"
28 #include "mono/utils/checked-build.h"
29 #include "mono/utils/mono-error-internals.h"
30 #include "mono/utils/mono-os-mutex.h"
32 // The dynamic images list is only needed to support the mempool reference tracking feature in checked-build.
33 static GPtrArray *dynamic_images;
34 static mono_mutex_t dynamic_images_mutex;
36 static void
37 dynamic_images_lock (void)
39 mono_os_mutex_lock (&dynamic_images_mutex);
42 static void
43 dynamic_images_unlock (void)
45 mono_os_mutex_unlock (&dynamic_images_mutex);
48 void
49 mono_dynamic_images_init (void)
51 mono_os_mutex_init (&dynamic_images_mutex);
54 #ifndef DISABLE_REFLECTION_EMIT
55 static void
56 string_heap_init (MonoDynamicStream *sh)
58 mono_dynstream_init (sh);
60 #endif
62 #ifndef DISABLE_REFLECTION_EMIT
63 static int
64 mono_blob_entry_hash (const char* str)
66 MONO_REQ_GC_NEUTRAL_MODE;
68 guint len, h;
69 const char *end;
70 len = mono_metadata_decode_blob_size (str, &str);
71 if (len > 0) {
72 end = str + len;
73 h = *str;
74 for (str += 1; str < end; str++)
75 h = (h << 5) - h + *str;
76 return h;
77 } else {
78 return 0;
82 static gboolean
83 mono_blob_entry_equal (const char *str1, const char *str2) {
84 MONO_REQ_GC_NEUTRAL_MODE;
86 int len, len2;
87 const char *end1;
88 const char *end2;
89 len = mono_metadata_decode_blob_size (str1, &end1);
90 len2 = mono_metadata_decode_blob_size (str2, &end2);
91 if (len != len2)
92 return 0;
93 return memcmp (end1, end2, len) == 0;
95 #endif
98 /**
99 * mono_find_dynamic_image_owner:
101 * Find the dynamic image, if any, which a given pointer is located in the memory of.
103 MonoImage *
104 mono_find_dynamic_image_owner (void *ptr)
106 MonoImage *owner = NULL;
107 int i;
109 dynamic_images_lock ();
111 if (dynamic_images)
113 for (i = 0; !owner && i < dynamic_images->len; ++i) {
114 MonoImage *image = (MonoImage *)g_ptr_array_index (dynamic_images, i);
115 if (mono_mempool_contains_addr (image->mempool, ptr))
116 owner = image;
120 dynamic_images_unlock ();
122 return owner;
125 static void
126 dynamic_image_lock (MonoDynamicImage *image)
128 MONO_ENTER_GC_SAFE;
129 mono_image_lock ((MonoImage*)image);
130 MONO_EXIT_GC_SAFE;
133 static void
134 dynamic_image_unlock (MonoDynamicImage *image)
136 mono_image_unlock ((MonoImage*)image);
139 #ifndef DISABLE_REFLECTION_EMIT
141 * mono_dynamic_image_register_token:
143 * Register the TOKEN->OBJ mapping in the mapping table in ASSEMBLY. This is required for
144 * the Module.ResolveXXXToken () methods to work.
146 void
147 mono_dynamic_image_register_token (MonoDynamicImage *assembly, guint32 token, MonoObjectHandle obj, int how_collide)
149 MONO_REQ_GC_UNSAFE_MODE;
151 g_assert (!MONO_HANDLE_IS_NULL (obj));
152 g_assert (strcmp (m_class_get_name (mono_handle_class (obj)), "EnumBuilder"));
153 dynamic_image_lock (assembly);
154 MonoObject *prev = (MonoObject *)mono_g_hash_table_lookup (assembly->tokens, GUINT_TO_POINTER (token));
155 if (prev) {
156 switch (how_collide) {
157 case MONO_DYN_IMAGE_TOK_NEW:
158 g_warning ("%s: Unexpected previous object when called with MONO_DYN_IMAGE_TOK_NEW", __func__);
159 break;
160 case MONO_DYN_IMAGE_TOK_SAME_OK:
161 if (prev != MONO_HANDLE_RAW (obj)) {
162 g_warning ("%s: condition `prev == MONO_HANDLE_RAW (obj)' not met", __func__);
164 break;
165 case MONO_DYN_IMAGE_TOK_REPLACE:
166 break;
167 default:
168 g_assert_not_reached ();
171 mono_g_hash_table_insert_internal (assembly->tokens, GUINT_TO_POINTER (token), MONO_HANDLE_RAW (obj));
172 dynamic_image_unlock (assembly);
174 #else
175 void
176 mono_dynamic_image_register_token (MonoDynamicImage *assembly, guint32 token, MonoObjectHandle obj, int how_collide)
179 #endif
181 static gboolean
182 lookup_dyn_token (MonoDynamicImage *assembly, guint32 token, MonoObjectHandle *object_handle)
184 MONO_REQ_GC_UNSAFE_MODE;
186 MonoObject *obj;
188 dynamic_image_lock (assembly);
189 obj = (MonoObject *)mono_g_hash_table_lookup (assembly->tokens, GUINT_TO_POINTER (token));
190 dynamic_image_unlock (assembly);
192 if (object_handle)
193 *object_handle = MONO_HANDLE_NEW (MonoObject, obj);
195 return obj != NULL;
198 #ifndef DISABLE_REFLECTION_EMIT
199 MonoObjectHandle
200 mono_dynamic_image_get_registered_token (MonoDynamicImage *dynimage, guint32 token, MonoError *error)
202 MonoObjectHandle obj;
203 lookup_dyn_token (dynimage, token, &obj);
204 return obj;
206 #else /* DISABLE_REFLECTION_EMIT */
207 MonoObjectHandle
208 mono_dynamic_image_get_registered_token (MonoDynamicImage *dynimage, guint32 token, MonoError *error)
210 g_assert_not_reached ();
211 return NULL_HANDLE;
213 #endif
217 * mono_dynamic_image_is_valid_token:
219 * Returns TRUE if token is valid in the given image.
222 gboolean
223 mono_dynamic_image_is_valid_token (MonoDynamicImage *image, guint32 token)
225 return lookup_dyn_token (image, token, NULL);
228 #ifndef DISABLE_REFLECTION_EMIT
230 #endif /* DISABLE_REFLECTION_EMIT */
232 #ifndef DISABLE_REFLECTION_EMIT
234 * mono_reflection_lookup_dynamic_token:
236 * Finish the Builder object pointed to by TOKEN and return the corresponding
237 * runtime structure. If HANDLE_CLASS is not NULL, it is set to the class required by
238 * mono_ldtoken. If valid_token is TRUE, assert if it is not found in the token->object
239 * mapping table.
241 * LOCKING: Take the loader lock
243 gpointer
244 mono_reflection_lookup_dynamic_token (MonoImage *image, guint32 token, gboolean valid_token, MonoClass **handle_class, MonoGenericContext *context, MonoError *error)
246 HANDLE_FUNCTION_ENTER ();
248 MonoDynamicImage *assembly = (MonoDynamicImage*)image;
249 MonoObjectHandle obj;
250 MonoClass *klass;
252 error_init (error);
254 lookup_dyn_token (assembly, token, &obj);
255 if (MONO_HANDLE_IS_NULL (obj)) {
256 if (valid_token)
257 g_error ("Could not find required dynamic token 0x%08x", token);
258 else {
259 mono_error_set_execution_engine (error, "Could not find dynamic token 0x%08x", token);
260 return NULL;
264 if (!handle_class)
265 handle_class = &klass;
266 gpointer const result = mono_reflection_resolve_object_handle (image, obj, handle_class, context, error);
267 HANDLE_FUNCTION_RETURN_VAL (result);
269 #else /* DISABLE_REFLECTION_EMIT */
270 gpointer
271 mono_reflection_lookup_dynamic_token (MonoImage *image, guint32 token, gboolean valid_token, MonoClass **handle_class, MonoGenericContext *context, MonoError *error)
273 error_init (error);
274 return NULL;
276 #endif /* DISABLE_REFLECTION_EMIT */
278 #ifndef DISABLE_REFLECTION_EMIT
280 static const unsigned char table_sizes [MONO_TABLE_NUM] = {
281 MONO_MODULE_SIZE,
282 MONO_TYPEREF_SIZE,
283 MONO_TYPEDEF_SIZE,
285 MONO_FIELD_SIZE,
287 MONO_METHOD_SIZE,
289 MONO_PARAM_SIZE,
290 MONO_INTERFACEIMPL_SIZE,
291 MONO_MEMBERREF_SIZE, /* 0x0A */
292 MONO_CONSTANT_SIZE,
293 MONO_CUSTOM_ATTR_SIZE,
294 MONO_FIELD_MARSHAL_SIZE,
295 MONO_DECL_SECURITY_SIZE,
296 MONO_CLASS_LAYOUT_SIZE,
297 MONO_FIELD_LAYOUT_SIZE, /* 0x10 */
298 MONO_STAND_ALONE_SIGNATURE_SIZE,
299 MONO_EVENT_MAP_SIZE,
301 MONO_EVENT_SIZE,
302 MONO_PROPERTY_MAP_SIZE,
304 MONO_PROPERTY_SIZE,
305 MONO_METHOD_SEMA_SIZE,
306 MONO_METHODIMPL_SIZE,
307 MONO_MODULEREF_SIZE, /* 0x1A */
308 MONO_TYPESPEC_SIZE,
309 MONO_IMPLMAP_SIZE,
310 MONO_FIELD_RVA_SIZE,
313 MONO_ASSEMBLY_SIZE, /* 0x20 */
314 MONO_ASSEMBLY_PROCESSOR_SIZE,
315 MONO_ASSEMBLYOS_SIZE,
316 MONO_ASSEMBLYREF_SIZE,
317 MONO_ASSEMBLYREFPROC_SIZE,
318 MONO_ASSEMBLYREFOS_SIZE,
319 MONO_FILE_SIZE,
320 MONO_EXP_TYPE_SIZE,
321 MONO_MANIFEST_SIZE,
322 MONO_NESTED_CLASS_SIZE,
324 MONO_GENERICPARAM_SIZE, /* 0x2A */
325 MONO_METHODSPEC_SIZE,
326 MONO_GENPARCONSTRAINT_SIZE
329 MonoDynamicImage*
330 mono_dynamic_image_create (MonoDynamicAssembly *assembly, char *assembly_name, char *module_name)
332 static const guchar entrycode [16] = {0xff, 0x25, 0};
333 MonoDynamicImage *image;
334 int i;
336 const char *version;
338 if (!strcmp (mono_get_runtime_info ()->framework_version, "2.1"))
339 version = "v2.0.50727"; /* HACK: SL 2 enforces the .net 2 metadata version */
340 else
341 version = mono_get_runtime_info ()->runtime_version;
343 image = g_new0 (MonoDynamicImage, 1);
345 MONO_PROFILER_RAISE (image_loading, (&image->image));
347 /*g_print ("created image %p\n", image);*/
348 /* keep in sync with image.c */
349 image->image.name = assembly_name;
350 image->image.assembly_name = image->image.name; /* they may be different */
351 image->image.module_name = module_name;
352 image->image.version = g_strdup (version);
353 image->image.md_version_major = 1;
354 image->image.md_version_minor = 1;
355 image->image.dynamic = TRUE;
357 image->image.references = g_new0 (MonoAssembly*, 1);
358 image->image.references [0] = NULL;
360 mono_image_init (&image->image);
362 image->token_fixups = mono_g_hash_table_new_type_internal ((GHashFunc)mono_object_hash_internal, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_REFLECTION, NULL, "Reflection Dynamic Image Token Fixup Table");
363 image->method_to_table_idx = g_hash_table_new (NULL, NULL);
364 image->field_to_table_idx = g_hash_table_new (NULL, NULL);
365 image->method_aux_hash = g_hash_table_new (NULL, NULL);
366 image->vararg_aux_hash = g_hash_table_new (NULL, NULL);
367 image->handleref = g_hash_table_new (NULL, NULL);
368 image->tokens = mono_g_hash_table_new_type_internal (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_REFLECTION, NULL, "Reflection Dynamic Image Token Table");
369 image->generic_def_objects = mono_g_hash_table_new_type_internal (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_REFLECTION, NULL, "Reflection Dynamic Image Generic Definition Table");
370 image->typespec = g_hash_table_new ((GHashFunc)mono_metadata_type_hash, (GCompareFunc)mono_metadata_type_equal);
371 image->typeref = g_hash_table_new ((GHashFunc)mono_metadata_type_hash, (GCompareFunc)mono_metadata_type_equal);
372 image->blob_cache = g_hash_table_new ((GHashFunc)mono_blob_entry_hash, (GCompareFunc)mono_blob_entry_equal);
373 image->gen_params = g_ptr_array_new ();
374 image->remapped_tokens = mono_g_hash_table_new_type_internal (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_REFLECTION, NULL, "Reflection Dynamic Image Remapped Token Table");
376 /*g_print ("string heap create for image %p (%s)\n", image, module_name);*/
377 string_heap_init (&image->sheap);
378 mono_dynstream_add_data (&image->us, "", 1);
379 mono_dynamic_image_add_to_blob_cached (image, "", 1, NULL, 0);
380 /* import tables... */
381 mono_dynstream_add_data (&image->code, entrycode, sizeof (entrycode));
382 image->iat_offset = mono_dynstream_add_zero (&image->code, 8); /* two IAT entries */
383 image->idt_offset = mono_dynstream_add_zero (&image->code, 2 * sizeof (MonoIDT)); /* two IDT entries */
384 image->imp_names_offset = mono_dynstream_add_zero (&image->code, 2); /* flags for name entry */
385 mono_dynstream_add_data (&image->code, "_CorExeMain", 12);
386 mono_dynstream_add_data (&image->code, "mscoree.dll", 12);
387 image->ilt_offset = mono_dynstream_add_zero (&image->code, 8); /* two ILT entries */
388 mono_dynstream_data_align (&image->code);
390 image->cli_header_offset = mono_dynstream_add_zero (&image->code, sizeof (MonoCLIHeader));
392 for (i=0; i < MONO_TABLE_NUM; ++i) {
393 image->tables [i].next_idx = 1;
394 image->tables [i].columns = table_sizes [i];
397 image->image.assembly = (MonoAssembly*)assembly;
398 image->run = assembly->run;
399 image->save = assembly->save;
400 image->pe_kind = 0x1; /* ILOnly */
401 image->machine = 0x14c; /* I386 */
403 MONO_PROFILER_RAISE (image_loaded, (&image->image));
405 dynamic_images_lock ();
407 if (!dynamic_images)
408 dynamic_images = g_ptr_array_new ();
410 g_ptr_array_add (dynamic_images, image);
412 dynamic_images_unlock ();
414 return image;
416 #else /* DISABLE_REFLECTION_EMIT */
417 MonoDynamicImage*
418 mono_dynamic_image_create (MonoDynamicAssembly *assembly, char *assembly_name, char *module_name)
420 g_assert_not_reached ();
421 return NULL;
423 #endif /* DISABLE_REFLECTION_EMIT */
425 guint32
426 mono_dynamic_image_add_to_blob_cached (MonoDynamicImage *assembly, gconstpointer b1, int s1, gconstpointer b2, int s2)
428 MONO_REQ_GC_NEUTRAL_MODE;
430 guint32 idx;
431 char *copy;
432 gpointer oldkey, oldval;
434 copy = (char *)g_malloc (s1+s2);
435 memcpy (copy, b1, s1);
436 memcpy (copy + s1, b2, s2);
437 if (g_hash_table_lookup_extended (assembly->blob_cache, copy, &oldkey, &oldval)) {
438 g_free (copy);
439 idx = GPOINTER_TO_UINT (oldval);
440 } else {
441 idx = mono_dynstream_add_data (&assembly->blob, b1, s1);
442 mono_dynstream_add_data (&assembly->blob, b2, s2);
443 g_hash_table_insert (assembly->blob_cache, copy, GUINT_TO_POINTER (idx));
445 return idx;
448 void
449 mono_dynimage_alloc_table (MonoDynamicTable *table, guint nrows)
451 MONO_REQ_GC_NEUTRAL_MODE;
453 table->rows = nrows;
454 g_assert (table->columns);
455 if (nrows + 1 >= table->alloc_rows) {
456 while (nrows + 1 >= table->alloc_rows) {
457 if (table->alloc_rows == 0)
458 table->alloc_rows = 16;
459 else
460 table->alloc_rows *= 2;
463 table->values = (guint32 *)g_renew (guint32, table->values, (table->alloc_rows) * table->columns);
468 static void
469 free_blob_cache_entry (gpointer key, gpointer val, gpointer user_data)
471 g_free (key);
474 static void
475 release_hashtable (MonoGHashTable **hash)
477 if (*hash) {
478 mono_g_hash_table_destroy (*hash);
479 *hash = NULL;
483 void
484 mono_dynamic_image_release_gc_roots (MonoDynamicImage *image)
486 release_hashtable (&image->token_fixups);
487 release_hashtable (&image->tokens);
488 release_hashtable (&image->remapped_tokens);
489 release_hashtable (&image->generic_def_objects);
492 // Free dynamic image pass one: Free resources but not image itself
493 void
494 mono_dynamic_image_free (MonoDynamicImage *image)
496 MonoDynamicImage *di = image;
497 GList *list;
498 int i;
500 if (di->typespec)
501 g_hash_table_destroy (di->typespec);
502 if (di->typeref)
503 g_hash_table_destroy (di->typeref);
504 if (di->handleref)
505 g_hash_table_destroy (di->handleref);
506 if (di->tokens)
507 mono_g_hash_table_destroy (di->tokens);
508 if (di->remapped_tokens)
509 mono_g_hash_table_destroy (di->remapped_tokens);
510 if (di->generic_def_objects)
511 mono_g_hash_table_destroy (di->generic_def_objects);
512 if (di->blob_cache) {
513 g_hash_table_foreach (di->blob_cache, free_blob_cache_entry, NULL);
514 g_hash_table_destroy (di->blob_cache);
516 if (di->standalonesig_cache)
517 g_hash_table_destroy (di->standalonesig_cache);
518 for (list = di->array_methods; list; list = list->next) {
519 ArrayMethod *am = (ArrayMethod *)list->data;
520 mono_sre_array_method_free (am);
522 g_list_free (di->array_methods);
523 if (di->gen_params) {
524 for (i = 0; i < di->gen_params->len; i++) {
525 GenericParamTableEntry *entry = (GenericParamTableEntry *)g_ptr_array_index (di->gen_params, i);
526 mono_sre_generic_param_table_entry_free (entry);
528 g_ptr_array_free (di->gen_params, TRUE);
530 if (di->token_fixups)
531 mono_g_hash_table_destroy (di->token_fixups);
532 if (di->method_to_table_idx)
533 g_hash_table_destroy (di->method_to_table_idx);
534 if (di->field_to_table_idx)
535 g_hash_table_destroy (di->field_to_table_idx);
536 if (di->method_aux_hash)
537 g_hash_table_destroy (di->method_aux_hash);
538 if (di->vararg_aux_hash)
539 g_hash_table_destroy (di->vararg_aux_hash);
540 g_free (di->strong_name);
541 g_free (di->win32_res);
542 if (di->public_key)
543 g_free (di->public_key);
545 /*g_print ("string heap destroy for image %p\n", di);*/
546 mono_dynamic_stream_reset (&di->sheap);
547 mono_dynamic_stream_reset (&di->code);
548 mono_dynamic_stream_reset (&di->resources);
549 mono_dynamic_stream_reset (&di->us);
550 mono_dynamic_stream_reset (&di->blob);
551 mono_dynamic_stream_reset (&di->tstream);
552 mono_dynamic_stream_reset (&di->guid);
553 for (i = 0; i < MONO_TABLE_NUM; ++i) {
554 g_free (di->tables [i].values);
557 dynamic_images_lock ();
559 if (dynamic_images)
560 g_ptr_array_remove (dynamic_images, di);
562 dynamic_images_unlock ();
565 // Free dynamic image pass two: Free image itself (might never get called in some debug modes)
566 void
567 mono_dynamic_image_free_image (MonoDynamicImage *image)
569 g_free (image);