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

	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_checked (method, NULL, params, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */
}
Esempio n. 2
0
MonoException *
mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
{
	MonoError error;
	MonoClass *klass;
	MonoObject *o;
	MonoMethod *method;
	MonoDomain *domain = mono_domain_get ();
	gpointer params [16];

	klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");

	o = mono_object_new_checked (domain, klass, &error);
	g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */

	method = mono_class_get_method_from_name (klass, ".ctor", 1);
	g_assert (method);

	params [0] = wrapped_exception;

	mono_runtime_invoke_checked (method, o, params, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */

	return (MonoException *)o;
}	
Esempio n. 3
0
/**
 * mono_exception_from_name_two_strings:
 * @image: the Mono image where to look for the class
 * @name_space: the namespace for the class
 * @name: class name
 * @a1: first string argument to pass
 * @a2: second string argument to pass
 *
 * Creates an exception from a constructor that takes two string
 * arguments.
 *
 * Returns: the initialized exception instance.
 */
MonoException *
mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
				      const char *name, MonoString *a1, MonoString *a2)
{
	MonoError error;
	MonoClass *klass;
	MonoException *ret;

	klass = mono_class_load_from_name (image, name_space, name);

	ret = create_exception_two_strings (klass, a1, a2, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */

	return ret;
}
Esempio n. 4
0
/**
 * mono_mlist_alloc:
 * @data: object to use as data
 *
 * Allocates a new managed list node with @data as the contents.
 * A managed list node also represents a singly-linked list.
 * Managed lists are garbage collected, so there is no free routine
 * and the user is required to keep references to the managed list
 * to prevent it from being garbage collected.
 */
MonoMList*
mono_mlist_alloc (MonoObject *data)
{
	MonoError error;
	MonoMList* res;
	if (!monolist_item_vtable) {
		MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoListItem");
		monolist_item_vtable = mono_class_vtable (mono_get_root_domain (), klass);
		g_assert (monolist_item_vtable);
	}
	res = (MonoMList*)mono_object_new_fast_checked (monolist_item_vtable, &error);
	mono_error_raise_exception (&error);
	MONO_OBJECT_SETREF (res, data, data);
	return res;
}
Esempio n. 5
0
/**
 * mono_exception_from_token_two_strings:
 *
 *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
 * IMAGE and TOKEN.
 */
MonoException *
mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
									   MonoString *a1, MonoString *a2)
{
	MonoError error;
	MonoClass *klass;
	MonoException *ret;

	klass = mono_class_get_checked (image, token, &error);
	mono_error_assert_ok (&error); /* FIXME handle the error. */

	ret = create_exception_two_strings (klass, a1, a2, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */

	return ret;
}
Esempio n. 6
0
static MonoException *
create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
{
	MonoError error;
	MonoDomain *domain = mono_domain_get ();
	MonoMethod *method = NULL;
	MonoObject *o;
	int count = 1;
	gpointer args [2];
	gpointer iter;
	MonoMethod *m;

	if (a2 != NULL)
		count++;
	
	o = mono_object_new_checked (domain, klass, &error);
	mono_error_assert_ok (&error);

	iter = NULL;
	while ((m = mono_class_get_methods (klass, &iter))) {
		MonoMethodSignature *sig;
		
		if (strcmp (".ctor", mono_method_get_name (m)))
			continue;
		sig = mono_method_signature (m);
		if (sig->param_count != count)
			continue;

		if (sig->params [0]->type != MONO_TYPE_STRING)
			continue;
		if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
			continue;
		method = m;
		break;
	}

	args [0] = a1;
	args [1] = a2;

	mono_runtime_invoke_checked (method, o, args, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */

	return (MonoException *) o;
}
Esempio n. 7
0
/* http://www.dotnet247.com/247reference/msgs/39/195403.aspx
// internal static string[] WindowsIdentity._GetRoles (IntPtr token)
*/
MonoArray*
ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token) 
{
	MonoArray *array = NULL;
	MonoDomain *domain = mono_domain_get (); 
#ifdef HOST_WIN32
	gint32 size = 0;

	GetTokenInformation (token, TokenGroups, NULL, size, (PDWORD)&size);
	if (size > 0) {
		TOKEN_GROUPS *tg = g_malloc0 (size);
		if (GetTokenInformation (token, TokenGroups, tg, size, (PDWORD)&size)) {
			int i=0;
			int num = tg->GroupCount;

			array = mono_array_new (domain, mono_get_string_class (), num);

			for (i=0; i < num; i++) {
				gint32 size = 0;
				gunichar2 *uniname = GetSidName (NULL, tg->Groups [i].Sid, &size);

				if (uniname) {
					MonoError error;
					MonoString *str = mono_string_new_utf16_checked (domain, uniname, size, &error);
					mono_error_raise_exception (&error);
					mono_array_setref (array, i, str);
					g_free (uniname);
				}
			}
		}
		g_free (tg);
	}
#else
	/* POSIX-compliant systems should use IsMemberOfGroupId or IsMemberOfGroupName */
	g_warning ("WindowsIdentity._GetRoles should never be called on POSIX");
#endif
	if (!array) {
		/* return empty array of string, i.e. string [0] */
		array = mono_array_new (domain, mono_get_string_class (), 0);
	}
	return array;
}
Esempio n. 8
0
MonoString*
ves_icall_System_Security_Principal_WindowsIdentity_GetTokenName (gpointer token)
{
	MonoError error;
	MonoString *result = NULL;
	gunichar2 *uniname = NULL;
	gint32 size = 0;

	mono_error_init (&error);
#ifdef HOST_WIN32
	GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size);
	if (size > 0) {
		TOKEN_USER *tu = g_malloc0 (size);
		if (GetTokenInformation (token, TokenUser, tu, size, (PDWORD)&size)) {
			uniname = GetSidName (NULL, tu->User.Sid, &size);
		}
		g_free (tu);
	}
#else 
	gchar *uname = GetTokenName ((uid_t) GPOINTER_TO_INT (token));

	if (uname) {
		size = strlen (uname);
		uniname = g_utf8_to_utf16 (uname, size, NULL, NULL, NULL);
		g_free (uname);
	}
#endif /* HOST_WIN32 */

	if (size > 0) {
		result = mono_string_new_utf16_checked (mono_domain_get (), uniname, size, &error);
	}
	else
		result = mono_string_new (mono_domain_get (), "");

	if (uniname)
		g_free (uniname);

	mono_error_raise_exception (&error);
	return result;
}
Esempio n. 9
0
/**
 * mono_get_exception_reflection_type_load:
 * @types: an array of types that were defined in the moduled loaded.
 * @exceptions: an array of exceptions that were thrown during the type loading.
 *
 * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
 */
