Esempio n. 1
0
void GDMonoAssembly::initialize() {

	// TODO refonly as well?
	mono_install_assembly_preload_hook(&GDMonoAssembly::_preload_hook, NULL);
	mono_install_assembly_search_hook(&GDMonoAssembly::_search_hook, NULL);
}
Esempio n. 2
0
static int
verify_image_file (const char *fname)
{
	GSList *errors = NULL, *tmp;
	MonoImage *image;
	MonoTableInfo *table;
	MonoAssembly *assembly;
	MonoImageOpenStatus status;
	int i, count = 0;
	const char* desc [] = {
		"Ok", "Error", "Warning", NULL, "CLS", NULL, NULL, NULL, "Not Verifiable"
	};

	image = mono_image_open_raw (fname, &status);
	if (!image) {
		printf ("Could not open %s\n", fname);
		return 1;
	}

	if (!mono_verifier_verify_pe_data (image, &errors))
		goto invalid_image;

	if (!mono_image_load_pe_data (image)) {
		printf ("Could not load pe data for assembly %s\n", fname);
		return 1;
	}

	if (!mono_verifier_verify_cli_data (image, &errors))
		goto invalid_image;

	if (!mono_image_load_cli_data (image)) {
		printf ("Could not load cli data for assembly %s\n", fname);
		return 1;
	}

	if (!mono_verifier_verify_table_data (image, &errors))
		goto invalid_image;

	mono_image_load_names (image);

	/*fake an assembly for class loading to work*/
	assembly = g_new0 (MonoAssembly, 1);
	assembly->in_gac = FALSE;
	assembly->image = image;
	image->assembly = assembly;
	mono_assembly_fill_assembly_name (image, &assembly->aname);

	/*Finish initializing the runtime*/
	mono_install_assembly_load_hook (pedump_assembly_load_hook, NULL);
	mono_install_assembly_search_hook (pedump_assembly_search_hook, NULL);

	mono_init_version ("pedump", image->version);

	mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));

	mono_icall_init ();
	mono_marshal_init ();


	if (!verify_partial_md && !mono_verifier_verify_full_table_data (image, &errors))
		goto invalid_image;


	table = &image->tables [MONO_TABLE_TYPEDEF];
	for (i = 1; i <= table->rows; ++i) {
		MonoError error;
		guint32 token = i | MONO_TOKEN_TYPE_DEF;
		MonoClass *klass = mono_class_get_checked (image, token, &error);
		if (!klass) {
			printf ("Could not load class with token %x due to %s\n", token, mono_error_get_message (&error));
			mono_error_cleanup (&error);
			continue;
		}
		mono_class_init (klass);
		if (mono_class_has_failure (klass)) {
			printf ("Error verifying class(0x%08x) %s.%s a type load error happened\n", token, klass->name_space, klass->name);
			++count;
		}

		mono_class_setup_vtable (klass);
		if (mono_class_has_failure (klass)) {
			printf ("Error verifying class(0x%08x) %s.%s a type load error happened\n", token, klass->name_space, klass->name);
			++count;
		}
	}
	if (count)
		return 5;
	return 0;

invalid_image:
	for (tmp = errors; tmp; tmp = tmp->next) {
		MonoVerifyInfo *info = (MonoVerifyInfo *)tmp->data;
		g_print ("%s: %s\n", desc [info->status], info->message);
		if (info->status == MONO_VERIFY_ERROR)
			count++;
	}
	mono_free_verify_list (errors);
	if (count)
		g_print ("Error count: %d\n", count);
	return 1;
}
Esempio n. 3
0
static HRESULT load_mono(LPCWSTR mono_path)
{
    static const WCHAR lib[] = {'\\','l','i','b',0};
    static const WCHAR etc[] = {'\\','e','t','c',0};
    WCHAR mono_dll_path[MAX_PATH+16];
    WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
    char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
    int trace_size;
    char trace_setting[256];
    int verbose_size;
    char verbose_setting[256];

    if (is_mono_shutdown)
    {
        ERR("Cannot load Mono after it has been shut down.\n");
        return E_FAIL;
    }

    if (!mono_handle)
    {
        strcpyW(mono_lib_path, mono_path);
        strcatW(mono_lib_path, lib);
        WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);

        strcpyW(mono_etc_path, mono_path);
        strcatW(mono_etc_path, etc);
        WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);

        if (!find_mono_dll(mono_path, mono_dll_path)) goto fail;

        mono_handle = LoadLibraryW(mono_dll_path);

        if (!mono_handle) goto fail;

