static guint64 debugger_compile_method (guint64 method_arg) { MonoMethod *method = (MonoMethod *) GUINT_TO_POINTER ((gsize) method_arg); gpointer addr; mono_debugger_lock (); addr = mono_compile_method (method); mono_debugger_unlock (); return (guint64) (gsize) addr; }
/* * actually, we might want to queue the finalize requests in a separate thread, * but we need to be careful about the execution domain of the thread... */ void mono_gc_run_finalize (void *obj, void *data) { MonoObject *exc = NULL; MonoObject *o; #ifndef HAVE_SGEN_GC MonoObject *o2; #endif MonoMethod* finalizer = NULL; MonoDomain *caller_domain = mono_domain_get (); MonoDomain *domain; RuntimeInvokeFunction runtime_invoke; GSList *l, *refs = NULL; o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data)); if (suspend_finalizers) return; domain = o->vtable->domain; #ifndef HAVE_SGEN_GC mono_domain_finalizers_lock (domain); o2 = g_hash_table_lookup (domain->finalizable_objects_hash, o); refs = mono_gc_remove_weak_track_object (domain, o); mono_domain_finalizers_unlock (domain); if (!o2) /* Already finalized somehow */ return; #endif if (refs) { /* * Support for GCHandles of type WeakTrackResurrection: * * Its not exactly clear how these are supposed to work, or how their * semantics can be implemented. We only implement one crucial thing: * these handles are only cleared after the finalizer has ran. */ for (l = refs; l; l = l->next) { guint32 gchandle = GPOINTER_TO_UINT (l->data); mono_gchandle_set_target (gchandle, o); } g_slist_free (refs); } /* make sure the finalizer is not called again if the object is resurrected */ object_register_finalizer (obj, NULL); if (o->vtable->klass == mono_defaults.internal_thread_class) { MonoInternalThread *t = (MonoInternalThread*)o; if (mono_gc_is_finalizer_internal_thread (t)) /* Avoid finalizing ourselves */ return; if (t->threadpool_thread && finalizing_root_domain) { /* Don't finalize threadpool threads when shutting down - they're finalized when the threadpool shuts down. */ add_thread_to_finalize (t); return; } } if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) { /* * These can't be finalized during unloading/shutdown, since that would * free the native code which can still be referenced by other * finalizers. * FIXME: This is not perfect, objects dying at the same time as * dynamic methods can still reference them even when !shutdown. */ return; } if (mono_runtime_get_no_exec ()) return; /* speedup later... and use a timeout */ /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */ /* Use _internal here, since this thread can enter a doomed appdomain */ mono_domain_set_internal (mono_object_domain (o)); /* delegates that have a native function pointer allocated are * registered for finalization, but they don't have a Finalize * method, because in most cases it's not needed and it's just a waste. */ if (o->vtable->klass->delegate) { MonoDelegate* del = (MonoDelegate*)o; if (del->delegate_trampoline) mono_delegate_free_ftnptr ((MonoDelegate*)o); mono_domain_set_internal (caller_domain); return; } finalizer = mono_class_get_finalizer (o->vtable->klass); #ifndef DISABLE_COM /* If object has a CCW but has no finalizer, it was only * registered for finalization in order to free the CCW. * Else it needs the regular finalizer run. * FIXME: what to do about ressurection and suppression * of finalizer on object with CCW. */ if (mono_marshal_free_ccw (o) && !finalizer) { mono_domain_set_internal (caller_domain); return; } #endif /* * To avoid the locking plus the other overhead of mono_runtime_invoke (), * create and precompile a wrapper which calls the finalize method using * a CALLVIRT. */ if (!domain->finalize_runtime_invoke) { MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE); domain->finalize_runtime_invoke = mono_compile_method (invoke); } runtime_invoke = domain->finalize_runtime_invoke; mono_runtime_class_init (o->vtable); runtime_invoke (o, NULL, &exc, NULL); if (exc) { /* fixme: do something useful */ } mono_domain_set_internal (caller_domain); }
/* * actually, we might want to queue the finalize requests in a separate thread, * but we need to be careful about the execution domain of the thread... */ void mono_gc_run_finalize (void *obj, void *data) { if (do_not_finalize) return; MonoObject *exc = NULL; MonoObject *o; #ifndef HAVE_SGEN_GC MonoObject *o2; #endif MonoMethod* finalizer = NULL; MonoDomain *caller_domain = mono_domain_get (); MonoDomain *domain; RuntimeInvokeFunction runtime_invoke; // This function is called from the innards of the GC, so our best alternative for now is to do polling here MONO_SUSPEND_CHECK (); o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data)); if (log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_DEBUG, "<%s at %p> Starting finalizer checks.", o->vtable->klass->name, o); if (suspend_finalizers) return; domain = o->vtable->domain; #ifndef HAVE_SGEN_GC mono_domain_finalizers_lock (domain); o2 = g_hash_table_lookup (domain->finalizable_objects_hash, o); mono_domain_finalizers_unlock (domain); if (!o2) /* Already finalized somehow */ return; #endif /* make sure the finalizer is not called again if the object is resurrected */ object_register_finalizer (obj, NULL); if (log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Registered finalizer as processed.", o->vtable->klass->name, o); if (o->vtable->klass == mono_defaults.internal_thread_class) { MonoInternalThread *t = (MonoInternalThread*)o; if (mono_gc_is_finalizer_internal_thread (t)) /* Avoid finalizing ourselves */ return; if (t->threadpool_thread && finalizing_root_domain) { /* Don't finalize threadpool threads when shutting down - they're finalized when the threadpool shuts down. */ add_thread_to_finalize (t); return; } } if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) { /* * These can't be finalized during unloading/shutdown, since that would * free the native code which can still be referenced by other * finalizers. * FIXME: This is not perfect, objects dying at the same time as * dynamic methods can still reference them even when !shutdown. */ return; } if (mono_runtime_get_no_exec ()) return; /* speedup later... and use a timeout */ /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */ /* Use _internal here, since this thread can enter a doomed appdomain */ mono_domain_set_internal (mono_object_domain (o)); /* delegates that have a native function pointer allocated are * registered for finalization, but they don't have a Finalize * method, because in most cases it's not needed and it's just a waste. */ if (o->vtable->klass->delegate) { MonoDelegate* del = (MonoDelegate*)o; if (del->delegate_trampoline) mono_delegate_free_ftnptr ((MonoDelegate*)o); mono_domain_set_internal (caller_domain); return; } finalizer = mono_class_get_finalizer (o->vtable->klass); /* If object has a CCW but has no finalizer, it was only * registered for finalization in order to free the CCW. * Else it needs the regular finalizer run. * FIXME: what to do about ressurection and suppression * of finalizer on object with CCW. */ if (mono_marshal_free_ccw (o) && !finalizer) { mono_domain_set_internal (caller_domain); return; } /* * To avoid the locking plus the other overhead of mono_runtime_invoke (), * create and precompile a wrapper which calls the finalize method using * a CALLVIRT. */ if (log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Compiling finalizer.", o->vtable->klass->name, o); if (!domain->finalize_runtime_invoke) { MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE); domain->finalize_runtime_invoke = mono_compile_method (invoke); } runtime_invoke = domain->finalize_runtime_invoke; mono_runtime_class_init (o->vtable); if (G_UNLIKELY (MONO_GC_FINALIZE_INVOKE_ENABLED ())) { MONO_GC_FINALIZE_INVOKE ((unsigned long)o, mono_object_get_size (o), o->vtable->klass->name_space, o->vtable->klass->name); } if (log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Calling finalizer.", o->vtable->klass->name, o); runtime_invoke (o, NULL, &exc, NULL); if (log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Returned from finalizer.", o->vtable->klass->name, o); if (exc) mono_thread_internal_unhandled_exception (exc); mono_domain_set_internal (caller_domain); }
static void replace_method (Package *pkg) { xmono::ReplaceMethodReq req; xmono::ReplaceMethodRsp rsp; std::string str((char*)pkg->body, pkg->all_len - sizeof (Package)); if (!req.ParseFromString (str)) { LOGD ("xmono::ReplaceMethodReq ParseFromString err!"); return; } std::string err; void *p, *old_p; uint8_t *code; int code_size; MonoMethodHeader *mh; MonoThread *thread; MonoMethod *new_method; MonoDomain *domain; MonoMethod * method = get_method_with_token (req.image_name ().c_str (), req.method_token ()); if (!method) { rsp.set_err (false); rsp.set_msg (helper_last_err ()); goto replace_method_end; } domain = mono_domain_get_by_id (req.domain_id ()); if (!domain) { rsp.set_err (false); rsp.set_msg ("can not get the domain from id"); goto replace_method_end; } mh = mono_method_get_header (method); if (req.ex_size () != mono_method_header_get_num_clauses (mh)) { rsp.set_err (false); rsp.set_msg ("ex size != mono_method_header_clauses size!"); goto replace_method_end; } for (int i = 0; i < req.ex_size (); i++) { xmono::ReplaceMethodReq_ExceptionClause const &e = req.ex (i); void *iter = 0; MonoExceptionClause *clauses = &mh->clauses[i]; MonoExceptionClause *old_e = (MonoExceptionClause*)iter; old_e->try_offset = e.try_offset (); old_e->try_len = e.try_len (); old_e->handler_offset = e.handler_offset (); old_e->handler_len = e.handler_len (); } code = new uint8_t[req.new_code ().size ()]; memcpy (code, req.new_code ().c_str (), req.new_code ().size ()); mh->code = code; mh->code_size = req.new_code ().size (); thread = mono_thread_attach (domain); /*128 是一个估计值, 在未来可能不稳定, 但当前只能如此*/ new_method = (MonoMethod*)calloc (128, 1); /*这个地方用malloc优于用new*/ memcpy (new_method, method, 128); pthread_mutex_lock (&replace_mutex); replace_method_dict[new_method] = true; pthread_mutex_unlock (&replace_mutex); p = mono_compile_method (new_method); memcpy (hooked_method_dict[method]->specific_hook + 4, &p, 4); pthread_mutex_lock (&hooked_mutex); old_p = mono_jit_info_get_code_start (hooked_method_dict[method]->jinfo); pthread_mutex_unlock (&hooked_mutex); mono_thread_detach (thread); LOGD ("compile method, new ptr : %p, old ptr : %p", p, old_p); rsp.set_err (true); rsp.set_msg ("replace_method successful."); replace_method_end: std::string out; rsp.SerializeToString (&out); ecmd_send (XMONO_ID_REPLACE_METHOD_RSP, (uint8_t const*)out.c_str (), out.size ()); return; }
void *new_mono_compile_method(MonoMethod *m) { return runInMonoCompileMethodHooker(m, mono_compile_method(m)); }
void CPipeServer::CompileMethod() { void *method=(void *)ReadQword(); void *result=mono_compile_method(method); WriteQword((UINT_PTR)result); }