Ejemplo n.º 1
0
void invoke_protected_memory_method (MonoArray *data, MonoObject *scope, gboolean encrypt)
{
    MonoClass *klass;
    MonoMethod *method;
    void *params [2];

    MONO_ARCH_SAVE_REGS;

    if (system_security_assembly == NULL) {
        system_security_assembly = mono_image_loaded ("System.Security");
        if (!system_security_assembly) {
            MonoAssembly *sa = mono_assembly_open ("System.Security.dll", NULL);
            if (!sa)
                g_assert_not_reached ();
            system_security_assembly = mono_assembly_get_image (sa);
        }
    }

    klass = mono_class_from_name (system_security_assembly,
                                  "System.Security.Cryptography", "ProtectedMemory");
    method = mono_class_get_method_from_name (klass, encrypt ? "Protect" : "Unprotect", 2);
    params [0] = data;
    params [1] = scope; /* MemoryProtectionScope.SameProcess */
    mono_runtime_invoke (method, NULL, params, NULL);
}
Ejemplo n.º 2
0
static MonoReflectionAssembly *
ves_icall_VInvoke_LoadAssemblyFromVirtuoso (MonoAppDomain *ad, MonoString *message)
{
  char *asm_name;
  caddr_t name = NULL;
  caddr_t code = NULL;
  long len;

  MonoAssembly *ass;
  MonoDomain *domain = virtuoso_domain;
  MonoImage *image = NULL;
#ifdef OLD_KIT_1_1_5
  MonoImageOpenStatus *status;
#else
  MonoImageOpenStatus status;
#endif

  asm_name = mono_string_to_utf8 (message);
  name = box_copy (asm_name);

  code = mono_get_assembly_by_name (&name);

  if (!code)
    return NULL;

  len = box_length (code);

  image = mono_image_open_from_data (code, len, 0, NULL);

  if (!image)
    return NULL;

#ifdef OLD_KIT_1_1_5
  ass = mono_assembly_open ("", NULL, image);
#else
  ass = mono_assembly_load_from (image, "", &status);
#endif

  if (!ass && !status)
    return NULL;

  return mono_assembly_get_object (domain, ass);
}
Ejemplo n.º 3
0
static int
mono_attach_load_agent (MonoDomain *domain, char *agent, char *args, MonoObject **exc)
{
	MonoError error;
	MonoAssembly *agent_assembly;
	MonoImage *image;
	MonoMethod *method;
	guint32 entry;
	MonoArray *main_args;
	gpointer pa [1];
	MonoImageOpenStatus open_status;

	agent_assembly = mono_assembly_open (agent, &open_status);
	if (!agent_assembly) {
		fprintf (stderr, "Cannot open agent assembly '%s': %s.\n", agent, mono_image_strerror (open_status));
		g_free (agent);
		return 2;
	}

	/* 
	 * Can't use mono_jit_exec (), as it sets things which might confuse the
	 * real Main method.
	 */
	image = mono_assembly_get_image (agent_assembly);
	entry = mono_image_get_entry_point (image);
	if (!entry) {
		g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
		g_free (agent);
		return 1;
	}

	method = mono_get_method_checked (image, entry, NULL, NULL, &error);
	if (method == NULL){
		g_print ("The entry point method of assembly '%s' could not be loaded due to %s\n", agent, mono_error_get_message (&error));
		mono_error_cleanup (&error);
		g_free (agent);
		return 1;
	}
	
	if (args) {
		main_args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, 1);
		mono_array_set (main_args, MonoString*, 0, mono_string_new (domain, args));
	} else {
Ejemplo n.º 4
0
/* Entry point called by LdrLoadDll of ntdll.dll after _CorValidateImage. */
BOOL STDMETHODCALLTYPE _CorDllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
{
	MonoAssembly* assembly;
	MonoImage* image;
	gchar* file_name;
	gchar* error;

	switch (dwReason)
	{
	case DLL_PROCESS_ATTACH:
		DisableThreadLibraryCalls (hInst);

		file_name = mono_get_module_file_name (hInst);

		if (mono_get_root_domain ()) {
			image = mono_image_open_from_module_handle (hInst, mono_path_resolve_symlinks (file_name), TRUE, NULL);
		} else {
			init_from_coree = TRUE;
			mono_runtime_load (file_name, NULL);
			error = (gchar*) mono_check_corlib_version ();
			if (error) {
				g_free (error);
				g_free (file_name);
				mono_runtime_quit ();
				return FALSE;
			}

			image = mono_image_open (file_name, NULL);
			if (image) {
				image->has_entry_point = TRUE;
				mono_close_exe_image ();
				/* Decrement reference count to zero. (Image will not be closed.) */
				mono_image_close (image);
			}
		}

		if (!image) {
			g_free (file_name);
			return FALSE;
		}

		/*
		 * FIXME: Find a better way to call mono_image_fixup_vtable. Only
		 * loader trampolines should be used and assembly loading should
		 * probably be delayed until the first call to an exported function.
		 */
		if (image->tables [MONO_TABLE_ASSEMBLY].rows && ((MonoCLIImageInfo*) image->image_info)->cli_cli_header.ch_vtable_fixups.rva)
			assembly = mono_assembly_open (file_name, NULL);

		g_free (file_name);
		break;
	case DLL_PROCESS_DETACH:
		if (lpReserved != NULL)
			/* The process is terminating. */
			return TRUE;
		file_name = mono_get_module_file_name (hInst);
		image = mono_image_loaded (file_name);
		if (image)
			mono_image_close (image);

		g_free (file_name);
		break;
	}

	return TRUE;
}
Ejemplo n.º 5
0
/* Called by ntdll.dll reagardless of entry point after _CorValidateImage. */
__int32 STDMETHODCALLTYPE _CorExeMain(void)
{
	MonoDomain* domain;
	MonoAssembly* assembly;
	MonoImage* image;
	MonoMethod* method;
	guint32 entry;
	gchar* file_name;
	gchar* error;
	int argc;
	gunichar2** argvw;
	gchar** argv;
	int i;

	file_name = mono_get_module_file_name (NULL);
	init_from_coree = TRUE;
	domain = mono_runtime_load (file_name, NULL);

	error = (gchar*) mono_check_corlib_version ();
	if (error) {
		g_free (error);
		g_free (file_name);
		MessageBox (NULL, L"Corlib not in sync with this runtime.", NULL, MB_ICONERROR);
		mono_runtime_quit ();
		ExitProcess (1);
	}

	assembly = mono_assembly_open (file_name, NULL);
	mono_close_exe_image ();
	if (!assembly) {
		g_free (file_name);
		MessageBox (NULL, L"Cannot open assembly.", NULL, MB_ICONERROR);
		mono_runtime_quit ();
		ExitProcess (1);
	}

	image = assembly->image;
	entry = mono_image_get_entry_point (image);
	if (!entry) {
		g_free (file_name);
		MessageBox (NULL, L"Assembly doesn't have an entry point.", NULL, MB_ICONERROR);
		mono_runtime_quit ();
		ExitProcess (1);
	}

	method = mono_get_method (image, entry, NULL);
	if (method == NULL) {
		g_free (file_name);
		MessageBox (NULL, L"The entry point method could not be loaded.", NULL, MB_ICONERROR);
		mono_runtime_quit ();
		ExitProcess (1);
	}

	argvw = CommandLineToArgvW (GetCommandLine (), &argc);
	argv = g_new0 (gchar*, argc);
	argv [0] = file_name;
	for (i = 1; i < argc; ++i)
		argv [i] = g_utf16_to_utf8 (argvw [i], -1, NULL, NULL, NULL);
	LocalFree (argvw);

	mono_runtime_run_main (method, argc, argv, NULL);
	mono_thread_manage ();

	mono_runtime_quit ();

	/* return does not terminate the process. */
	ExitProcess (mono_environment_exitcode_get ());
}
Ejemplo n.º 6
0
int
main (int argc, char *argv[]) {
	MonoAssembly *assembly = NULL;
	const char *aname = NULL;
	const char *outfile = NULL;
	const char *rootfile = NULL;
	int i;
	gboolean all_meta = FALSE;

	mono_init (argv [0]);

	type_table = g_hash_table_new (NULL, NULL);
	method_table = g_hash_table_new (NULL, NULL);
	field_table = g_hash_table_new (NULL, NULL);
	image_table = g_hash_table_new (NULL, NULL);

	for (i = 1; i < argc; ++i) {
		all_meta = FALSE;
		aname = argv [i];
		if (strcmp (aname, "-v") == 0) {
			verbose++;
			continue;
		} else if (strcmp (aname, "-e") == 0) {
			force_enums = 1;
			continue;
		} else if (strcmp (aname, "-h") == 0) {
			usage (0);
		} else if (strcmp (aname, "-o") == 0) {
			i++;
			if (i >= argc)
				usage (1);
			outfile = argv [i];
			continue;
		} else if (strcmp (aname, "-F") == 0) {
			i++;
			if (i >= argc)
				usage (1);
			all_meta = TRUE;
			aname = argv [i];
		} else if (strcmp (aname, "-a") == 0) {
			i++;
			if (i >= argc)
				usage (1);
			rootfile = argv [i];
			continue;
		}
		assembly = mono_assembly_open (aname, NULL);
		if (!assembly) {
			g_print ("cannot open assembly %s\n", aname);
			exit (1);
		}
		process_assembly (assembly, all_meta);
	}
	if (!assembly)
		usage (1);
	if (rootfile)
		load_roots (rootfile);
	process_images ();
	dump_images (outfile);

	return 0;
}
Ejemplo n.º 7
0
int
main (void)
{
	int res = 0;
	MonoDomain *domain = NULL;
	MonoAssembly *assembly = NULL;
	MonoImage *prog_image = NULL;
	MonoImage *corlib = NULL;
	MonoClass *prog_klass, *console_klass;
	MonoMethod *meth;
	MonoImageOpenStatus status;

	//FIXME This is a fugly hack due to embedding simply not working from the tree
	mono_set_assemblies_path ("../../mcs/class/lib/net_4_x");

	test_methods = g_array_new (FALSE, TRUE, sizeof (MonoMethod *));
	if (!test_methods) {
		res = 1;
		printf ("FAILED INITIALIZING METHODS ARRAY\n");
		goto out;
	}

	domain = mono_jit_init_version ("TEST RUNNER", "mobile");
	assembly = mono_assembly_open (TESTPROG, &status);
	if (!domain || !assembly) {
		res = 1;
		printf("FAILED LOADING TEST PROGRAM\n");
		goto out;
	}

	mono_callspec_set_assembly(assembly);

	prog_image = mono_assembly_get_image (assembly);

	prog_klass = test_mono_class_from_name (prog_image, "Baz", "Foo");
	if (!prog_klass) {
		res = 1;
		printf ("FAILED FINDING Baz.Foo\n");
		goto out;
	}
	meth = mono_class_get_method_from_name (prog_klass, "Bar", 0);
	if (!meth) {
		res = 1;
		printf ("FAILED FINDING Baz.Foo:Bar ()\n");
		goto out;
	}
	g_array_append_val (test_methods, meth);
	meth = mono_class_get_method_from_name (prog_klass, "Bar", 1);
	if (!meth) {
		res = 1;
		printf ("FAILED FINDING Baz.Foo:Bar (string)\n");
		goto out;
	}
	g_array_append_val (test_methods, meth);

	prog_klass = test_mono_class_from_name (prog_image, "Baz", "Goo");
	if (!prog_klass) {
		res = 1;
		printf ("FAILED FINDING Baz.Goo\n");
		goto out;
	}
	meth = mono_class_get_method_from_name (prog_klass, "Bar", 1);
	if (!meth) {
		res = 1;
		printf ("FAILED FINDING Baz.Goo:Bar (string)\n");
		goto out;
	}
	g_array_append_val (test_methods, meth);

	prog_klass = test_mono_class_from_name (prog_image, "Baz", "Foo2");
	if (!prog_klass) {
		res = 1;
		printf ("FAILED FINDING Baz.Foo2\n");
		goto out;
	}
	meth = mono_class_get_method_from_name (prog_klass, "Bar", 1);
	if (!meth) {
		res = 1;
		printf ("FAILED FINDING Baz.Foo2:Bar (string)\n");
		goto out;
	}
	g_array_append_val (test_methods, meth);

	corlib = mono_get_corlib ();

	console_klass = test_mono_class_from_name (corlib, "System", "Console");
	if (!console_klass) {
		res = 1;
		printf ("FAILED FINDING System.Console\n");
		goto out;
	}
	meth = mono_class_get_method_from_name (console_klass, "WriteLine", 1);
	if (!meth) {
		res = 1;
		printf ("FAILED FINDING System.Console:WriteLine\n");
		goto out;
	}
	g_array_append_val (test_methods, meth);

	res = test_all_callspecs ();
out:
	return res;
}
Ejemplo n.º 8
0
int
main (int argc, char *argv [])
{
	int image_result = 0;
	MonoImage *image;
	char *file = NULL;
	char *flags = NULL;
	MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE;
	const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", "partial-md", NULL};
	guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, VERIFY_CODE_ONLY, MONO_VERIFY_FAIL_FAST, MONO_VERIFY_NON_STRICT, VALID_ONLY_FLAG, VERIFY_METADATA_ONLY, VERIFY_PARTIAL_METADATA, 0};
	int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0;
	
	for (i = 1; i < argc; i++){
		if (argv [i][0] != '-'){
			file = argv [i];
			continue;
		}

		if (strcmp (argv [i], "--help") == 0)
			usage ();
		else if (strcmp (argv [i], "--verify") == 0) {
			verify_pe = 1;
			dump_data = 0;
			++i;
			flags = argv [i];
		} else {
			usage ();
		}
	}
	
	if (!file)
		usage ();

#ifndef DISABLE_PERFCOUNTERS
	mono_perfcounters_init ();
#endif
	mono_counters_init ();
	mono_metadata_init ();
	mono_images_init ();
	mono_assemblies_init ();
	mono_loader_init ();
 
	if (verify_pe) {
		char *tok = strtok (flags, ",");

		verify_metadata = 1;
		verify_code = 0;
		while (tok) {
			for (i = 0; flag_desc [i]; ++i) {
				if (strcmp (tok, flag_desc [i]) == 0) {
					if (flag_vals [i] == VERIFY_CODE_ONLY) {
						verify_metadata = 0;
						verify_code = 1;
					} else if(flag_vals [i] == MONO_VERIFY_ALL) {
						verify_code = 1;
					} else if(flag_vals [i] == VERIFY_METADATA_ONLY) {
						verify_metadata = 0;
						run_new_metadata_verifier = 1;
					} else if(flag_vals [i] == VERIFY_PARTIAL_METADATA) {
						verify_partial_md = 1;
					}
					if (flag_vals [i] == VALID_ONLY_FLAG)
						verifier_mode = MONO_VERIFIER_MODE_VALID;
					else
						verify_flags |= flag_vals [i];
					break;
				}
			}
			if (!flag_desc [i])
				g_print ("Unknown verify flag %s\n", tok);
			tok = strtok (NULL, ",");
		}

		mono_verifier_set_mode (verifier_mode);
		/**/
	}

	if (verify_pe || run_new_metadata_verifier) {
		run_new_metadata_verifier = 1;
	}
	
	if (run_new_metadata_verifier) {
		mono_verifier_set_mode (verifier_mode);

		image_result = verify_image_file (file);
		if (image_result == 1 || !verify_code)
			return image_result;
	}

	image = mono_image_open (file, NULL);
	if (!image){
		fprintf (stderr, "Cannot open image %s\n", file);
		exit (1);
	}

	if (dump_data)
		dump_dotnet_iinfo (image);
	if (verify_pe) {
		MonoAssembly *assembly;
		MonoImage *image;
		MonoImageOpenStatus status;
		int code_result;

		mono_verifier_set_mode (verifier_mode);

		assembly = mono_assembly_open (file, NULL);
		/*fake an assembly for netmodules so the verifier works*/
		if (!assembly && (image = mono_image_open (file, &status)) && image->tables [MONO_TABLE_ASSEMBLY].rows == 0) {
			assembly = g_new0 (MonoAssembly, 1);
			assembly->in_gac = FALSE;
			assembly->image = image;
			image->assembly = assembly;
		}

		if (!assembly) {
			g_print ("Could not open assembly %s\n", file);
			return 4;
		}

		code_result = dump_verify_info (assembly->image, verify_flags);
		return code_result ? code_result : image_result;
	} else
		mono_image_close (image);
	
	return 0;
}
Ejemplo n.º 9
0
static void CDECL ReallyFixupVTable(struct dll_fixup *fixup)
{
    HRESULT hr=S_OK;
    WCHAR filename[MAX_PATH];
    ICLRRuntimeInfo *info=NULL;
    RuntimeHost *host;
    char *filenameA;
    MonoImage *image=NULL;
    MonoAssembly *assembly=NULL;
    MonoImageOpenStatus status=0;
    MonoDomain *domain;

    if (fixup->done) return;

    /* It's possible we'll have two threads doing this at once. This is
     * considered preferable to the potential deadlock if we use a mutex. */

    GetModuleFileNameW(fixup->dll, filename, MAX_PATH);

    TRACE("%p,%p,%s\n", fixup, fixup->dll, debugstr_w(filename));

    filenameA = WtoA(filename);
    if (!filenameA)
        hr = E_OUTOFMEMORY;

    if (SUCCEEDED(hr))
        hr = get_runtime_info(filename, NULL, NULL, 0, 0, FALSE, &info);

    if (SUCCEEDED(hr))
        hr = ICLRRuntimeInfo_GetRuntimeHost(info, &host);

    if (SUCCEEDED(hr))
        hr = RuntimeHost_GetDefaultDomain(host, &domain);

    if (SUCCEEDED(hr))
    {
        mono_thread_attach(domain);

        assembly = mono_assembly_open(filenameA, &status);
    }

    if (assembly)
    {
        int i;

        /* Mono needs an image that belongs to an assembly. */
        image = mono_assembly_get_image(assembly);

        if (fixup->fixup->type & COR_VTABLE_32BIT)
        {
            DWORD *vtable = fixup->vtable;
            DWORD *tokens = fixup->tokens;
            for (i=0; i<fixup->fixup->count; i++)
            {
                TRACE("%x\n", tokens[i]);
                vtable[i] = PtrToUint(mono_marshal_get_vtfixup_ftnptr(
                    image, tokens[i], fixup->fixup->type));
            }
        }

        fixup->done = TRUE;
    }

    if (info != NULL)
        ICLRRuntimeInfo_Release(info);

    HeapFree(GetProcessHeap(), 0, filenameA);

    if (!fixup->done)
    {
        ERR("unable to fixup vtable, hr=%x, status=%d\n", hr, status);
        /* If we returned now, we'd get an infinite loop. */
        assert(0);
    }
}