#include <mono/utils/mono-memory-model.h>
#include <mono/utils/checked-build.h>
#include <mono/utils/mono-threads.h>
+#include <mono/utils/mono-threads-coop.h>
#include "cominterop.h"
static void
MonoException *exc_to_store = mono_error_convert_to_exception (error);
/* What we really want to do here is clone the error object and store one copy in the
* domain's exception hash and use the other one to error out here. */
+ mono_error_init (error);
mono_error_set_exception_instance (error, exc_to_store);
/*
* Store the exception object so it could be thrown on subsequent
#ifndef DISABLE_REMOTING
static gpointer
-default_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target)
+default_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target, MonoError *error)
{
g_error ("remoting not installed");
return NULL;
* re-acquire them and check if another thread has created the vtable in the meantime.
*/
/* Special case System.MonoType to avoid infinite recursion */
- if (klass != mono_defaults.monotype_class) {
+ if (klass != mono_defaults.runtimetype_class) {
vt->type = mono_type_get_object_checked (domain, &klass->byval_arg, error);
if (!is_ok (error)) {
mono_domain_unlock (domain);
return NULL;
}
- if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.monotype_class)
+ if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.runtimetype_class)
/* This is unregistered in
unregister_vtable_reflection_type() in
domain.c. */
klass->runtime_info = runtime_info;
}
- if (klass == mono_defaults.monotype_class) {
+ if (klass == mono_defaults.runtimetype_class) {
vt->type = mono_type_get_object_checked (domain, &klass->byval_arg, error);
if (!is_ok (error)) {
mono_domain_unlock (domain);
return NULL;
}
- if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.monotype_class)
+ if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.runtimetype_class)
/* This is unregistered in
unregister_vtable_reflection_type() in
domain.c. */
* mono_class_proxy_vtable:
* @domain: the application domain
* @remove_class: the remote class
+ * @error: set on error
*
* Creates a vtable for transparent proxies. It is basically
* a copy of the real vtable of the class wrapped in @remote_class,
* but all function pointers invoke the remoting functions, and
* vtable->klass points to the transparent proxy class, and not to @class.
+ *
+ * On failure returns NULL and sets @error
*/
static MonoVTable *
-mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRemotingTarget target_type)
+mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRemotingTarget target_type, MonoError *error)
{
MONO_REQ_GC_UNSAFE_MODE;
- MonoError error;
MonoVTable *vt, *pvt;
int i, j, vtsize, max_interface_id, extra_interface_vtsize = 0;
MonoClass *k;
GSList *extra_interfaces = NULL;
MonoClass *klass = remote_class->proxy_class;
gpointer *interface_offsets;
- uint8_t *bitmap;
+ uint8_t *bitmap = NULL;
int bsize;
size_t imt_table_bytes;
int bcsize;
#endif
+ mono_error_init (error);
+
vt = mono_class_vtable (domain, klass);
g_assert (vt); /*FIXME property handle failure*/
max_interface_id = vt->max_interface_id;
method_count = mono_class_num_methods (iclass);
- ifaces = mono_class_get_implemented_interfaces (iclass, &error);
- g_assert (mono_error_ok (&error)); /*FIXME do proper error handling*/
+ ifaces = mono_class_get_implemented_interfaces (iclass, error);
+ if (!is_ok (error))
+ goto failure;
if (ifaces) {
for (i = 0; i < ifaces->len; ++i) {
MonoClass *ic = (MonoClass *)g_ptr_array_index (ifaces, i);
method_count += mono_class_num_methods (ic);
}
g_ptr_array_free (ifaces, TRUE);
+ ifaces = NULL;
}
extra_interface_vtsize += method_count * sizeof (gpointer);
for (i = 0; i < klass->vtable_size; ++i) {
MonoMethod *cm;
- if ((cm = klass->vtable [i]))
- pvt->vtable [i] = arch_create_remoting_trampoline (domain, cm, target_type);
- else
+ if ((cm = klass->vtable [i])) {
+ pvt->vtable [i] = arch_create_remoting_trampoline (domain, cm, target_type, error);
+ if (!is_ok (error))
+ goto failure;
+ } else
pvt->vtable [i] = NULL;
}
MonoMethod* m;
gpointer iter = NULL;
while ((m = mono_class_get_methods (k, &iter)))
- if (!pvt->vtable [m->slot])
- pvt->vtable [m->slot] = arch_create_remoting_trampoline (domain, m, target_type);
+ if (!pvt->vtable [m->slot]) {
+ pvt->vtable [m->slot] = arch_create_remoting_trampoline (domain, m, target_type, error);
+ if (!is_ok (error))
+ goto failure;
+ }
}
}
iter = NULL;
j = 0;
- while ((cm = mono_class_get_methods (interf, &iter)))
- pvt->vtable [slot + j++] = arch_create_remoting_trampoline (domain, cm, target_type);
+ while ((cm = mono_class_get_methods (interf, &iter))) {
+ pvt->vtable [slot + j++] = arch_create_remoting_trampoline (domain, cm, target_type, error);
+ if (!is_ok (error))
+ goto failure;
+ }
slot += mono_class_num_methods (interf);
}
pvt->interface_bitmap = bitmap;
#endif
return pvt;
+failure:
+ if (extra_interfaces)
+ g_slist_free (extra_interfaces);
+#ifdef COMPRESSED_INTERFACE_BITMAP
+ g_free (bitmap);
+#endif
+ return NULL;
}
#endif /* DISABLE_REMOTING */
}
gpointer
-mono_remote_class_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRealProxy *rp)
+mono_remote_class_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRealProxy *rp, MonoError *error)
{
MONO_REQ_GC_UNSAFE_MODE;
+ mono_error_init (error);
+
mono_loader_lock (); /*FIXME mono_class_from_mono_type and mono_class_proxy_vtable take it*/
mono_domain_lock (domain);
if (rp->target_domain_id != -1) {
if (remote_class->xdomain_vtable == NULL)
- remote_class->xdomain_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_APPDOMAIN);
+ remote_class->xdomain_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_APPDOMAIN, error);
mono_domain_unlock (domain);
mono_loader_unlock ();
+ return_val_if_nok (error, NULL);
return remote_class->xdomain_vtable;
}
if (remote_class->default_vtable == NULL) {
klass = mono_class_from_mono_type (type);
#ifndef DISABLE_COM
if ((mono_class_is_com_object (klass) || (mono_class_get_com_object_class () && klass == mono_class_get_com_object_class ())) && !mono_vtable_is_remote (mono_class_vtable (mono_domain_get (), klass)))
- remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_COMINTEROP);
+ remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_COMINTEROP, error);
else
#endif
- remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_UNKNOWN);
+ remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_UNKNOWN, error);
+ /* N.B. both branches of the if modify error */
+ if (!is_ok (error)) {
+ mono_domain_unlock (domain);
+ mono_loader_unlock ();
+ return NULL;
+ }
}
mono_domain_unlock (domain);
* @domain: the application domain
* @tproxy: the proxy whose remote class has to be upgraded.
* @klass: class to which the remote class can be casted.
+ * @error: set on error
*
* Updates the vtable of the remote class by adding the necessary method slots
* and interface offsets so it can be safely casted to klass. klass can be a
- * class or an interface.
+ * class or an interface. On success returns TRUE, on failure returns FALSE and sets @error.
*/
-void
-mono_upgrade_remote_class (MonoDomain *domain, MonoObject *proxy_object, MonoClass *klass)
+gboolean
+mono_upgrade_remote_class (MonoDomain *domain, MonoObject *proxy_object, MonoClass *klass, MonoError *error)
{
MONO_REQ_GC_UNSAFE_MODE;
MonoRemoteClass *remote_class;
gboolean redo_vtable;
+ mono_error_init (error);
mono_loader_lock (); /*FIXME mono_remote_class_vtable requires it.*/
mono_domain_lock (domain);
if (redo_vtable) {
tproxy->remote_class = clone_remote_class (domain, remote_class, klass);
- proxy_object->vtable = (MonoVTable *)mono_remote_class_vtable (domain, tproxy->remote_class, tproxy->rp);
+ proxy_object->vtable = (MonoVTable *)mono_remote_class_vtable (domain, tproxy->remote_class, tproxy->rp, error);
+ if (!is_ok (error))
+ goto leave;
}
+leave:
mono_domain_unlock (domain);
mono_loader_unlock ();
+ return is_ok (error);
}
#endif /* DISABLE_REMOTING */
if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
mono_profiler_method_start_invoke (method);
- MONO_ENTER_GC_UNSAFE;
-
result = callbacks.runtime_invoke (method, obj, params, exc, error);
- MONO_EXIT_GC_UNSAFE;
-
if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
mono_profiler_method_end_invoke (method);
mono_error_cleanup (&error);
} else {
res = mono_runtime_invoke_checked (method, obj, params, &error);
- mono_error_raise_exception (&error);
+ mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
}
return res;
}
}
} else {
MonoObject *result = mono_runtime_delegate_invoke_checked (delegate, params, &error);
- mono_error_raise_exception (&error); /* FIXME don't raise here */
+ mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
return result;
}
}
}
} else {
MonoObject *result = mono_runtime_try_invoke_array (method, obj, params, NULL, &error);
- mono_error_raise_exception (&error); /* FIXME don't raise here */
+ mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
return result;
}
}
if (*(MonoBoolean *) mono_object_unbox(res)) {
/* Update the vtable of the remote type, so it can safely cast to this new type */
- mono_upgrade_remote_class (domain, obj, klass);
+ mono_upgrade_remote_class (domain, obj, klass, error);
+ return_val_if_nok (error, NULL);
return obj;
}
}
return interned;
}
+/*
+ * mono_ldstr_utf8:
+ *
+ * Same as mono_ldstr, but return a NULL terminated utf8 string instead
+ * of an object.
+ */
+char*
+mono_ldstr_utf8 (MonoImage *image, guint32 idx, MonoError *error)
+{
+ const char *str;
+ size_t len2;
+ long written = 0;
+ char *as;
+ GError *gerror = NULL;
+
+ mono_error_init (error);
+
+ if (!mono_verifier_verify_string_signature (image, idx, NULL))
+ return NULL; /*FIXME we should probably be raising an exception here*/
+ str = mono_metadata_user_string (image, idx);
+
+ len2 = mono_metadata_decode_blob_size (str, &str);
+ len2 >>= 1;
+
+ as = g_utf16_to_utf8 ((guint16*)str, len2, NULL, &written, &gerror);
+ if (gerror) {
+ mono_error_set_argument (error, "string", "%s", gerror->message);
+ g_error_free (gerror);
+ return NULL;
+ }
+ /* g_utf16_to_utf8 may not be able to complete the convertion (e.g. NULL values were found, #335488) */
+ if (len2 > written) {
+ /* allocate the total length and copy the part of the string that has been converted */
+ char *as2 = (char *)g_malloc0 (len2);
+ memcpy (as2, as, written);
+ g_free (as);
+ as = as2;
+ }
+
+ return as;
+}
+
/**
* mono_string_to_utf8:
* @s: a System.String
MonoError error;
char *result = mono_string_to_utf8_checked (s, &error);
- if (!mono_error_ok (&error))
- mono_error_raise_exception (&error);
+ if (!is_ok (&error)) {
+ mono_error_cleanup (&error);
+ return NULL;
+ }
return result;
}
}
/**
- * mono_object_to_string:
+ * prepare_to_string_method:
* @obj: The object
- * @exc: Any exception thrown by ToString (). May be NULL.
+ * @target: Set to @obj or unboxed value if a valuetype
*
- * Returns: the result of calling ToString () on an object.
+ * Returns: the ToString override for @obj. If @obj is a valuetype, @target is unboxed otherwise it's @obj.
*/
-MonoString *
-mono_object_to_string (MonoObject *obj, MonoObject **exc)
+static MonoMethod *
+prepare_to_string_method (MonoObject *obj, void **target)
{
MONO_REQ_GC_UNSAFE_MODE;
static MonoMethod *to_string = NULL;
- MonoError error;
MonoMethod *method;
- MonoString *s;
- void *target = obj;
-
+ g_assert (target);
g_assert (obj);
+ *target = obj;
+
if (!to_string)
to_string = mono_class_get_method_from_name_flags (mono_get_object_class (), "ToString", 0, METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_PUBLIC);
// Unbox value type if needed
if (mono_class_is_valuetype (mono_method_get_class (method))) {
- target = mono_object_unbox (obj);
+ *target = mono_object_unbox (obj);
}
+ return method;
+}
+/**
+ * mono_object_to_string:
+ * @obj: The object
+ * @exc: Any exception thrown by ToString (). May be NULL.
+ *
+ * Returns: the result of calling ToString () on an object.
+ */
+MonoString *
+mono_object_to_string (MonoObject *obj, MonoObject **exc)
+{
+ MonoError error;
+ MonoString *s = NULL;
+ void *target;
+ MonoMethod *method = prepare_to_string_method (obj, &target);
if (exc) {
s = (MonoString *) mono_runtime_try_invoke (method, target, NULL, exc, &error);
if (*exc == NULL && !mono_error_ok (&error))
mono_error_cleanup (&error);
} else {
s = (MonoString *) mono_runtime_invoke_checked (method, target, NULL, &error);
- mono_error_raise_exception (&error); /* FIXME don't raise here */
+ mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
}
return s;
}
+/**
+ * mono_object_to_string_checked:
+ * @obj: The object
+ * @error: Set on error.
+ *
+ * Returns: the result of calling ToString () on an object. If the
+ * method cannot be invoked or if it raises an exception, sets @error
+ * and returns NULL.
+ */
+MonoString *
+mono_object_to_string_checked (MonoObject *obj, MonoError *error)
+{
+ mono_error_init (error);
+ void *target;
+ MonoMethod *method = prepare_to_string_method (obj, &target);
+ return (MonoString*) mono_runtime_invoke_checked (method, target, NULL, error);
+}
+
+/**
+ * mono_object_try_to_string:
+ * @obj: The object
+ * @exc: Any exception thrown by ToString (). Must not be NULL.
+ * @error: Set if method cannot be invoked.
+ *
+ * Returns: the result of calling ToString () on an object. If the
+ * method cannot be invoked sets @error, if it raises an exception sets @exc,
+ * and returns NULL.
+ */
+MonoString *
+mono_object_try_to_string (MonoObject *obj, MonoObject **exc, MonoError *error)
+{
+ g_assert (exc);
+ mono_error_init (error);
+ void *target;
+ MonoMethod *method = prepare_to_string_method (obj, &target);
+ return (MonoString*) mono_runtime_try_invoke (method, target, NULL, exc, error);
+}
+
+
+
/**
* mono_print_unhandled_exception:
* @exc: The exception
free_message = TRUE;
} else {
MonoObject *other_exc = NULL;
- str = mono_object_to_string (exc, &other_exc);
+ str = mono_object_try_to_string (exc, &other_exc, &error);
+ if (other_exc == NULL && !is_ok (&error))
+ other_exc = (MonoObject*)mono_error_convert_to_exception (&error);
+ else
+ mono_error_cleanup (&error);
if (other_exc) {
char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other_exc);