Fix leaks, checked build support around netmodules loaded as "files"

Test mcs/tests/test-418.exe was failing when running a checked build
with MONO_CHECK_MODE=metadata. I audited how images track their
modules and fixed the following issues:

1. The checked build metadata "reference audit" was following
references stored in the "modules" field, but not the "files" field. I
changed the audit code to follow both.
2. Loading a module into the "modules[]" field was causing a leak
because mono_image_addref() was being called after
mono_image_open_full(), even though mono_image_open_full itself
returns the module +1. I removed the addref.
3. Loading a module into the "files[]" field was causing a leak
because we were opening the module with mono_image_open_full and then
never attempting to close it. I modified our image close to do the
same thing to "files[]" we do to "modules[]".

In order for 1 and 3 to work, I modified MonoImages to track the size
of the "files[]" field.
This commit is contained in:
Andi McClure committed 2016-09-19 17:55:08 -04:00
1 parent a26fda4e8c
commit f822445711
3 files changed
+47 -30

No files matched your search

+29 -15
View File
@@ -685,7 +685,6 @@ mono_image_load_module (MonoImage *image, int idx)
module_ref = g_build_filename (base_dir, name, NULL);
image->modules [idx - 1] = mono_image_open_full (module_ref, &status, refonly);
if (image->modules [idx - 1]) {
mono_image_addref (image->modules [idx - 1]);
image->modules [idx - 1]->assembly = image->assembly;
#ifdef HOST_WIN32
if (image->modules [idx - 1]->is_module_handle)
@@ -1653,6 +1652,17 @@ mono_wrapper_caches_free (MonoWrapperCaches *cache)
free_hash (cache->thunk_invoke_cache);
}
static void
mono_image_close_except_pools_all (MonoImage**images, int image_count)
{
for (int i = 0; i < image_count; ++i) {
if (images [i]) {
if (!mono_image_close_except_pools (images [i]))
images [i] = NULL;
}
}
}
/*
* Returns whether mono_image_close_finish() must be called as well.
* We must unload images in two steps because clearing the domain in
@@ -1773,7 +1783,6 @@ mono_image_close_except_pools (MonoImage *image)
g_free (image->name);
g_free (image->guid);
g_free (image->version);
g_free (image->files);
}
if (image->method_cache)
@@ -1852,12 +1861,8 @@ mono_image_close_except_pools (MonoImage *image)
g_free (image->image_info);
}
for (i = 0; i < image->module_count; ++i) {
if (image->modules [i]) {
if (!mono_image_close_except_pools (image->modules [i]))
image->modules [i] = NULL;
}
}
mono_image_close_except_pools_all (image->files, image->file_count);
mono_image_close_except_pools_all (image->modules, image->module_count);
if (image->modules_loaded)
g_free (image->modules_loaded);
@@ -1876,6 +1881,17 @@ mono_image_close_except_pools (MonoImage *image)
return TRUE;
}
static void
mono_image_close_all (MonoImage**images, int image_count)
{
for (int i = 0; i < image_count; ++i) {
if (images [i])
mono_image_close_finish (images [i]);
}
if (images)
g_free (images);
}
void
mono_image_close_finish (MonoImage *image)
{
@@ -1891,12 +1907,8 @@ mono_image_close_finish (MonoImage *image)
image->references = NULL;
}
for (i = 0; i < image->module_count; ++i) {
if (image->modules [i])
mono_image_close_finish (image->modules [i]);
}
if (image->modules)
g_free (image->modules);
mono_image_close_all (image->files, image->file_count);
mono_image_close_all (image->modules, image->module_count);
#ifndef DISABLE_PERFCOUNTERS
mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
@@ -2195,8 +2207,10 @@ mono_image_load_file_for_image (MonoImage *image, int fileidx)
res->modules [i]->assembly = image->assembly;
}
if (!image->files)
if (!image->files) {
image->files = g_new0 (MonoImage*, t->rows);
image->file_count = t->rows;
}
image->files [fileidx - 1] = res;
mono_image_unlock (image);
/* vtable fixup can't happen with the image lock held */
+11 -9
View File
@@ -175,10 +175,12 @@ typedef struct {
struct _MonoImage {
/*
* The number of assemblies which reference this MonoImage though their 'image'
* field plus the number of images which reference this MonoImage through their
* 'modules' field, plus the number of threads holding temporary references to
* this image between calls of mono_image_open () and mono_image_close ().
* This count is incremented during these situations:
* - An assembly references this MonoImage though its 'image' field
* - This MonoImage is present in the 'files' field of an image
* - This MonoImage is present in the 'modules' field of an image
* - A thread is holding a temporary reference to this MonoImage between
* calls to mono_image_open and mono_image_close ()
*/
int ref_count;
@@ -258,16 +260,16 @@ struct _MonoImage {
MonoAssembly **references;
int nreferences;
/* Code files in the assembly. */
/* Code files in the assembly. The main assembly has a "file" table and also a "module"
* table, where the module table is a subset of the file table. We track both lists,
* and because we can lazy-load them at different times we reference-increment both.
*/
MonoImage **modules;
guint32 module_count;
gboolean *modules_loaded;
/*
* Files in the assembly. Items are either NULL or alias items in modules, so this does not impact ref_count.
* Protected by the image lock.
*/
MonoImage **files;
guint32 file_count;
gpointer aot_module;
+7 -6
View File
@@ -473,6 +473,12 @@ check_image_may_reference_image(MonoImage *from, MonoImage *to)
// For each queued image visit all directly referenced images
int inner_idx;
// 'files' and 'modules' semantically contain the same items but because of lazy loading we must check both
for (inner_idx = 0; !success && inner_idx < checking->file_count; inner_idx++)
{
CHECK_IMAGE_VISIT (checking->files[inner_idx]);
}
for (inner_idx = 0; !success && inner_idx < checking->module_count; inner_idx++)
{
CHECK_IMAGE_VISIT (checking->modules[inner_idx]);
@@ -480,12 +486,7 @@ check_image_may_reference_image(MonoImage *from, MonoImage *to)
for (inner_idx = 0; !success && inner_idx < checking->nreferences; inner_idx++)
{
// References are lazy-loaded and thus allowed to be NULL.
// If they are NULL, we don't care about them for this search, because they haven't impacted ref_count yet.
if (checking->references[inner_idx])
{
CHECK_IMAGE_VISIT (checking->references[inner_idx]->image);
}
CHECK_IMAGE_VISIT (checking->references[inner_idx]->image);
}
mono_image_unlock (checking);