[runtime] MonoError-ize mono_compile_method
[mono.git] / mono / metadata / remoting.c
index 6555cdec7a0d49f1e7da05a0cd841ca99bf47139..8a8b22676ed1301f5217958956ee6062337ce4e1 100644 (file)
@@ -81,6 +81,9 @@ static MonoMethod *method_rs_serialize, *method_rs_deserialize, *method_exc_fixe
 static MonoMethod *method_set_call_context, *method_needs_context_sink, *method_rs_serialize_exc;
 #endif
 
+static gpointer
+mono_compile_method_icall (MonoMethod *method);
+
 static void
 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
 {
@@ -194,6 +197,10 @@ mono_remoting_marshal_init (void)
                register_icall (mono_marshal_xdomain_copy_out_value, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE);
                register_icall (mono_remoting_wrapper, "mono_remoting_wrapper", "object ptr ptr", FALSE);
                register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
+               register_icall (mono_compile_method_icall, "mono_compile_method_icall", "ptr ptr", FALSE);
+               /* mono_load_remote_field_new_icall registered  by mini-runtime.c */
+               /* mono_store_remote_field_new_icall registered  by mini-runtime.c */
+
        }
 
        icalls_registered = TRUE;
@@ -376,7 +383,8 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                                        /* runtime_invoke expects a boxed instance */
                                        if (mono_class_is_nullable (mono_class_from_mono_type (sig->params [i]))) {
                                                mparams[i] = mono_nullable_box ((guint8 *)params [i], klass, &error);
-                                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                                               if (!is_ok (&error))
+                                                       goto fail;
                                        } else
                                                mparams[i] = params [i];
                                }
@@ -386,7 +394,8 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                }
 
                res = mono_runtime_invoke_checked (method, method->klass->valuetype? mono_object_unbox ((MonoObject*)this_obj): this_obj, mparams, &error);
-               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               if (!is_ok (&error))
+                       goto fail;
 
                return res;
        }
@@ -394,16 +403,31 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
        msg = mono_method_call_message_new (method, params, NULL, NULL, NULL);
 
        res = mono_remoting_invoke ((MonoObject *)this_obj->rp, msg, &exc, &out_args, &error);
-       mono_error_raise_exception (&error); /* FIXME don't raise here */
+       if (!is_ok (&error))
+               goto fail;
 
-       if (exc)
-               mono_raise_exception ((MonoException *)exc);
+       if (exc) {
+               mono_error_init (&error);
+               mono_error_set_exception_instance (&error, (MonoException *)exc);
+               goto fail;
+       }
 
-       mono_method_return_message_restore (method, params, out_args);
+       mono_method_return_message_restore (method, params, out_args, &error);
+       if (!is_ok (&error)) goto fail;
 
        return res;
+fail:
+       /* This icall will be called from managed code, and more over
+        * from a protected wrapper so interruptions such as pending
+        * exceptions will not be honored.  (See
+        * is_running_protected_wrapper () in threads.c and
+        * mono_marshal_get_remoting_invoke () in remoting.c)
+        */
+       mono_error_raise_exception (&error);
+       return NULL;
 } 
 
+
 MonoMethod *
 mono_marshal_get_remoting_invoke (MonoMethod *method)
 {
@@ -451,6 +475,11 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
        mono_mb_emit_ptr (mb, method);
        mono_mb_emit_ldloc (mb, params_var);
        mono_mb_emit_icall (mb, mono_remoting_wrapper);
+       // FIXME: this interrupt checkpoint code is a no-op since 'mb'
+       //  is a MONO_WRAPPER_REMOTING_INVOKE, and
+       //  mono_thread_interruption_checkpoint_request (FALSE)
+       //  considers such wrappers "protected" and always returns
+       //  NULL as if there's no pending interruption.
        mono_marshal_emit_thread_interrupt_checkpoint (mb);
 
        if (sig->ret->type == MONO_TYPE_VOID) {
@@ -534,8 +563,10 @@ mono_marshal_set_domain_by_id (gint32 id, MonoBoolean push)
        MonoDomain *current_domain = mono_domain_get ();
        MonoDomain *domain = mono_domain_get_by_id (id);
 
-       if (!domain || !mono_domain_set (domain, FALSE))        
-               mono_raise_exception (mono_get_exception_appdomain_unloaded ());
+       if (!domain || !mono_domain_set (domain, FALSE)) {
+               mono_set_pending_exception (mono_get_exception_appdomain_unloaded ());
+               return 0;
+       }
 
        if (push)
                mono_thread_push_appdomain_ref (domain);
@@ -552,6 +583,15 @@ mono_marshal_emit_switch_domain (MonoMethodBuilder *mb)
        mono_mb_emit_icall (mb, mono_marshal_set_domain_by_id);
 }
 
+gpointer
+mono_compile_method_icall (MonoMethod *method)
+{
+       MonoError error;
+       gpointer result = mono_compile_method_checked (method, &error);
+       mono_error_set_pending_exception (&error);
+       return result;
+}
+
 /* mono_marshal_emit_load_domain_method ()
  * Loads into the stack a pointer to the code of the provided method for
  * the current domain.
@@ -563,7 +603,7 @@ mono_marshal_emit_load_domain_method (MonoMethodBuilder *mb, MonoMethod *method)
         * that compiles the method).
         */
        mono_mb_emit_ptr (mb, method);
-       mono_mb_emit_icall (mb, mono_compile_method);
+       mono_mb_emit_icall (mb, mono_compile_method_icall);
 }
 #endif
 