#define LOAD_MONO_FUNCTION(x) do { \
    x = (void*)GetProcAddress(mono_handle, #x); \
    if (!x) { \
        goto fail; \
    } \
} while (0);

        LOAD_MONO_FUNCTION(mono_assembly_get_image);
        LOAD_MONO_FUNCTION(mono_assembly_load_from);
        LOAD_MONO_FUNCTION(mono_assembly_open);
        LOAD_MONO_FUNCTION(mono_config_parse);
        LOAD_MONO_FUNCTION(mono_class_from_mono_type);
        LOAD_MONO_FUNCTION(mono_class_from_name);
        LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
        LOAD_MONO_FUNCTION(mono_domain_assembly_open);
        LOAD_MONO_FUNCTION(mono_domain_get);
        LOAD_MONO_FUNCTION(mono_domain_get_by_id);
        LOAD_MONO_FUNCTION(mono_domain_set);
        LOAD_MONO_FUNCTION(mono_domain_set_config);
        LOAD_MONO_FUNCTION(mono_free);
        LOAD_MONO_FUNCTION(mono_image_open);
        LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
        LOAD_MONO_FUNCTION(mono_jit_exec);
        LOAD_MONO_FUNCTION(mono_jit_init_version);
        LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
        LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
        LOAD_MONO_FUNCTION(mono_object_get_domain);
        LOAD_MONO_FUNCTION(mono_object_get_virtual_method);
        LOAD_MONO_FUNCTION(mono_object_new);
        LOAD_MONO_FUNCTION(mono_object_unbox);
        LOAD_MONO_FUNCTION(mono_profiler_install);
        LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
        LOAD_MONO_FUNCTION(mono_runtime_invoke);
        LOAD_MONO_FUNCTION(mono_runtime_object_init);
        LOAD_MONO_FUNCTION(mono_runtime_quit);
        LOAD_MONO_FUNCTION(mono_set_dirs);
        LOAD_MONO_FUNCTION(mono_set_verbose_level);
        LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
        LOAD_MONO_FUNCTION(mono_string_new);
        LOAD_MONO_FUNCTION(mono_thread_attach);
        LOAD_MONO_FUNCTION(mono_thread_manage);
        LOAD_MONO_FUNCTION(mono_trace_set_assembly);

#undef LOAD_MONO_FUNCTION

#define LOAD_OPT_MONO_FUNCTION(x, default) do { \
    x = (void*)GetProcAddress(mono_handle, #x); \
    if (!x) { \
        x = default; \
    } \
} while (0);

        LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
        LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler, set_print_handler_dummy);
        LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler, set_print_handler_dummy);

#undef LOAD_OPT_MONO_FUNCTION

        mono_profiler_install(NULL, mono_shutdown_callback_fn);

        mono_trace_set_print_handler(mono_print_handler_fn);
        mono_trace_set_printerr_handler(mono_print_handler_fn);

        mono_set_dirs(mono_lib_path_a, mono_etc_path_a);

        mono_config_parse(NULL);

        mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);

        trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));

        if (trace_size)
        {
            mono_jit_set_trace_options(trace_setting);
        }

        verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));

        if (verbose_size)
        {
            mono_set_verbose_level(verbose_setting[0] - '0');
        }
    }

    return S_OK;

