Esempio n. 1
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;
}
Esempio n. 2
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;
	MonoThreadInfoRuntimeCallbacks ticallbacks;
	
	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 ();

	//We have to force the runtime to load the corlib under verification as its own corlib so core types are properly populated in mono_defaults.
	if (strstr (file, "mscorlib.dll"))
		g_setenv ("MONO_PATH", g_path_get_dirname (file), 1);
	assembly_directory [0] = g_path_get_dirname (file);
	assembly_directory [1] = NULL;

#ifndef DISABLE_PERFCOUNTERS
	mono_perfcounters_init ();
#endif
	mono_counters_init ();
	mono_tls_init_runtime_keys ();
	memset (&ticallbacks, 0, sizeof (ticallbacks));
	ticallbacks.thread_state_init = thread_state_init;
#ifndef HOST_WIN32
	mono_w32handle_init ();
#endif
	mono_thread_info_runtime_init (&ticallbacks);

	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) {
		MonoAssemblyOpenRequest req;
		MonoAssembly *assembly;
		MonoImage *image;
		MonoImageOpenStatus status;
		int code_result;

		mono_verifier_set_mode (verifier_mode);

		mono_assembly_request_prepare (&req.request, sizeof (req), MONO_ASMCTX_DEFAULT);
		assembly = mono_assembly_request_open (file, &req, 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, verifier_mode == MONO_VERIFIER_MODE_VALID);
		return code_result ? code_result : image_result;
	} else
		mono_image_close (image);
	
	return 0;
}