MonoException *
mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
{
	MonoError error;
	MonoClass *klass;
	gpointer args [2];
	MonoObject *exc;
	MonoMethod *method;
	gpointer iter;

	klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");

	mono_class_init (klass);

	/* Find the Type[], Exception[] ctor */
	iter = NULL;
	while ((method = mono_class_get_methods (klass, &iter))) {
		if (!strcmp (".ctor", mono_method_get_name (method))) {
			MonoMethodSignature *sig = mono_method_signature (method);

			if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
				break;
		}
		method = NULL;
	}
	g_assert (method);

	args [0] = types;
	args [1] = exceptions;

	exc = mono_object_new_checked (mono_domain_get (), klass, &error);
	mono_error_assert_ok (&error);

	mono_runtime_invoke_checked (method, exc, args, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */

	return (MonoException *) exc;
}
Esempio n. 10
0
/**
 * mono_get_exception_type_initialization:
 * @type_name: the name of the type that failed to initialize.
 * @inner: the inner exception.
 *
 * Returns: a new instance of the `System.TypeInitializationException`
 */
MonoException *
mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
{
	MonoError error;
	MonoClass *klass;
	gpointer args [2];
	MonoObject *exc;
	MonoMethod *method;
	gpointer iter;

	klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");

	mono_class_init (klass);

	iter = NULL;
	while ((method = mono_class_get_methods (klass, &iter))) {
		if (!strcmp (".ctor", mono_method_get_name (method))) {
			MonoMethodSignature *sig = mono_method_signature (method);

			if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
				break;
		}
		method = NULL;
	}
	g_assert (method);

	args [0] = mono_string_new (mono_domain_get (), type_name);
	args [1] = inner;

	exc = mono_object_new_checked (mono_domain_get (), klass, &error);
	mono_error_assert_ok (&error);
	mono_runtime_invoke_checked (method, exc, args, &error);
	mono_error_raise_exception (&error); /* FIXME don't raise here */

	return (MonoException *) exc;
}
Esempio n. 11
0
__int32 STDMETHODCALLTYPE _CorExeMain(void)
{
	MonoError error;
	MonoDomain* domain;
	MonoAssembly* assembly;
	MonoImage* image;
	MonoMethod* method;
	guint32 entry;
	gchar* file_name;
	gchar* corlib_version_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);

	corlib_version_error = (gchar*) mono_check_corlib_version ();
	if (corlib_version_error) {
		g_free (corlib_version_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_checked (image, entry, NULL, NULL, &error);
	if (method == NULL) {
		g_free (file_name);
		mono_error_cleanup (&error); /* FIXME don't swallow the error */
		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_checked (method, argc, argv, &error);
	mono_error_raise_exception (&error); /* OK, triggers unhandled exn handler */
	mono_thread_manage ();

	mono_runtime_quit ();

	/* return does not terminate the process. */
	ExitProcess (mono_environment_exitcode_get ());
}