MonoPPDBFile* mono_ppdb_load_file (MonoImage *image, const guint8 *raw_contents, int size) { MonoImage *ppdb_image = NULL; const char *filename; char *s, *ppdb_filename; MonoImageOpenStatus status; guint8 pe_guid [16]; gint32 pe_age; gint32 pe_timestamp; MonoPPDBFile *ppdb; if (raw_contents) { if (size > 4 && strncmp ((char*)raw_contents, "BSJB", 4) == 0) ppdb_image = mono_image_open_from_data_internal ((char*)raw_contents, size, TRUE, &status, FALSE, TRUE, NULL); } else { /* ppdb files drop the .exe/.dll extension */ filename = mono_image_get_filename (image); if (strlen (filename) > 4 && (!strcmp (filename + strlen (filename) - 4, ".exe") || !strcmp (filename + strlen (filename) - 4, ".dll"))) { s = g_strdup (filename); s [strlen (filename) - 4] = '\0'; ppdb_filename = g_strdup_printf ("%s.pdb", s); g_free (s); } else { ppdb_filename = g_strdup_printf ("%s.pdb", filename); } ppdb_image = mono_image_open_metadata_only (ppdb_filename, &status); if (!ppdb_image) g_free (ppdb_filename); } if (!ppdb_image) return NULL; /* * Check that the images match. * The same id is stored in the Debug Directory of the PE file, and in the * #Pdb stream in the ppdb file. */ if (get_pe_debug_guid (image, pe_guid, &pe_age, &pe_timestamp)) { PdbStreamHeader *pdb_stream = (PdbStreamHeader*)ppdb_image->heap_pdb.data; g_assert (pdb_stream); /* The pdb id is a concentation of the pe guid and the timestamp */ if (memcmp (pe_guid, pdb_stream->guid, 16) != 0 || memcmp (&pe_timestamp, pdb_stream->guid + 16, 4) != 0) { g_warning ("Symbol file %s doesn't match image %s", ppdb_image->name, image->name); mono_image_close (ppdb_image); return NULL; } } ppdb = g_new0 (MonoPPDBFile, 1); ppdb->image = ppdb_image; ppdb->doc_hash = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) doc_free); ppdb->method_hash = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_free); return ppdb; }
//============================================================ // <T>关闭处理。</T> // // @return 处理结果 //============================================================ TResult FMonoLibrary::Close(){ TResult resultCd = FScriptLibrary::Close(); if(_pMonoImage != NULL){ mono_image_close(_pMonoImage); MO_CLEAR(_pMonoImage); } return resultCd; }
void Application::StopMono() { for (auto& img : images) mono_image_close(img); instances.clear(); images.clear(); unload_domain(); }
static void test_set_free (test_set_t *set) { free (set->name); free (set->assembly); free (set->assembly_data); if (set->image) mono_image_close (set->image); }
static void free_debug_handle (MonoDebugHandle *handle) { if (handle->symfile) mono_debug_close_mono_symbol_file (handle->symfile); /* decrease the refcount added with mono_image_addref () */ mono_image_close (handle->image); g_free (handle); }
void mono_ppdb_close (MonoDebugHandle *handle) { MonoPPDBFile *ppdb = handle->ppdb; mono_image_close (ppdb->image); g_hash_table_destroy (ppdb->doc_hash); g_hash_table_destroy (ppdb->method_hash); g_free (ppdb); }
//##################################################################### // Function loadAssembly //##################################################################### bool MonoSystem::loadMemoryAssembly(char* membuf, unsigned int len) const { MonoAssembly *ass; MonoImageOpenStatus status = MONO_IMAGE_OK; MonoImage *image = mono_image_open_from_data_full(membuf, len, TRUE, &status, false); if (!image) { // bad image format printf("Error loading image from memory: %d\n", status); return false; } ass = mono_assembly_load_from_full (image, "", &status, false); if (!ass) { printf("Error loading assembly from memory: %d\n", status); // FIXME real error reporting // bad image format? mono_image_close (image); return false; } return true; }
void GDMonoAssembly::unload() { ERR_FAIL_COND(!loaded); #ifdef DEBUG_ENABLED if (pdb_data.size()) { mono_debug_close_image(image); pdb_data.clear(); } #endif for (Map<MonoClass *, GDMonoClass *>::Element *E = cached_raw.front(); E; E = E->next()) { memdelete(E->value()); } cached_classes.clear(); cached_raw.clear(); mono_image_close(image); assembly = NULL; image = NULL; loaded = false; }
/* Entry point called by LdrLoadDll of ntdll.dll after _CorValidateImage. */ BOOL STDMETHODCALLTYPE _CorDllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) { MonoAssembly* assembly; MonoImage* image; gchar* file_name; gchar* error; switch (dwReason) { case DLL_PROCESS_ATTACH: DisableThreadLibraryCalls (hInst); file_name = mono_get_module_file_name (hInst); if (mono_get_root_domain ()) { image = mono_image_open_from_module_handle (hInst, mono_path_resolve_symlinks (file_name), TRUE, NULL); } else { init_from_coree = TRUE; mono_runtime_load (file_name, NULL); error = (gchar*) mono_check_corlib_version (); if (error) { g_free (error); g_free (file_name); mono_runtime_quit (); return FALSE; } image = mono_image_open (file_name, NULL); if (image) { image->has_entry_point = TRUE; mono_close_exe_image (); /* Decrement reference count to zero. (Image will not be closed.) */ mono_image_close (image); } } if (!image) { g_free (file_name); return FALSE; } /* * FIXME: Find a better way to call mono_image_fixup_vtable. Only * loader trampolines should be used and assembly loading should * probably be delayed until the first call to an exported function. */ if (image->tables [MONO_TABLE_ASSEMBLY].rows && ((MonoCLIImageInfo*) image->image_info)->cli_cli_header.ch_vtable_fixups.rva) assembly = mono_assembly_open (file_name, NULL); g_free (file_name); break; case DLL_PROCESS_DETACH: if (lpReserved != NULL) /* The process is terminating. */ return TRUE; file_name = mono_get_module_file_name (hInst); image = mono_image_loaded (file_name); if (image) mono_image_close (image); g_free (file_name); break; } return TRUE; }
bool Application::StartMonoAndLoadAssemblies() { // shutdown the child domain StopMono(); // create a new child domain if (!StartMono()) { mono_environment_exitcode_set(-1); return true; } std::string dll = "EmbedThings.dll"; std::string filename = File::BuildRootedPath(assemblyDir, dll); size_t length; // read our entry point assembly char* data = File::Read(filename.c_str(), &length); printf_console("Loading %s into Domain\n", dll.c_str()); MonoImageOpenStatus status; // open the assembly from the data we read, so we never lock files auto image = mono_image_open_from_data_with_name(data, length, true /* copy data */, &status, false /* ref only */, filename.c_str()); if (status != MONO_IMAGE_OK || image == nullptr) { printf_console("Failed loading assembly %s\n", dll); return true; } // load the assembly auto assembly = mono_assembly_load_from_full(image, filename.c_str(), &status, false); if (status != MONO_IMAGE_OK || assembly == nullptr) { mono_image_close(image); printf_console("Failed loading assembly %s\n", dll); return true; } // save the image for lookups later and for cleaning up images.push_back(image); if (!assembly) { printf_console("Couldn't find assembly %s\n", filename.c_str()); return true; } // locate the class we want to load MonoClass* klass = mono_class_from_name(image, "EmbedThings", "EntryPoint"); if (klass == nullptr) { printf_console("Failed loading class %s\n", "EmbedThings.EntryPoint"); return true; } // create the class (doesn't run constructors) MonoObject* obj = mono_object_new(mono_domain_get(), klass); if (obj == nullptr) { printf_console("Failed loading class instance %s\n", "EmbedThings.EntryPoint"); return true; } // initialize the class instance (runs default constructors) mono_runtime_object_init(obj); if (obj == nullptr) { printf_console("Failed initializing class instance %s\n", "EmbedThings.EntryPoint"); return true; } // save the class instance for lookups later instances.push_back(obj); // find the Run() method auto method = find_method(klass, "Run"); MonoObject *result, *exception; // call the Run method. This will block until the managed code decides to exit result = mono_runtime_invoke(method, obj, NULL, NULL); int val = *(int*)mono_object_unbox(result); // if the managed code returns with 0, it wants to exit completely if (val == 0) { return true; } return false; }
int main (int argc, char *argv []) { int image_result = 0; MonoImage *image; char *file = NULL; char *flags = NULL; MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE; const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", "partial-md", NULL}; guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, VERIFY_CODE_ONLY, MONO_VERIFY_FAIL_FAST, MONO_VERIFY_NON_STRICT, VALID_ONLY_FLAG, VERIFY_METADATA_ONLY, VERIFY_PARTIAL_METADATA, 0}; int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0; for (i = 1; i < argc; i++){ if (argv [i][0] != '-'){ file = argv [i]; continue; } if (strcmp (argv [i], "--help") == 0) usage (); else if (strcmp (argv [i], "--verify") == 0) { verify_pe = 1; dump_data = 0; ++i; flags = argv [i]; } else { usage (); } } if (!file) usage (); #ifndef DISABLE_PERFCOUNTERS mono_perfcounters_init (); #endif mono_counters_init (); mono_metadata_init (); mono_images_init (); mono_assemblies_init (); mono_loader_init (); if (verify_pe) { char *tok = strtok (flags, ","); verify_metadata = 1; verify_code = 0; while (tok) { for (i = 0; flag_desc [i]; ++i) { if (strcmp (tok, flag_desc [i]) == 0) { if (flag_vals [i] == VERIFY_CODE_ONLY) { verify_metadata = 0; verify_code = 1; } else if(flag_vals [i] == MONO_VERIFY_ALL) { verify_code = 1; } else if(flag_vals [i] == VERIFY_METADATA_ONLY) { verify_metadata = 0; run_new_metadata_verifier = 1; } else if(flag_vals [i] == VERIFY_PARTIAL_METADATA) { verify_partial_md = 1; } if (flag_vals [i] == VALID_ONLY_FLAG) verifier_mode = MONO_VERIFIER_MODE_VALID; else verify_flags |= flag_vals [i]; break; } } if (!flag_desc [i]) g_print ("Unknown verify flag %s\n", tok); tok = strtok (NULL, ","); } mono_verifier_set_mode (verifier_mode); /**/ } if (verify_pe || run_new_metadata_verifier) { run_new_metadata_verifier = 1; } if (run_new_metadata_verifier) { mono_verifier_set_mode (verifier_mode); image_result = verify_image_file (file); if (image_result == 1 || !verify_code) return image_result; } image = mono_image_open (file, NULL); if (!image){ fprintf (stderr, "Cannot open image %s\n", file); exit (1); } if (dump_data) dump_dotnet_iinfo (image); if (verify_pe) { MonoAssembly *assembly; MonoImage *image; MonoImageOpenStatus status; int code_result; mono_verifier_set_mode (verifier_mode); assembly = mono_assembly_open (file, NULL); /*fake an assembly for netmodules so the verifier works*/ if (!assembly && (image = mono_image_open (file, &status)) && image->tables [MONO_TABLE_ASSEMBLY].rows == 0) { assembly = g_new0 (MonoAssembly, 1); assembly->in_gac = FALSE; assembly->image = image; image->assembly = assembly; } if (!assembly) { g_print ("Could not open assembly %s\n", file); return 4; } code_result = dump_verify_info (assembly->image, verify_flags); return code_result ? code_result : image_result; } else mono_image_close (image); return 0; }
int main (int argc, char *argv []) { int image_result = 0; MonoImage *image; char *file = NULL; char *flags = NULL; MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_VERIFIABLE; const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", "partial-md", NULL}; guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, VERIFY_CODE_ONLY, MONO_VERIFY_FAIL_FAST, MONO_VERIFY_NON_STRICT, VALID_ONLY_FLAG, VERIFY_METADATA_ONLY, VERIFY_PARTIAL_METADATA, 0}; int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0; MonoThreadInfoRuntimeCallbacks ticallbacks; for (i = 1; i < argc; i++){ if (argv [i][0] != '-'){ file = argv [i]; continue; } if (strcmp (argv [i], "--help") == 0) usage (); else if (strcmp (argv [i], "--verify") == 0) { verify_pe = 1; dump_data = 0; ++i; flags = argv [i]; } else { usage (); } } if (!file) usage (); //We have to force the runtime to load the corlib under verification as its own corlib so core types are properly populated in mono_defaults. if (strstr (file, "mscorlib.dll")) g_setenv ("MONO_PATH", g_path_get_dirname (file), 1); assembly_directory [0] = g_path_get_dirname (file); assembly_directory [1] = NULL; #ifndef DISABLE_PERFCOUNTERS mono_perfcounters_init (); #endif mono_counters_init (); mono_tls_init_runtime_keys (); memset (&ticallbacks, 0, sizeof (ticallbacks)); ticallbacks.thread_state_init = thread_state_init; #ifndef HOST_WIN32 mono_w32handle_init (); #endif mono_thread_info_runtime_init (&ticallbacks); mono_metadata_init (); mono_images_init (); mono_assemblies_init (); mono_loader_init (); if (verify_pe) { char *tok = strtok (flags, ","); verify_metadata = 1; verify_code = 0; while (tok) { for (i = 0; flag_desc [i]; ++i) { if (strcmp (tok, flag_desc [i]) == 0) { if (flag_vals [i] == VERIFY_CODE_ONLY) { verify_metadata = 0; verify_code = 1; } else if(flag_vals [i] == MONO_VERIFY_ALL) { verify_code = 1; } else if(flag_vals [i] == VERIFY_METADATA_ONLY) { verify_metadata = 0; run_new_metadata_verifier = 1; } else if(flag_vals [i] == VERIFY_PARTIAL_METADATA) { verify_partial_md = 1; } if (flag_vals [i] == VALID_ONLY_FLAG) verifier_mode = MONO_VERIFIER_MODE_VALID; else verify_flags |= flag_vals [i]; break; } } if (!flag_desc [i]) g_print ("Unknown verify flag %s\n", tok); tok = strtok (NULL, ","); } mono_verifier_set_mode (verifier_mode); /**/ } if (verify_pe || run_new_metadata_verifier) { run_new_metadata_verifier = 1; } if (run_new_metadata_verifier) { mono_verifier_set_mode (verifier_mode); image_result = verify_image_file (file); if (image_result == 1 || !verify_code) return image_result; } image = mono_image_open (file, NULL); if (!image){ fprintf (stderr, "Cannot open image %s\n", file); exit (1); } if (dump_data) dump_dotnet_iinfo (image); if (verify_pe) { MonoAssemblyOpenRequest req; MonoAssembly *assembly; MonoImage *image; MonoImageOpenStatus status; int code_result; mono_verifier_set_mode (verifier_mode); mono_assembly_request_prepare (&req.request, sizeof (req), MONO_ASMCTX_DEFAULT); assembly = mono_assembly_request_open (file, &req, NULL); /*fake an assembly for netmodules so the verifier works*/ if (!assembly && (image = mono_image_open (file, &status)) && image->tables [MONO_TABLE_ASSEMBLY].rows == 0) { assembly = g_new0 (MonoAssembly, 1); assembly->in_gac = FALSE; assembly->image = image; image->assembly = assembly; } if (!assembly) { g_print ("Could not open assembly %s\n", file); return 4; } code_result = dump_verify_info (assembly->image, verify_flags, verifier_mode == MONO_VERIFIER_MODE_VALID); return code_result ? code_result : image_result; } else mono_image_close (image); return 0; }