Exemplo n.º 1
0
static void
mono_set_port (void)
{
  MonoObject *exc;
  MonoMethod *method;
  MonoClass *class;
  MonoMethodDesc *desc;
  MonoArray *v_args = NULL;
  MonoAppDomain *domain_cls = mono_domain_get_appdomain (virtuoso_domain);
  char *port;

  port = virtuoso_odbc_port ();


  class = mono_object_get_class ((MonoObject *) domain_cls);

  desc = mono_method_desc_new ("System.AppDomain:SetData", 1);
  method = mono_method_desc_search_in_class (desc, class);

  v_args = MAKE_PARAM_ARRAY (virtuoso_domain, 2);
  SET_STRING_ARG (virtuoso_domain, v_args, 0, "OpenLink.Virtuoso.InProcessPort");
  SET_STRING_ARG (virtuoso_domain, v_args, 1, port);

  mono_runtime_invoke_array (method, domain_cls, v_args, &exc);
}
Exemplo n.º 2
0
static MonoObject *
mono_jit_exec_virt (MonoAssembly *assembly, char *mtd_name, MonoArray *v_args)
{
  MonoImage *image = mono_assembly_get_image (assembly);
  MonoMethod *method;
  MonoClass *class;
  MonoMethodDesc *desc;
  MonoObject *exc = NULL, *ret;

  class = mono_class_from_name (image, "", "VInvoke");
  desc = mono_method_desc_new (mtd_name, 1);
  method = mono_method_desc_search_in_image (desc, image);

  mono_assembly_set_main (
      mono_image_get_assembly (
	mono_class_get_image (
	  mono_method_get_class (method))));
  /*mono_start_method = mono_marshal_get_runtime_invoke (method);*/

  ret = virt_mono_runtime_exec_main (method, v_args, &exc);
  if (exc)
    virt_mono_throw_unhandled_exception (exc);

  return ret;
}
Exemplo n.º 3
0
Arquivo: trace.c Projeto: RavenB/mono
static int
get_spec (int *last)
{
	int token = get_token ();
	if (token == TOKEN_EXCLUDE){
		token = get_spec (last);
		if (token == TOKEN_EXCLUDE){
			fprintf (stderr, "Expecting an expression");
			return TOKEN_ERROR;
		}
		if (token == TOKEN_ERROR)
			return token;
		trace_spec.ops [(*last)-1].exclude = 1;
		return TOKEN_SEPARATOR;
	}
	if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
		return token;
	
	if (token == TOKEN_METHOD){
		MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
		if (desc == NULL){
			fprintf (stderr, "Invalid method name: %s\n", value);
			return TOKEN_ERROR;
		}
		trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
		trace_spec.ops [*last].data = desc;
	} else if (token == TOKEN_ALL)
		trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
	else if (token == TOKEN_PROGRAM)
		trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
	else if (token == TOKEN_WRAPPER)
		trace_spec.ops [*last].op = MONO_TRACEOP_WRAPPER;
	else if (token == TOKEN_NAMESPACE){
		trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
		trace_spec.ops [*last].data = g_strdup (value);
	} else if (token == TOKEN_CLASS || token == TOKEN_EXCEPTION){
		char *p = strrchr (value, '.');
		if (p) {
			*p++ = 0;
			trace_spec.ops [*last].data = g_strdup (value);
			trace_spec.ops [*last].data2 = g_strdup (p);
		}
		else {
			trace_spec.ops [*last].data = g_strdup ("");
			trace_spec.ops [*last].data2 = g_strdup (value);
		}
		trace_spec.ops [*last].op = token == TOKEN_CLASS ? MONO_TRACEOP_CLASS : MONO_TRACEOP_EXCEPTION;
	} else if (token == TOKEN_STRING){
		trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
		trace_spec.ops [*last].data = g_strdup (value);
	} else if (token == TOKEN_DISABLED) {
		trace_spec.enabled = FALSE;
	} else {
		fprintf (stderr, "Syntax error in trace option specification\n");
		return TOKEN_ERROR;
	}
	(*last)++;
	return TOKEN_SEPARATOR;
}
Exemplo n.º 4
0
GDMonoMethod *GDMonoClass::get_method_with_desc(const String &p_description, bool p_include_namespace) {

	MonoMethodDesc *desc = mono_method_desc_new(p_description.utf8().get_data(), p_include_namespace);
	MonoMethod *method = mono_method_desc_search_in_class(desc, mono_class);
	mono_method_desc_free(desc);

	ERR_FAIL_COND_V(mono_method_get_class(method) != mono_class, NULL);

	return get_method(method);
}
Exemplo n.º 5
0
MonoMethod *getMethodFromClass(MonoClass *cls, char *name) {
    MonoMethodDesc *mdesc;
    MonoMethod *method;

    mdesc = mono_method_desc_new(name, 1);
    method = mono_method_desc_search_in_class(mdesc, cls);
    mono_method_desc_free(mdesc);

    return method;
}
Exemplo n.º 6
0
// Get string from a Mono exception 
char* PyNet_ExceptionToString(MonoObject *e) {
    MonoMethodDesc* mdesc = mono_method_desc_new(":ToString()", FALSE);
    MonoMethod* mmethod = mono_method_desc_search_in_class(mdesc, mono_get_object_class());
    mono_method_desc_free(mdesc);

    mmethod = mono_object_get_virtual_method(e, mmethod);
    MonoString* monoString = (MonoString*) mono_runtime_invoke(mmethod, e, NULL, NULL);
    mono_runtime_invoke(mmethod, e, NULL, NULL);
    return mono_string_to_utf8(monoString);
}
Exemplo n.º 7
0
int main()
{
#if LOADDYNAMIC
	SetupMono();
#endif

#if WIN32
	SetEnvironmentVariable(L"MONO_PATH",L"..\\..\\..\\builds\\monodistribution\\lib\\mono\\2.0");
#else
	setenv("MONO_PATH","../../../builds/monodistribution/lib/mono/2.0",1);
#endif

        MonoDomain* domain = mono_jit_init_version ("Unity Root Domain","v2.0.50727");

        //create and set child domain
        MonoDomain* child = mono_domain_create_appdomain("Unity Child Domain",NULL);
        mono_domain_set(child,0);

        //load assembly and call entrypoint
        MonoAssembly* ass = mono_domain_assembly_open (mono_domain_get (),"lucas.exe");
        MonoImage* img = mono_assembly_get_image(ass);
        printf("image %p\n",img);
        MonoMethodDesc* desc = mono_method_desc_new("Main2:Main",1);
        MonoMethod* m = mono_method_desc_search_in_image(desc,img);
        printf("method %p\n",m);
        MonoObject* exc;
        MonoObject* newinst = mono_object_new(mono_domain_get(),mono_method_get_class(m));
        MonoObject* ret = mono_runtime_invoke(m,newinst,0,&exc);
        printf ("Exception: %p\n",exc);
        if (exc)
        {
                MonoException* exc2 = (MonoException*) exc;
                printf ("exc msg:%s\n",mono_class_get_name(mono_object_get_class((MonoObject*)exc)));
        }
        printf ("ret: %p\n",ret);

		Sleep(0);

        //switch back to root domain
        mono_domain_set(domain,0);
        mono_domain_unload(child);
		mono_runtime_set_shutting_down ();
		mono_threads_set_shutting_down ();
		mono_thread_pool_cleanup ();
		mono_domain_finalize(mono_get_root_domain(),2000);
		mono_runtime_cleanup(mono_get_root_domain());

		printf("Unloading mono\n");
#if LOADDYNAMIC
	CleanupMono();
#endif

		while(1){} //sleep so stale monothreads have a chance to crash
        return 0;
}
Exemplo n.º 8
0
CMMethodDescRef CMMethodDescCreate(CFAllocatorRef allocator, CFStringRef name, bool useNamespace) {
    CMMethodDescRef methodDesc = NULL;
    if (name) {
        methodDesc = CFAllocatorAllocate(allocator, sizeof(__CMMethodDescType), 0);
        if (methodDesc) {
            methodDesc->allocator = allocator ? CFRetain(allocator) : NULL;
            methodDesc->retainCount = 1;
            methodDesc->useNamespace = useNamespace;
            methodDesc->name = CFRetain(name);
            IF_UTF8(allocator, name, utf8, {
                if (NULL == (methodDesc->monoMethodDesc = mono_method_desc_new(utf8, useNamespace))) {
                    // TODO: error
                }
            });
        }
    }
Exemplo n.º 9
0
	void MonoAssembly::invoke(const String& functionName)
	{
		MonoMethodDesc* methodDesc = mono_method_desc_new(functionName.c_str(), false);

		if(methodDesc != nullptr)
		{
			::MonoMethod* entry = mono_method_desc_search_in_image(methodDesc, mMonoImage);

			if(entry != nullptr)
			{
				MonoObject* exception = nullptr;
				mono_runtime_invoke(entry, nullptr, nullptr, &exception);

				MonoUtil::throwIfException(exception);
			}
		}
	}
Exemplo n.º 10
0
CScriptDomain::~CScriptDomain()
{
	m_bDestroying = true;

	for each(auto assembly in m_assemblies)
		delete assembly;

	m_assemblies.clear();

	if(m_bRootDomain)
		mono_jit_cleanup(m_pDomain);
	else
	{
		if(IsActive())
			mono_domain_set(mono_get_root_domain(), false);

		mono_domain_finalize(m_pDomain, 2000);

		MonoObject *pException;
		try
		{
			mono_domain_try_unload(m_pDomain, &pException);
		}
		catch(char *ex)
		{
			MonoWarning("An exception was raised during ScriptDomain unload: %s", ex);
		}

		if(pException)	
		{	
			MonoWarning("An exception was raised during ScriptDomain unload:");
			MonoMethod *pExceptionMethod = mono_method_desc_search_in_class(mono_method_desc_new("::ToString()", false),mono_get_exception_class());		
			MonoString *exceptionString = (MonoString *)mono_runtime_invoke(pExceptionMethod, pException, nullptr, nullptr);		
			CryLogAlways(ToCryString((mono::string)exceptionString));
		}
	}

	g_pScriptSystem->OnDomainReleased(this);
}
Exemplo n.º 11
0
/**
 * Returns the unmanaged thunk for the given @method_name method
 * descriptor.
 */
void *sp_get_method_thunk(const char *method_name)
{
	MonoMethodDesc *method_desc;
	MonoMethod *method;
	
	/* Create a MonoMethodDesc for the supplied method_name. */
	method_desc = mono_method_desc_new(method_name, 1);
	if (method_desc == NULL) {
		printf("error: unable to create method desc\n");
		_exit(0);
	}
	
	/* Try and find the method in the loaded assembly image. */
	method = mono_method_desc_search_in_image(method_desc, m_image);
	if (method == NULL) {
		printf("error: unable to find method\n");
		_exit(0);
	}

	/* Return the unmanaged thunk for the method. */
	return mono_method_get_unmanaged_thunk(method);
}
Exemplo n.º 12
0
void mono_cfg_dump_create_context (MonoCompile *cfg)
{
	cfg->gdump_ctx = NULL;

	if (!cfg_dump_method_inited) {
		cfg_dump_method_name = g_getenv ("MONO_JIT_DUMP_METHOD");
		cfg_dump_method_inited = TRUE;
	}
	if (!cfg_dump_method_name)
		return;
	const char *name = cfg_dump_method_name;

	if ((strchr (name, '.') > name) || strchr (name, ':')) {
		MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
		gboolean failed = !mono_method_desc_full_match (desc, cfg->method);
		mono_method_desc_free (desc);
		if (failed)
			return;
	} else
		if (strcmp (cfg->method->name, name) != 0)
			return;

	g_debug ("cfg_dump: create context for \"%s::%s\"", m_class_get_name (cfg->method->klass), cfg->method->name);
	int fd = create_socket (DEFAULT_HOST, DEFAULT_PORT);
	if (fd < 0) {
		g_warning ("cfg_dump: couldn't create socket: %s::%d", DEFAULT_HOST, DEFAULT_PORT);
		return;
	}

	MonoGraphDumper *ctx = (MonoGraphDumper *) mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGraphDumper));
	ctx->fd = fd;
	ctx->constant_pool = g_hash_table_new ((GHashFunc) constant_pool_hash, constant_pool_equal);
	ctx->insn2id = g_hash_table_new ((GHashFunc) instruction_hash, instruction_equal);
	ctx->next_cp_id = 1;
	ctx->next_insn_id = 0;

	cfg->gdump_ctx = ctx;
}
Exemplo n.º 13
0
static void processThroughMono(std::string& dll)
{    
    //Initialize mono runtime
    MonoDomain* domain = mono_jit_init (dll.c_str());
    MonoAssembly *assembly;

    assembly = mono_domain_assembly_open (domain, dll.c_str());
    assert(assembly);

    MonoImage *image =  mono_assembly_get_image  (assembly);
    assert(image);
    MonoMethodDesc* desc = mono_method_desc_new ("TestDLL.Program:process", true);
    assert(desc);

    MonoMethod* method = mono_method_desc_search_in_image (desc, image);
    assert(method);

    //Create our container
    Container* c = new Container();

    //Cast to uint64
    uint64_t ptr = reinterpret_cast<uint64_t>(c);

    //Fill it as an argument before the mono method invokation
    void* args[1];
    args[0] = &ptr;

    //Invoke C# code
    mono_runtime_invoke (method, nullptr, args, nullptr);

    //Clean mono runtime
    mono_jit_cleanup (domain);

    //We did it!
    c->dump();

}
Exemplo n.º 14
0
static void
load_roots (const char* filename)
{
	FILE *file;
	char buf [2048];
	char *p, *s;
	int line = 0;
	MonoImage *image = NULL;
	MonoClass *klass = NULL;
	MonoClassField *field;
	MonoMethodDesc *mdesc;
	MonoMethod *method;

	if (!(file = fopen (filename, "r")))
		return;
	
	while (fgets (buf, sizeof (buf), file)) {
		/* FIXME:
		 * decide on the format to use to express types, fields, methods,
		 * maybe the same used on output from the tool, but with explicit
		 * names and signatures instead of token indexes
		 * add wildcard support
		 */
		++line;
		s = buf;
		while (*s && g_ascii_isspace (*s)) ++s;
		switch (*s) {
		case 0:
		case '#':
			continue; /* comment */
		case '[':
			p = strchr (s, ']');
			if (!p)
				g_error ("invalid assembly format at line %d\n", line);
			*p = 0;
			p = s + 1;
			image = find_image (p);
			if (!image)
				g_error ("image not loaded: %s\n", p);
			klass = NULL;
		 	break;
		case 'T':
			if (s [1] != ':')
				g_error ("invalid type format at line %d\n", line);
			if (!image)
				break;
			klass = find_class (image, s + 2);
			break;
		case 'F':
			if (s [1] != ':')
				g_error ("invalid field format at line %d\n", line);
			if (!image || !klass)
				break;
			p = s + 2;
			if (*p == '*') {
				handle_type (klass, TYPE_FIELDS);
				break;
			}
			field = mono_class_get_field_from_name (klass, p);
			if (!field)
				g_warning ("no field '%s' at line %d\n", p, line);
			else
				add_field (field);
			break;
		case 'M':
			if (s [1] != ':')
				g_error ("invalid method format at line %d\n", line);
			if (!image || !klass)
				break;
			p = s + 2;
			if (*p == '*') {
				handle_type (klass, TYPE_METHODS);
				break;
			}
			mdesc = mono_method_desc_new (p, FALSE);
			if (!mdesc) {
				g_error ("invalid method desc at line %d\n", line);
			}
			method = mono_method_desc_search_in_class (mdesc, klass);
			if (!method)
				g_warning ("no method '%s' at line %d\n", p, line);
			else
				add_types_from_method (method);
			mono_method_desc_free (mdesc);
			break;
		default:
			g_error ("invalid format at line %d\n", line);
		}
	}
	fclose (file);
}
Exemplo n.º 15
0
JNIEXPORT jboolean JNICALL Java_com_koushikdutta_monojavabridge_MonoBridge_initializeMono
  (JNIEnv *env, jclass clazz, jstring debuggerAgentOptions)
{
   // setenv("HOME", "/data/data/com.koushikdutta.twitter/", 1);
#ifdef PLATFORM_ANDROID
    if (debuggerAgentOptions != NULL)
    {
        LOGI("Debugger enabled...");
        int length = (*env)->GetStringLength(env, debuggerAgentOptions);
        const jbyte *str = (*env)->GetStringUTFChars(env, debuggerAgentOptions, NULL);
        char *copy = (char*)malloc(length + 1);
        copy[length] = NULL;
        memcpy(copy, str, length);
        mono_debugger_agent_parse_options(copy);
        free(copy);
        (*env)->ReleaseStringUTFChars(env, debuggerAgentOptions, str);
        mono_debug_init (MONO_DEBUG_FORMAT_MONO);
    }
#endif
    
    //guint32 opt = mono_parse_default_optimizations(NULL);
	//mono_set_defaults (1, opt);
    //mono_trace_parse_options ("");
    
    setenv("MONO_PATH", "/data/data/com.koushikdutta.mono/", 0);

#ifdef PLATFORM_ANDROID
    LOGI("mono_jit_init...");
#endif
	g_Domain = mono_jit_init (MONOJAVABRIDGE_DLL);

#ifdef PLATFORM_ANDROID
    LOGI("mono_domain_assembly_open...", NULL);
#endif
    g_Assembly = mono_domain_assembly_open (g_Domain, MONOJAVABRIDGE_DLL);
    if (!g_Assembly)
    {
        printf("Unable to load MonoJavaBridge.dll.");
        return FALSE;
    }

    mono_add_internal_call("MonoJavaBridge.JavaBridge::mono_object_to_pointer(object)", mono_objectpointer_conversion);
    mono_add_internal_call("MonoJavaBridge.JavaBridge::mono_pointer_to_object(intptr)", mono_objectpointer_conversion);
    mono_add_internal_call("MonoJavaBridge.JavaBridge::log(intptr)", logcat_print);

    MonoImage *image = mono_assembly_get_image(g_Assembly);
    MonoMethodDesc* desc = mono_method_desc_new ("MonoJavaBridge.JavaBridge:Initialize(intptr)", 1);
    MonoMethod* method = mono_method_desc_search_in_image (desc, image);
    mono_method_desc_free(desc);
    pointer args[1];
    args[0] = &g_JavaVM;
    mono_runtime_invoke(method, NULL, args, NULL);

    desc = mono_method_desc_new ("MonoJavaBridge.JavaBridge:Link(intptr,intptr,intptr,intptr)", 1);
    g_Link = mono_method_desc_search_in_image (desc, image);
    mono_method_desc_free(desc);

    desc = mono_method_desc_new ("MonoJavaBridge.JavaBridge:LoadAssembly(intptr)", 1);
    g_LoadAssembly = mono_method_desc_search_in_image (desc, image);
    mono_method_desc_free(desc);
    
    return TRUE;
}
Exemplo n.º 16
0
void try_mono() {
  MonoDomain *domain;
  MonoAssembly *ma;
  MonoImage *mi;
  MonoClass *mc;
  MonoMethodDesc *mmd;
  MonoMethod *mm;
  MonoObject *mo;
  FILE *mscorlib;
  char *corlib_data = NULL;
  void *args [2];
  static int x = 123000;
  args [0] = &x;
  args [1] = "hello world";

#if defined(__native_client__)
  mscorlib = fopen("mscorlib.dll", "r");
  if (NULL != mscorlib) {
    size_t size;
    struct stat st;
    if (0 == stat("mscorlib.dll", &st)) {
      size = st.st_size;
      printf("reading mscorlib.dll, size %ld\n", size);
      corlib_data = malloc(size);
      if (corlib_data != NULL) {
        while (fread(corlib_data, 1, size, mscorlib) != 0) ;
        if (!ferror(mscorlib)) {
          mono_set_corlib_data(corlib_data, size);
        } else {
          perror("error reading mscorlib.dll");
          free(corlib_data);
          corlib_data = NULL;
        }
      } else {
        perror("Could not allocate memory");
      }
    } else {
      perror("stat error");
    }
    fclose(mscorlib);
  }
#endif

#ifdef AOT_VERSION
  printf("address of mono_aot_module_mscorlib_info:  %p\n", mono_aot_module_mscorlib_info);
  printf("address of mono_aot_module_hw_info:  %p\n", mono_aot_module_hw_info);

  // mono_jit_set_aot_only(TRUE) should be enabled now.
  // if not enabled, I suspect we're still jitting...
  mono_jit_set_aot_only(TRUE);

  mono_aot_register_module(mono_aot_module_mscorlib_info);
  mono_aot_register_module(mono_aot_module_hw_info);
#endif

  mono_debug_init(MONO_DEBUG_FORMAT_MONO);
  domain = mono_jit_init("hw.exe", "v2.0.50727");

  printf("mono domain: %p\n", domain);

  ma = mono_domain_assembly_open(domain, "hw.exe");
  printf("mono assembly: %p\n", ma);

  mi = mono_assembly_get_image(ma);
  printf("mono image: %p\n", mi);

  mc = mono_class_from_name(mi, "Test", "HelloWorld");
  printf("mono class: %p\n", mc);

  mmd = mono_method_desc_new("Test.HelloWorld:Foobar(int,string)", TRUE);
  printf("mono desc method: %p\n", mmd);

  mm = mono_method_desc_search_in_image(mmd, mi);
  printf("mono method: %p\n", mm);

  // add c functions for mono test code to invoke
  mono_add_internal_call("Test.c_code::my_c_func", (void *) my_c_func);
  mono_add_internal_call("Test.c_code::my_c_pass", (void *) my_c_pass);

  mo = mono_runtime_invoke(mm, NULL, args, NULL);

  printf("mono object: %p\n", mo);
  if (NULL != corlib_data) free(corlib_data);
}