@@ -1206,10 +1246,15 @@ mono_marshal_get_remoting_invoke_for_target (MonoMethod *method, MonoRemotingTar
 G_GNUC_UNUSED static gpointer
 mono_marshal_load_remoting_wrapper (MonoRealProxy *rp, MonoMethod *method)
 {
+       MonoError error;
+       MonoMethod *marshal_method = NULL;
        if (rp->target_domain_id != -1)
-               return mono_compile_method (mono_marshal_get_xappdomain_invoke (method));
+               marshal_method = mono_marshal_get_xappdomain_invoke (method);
        else
-               return mono_compile_method (mono_marshal_get_remoting_invoke (method));
+               marshal_method = mono_marshal_get_remoting_invoke (method);
+       gpointer compiled_ptr = mono_compile_method_checked (marshal_method, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
+       return compiled_ptr;
 }
 
 MonoMethod *
@@ -1285,7 +1330,7 @@ mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
 MonoMethod *
 mono_marshal_get_ldfld_remote_wrapper (MonoClass *klass)
 {
-       MonoMethodSignature *sig, *csig;
+       MonoMethodSignature *sig;
        MonoMethodBuilder *mb;
        MonoMethod *res;
        static MonoMethod* cached = NULL;
@@ -1312,15 +1357,7 @@ mono_marshal_get_ldfld_remote_wrapper (MonoClass *klass)
        mono_mb_emit_ldarg (mb, 1);
        mono_mb_emit_ldarg (mb, 2);
 
-       csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
-       csig->params [0] = &mono_defaults.object_class->byval_arg;
-       csig->params [1] = &mono_defaults.int_class->byval_arg;
-       csig->params [2] = &mono_defaults.int_class->byval_arg;
-       csig->ret = &mono_defaults.object_class->byval_arg;
-       csig->pinvoke = 1;
-
-       mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
-       mono_marshal_emit_thread_interrupt_checkpoint (mb);
+       mono_mb_emit_icall (mb, mono_load_remote_field_new_icall);
 
        mono_mb_emit_byte (mb, CEE_RET);
 #endif
@@ -1643,7 +1680,7 @@ mono_marshal_get_ldflda_wrapper (MonoType *type)
 MonoMethod *
 mono_marshal_get_stfld_remote_wrapper (MonoClass *klass)
 {
-       MonoMethodSignature *sig, *csig;
+       MonoMethodSignature *sig;
        MonoMethodBuilder *mb;
        MonoMethod *res;
        static MonoMethod *cached = NULL;
@@ -1672,16 +1709,7 @@ mono_marshal_get_stfld_remote_wrapper (MonoClass *klass)
        mono_mb_emit_ldarg (mb, 2);
        mono_mb_emit_ldarg (mb, 3);
 
-       csig = mono_metadata_signature_alloc (mono_defaults.corlib, 4);
-       csig->params [0] = &mono_defaults.object_class->byval_arg;
-       csig->params [1] = &mono_defaults.int_class->byval_arg;
-       csig->params [2] = &mono_defaults.int_class->byval_arg;
-       csig->params [3] = &mono_defaults.object_class->byval_arg;
-       csig->ret = &mono_defaults.void_class->byval_arg;
-       csig->pinvoke = 1;
-
-       mono_mb_emit_native_call (mb, csig, mono_store_remote_field_new);
-       mono_marshal_emit_thread_interrupt_checkpoint (mb);
+       mono_mb_emit_icall (mb, mono_store_remote_field_new_icall);
 
        mono_mb_emit_byte (mb, CEE_RET);
 #endif
@@ -2028,7 +2056,9 @@ mono_marshal_xdomain_copy_value (MonoObject *val)
                MonoArray *acopy;
                MonoXDomainMarshalType mt = mono_get_xdomain_marshal_type (&(mono_object_class (val)->element_class->byval_arg));
                if (mt == MONO_MARSHAL_SERIALIZE) return NULL;
-               acopy = mono_array_clone_in_domain (domain, (MonoArray *) val);
+               acopy = mono_array_clone_in_domain (domain, (MonoArray *) val, &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+
                if (mt == MONO_MARSHAL_COPY) {
                        int i, len = mono_array_length (acopy);
                        for (i = 0; i < len; i++) {