fail:
    ERR("Could not load Mono into this process\n");
    FreeLibrary(mono_handle);
    mono_handle = NULL;
    return E_FAIL;
}
Esempio n. 4
0
static int
verify_image_file (const char *fname)
{
	ERROR_DECL (error);

	MonoImage *image;
	MonoTableInfo *table;
	MonoAssembly *assembly;
	MonoImageOpenStatus status;
	int i, count = 0;

	if (!strstr (fname, "mscorlib.dll")) {
		image = mono_image_open_raw (fname, &status);
		if (!image) {
			printf ("Could not open %s\n", fname);
			return 1;
		}

		if (!mono_verifier_verify_pe_data (image, error))
			goto invalid_image;

		if (!mono_image_load_pe_data (image)) {
			printf ("Could not load pe data for assembly %s\n", fname);
			return 1;
		}

		if (!mono_verifier_verify_cli_data (image, error))
			goto invalid_image;

		if (!mono_image_load_cli_data (image)) {
			printf ("Could not load cli data for assembly %s\n", fname);
			return 1;
		}

		if (!mono_verifier_verify_table_data (image, error))
			goto invalid_image;

		mono_image_load_names (image);

		/*fake an assembly for class loading to work*/
		assembly = g_new0 (MonoAssembly, 1);
		assembly->in_gac = FALSE;
		assembly->image = image;
		image->assembly = assembly;
		mono_assembly_fill_assembly_name (image, &assembly->aname);

		/*Finish initializing the runtime*/
		mono_install_assembly_load_hook (pedump_assembly_load_hook, NULL);
		mono_install_assembly_search_hook (pedump_assembly_search_hook, NULL);

		mono_init_version ("pedump", image->version);

		mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));

		mono_icall_init ();
		mono_marshal_init ();
	} else {
		/*Finish initializing the runtime*/
		mono_install_assembly_load_hook (pedump_assembly_load_hook, NULL);
		mono_install_assembly_search_hook (pedump_assembly_search_hook, NULL);

		mono_init_version ("pedump", NULL);

		mono_install_assembly_preload_hook (pedump_preload, GUINT_TO_POINTER (FALSE));

		mono_icall_init ();
		mono_marshal_init ();
		image = mono_get_corlib ();

		if (!mono_verifier_verify_pe_data (image, error))
			goto invalid_image;

		if (!mono_image_load_pe_data (image)) {
			printf ("Could not load pe data for assembly %s\n", fname);
			return 1;
		}

		if (!mono_verifier_verify_cli_data (image, error))
			goto invalid_image;

		if (!mono_image_load_cli_data (image)) {
			printf ("Could not load cli data for assembly %s\n", fname);
			return 1;
		}

		if (!mono_verifier_verify_table_data (image, error))
			goto invalid_image;
	}

	if (!verify_partial_md && !mono_verifier_verify_full_table_data (image, error))
		goto invalid_image;


	table = &image->tables [MONO_TABLE_TYPEDEF];
	for (i = 1; i <= table->rows; ++i) {
		ERROR_DECL (error);
		guint32 token = i | MONO_TOKEN_TYPE_DEF;
		MonoClass *klass = mono_class_get_checked (image, token, error);
		if (!klass) {
			printf ("Could not load class with token %x due to %s\n", token, mono_error_get_message (error));
			mono_error_cleanup (error);
			continue;
		}
		mono_class_init_internal (klass);
		if (mono_class_has_failure (klass)) {
			ERROR_DECL (type_load_error);
			mono_error_set_for_class_failure (type_load_error, klass);
			printf ("Could not initialize class(0x%08x) %s.%s due to %s\n", token, m_class_get_name_space (klass), m_class_get_name (klass), mono_error_get_message (type_load_error));
			mono_error_cleanup (type_load_error);
			++count;
		}

		mono_class_setup_vtable (klass);
		if (mono_class_has_failure (klass)) {
			ERROR_DECL (type_load_error);
			mono_error_set_for_class_failure (type_load_error, klass);
			printf ("Could not initialize vtable of class(0x%08x) %s.%s due to %s\n", token, m_class_get_name_space (klass), m_class_get_name (klass), mono_error_get_message (type_load_error));
			mono_error_cleanup (type_load_error);
			++count;
		}
	}
	if (count)
		return 5;
	return 0;

invalid_image:
	if (!is_ok (error)) {
		g_print ("FAIL: %s\n", mono_error_get_message (error));
		mono_error_cleanup (error);
		++count;
	}
	if (count)
		g_print ("Error count: %d\n", count);
	return 1;
}
Esempio n. 5
0
void GDMonoAssembly::initialize() {

	mono_install_assembly_preload_hook(&gdmono_MonoAssemblyPreLoad, NULL);
}
Esempio n. 6
0
static void
install_assembly_loader_hooks (void)
{
	mono_install_assembly_preload_hook (mono_core_preload_hook, (void*)trusted_platform_assemblies);
}