From c549251d34f6d0425f61fb574584cfaf277cd986 Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Wed, 10 Jul 2019 09:21:56 -0700 Subject: [PATCH] [netcore] Don't default to mscorlib in Assembly.GetType (mono/mono#15488) * [netcore] Don't default to mscorlib in Assembly.GetType * Add argument for whether or not to search mscorlib * Reorder and rename search_mscorlib argument * Use gboolean exclusively Commit migrated from https://github.com/mono/mono/commit/370e76dfc83616042adde1b2ffd130e25363002e --- src/mono/mono/metadata/icall.c | 14 ++++---- src/mono/mono/metadata/reflection-internals.h | 2 +- src/mono/mono/metadata/reflection.c | 49 ++++++++++++++------------- src/mono/mono/mini/debugger-agent.c | 4 +-- src/mono/netcore/CoreFX.issues.rsp | 4 --- 5 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/mono/mono/metadata/icall.c b/src/mono/mono/metadata/icall.c index 66efd1c..52e31ef 100644 --- a/src/mono/mono/metadata/icall.c +++ b/src/mono/mono/metadata/icall.c @@ -1739,7 +1739,7 @@ type_from_parsed_name (MonoTypeNameParse *info, MonoStackCrawlMark *stack_mark, if (assembly) { /* When loading from the current assembly, AppDomain.TypeResolve will not be called yet */ - type = mono_reflection_get_type_checked (rootimage, assembly->image, info, ignoreCase, &type_resolve, error); + type = mono_reflection_get_type_checked (rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error); goto_if_nok (error, fail); } @@ -1753,12 +1753,12 @@ type_from_parsed_name (MonoTypeNameParse *info, MonoStackCrawlMark *stack_mark, // as the root and then even the detour into generics would still not screw us when we went to load Local. if (!info->assembly.name && !type) { /* try mscorlib */ - type = mono_reflection_get_type_checked (rootimage, NULL, info, ignoreCase, &type_resolve, error); + type = mono_reflection_get_type_checked (rootimage, NULL, info, ignoreCase, TRUE, &type_resolve, error); goto_if_nok (error, fail); } if (assembly && !type && type_resolve) { type_resolve = FALSE; /* This will invoke TypeResolve if not done in the first 'if' */ - type = mono_reflection_get_type_checked (rootimage, assembly->image, info, ignoreCase, &type_resolve, error); + type = mono_reflection_get_type_checked (rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error); goto_if_nok (error, fail); } @@ -4881,7 +4881,7 @@ get_type_from_module_builder_module (MonoArrayHandle modules, int i, MonoTypeNam MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL); MONO_HANDLE_ARRAY_GETREF (mb, modules, i); MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image); - type = mono_reflection_get_type_checked (&dynamic_image->image, &dynamic_image->image, info, ignoreCase, type_resolve, error); + type = mono_reflection_get_type_checked (&dynamic_image->image, &dynamic_image->image, info, ignoreCase, FALSE, type_resolve, error); HANDLE_FUNCTION_RETURN_VAL (type); } @@ -4894,7 +4894,7 @@ get_type_from_module_builder_loaded_modules (MonoArrayHandle loaded_modules, int MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL); MONO_HANDLE_ARRAY_GETREF (mod, loaded_modules, i); MonoImage *image = MONO_HANDLE_GETVAL (mod, image); - type = mono_reflection_get_type_checked (image, image, info, ignoreCase, type_resolve, error); + type = mono_reflection_get_type_checked (image, image, info, ignoreCase, FALSE, type_resolve, error); HANDLE_FUNCTION_RETURN_VAL (type); } @@ -4944,7 +4944,7 @@ ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHand if (!MONO_HANDLE_IS_NULL (module)) { MonoImage *image = MONO_HANDLE_GETVAL (module, image); if (image) { - type = mono_reflection_get_type_checked (image, image, &info, ignoreCase, &type_resolve, error); + type = mono_reflection_get_type_checked (image, image, &info, ignoreCase, FALSE, &type_resolve, error); if (!is_ok (error)) { g_free (str); mono_reflection_free_type_info (&info); @@ -4994,7 +4994,7 @@ ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHand } } else { - type = mono_reflection_get_type_checked (assembly->image, assembly->image, &info, ignoreCase, &type_resolve, error); + type = mono_reflection_get_type_checked (assembly->image, assembly->image, &info, ignoreCase, FALSE, &type_resolve, error); if (!is_ok (error)) { g_free (str); mono_reflection_free_type_info (&info); diff --git a/src/mono/mono/metadata/reflection-internals.h b/src/mono/mono/metadata/reflection-internals.h index 31f2386..da4e51c 100644 --- a/src/mono/mono/metadata/reflection-internals.h +++ b/src/mono/mono/metadata/reflection-internals.h @@ -41,7 +41,7 @@ MonoType * mono_reflection_type_handle_mono_type (MonoReflectionTypeHandle ref_type, MonoError *error); MonoType* -mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, mono_bool ignorecase, mono_bool *type_resolve, MonoError *error); +mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, gboolean *type_resolve, MonoError *error); MonoType* mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError *error); diff --git a/src/mono/mono/metadata/reflection.c b/src/mono/mono/metadata/reflection.c index f6d835d..a94b1f2 100644 --- a/src/mono/mono/metadata/reflection.c +++ b/src/mono/mono/metadata/reflection.c @@ -53,7 +53,7 @@ #include "icall-decl.h" static void get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types); -static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error); +static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, gboolean *type_resolve, MonoError *error); /* Class lazy loading functions */ static GENERATE_GET_CLASS_WITH_CACHE (mono_assembly, "System.Reflection", "RuntimeAssembly") @@ -1921,7 +1921,7 @@ mono_reflection_parse_type_checked (char *name, MonoTypeNameParse *info, MonoErr } static MonoType* -_mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, MonoError *error) +_mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, gboolean search_mscorlib, MonoError *error) { gboolean type_resolve = FALSE; MonoType *type; @@ -1944,17 +1944,17 @@ _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, return NULL; } image = assembly->image; - } else if (!image) { + } else if (!image && search_mscorlib) { image = mono_defaults.corlib; } - type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error); - if (type == NULL && !info->assembly.name && image != mono_defaults.corlib) { + type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, search_mscorlib, &type_resolve, error); + if (type == NULL && !info->assembly.name && image != mono_defaults.corlib && search_mscorlib) { /* ignore the error and try again */ mono_error_cleanup (error); error_init (error); image = mono_defaults.corlib; - type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error); + type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, search_mscorlib, &type_resolve, error); } return type; @@ -1966,7 +1966,7 @@ _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, * Returns: may return NULL on success, sets error on failure. */ static MonoType* -mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error) +mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, MonoError *error) { HANDLE_FUNCTION_ENTER (); MonoClass *klass; @@ -2061,7 +2061,7 @@ mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoT for (i = 0; i < info->type_arguments->len; i++) { MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i); - type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, error); + type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, search_mscorlib, error); if (!type_args [i]) { g_free (type_args); goto leave; @@ -2115,7 +2115,7 @@ leave: MonoType* mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve) { ERROR_DECL (error); - MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, type_resolve, error); + MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, TRUE, type_resolve, error); mono_error_cleanup (error); return result; } @@ -2127,18 +2127,19 @@ mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ig * \param info type description structure * \param ignorecase flag for case-insensitive string compares * \param type_resolve whenever type resolve was already tried + * \param * \param error set on error. * Build a \c MonoType from the type description in \p info. On failure returns NULL and sets \p error. */ MonoType* -mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) { +mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, gboolean *type_resolve, MonoError *error) { error_init (error); - return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, type_resolve, error); + return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, search_mscorlib, type_resolve, error); } static MonoType* -module_builder_array_get_type (MonoArrayHandle module_builders, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error) +module_builder_array_get_type (MonoArrayHandle module_builders, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, MonoError *error) { HANDLE_FUNCTION_ENTER (); error_init (error); @@ -2146,12 +2147,12 @@ module_builder_array_get_type (MonoArrayHandle module_builders, int i, MonoImage MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL); MONO_HANDLE_ARRAY_GETREF (mb, module_builders, i); MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image); - type = mono_reflection_get_type_internal (rootimage, &dynamic_image->image, info, ignorecase, error); + type = mono_reflection_get_type_internal (rootimage, &dynamic_image->image, info, ignorecase, search_mscorlib, error); HANDLE_FUNCTION_RETURN_VAL (type); } static MonoType* -module_array_get_type (MonoArrayHandle modules, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error) +module_array_get_type (MonoArrayHandle modules, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, MonoError *error) { HANDLE_FUNCTION_ENTER (); error_init (error); @@ -2159,12 +2160,12 @@ module_array_get_type (MonoArrayHandle modules, int i, MonoImage *rootimage, Mon MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL); MONO_HANDLE_ARRAY_GETREF (mod, modules, i); MonoImage *image = MONO_HANDLE_GETVAL (mod, image); - type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error); + type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, search_mscorlib, error); HANDLE_FUNCTION_RETURN_VAL (type); } static MonoType* -mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error) +mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, MonoError *error) { HANDLE_FUNCTION_ENTER (); MonoType *type = NULL; @@ -2183,7 +2184,7 @@ mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *a if (!MONO_HANDLE_IS_NULL (modules)) { int n = mono_array_handle_length (modules); for (i = 0; i < n; ++i) { - type = module_builder_array_get_type (modules, i, rootimage, info, ignorecase, error); + type = module_builder_array_get_type (modules, i, rootimage, info, ignorecase, search_mscorlib, error); if (type) break; goto_if_nok (error, leave); @@ -2196,7 +2197,7 @@ mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *a if (!type && !MONO_HANDLE_IS_NULL(loaded_modules)) { int n = mono_array_handle_length (loaded_modules); for (i = 0; i < n; ++i) { - type = module_array_get_type (loaded_modules, i, rootimage, info, ignorecase, error); + type = module_array_get_type (loaded_modules, i, rootimage, info, ignorecase, search_mscorlib, error); if (type) break; goto_if_nok (error, leave); @@ -2208,7 +2209,7 @@ leave: } MonoType* -mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) +mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean search_mscorlib, gboolean *type_resolve, MonoError *error) { HANDLE_FUNCTION_ENTER (); @@ -2220,9 +2221,9 @@ mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, error_init (error); if (image && image_is_dynamic (image)) - type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, error); + type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, search_mscorlib, error); else - type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error); + type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, search_mscorlib, error); goto_if_nok (error, return_null); if (type) @@ -2255,10 +2256,10 @@ mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoAssembly *assembly = MONO_HANDLE_GETVAL (reflection_assembly, assembly); if (assembly_is_dynamic (assembly)) type = mono_reflection_get_type_internal_dynamic (rootimage, assembly, - info, ignorecase, error); + info, ignorecase, search_mscorlib, error); else type = mono_reflection_get_type_internal (rootimage, assembly->image, - info, ignorecase, error); + info, ignorecase, search_mscorlib, error); } goto_if_nok (error, return_null); goto exit; @@ -2345,7 +2346,7 @@ mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError mono_error_cleanup (parse_error); goto leave; } - type = _mono_reflection_get_type_from_info (&info, image, FALSE, error); + type = _mono_reflection_get_type_from_info (&info, image, FALSE, TRUE, error); leave: g_free (tmp); mono_reflection_free_type_info (&info); diff --git a/src/mono/mono/mini/debugger-agent.c b/src/mono/mono/mini/debugger-agent.c index d96298f..4e758c3 100644 --- a/src/mono/mono/mini/debugger-agent.c +++ b/src/mono/mono/mini/debugger-agent.c @@ -6474,7 +6474,7 @@ get_types (gpointer key, gpointer value, gpointer user_data) if (ass->image) { ERROR_DECL (probe_type_error); /* FIXME really okay to call while holding locks? */ - t = mono_reflection_get_type_checked (ass->image, ass->image, ud->info, ud->ignore_case, &type_resolve, probe_type_error); + t = mono_reflection_get_type_checked (ass->image, ass->image, ud->info, ud->ignore_case, TRUE, &type_resolve, probe_type_error); mono_error_cleanup (probe_type_error); if (t) { g_ptr_array_add (ud->res_classes, mono_type_get_class (t)); @@ -7338,7 +7338,7 @@ assembly_commands (int command, guint8 *p, guint8 *end, Buffer *buf) } else { if (info.assembly.name) NOT_IMPLEMENTED; - t = mono_reflection_get_type_checked (ass->image, ass->image, &info, ignorecase, &type_resolve, error); + t = mono_reflection_get_type_checked (ass->image, ass->image, &info, ignorecase, TRUE, &type_resolve, error); if (!is_ok (error)) { mono_error_cleanup (error); /* FIXME don't swallow the error */ mono_reflection_free_type_info (&info); diff --git a/src/mono/netcore/CoreFX.issues.rsp b/src/mono/netcore/CoreFX.issues.rsp index 85df3bf..4d2b4f9 100644 --- a/src/mono/netcore/CoreFX.issues.rsp +++ b/src/mono/netcore/CoreFX.issues.rsp @@ -495,10 +495,6 @@ # https://github.com/mono/mono/issues/15074 -nomethod System.Reflection.Tests.AssemblyTests.LoadFile -# Expects TypeLoadException but none is returned -# https://github.com/mono/mono/issues/15075 --nomethod System.Reflection.Tests.AssemblyTests.GetType_DoesntSearchMscorlib - # Returns the same string, but still fails. # https://github.com/mono/mono/issues/15076 -nomethod System.Reflection.Tests.AssemblyTests.LoadFrom_SameIdentityAsAssemblyWithDifferentPath_ReturnsEqualAssemblies -- 2.7.4