Skip to content

Commit cf435b3

Browse files
committed
[mono][interp] Fix memory leaks for interpreted dynamic methods.
* Add a mempool to MonoDynamicMethod. * Modify the intepreter code to allocate from this mempool when interpreting dynamic methods.
1 parent b7a4ea2 commit cf435b3

File tree

4 files changed

+74
-22
lines changed

4 files changed

+74
-22
lines changed

src/mono/mono/metadata/class-internals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct _MonoMethodWrapper {
102102
struct _MonoDynamicMethod {
103103
MonoMethodWrapper method;
104104
MonoAssembly *assembly;
105+
MonoMemPool *mp;
105106
};
106107

107108
struct _MonoMethodPInvoke {

src/mono/mono/mini/interp/interp.c

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,15 @@ mono_interp_get_imethod (MonoMethod *method)
488488

489489
sig = mono_method_signature_internal (method);
490490

491-
imethod = (InterpMethod*)m_method_alloc0 (method, sizeof (InterpMethod));
491+
MonoMemPool *dyn_mp = NULL;
492+
if (method->dynamic)
493+
// FIXME: Use this in more places
494+
dyn_mp = mono_mempool_new_size (sizeof (InterpMethod));
495+
496+
if (dyn_mp)
497+
imethod = (InterpMethod*)mono_mempool_alloc0 (dyn_mp, sizeof (InterpMethod));
498+
else
499+
imethod = (InterpMethod*)m_method_alloc0 (method, sizeof (InterpMethod));
492500
imethod->method = method;
493501
imethod->param_count = sig->param_count;
494502
imethod->hasthis = sig->hasthis;
@@ -504,15 +512,22 @@ mono_interp_get_imethod (MonoMethod *method)
504512
imethod->rtype = m_class_get_byval_arg (mono_defaults.string_class);
505513
else
506514
imethod->rtype = mini_get_underlying_type (sig->ret);
507-
imethod->param_types = (MonoType**)m_method_alloc0 (method, sizeof (MonoType*) * sig->param_count);
515+
if (dyn_mp)
516+
imethod->param_types = (MonoType**)mono_mempool_alloc0 (dyn_mp, sizeof (MonoType*) * sig->param_count);
517+
else
518+
imethod->param_types = (MonoType**)m_method_alloc0 (method, sizeof (MonoType*) * sig->param_count);
508519
for (i = 0; i < sig->param_count; ++i)
509520
imethod->param_types [i] = mini_get_underlying_type (sig->params [i]);
510521

511522
jit_mm_lock (jit_mm);
512523
InterpMethod *old_imethod;
513-
if (!((old_imethod = mono_internal_hash_table_lookup (&jit_mm->interp_code_hash, method))))
524+
if (!((old_imethod = mono_internal_hash_table_lookup (&jit_mm->interp_code_hash, method)))) {
514525
mono_internal_hash_table_insert (&jit_mm->interp_code_hash, method, imethod);
515-
else {
526+
if (method->dynamic)
527+
((MonoDynamicMethod*)method)->mp = dyn_mp;
528+
} else {
529+
if (dyn_mp)
530+
mono_mempool_destroy (dyn_mp);
516531
imethod = old_imethod; /* leak the newly allocated InterpMethod to the mempool */
517532
}
518533
jit_mm_unlock (jit_mm);
@@ -1268,6 +1283,15 @@ compute_arg_offset (MonoMethodSignature *sig, int index)
12681283
return offset;
12691284
}
12701285

1286+
static gpointer
1287+
imethod_alloc0 (InterpMethod *imethod, size_t size)
1288+
{
1289+
if (imethod->method->dynamic)
1290+
return mono_mempool_alloc0 (((MonoDynamicMethod*)imethod->method)->mp, (guint)size);
1291+
else
1292+
return m_method_alloc0 (imethod->method, (guint)size);
1293+
}
1294+
12711295
static guint32*
12721296
initialize_arg_offsets (InterpMethod *imethod, MonoMethodSignature *csig)
12731297
{
@@ -1280,7 +1304,7 @@ initialize_arg_offsets (InterpMethod *imethod, MonoMethodSignature *csig)
12801304
if (!sig)
12811305
sig = mono_method_signature_internal (imethod->method);
12821306
int arg_count = sig->hasthis + sig->param_count;
1283-
guint32 *arg_offsets = (guint32*) g_malloc ((arg_count + 1) * sizeof (int));
1307+
guint32 *arg_offsets = (guint32*)imethod_alloc0 (imethod, (arg_count + 1) * sizeof (int));
12841308
int index = 0, offset = 0;
12851309

12861310
if (sig->hasthis) {
@@ -1302,8 +1326,8 @@ initialize_arg_offsets (InterpMethod *imethod, MonoMethodSignature *csig)
13021326
arg_offsets [index] = ALIGN_TO (offset, MINT_STACK_SLOT_SIZE);
13031327

13041328
mono_memory_write_barrier ();
1305-
if (mono_atomic_cas_ptr ((gpointer*)&imethod->arg_offsets, arg_offsets, NULL) != NULL)
1306-
g_free (arg_offsets);
1329+
/* If this fails, the new one is leaked in the mem manager */
1330+
mono_atomic_cas_ptr ((gpointer*)&imethod->arg_offsets, arg_offsets, NULL);
13071331
return imethod->arg_offsets;
13081332
}
13091333

@@ -3259,8 +3283,7 @@ interp_create_method_pointer_llvmonly (MonoMethod *method, gboolean unbox, MonoE
32593283

32603284
addr = mini_llvmonly_create_ftndesc (method, entry_wrapper, entry_ftndesc);
32613285

3262-
// FIXME:
3263-
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
3286+
MonoJitMemoryManager *jit_mm = jit_mm_for_method (method);
32643287
jit_mm_lock (jit_mm);
32653288
if (!jit_mm->interp_method_pointer_hash)
32663289
jit_mm->interp_method_pointer_hash = g_hash_table_new (NULL, NULL);
@@ -3438,12 +3461,24 @@ static void
34383461
interp_free_method (MonoMethod *method)
34393462
{
34403463
MonoJitMemoryManager *jit_mm = jit_mm_for_method (method);
3464+
InterpMethod *imethod;
3465+
MonoDynamicMethod *dmethod = (MonoDynamicMethod*)method;
34413466

34423467
jit_mm_lock (jit_mm);
3443-
/* InterpMethod is allocated in the domain mempool. We might haven't
3444-
* allocated an InterpMethod for this instance yet */
3468+
imethod = (InterpMethod*)mono_internal_hash_table_lookup (&jit_mm->interp_code_hash, method);
34453469
mono_internal_hash_table_remove (&jit_mm->interp_code_hash, method);
3470+
if (imethod && jit_mm->interp_method_pointer_hash) {
3471+
if (imethod->jit_entry)
3472+
g_hash_table_remove (jit_mm->interp_method_pointer_hash, imethod->jit_entry);
3473+
if (imethod->llvmonly_unbox_entry)
3474+
g_hash_table_remove (jit_mm->interp_method_pointer_hash, imethod->llvmonly_unbox_entry);
3475+
}
34463476
jit_mm_unlock (jit_mm);
3477+
3478+
if (dmethod->mp) {
3479+
mono_mempool_destroy (dmethod->mp);
3480+
dmethod->mp = NULL;
3481+
}
34473482
}
34483483

34493484
#if COUNT_OPS
@@ -8007,7 +8042,7 @@ interp_run_clause_with_il_state (gpointer il_state_ptr, int clause_index, MonoOb
80078042
imethod = mono_interp_get_imethod (il_state->method);
80088043
if (!imethod->transformed) {
80098044
// In case method is in process of being tiered up, make sure it is compiled
8010-
mono_interp_transform_method (imethod, context, error);
8045+
mono_interp_transform_method (imethod, context, error);
80118046
mono_error_assert_ok (error);
80128047
}
80138048

src/mono/mono/mini/interp/transform.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,15 @@ emit_ldptr (TransformData *td, gpointer data)
12871287
static MintICallSig
12881288
interp_get_icall_sig (MonoMethodSignature *sig);
12891289

1290+
static gpointer
1291+
imethod_alloc0 (TransformData *td, size_t size)
1292+
{
1293+
if (td->rtm->method->dynamic)
1294+
return mono_mempool_alloc0 (((MonoDynamicMethod*)td->rtm->method)->mp, (guint)size);
1295+
else
1296+
return mono_mem_manager_alloc0 (td->mem_manager, (guint)size);
1297+
}
1298+
12901299
static void
12911300
interp_generate_icall_throw (TransformData *td, MonoJitICallInfo *icall_info, gpointer arg1, gpointer arg2)
12921301
{
@@ -7657,7 +7666,7 @@ generate_code (TransformData *td, MonoMethod *method, MonoMethodHeader *header,
76577666
push_simple_type (td, STACK_TYPE_I);
76587667
interp_ins_set_dreg (td->last_ins, td->sp [-1].local);
76597668
/* This is a memory slot used by the wrapper */
7660-
gpointer addr = mono_mem_manager_alloc0 (td->mem_manager, sizeof (gpointer));
7669+
gpointer addr = imethod_alloc0 (td, sizeof (gpointer));
76617670
td->last_ins->data [0] = get_data_item_index (td, addr);
76627671
break;
76637672
}
@@ -8637,11 +8646,11 @@ generate_compacted_code (InterpMethod *rtm, TransformData *td)
86378646
size = compute_native_offset_estimates (td);
86388647

86398648
// Generate the compacted stream of instructions
8640-
td->new_code = ip = (guint16*)mono_mem_manager_alloc0 (td->mem_manager, size * sizeof (guint16));
8649+
td->new_code = ip = (guint16*)imethod_alloc0 (td, size * sizeof (guint16));
86418650

86428651
if (td->patchpoint_data_n) {
86438652
g_assert (mono_interp_tiering_enabled ());
8644-
td->patchpoint_data = (int*)mono_mem_manager_alloc0 (td->mem_manager, (td->patchpoint_data_n * 2 + 1) * sizeof (int));
8653+
td->patchpoint_data = (int*)imethod_alloc0 (td, (td->patchpoint_data_n * 2 + 1) * sizeof (int));
86458654
td->patchpoint_data [td->patchpoint_data_n * 2] = G_MAXINT32;
86468655
}
86478656

@@ -8697,7 +8706,7 @@ generate_compacted_code (InterpMethod *rtm, TransformData *td)
86978706

86988707
#if HOST_BROWSER
86998708
if (backward_branch_offsets_count > 0) {
8700-
rtm->backward_branch_offsets = mono_mem_manager_alloc(td->mem_manager, backward_branch_offsets_count * sizeof(guint16));
8709+
rtm->backward_branch_offsets = imethod_alloc0 (td, backward_branch_offsets_count * sizeof(guint16));
87018710
rtm->backward_branch_offsets_count = backward_branch_offsets_count;
87028711
memcpy(rtm->backward_branch_offsets, backward_branch_offsets, backward_branch_offsets_count * sizeof(guint16));
87038712
}
@@ -8709,7 +8718,7 @@ generate_compacted_code (InterpMethod *rtm, TransformData *td)
87098718
static void
87108719
interp_mark_reachable_bblocks (TransformData *td)
87118720
{
8712-
InterpBasicBlock **queue = mono_mem_manager_alloc0 (td->mem_manager, td->bb_count * sizeof (InterpBasicBlock*));
8721+
InterpBasicBlock **queue = mono_mempool_alloc0 (td->mempool, td->bb_count * sizeof (InterpBasicBlock*));
87138722
InterpBasicBlock *current;
87148723
int cur_index = 0;
87158724
int next_position = 0;
@@ -11208,7 +11217,7 @@ generate (MonoMethod *method, MonoMethodHeader *header, InterpMethod *rtm, MonoG
1120811217
code_len_u8 = GPTRDIFF_TO_UINT32 ((guint8 *) td->new_code_end - (guint8 *) td->new_code);
1120911218
code_len_u16 = GPTRDIFF_TO_UINT32 (td->new_code_end - td->new_code);
1121011219

11211-
rtm->clauses = (MonoExceptionClause*)mono_mem_manager_alloc0 (td->mem_manager, header->num_clauses * sizeof (MonoExceptionClause));
11220+
rtm->clauses = (MonoExceptionClause*)imethod_alloc0 (td, header->num_clauses * sizeof (MonoExceptionClause));
1121211221
memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
1121311222
rtm->code = (gushort*)td->new_code;
1121411223
rtm->init_locals = header->init_locals;
@@ -11232,6 +11241,8 @@ generate (MonoMethod *method, MonoMethodHeader *header, InterpMethod *rtm, MonoG
1123211241
rtm->alloca_size = td->total_locals_size + td->max_stack_size;
1123311242
g_assert ((rtm->alloca_size % MINT_STACK_ALIGNMENT) == 0);
1123411243
rtm->locals_size = td->param_area_offset;
11244+
// FIXME: Can't allocate this using imethod_alloc0 as its registered with mono_interp_register_imethod_data_items ()
11245+
//rtm->data_items = (gpointer*)imethod_alloc0 (td, td->n_data_items * sizeof (td->data_items [0]));
1123511246
rtm->data_items = (gpointer*)mono_mem_manager_alloc0 (td->mem_manager, td->n_data_items * sizeof (td->data_items [0]));
1123611247
memcpy (rtm->data_items, td->data_items, td->n_data_items * sizeof (td->data_items [0]));
1123711248

@@ -11245,7 +11256,7 @@ generate (MonoMethod *method, MonoMethodHeader *header, InterpMethod *rtm, MonoG
1124511256
int jinfo_len;
1124611257
jinfo_len = mono_jit_info_size ((MonoJitInfoFlags)0, header->num_clauses, 0);
1124711258
MonoJitInfo *jinfo;
11248-
jinfo = (MonoJitInfo *)mono_mem_manager_alloc0 (td->mem_manager, jinfo_len);
11259+
jinfo = (MonoJitInfo *)imethod_alloc0 (td, jinfo_len);
1124911260
jinfo->is_interp = 1;
1125011261
rtm->jinfo = jinfo;
1125111262
mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len_u8, (MonoJitInfoFlags)0, header->num_clauses, 0);

src/mono/mono/mini/llvmonly-runtime.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ mini_llvmonly_get_delegate_arg (MonoMethod *method, gpointer method_ptr)
116116
MonoFtnDesc*
117117
mini_llvmonly_create_ftndesc (MonoMethod *m, gpointer addr, gpointer arg)
118118
{
119-
MonoFtnDesc *ftndesc = (MonoFtnDesc*)m_method_alloc0 (m, sizeof (MonoFtnDesc));
119+
MonoFtnDesc *ftndesc;
120+
121+
if (m->dynamic && ((MonoDynamicMethod*)m)->mp)
122+
ftndesc = mono_mempool_alloc0 (((MonoDynamicMethod*)m)->mp, sizeof (MonoFtnDesc));
123+
else
124+
ftndesc = (MonoFtnDesc*)m_method_alloc0 (m, sizeof (MonoFtnDesc));
120125
ftndesc->addr = addr;
121126
ftndesc->arg = arg;
122127
ftndesc->method = m;
@@ -459,8 +464,8 @@ mini_llvmonly_get_vtable_trampoline (MonoVTable *vt, int slot_index, int index)
459464
if (slot_index < 0) {
460465
/* Initialize the IMT trampoline to a 'trampoline' so the generated code doesn't have to initialize it */
461466
// FIXME: Memory management
462-
gpointer *ftndesc = g_malloc (2 * sizeof (gpointer));
463-
IMTTrampInfo *info = g_new0 (IMTTrampInfo, 1);
467+
gpointer *ftndesc = m_class_alloc0 (vt->klass, 2 * sizeof (gpointer));
468+
IMTTrampInfo *info = m_class_alloc0 (vt->klass, sizeof (IMTTrampInfo));
464469
info->vtable = vt;
465470
info->slot = index;
466471
ftndesc [0] = (gpointer)mini_llvmonly_initial_imt_tramp;

0 commit comments

Comments
 (0)