Skip to content

Commit

Permalink
[netcore] Don't default to mscorlib in Assembly.GetType (mono/mono#15488
Browse files Browse the repository at this point in the history
)

* [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 mono/mono@370e76d
  • Loading branch information
CoffeeFlux authored Jul 10, 2019
1 parent 1c50434 commit c549251
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
14 changes: 7 additions & 7 deletions src/mono/mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/reflection-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 25 additions & 24 deletions src/mono/mono/metadata/reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2115,7 +2115,7 @@ mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoT
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;
}
Expand All @@ -2127,44 +2127,45 @@ 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);
MonoType *type = NULL;
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);
MonoType *type = NULL;
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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -2208,7 +2209,7 @@ mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *a
}

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 ();

Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/mono/mono/mini/debugger-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/mono/netcore/CoreFX.issues.rsp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c549251

Please sign in to comment.