Skip to content

Commit

Permalink
[mono][aot] Fix some memory leaks. (#98147)
Browse files Browse the repository at this point in the history
Extracted from #97096.

Author: Johan Lorensson <lateralusx.github@gmail.com>
  • Loading branch information
vargaz committed Feb 8, 2024
1 parent e2b7553 commit f682619
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
42 changes: 26 additions & 16 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4212,22 +4212,23 @@ get_plt_entry (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
res->debug_sym = get_plt_entry_debug_sym (acfg, res->ji, acfg->plt_entry_debug_sym_cache);
if (synchronized) {
/* Avoid duplicate symbols because we don't cache */
char *tmp_symbol = res->symbol;
res->symbol = g_strdup_printf ("%s_%d", res->symbol, synchronized_symbol_idx);
if (res->debug_sym)
g_free (tmp_symbol);
if (res->debug_sym) {
char *tmp_debug_sym = res->debug_sym;
res->debug_sym = g_strdup_printf ("%s_%d", res->debug_sym, synchronized_symbol_idx);
g_free (tmp_debug_sym);
}
synchronized_symbol_idx ++;
}

if (res->debug_sym)
res->llvm_symbol = g_strdup_printf ("%s_%s_llvm", res->symbol, res->debug_sym);
res->llvm_symbol = mono_mempool_strdup_printf (acfg->mempool, "%s_%s_llvm", res->symbol, res->debug_sym);
else
res->llvm_symbol = g_strdup_printf ("%s_llvm", res->symbol);
if (strstr (res->llvm_symbol, acfg->temp_prefix) == res->llvm_symbol) {
/* The llvm symbol shouldn't be temporary, since the llvm generated object file references it */
char *tmp = res->llvm_symbol;
res->llvm_symbol = g_strdup (res->llvm_symbol + strlen (acfg->temp_prefix));
g_free (tmp);
}
res->llvm_symbol = mono_mempool_strdup_printf (acfg->mempool, "%s_llvm", res->symbol);
if (strstr (res->llvm_symbol, acfg->temp_prefix) == res->llvm_symbol)
res->llvm_symbol = res->llvm_symbol + strlen (acfg->temp_prefix);

g_hash_table_insert (acfg->patch_to_plt_entry [new_ji->type], new_ji, res);

Expand Down Expand Up @@ -7003,7 +7004,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
/*
* sanitize_symbol:
*
* Return a modified version of S which only includes characters permissible in symbols.
* Returns either S or a malloc-ed modified version of S which only includes characters permissible in symbols.
*/
static char*
sanitize_symbol (MonoAotCompile *acfg, char *s)
Expand Down Expand Up @@ -7045,7 +7046,7 @@ sanitize_symbol (MonoAotCompile *acfg, char *s)
}
}

res = mono_mempool_strdup (acfg->mempool, gs->str);
res = g_strdup (gs->str);
g_string_free (gs, TRUE);
return res;
}
Expand Down Expand Up @@ -8039,7 +8040,12 @@ get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cac

g_free (prefix);

return sanitize_symbol (acfg, debug_sym);
char *tmp_debug_sym = debug_sym;
debug_sym = sanitize_symbol (acfg, debug_sym);
if (tmp_debug_sym != debug_sym)
g_free (tmp_debug_sym);

return debug_sym;
}

/*
Expand Down Expand Up @@ -9863,11 +9869,15 @@ mono_aot_get_method_name (MonoCompile *cfg)
}
}

if (llvm_acfg->aot_opts.static_link)
if (llvm_acfg->aot_opts.static_link) {
/* Include the assembly name too to avoid duplicate symbol errors */
return g_strdup_printf ("%s_%s", llvm_acfg->assembly_name_sym, get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash));
else
char *tmp_debug_sym = get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash);
char *debug_sym = g_strdup_printf ("%s_%s", llvm_acfg->assembly_name_sym, tmp_debug_sym);
g_free (tmp_debug_sym);
return debug_sym;
} else {
return get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash);
}
}

static gboolean
Expand Down Expand Up @@ -15372,7 +15382,7 @@ emit_aot_image (MonoAotCompile *acfg)
} else {
cfg->asm_symbol = g_strdup_printf ("%s%sm_%x", acfg->temp_prefix, acfg->llvm_label_prefix, method_index);
}
cfg->asm_debug_symbol = cfg->asm_symbol;
cfg->asm_debug_symbol = g_strdup (cfg->asm_symbol);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/mono/mono/mini/mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@ mono_empty_compile (MonoCompile *cfg)
cfg->headers_to_free = NULL;

if (cfg->mempool) {
//mono_mempool_stats (cfg->mempool);
//mono_mempool_stats (cfg->mempool);
mono_mempool_destroy (cfg->mempool);
cfg->mempool = NULL;
}
Expand Down Expand Up @@ -1788,6 +1788,10 @@ mono_destroy_compile (MonoCompile *cfg)

mono_debug_free_method (cfg);

g_free (cfg->asm_symbol);
g_free (cfg->asm_debug_symbol);
g_free (cfg->llvm_method_name);

g_free (cfg->varinfo);
g_free (cfg->vars);
g_free (cfg->exception_message);
Expand Down
7 changes: 5 additions & 2 deletions src/mono/mono/mini/seq-points.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,13 @@ mono_save_seq_point_info (MonoCompile *cfg, MonoJitInfo *jinfo)
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
jit_mm_lock (jit_mm);
// FIXME: The lookup can fail if the method is JITted recursively though a type cctor
if (!g_hash_table_lookup (jit_mm->seq_points, cfg->method_to_register))
MonoSeqPointInfo *existing_seq_points = NULL;
if (!g_hash_table_lookup_extended (jit_mm->seq_points, cfg->method_to_register, NULL, (gpointer *)&existing_seq_points)) {
g_hash_table_insert (jit_mm->seq_points, cfg->method_to_register, cfg->seq_point_info);
else
} else {
mono_seq_point_info_free (cfg->seq_point_info);
cfg->seq_point_info = existing_seq_points;
}
jit_mm_unlock (jit_mm);

g_assert (jinfo);
Expand Down

0 comments on commit f682619

Please sign in to comment.